]> git.donarmstrong.com Git - lilypond.git/blob - lily/self-aligment-interface.cc
* lily/grob.cc: edit doc string.
[lilypond.git] / lily / self-aligment-interface.cc
1 #include "self-alignment-interface.hh"
2 #include "warn.hh"
3
4 /*
5   Position centered on parent.
6  */
7 MAKE_SCHEME_CALLBACK (Self_alignment_interface,centered_on_parent,2);
8 SCM
9 Self_alignment_interface::centered_on_parent (SCM element_smob, SCM axis)
10 {
11   Grob *me = unsmob_grob (element_smob);
12   Axis a = (Axis) gh_scm2int (axis);
13   Grob *him = me->get_parent (a);
14   Interval he = him->extent (him,a);
15   
16   return  gh_double2scm (he.is_empty () ? 0.0 : he.center ());
17 }
18
19 MAKE_SCHEME_CALLBACK (Self_alignment_interface,aligned_on_parent,2);
20 SCM
21 Self_alignment_interface::aligned_on_parent (SCM element_smob, SCM axis)
22 {
23   Grob *me = unsmob_grob (element_smob);
24   Axis a = (Axis) gh_scm2int (axis);
25   Grob *him = me->get_parent (a);
26   Interval he = him->extent (him,a);
27   
28   SCM sym= (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X"): ly_symbol2scm ("self-alignment-Y");
29   SCM align_prop (me->internal_get_grob_property (sym));
30
31   if (!gh_number_p (align_prop))
32     return gh_int2scm (0);
33
34   Real x = 0.0;
35
36   Real align = gh_scm2double (align_prop);
37       
38   Interval ext (me->extent (me,a));
39   if (ext.is_empty ())
40     {
41       programming_error ("I'm empty. Can't align on self");
42       
43     }
44   else
45     x -= ext.linear_combination (align) ;
46
47   if (!he.is_empty ())
48     {
49       x += he.linear_combination (align);
50     }
51
52   return gh_double2scm (x);
53 }
54
55 /*
56   Position centered on parent.
57  */
58 MAKE_SCHEME_CALLBACK (Self_alignment_interface,centered_on_other_axis_parent,2);
59 SCM
60 Self_alignment_interface::centered_on_other_axis_parent (SCM element_smob,
61                                                          SCM axis)
62 {
63   Grob *me = unsmob_grob (element_smob);
64   Axis a = (Axis) gh_scm2int (axis);
65   Grob *him = me->get_parent (other_axis (a));
66   Interval he = him->extent (him,a);
67   
68   return  gh_double2scm (he.is_empty () ? 0.0 : he.center ());
69 }
70
71
72
73
74 /**
75   callback that centers the element on itself
76
77   Requires that self-alignment-{X,Y} be set.
78  */
79 MAKE_SCHEME_CALLBACK (Self_alignment_interface,aligned_on_self,2);
80 SCM
81 Self_alignment_interface::aligned_on_self (SCM element_smob, SCM axis)
82 {
83   Grob *me = unsmob_grob (element_smob);
84   Axis a = (Axis) gh_scm2int (axis);
85
86   SCM sym= (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X"): ly_symbol2scm ("self-alignment-Y");
87   
88   SCM align (me->internal_get_grob_property (sym));
89   if (gh_number_p (align))
90     {
91       Interval ext (me->extent (me,a));
92
93       if (ext.is_empty ())
94         {
95           programming_error ("I'm empty. Can't align on self");
96           return gh_double2scm (0.0);
97         }
98       else
99         {
100           return gh_double2scm (- ext.linear_combination (gh_scm2double (align)));
101         }
102     }
103   return gh_double2scm (0.0);
104 }
105
106
107 ADD_INTERFACE (Self_alignment_interface, "self-alignment-interface",
108   "Position self using some alignment",
109   "self-alignment-X self-alignment-Y");
110