]> 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
11 #include "warn.hh"
12
13 /* Position centered on parent.  */
14 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_parent, 2);
15 SCM
16 Self_alignment_interface::centered_on_parent (SCM element_smob, SCM axis)
17 {
18   Grob *me = unsmob_grob (element_smob);
19   Axis a = (Axis) scm_to_int (axis);
20   Grob *him = me->get_parent (a);
21   Interval he = him->extent (him, a);
22   
23   return  scm_make_real (he.is_empty () ? 0.0 : he.center ());
24 }
25
26 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_parent, 2);
27 SCM
28 Self_alignment_interface::aligned_on_parent (SCM element_smob, SCM axis)
29 {
30   Grob *me = unsmob_grob (element_smob);
31   Axis a = (Axis) scm_to_int (axis);
32   Grob *him = me->get_parent (a);
33   Interval he = him->extent (him, a);
34   
35   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
36     : ly_symbol2scm ("self-alignment-Y");
37   SCM align_prop (me->internal_get_property (sym));
38
39   if (!scm_is_number (align_prop))
40     return scm_int2num (0);
41
42   Real x = 0.0;
43   Real align = scm_to_double (align_prop);
44       
45   Interval ext (me->extent (me, a));
46   if (ext.is_empty ())
47     programming_error ("I'm empty. Can't align on self");
48   else
49     x -= ext.linear_combination (align) ;
50
51   if (!he.is_empty ())
52     x += he.linear_combination (align);
53
54   return scm_make_real (x);
55 }
56
57 /* Position centered on parent. */
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) scm_to_int (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 /** callback that centers the element on itself
72     Requires that self-alignment-{X, Y} be set.  */
73 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_self, 2);
74 SCM
75 Self_alignment_interface::aligned_on_self (SCM element_smob, SCM axis)
76 {
77   Grob *me = unsmob_grob (element_smob);
78   Axis a = (Axis) scm_to_int (axis);
79
80   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
81     : ly_symbol2scm ("self-alignment-Y");
82   
83   SCM align (me->internal_get_property (sym));
84   if (scm_is_number (align))
85     {
86       Interval ext (me->extent (me, a));
87       if (ext.is_empty ())
88         programming_error ("I'm empty. Can't align on self");
89       else
90         return scm_make_real (- ext.linear_combination (scm_to_double (align)));
91     }
92   return scm_make_real (0.0);
93 }
94
95
96 ADD_INTERFACE (Self_alignment_interface, "self-alignment-interface",
97                "Position this object on itself and/or on its parent. To this end, the following functions "
98                " are provided: \n"
99                "@table @code \n"
100                "@item Self_alignment_interface::aligned_on_self\n"
101                "  Align self on reference point, using @code{self-alignment-X} and "
102                "@code{self-alignment-Y}."
103                "@item Self_alignment_interface::aligned_on_parent\n"
104                "@item Self_alignment_interface::centered_on_parent\n"
105                "  Shift the object so its own reference point is centered on the  "
106                " extent of the parent \n"
107                "@item Self_alignment_interface::centered_on_other_axis_parent\n"
108                " For X-axis, center on the Y-parent, and vice versa.\n "
109                "@end table\n", 
110                "self-alignment-X self-alignment-Y");
111