]> git.donarmstrong.com Git - lilypond.git/blob - lily/self-aligment-interface.cc
* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[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) ly_scm2int (axis);
13   Grob *him = me->get_parent (a);
14   Interval he = him->extent (him,a);
15   
16   return  scm_make_real (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) ly_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_property (sym));
30
31   if (!ly_number_p (align_prop))
32     return scm_int2num (0);
33
34   Real x = 0.0;
35
36   Real align = ly_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 scm_make_real (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) ly_scm2int (axis);
65   Grob *him = me->get_parent (other_axis (a));
66   Interval he = him->extent (him,a);
67   
68   return  scm_make_real (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) ly_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_property (sym));
89   if (ly_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 scm_make_real (0.0);
97         }
98       else
99         {
100           return scm_make_real (- ext.linear_combination (ly_scm2double (align)));
101         }
102     }
103   return scm_make_real (0.0);
104 }
105
106
107 ADD_INTERFACE (Self_alignment_interface, "self-alignment-interface",
108                "Position this object on itself and/or on its parent. To this end, the following functions "
109                " are provided: \n"
110                "@table @code \n"
111                "@item Self_alignment_interface::aligned_on_self\n"
112                "  Align self on reference point, using @code{self-alignment-X} and "
113                "@code{self-alignment-Y}."
114                "@item Self_alignment_interface::aligned_on_parent\n"
115                "@item Self_alignment_interface::centered_on_parent\n"
116                "  Shift the object so its own reference point is centered on the  "
117                " extent of the parent \n"
118                "@item Self_alignment_interface::centered_on_other_axis_parent\n"
119                " For X-axis, center on the Y-parent, and vice versa.\n "
120                "@end table\n"
121                ,
122                "self-alignment-X self-alignment-Y");
123