]> git.donarmstrong.com Git - lilypond.git/blob - lily/self-aligment-interface.cc
*** empty log message ***
[lilypond.git] / lily / self-aligment-interface.cc
1 /*
2   self-alignment-interface.cc
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "self-alignment-interface.hh"
10 #include "warn.hh"
11
12 /* Position centered on parent.  */
13 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_parent, 2);
14 SCM
15 Self_alignment_interface::centered_on_parent (SCM element_smob, SCM axis)
16 {
17   Grob *me = unsmob_grob (element_smob);
18   Axis a = (Axis) ly_scm2int (axis);
19   Grob *him = me->get_parent (a);
20   Interval he = him->extent (him, a);
21   
22   return  scm_make_real (he.is_empty () ? 0.0 : he.center ());
23 }
24
25 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_parent, 2);
26 SCM
27 Self_alignment_interface::aligned_on_parent (SCM element_smob, SCM axis)
28 {
29   Grob *me = unsmob_grob (element_smob);
30   Axis a = (Axis) ly_scm2int (axis);
31   Grob *him = me->get_parent (a);
32   Interval he = him->extent (him, a);
33   
34   SCM sym= (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
35     : ly_symbol2scm ("self-alignment-Y");
36   SCM align_prop (me->internal_get_property (sym));
37
38   if (!ly_c_number_p (align_prop))
39     return scm_int2num (0);
40
41   Real x = 0.0;
42   Real align = ly_scm2double (align_prop);
43       
44   Interval ext (me->extent (me, a));
45   if (ext.is_empty ())
46     programming_error ("I'm empty. Can't align on self");
47   else
48     x -= ext.linear_combination (align) ;
49
50   if (!he.is_empty ())
51     x += he.linear_combination (align);
52
53   return scm_make_real (x);
54 }
55
56 /* Position centered on parent. */
57 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_other_axis_parent, 2);
58 SCM
59 Self_alignment_interface::centered_on_other_axis_parent (SCM element_smob, 
60                                                          SCM axis)
61 {
62   Grob *me = unsmob_grob (element_smob);
63   Axis a = (Axis) ly_scm2int (axis);
64   Grob *him = me->get_parent (other_axis (a));
65   Interval he = him->extent (him, a);
66   
67   return  scm_make_real (he.is_empty () ? 0.0 : he.center ());
68 }
69
70 /** callback that centers the element on itself
71     Requires that self-alignment-{X, Y} be set.  */
72 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_self, 2);
73 SCM
74 Self_alignment_interface::aligned_on_self (SCM element_smob, SCM axis)
75 {
76   Grob *me = unsmob_grob (element_smob);
77   Axis a = (Axis) ly_scm2int (axis);
78
79   SCM sym= (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
80     : ly_symbol2scm ("self-alignment-Y");
81   
82   SCM align (me->internal_get_property (sym));
83   if (ly_c_number_p (align))
84     {
85       Interval ext (me->extent (me, a));
86       if (ext.is_empty ())
87         programming_error ("I'm empty. Can't align on self");
88       else
89         return scm_make_real (- ext.linear_combination (ly_scm2double (align)));
90     }
91   return scm_make_real (0.0);
92 }
93
94
95 ADD_INTERFACE (Self_alignment_interface, "self-alignment-interface",
96                "Position this object on itself and/or on its parent. To this end, the following functions "
97                " are provided: \n"
98                "@table @code \n"
99                "@item Self_alignment_interface::aligned_on_self\n"
100                "  Align self on reference point, using @code{self-alignment-X} and "
101                "@code{self-alignment-Y}."
102                "@item Self_alignment_interface::aligned_on_parent\n"
103                "@item Self_alignment_interface::centered_on_parent\n"
104                "  Shift the object so its own reference point is centered on the  "
105                " extent of the parent \n"
106                "@item Self_alignment_interface::centered_on_other_axis_parent\n"
107                " For X-axis, center on the Y-parent, and vice versa.\n "
108                "@end table\n", 
109                "self-alignment-X self-alignment-Y");
110