2 self-alignment-interface.cc
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "self-alignment-interface.hh"
13 /* Position centered on parent. */
14 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_parent, 2);
16 Self_alignment_interface::centered_on_parent (SCM element_smob, SCM axis)
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);
23 return scm_make_real (he.is_empty () ? 0.0 : he.center ());
26 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_parent, 2);
28 Self_alignment_interface::aligned_on_parent (SCM element_smob, SCM axis)
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);
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));
39 if (!scm_is_number (align_prop))
40 return scm_int2num (0);
43 Real align = scm_to_double (align_prop);
45 Interval ext (me->extent (me, a));
47 programming_error ("I'm empty. Can't align on self");
49 x -= ext.linear_combination (align) ;
52 x += he.linear_combination (align);
54 return scm_make_real (x);
57 /* Position centered on parent. */
58 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_other_axis_parent, 2);
60 Self_alignment_interface::centered_on_other_axis_parent (SCM element_smob,
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);
68 return scm_make_real (he.is_empty () ? 0.0 : he.center ());
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);
75 Self_alignment_interface::aligned_on_self (SCM element_smob, SCM axis)
77 Grob *me = unsmob_grob (element_smob);
78 Axis a = (Axis) scm_to_int (axis);
80 SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
81 : ly_symbol2scm ("self-alignment-Y");
83 SCM align (me->internal_get_property (sym));
84 if (scm_is_number (align))
86 Interval ext (me->extent (me, a));
88 programming_error ("I'm empty. Can't align on self");
90 return scm_make_real (- ext.linear_combination (scm_to_double (align)));
92 return scm_make_real (0.0);
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 "
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 "
110 "self-alignment-X self-alignment-Y");