* lily/note-collision.cc (do_shifts): align colliding notes to
[lilypond.git] / lily / self-aligment-interface.cc
blob2701772381ccea22eaeb8e739208316a0d7d0055
1 /*
2 self-alignment-interface.cc
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include "self-alignment-interface.hh"
10 #include "warn.hh"
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)
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);
22 return scm_make_real (he.is_empty () ? 0.0 : he.center ());
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)
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);
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));
38 if (!ly_c_number_p (align_prop))
39 return scm_int2num (0);
41 Real x = 0.0;
42 Real align = ly_scm2double (align_prop);
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) ;
50 if (!he.is_empty ())
51 x += he.linear_combination (align);
53 return scm_make_real (x);
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)
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);
67 return scm_make_real (he.is_empty () ? 0.0 : he.center ());
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)
76 Grob *me = unsmob_grob (element_smob);
77 Axis a = (Axis) ly_scm2int (axis);
79 SCM sym= (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
80 : ly_symbol2scm ("self-alignment-Y");
82 SCM align (me->internal_get_property (sym));
83 if (ly_c_number_p (align))
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)));
91 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");