*** empty log message ***
[lilypond.git] / lily / self-aligment-interface.cc
blob9da0a406c212007d5250295df81a0dbd5af578d8
1 /*
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>
7 */
9 #include "self-alignment-interface.hh"
11 #include "warn.hh"
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)
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);
27 SCM
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);
42 Real x = 0.0;
43 Real align = scm_to_double (align_prop);
45 Interval ext (me->extent (me, a));
46 if (ext.is_empty ())
47 programming_error ("can't align on self: empty element");
48 else
49 x -= ext.linear_combination (align);
51 if (!he.is_empty ())
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);
59 SCM
60 Self_alignment_interface::centered_on_other_axis_parent (SCM element_smob,
61 SCM axis)
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);
74 SCM
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));
87 if (ext.is_empty ())
88 programming_error ("can't align on self: empty element");
89 else
90 return scm_make_real (- ext.linear_combination (scm_to_double (align)));
92 return scm_make_real (0.0);
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");