2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "self-alignment-interface.hh"
23 #include "note-column.hh"
24 #include "paper-column.hh"
25 #include "pointer-group-interface.hh"
28 MAKE_SCHEME_CALLBACK (Self_alignment_interface, y_aligned_on_self, 1);
30 Self_alignment_interface::y_aligned_on_self (SCM element)
32 return aligned_on_self (Grob::unsmob (element), Y_AXIS, false, 0, 0);
35 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_aligned_on_self, 1);
37 Self_alignment_interface::x_aligned_on_self (SCM element)
39 return aligned_on_self (Grob::unsmob (element), X_AXIS, false, 0, 0);
42 MAKE_SCHEME_CALLBACK (Self_alignment_interface, pure_y_aligned_on_self, 3);
44 Self_alignment_interface::pure_y_aligned_on_self (SCM smob, SCM start, SCM end)
46 return aligned_on_self (Grob::unsmob (smob), Y_AXIS, true, robust_scm2int (start, 0), robust_scm2int (end, INT_MAX));
50 Self_alignment_interface::aligned_on_self (Grob *me, Axis a, bool pure, int start, int end)
52 SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
53 : ly_symbol2scm ("self-alignment-Y");
55 SCM align (me->internal_get_property (sym));
56 if (scm_is_number (align))
58 Interval ext (me->maybe_pure_extent (me, a, pure, start, end));
59 // Empty extent doesn't mean an error - we simply don't align such grobs.
60 // However, empty extent and non-empty stencil would be suspicious.
62 return scm_from_double (- ext.linear_combination (scm_to_double (align)));
63 else if (me->get_stencil ())
64 warning (me->name () + " has empty extent and non-empty stencil.");
66 return scm_from_double (0.0);
70 Self_alignment_interface::centered_on_object (Grob *him, Axis a)
72 return scm_from_double (robust_relative_extent (him, him, a).center ());
75 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_x_parent, 1);
77 Self_alignment_interface::centered_on_x_parent (SCM smob)
79 return centered_on_object (Grob::unsmob (smob)->get_parent (X_AXIS), X_AXIS);
82 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_y_parent, 1);
84 Self_alignment_interface::centered_on_y_parent (SCM smob)
86 return centered_on_object (Grob::unsmob (smob)->get_parent (Y_AXIS), Y_AXIS);
89 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_x_parent, 1);
91 Self_alignment_interface::aligned_on_x_parent (SCM smob)
93 return aligned_on_parent (Grob::unsmob (smob), X_AXIS);
96 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_y_parent, 1);
98 Self_alignment_interface::aligned_on_y_parent (SCM smob)
100 return aligned_on_parent (Grob::unsmob (smob), Y_AXIS);
104 Self_alignment_interface::aligned_on_parent (Grob *me, Axis a)
106 Grob *him = me->get_parent (a);
108 if (Paper_column::has_interface (him))
110 PaperColumn extents aren't reliable (they depend on size and alignment
111 of PaperColumn's children), so we align on NoteColumn instead.
112 This happens e.g. for lyrics without associatedVoice.
114 he = Paper_column::get_interface_extent
115 (him, ly_symbol2scm ("note-column-interface"), a);
118 if (ly_scm2bool(me->internal_get_property (ly_symbol2scm ("X-align-on-main-noteheads")))
119 && Note_column::has_interface (him))
120 he = Note_column::calc_main_heads_extent(him);
122 he = him->extent (him, a);
125 SCM self_align = (a == X_AXIS)
126 ? me->internal_get_property (ly_symbol2scm ("self-alignment-X"))
127 : me->internal_get_property (ly_symbol2scm ("self-alignment-Y"));
129 SCM par_align = (a == X_AXIS)
130 ? me->internal_get_property (ly_symbol2scm ("parent-alignment-X"))
131 : me->internal_get_property (ly_symbol2scm ("parent-alignment-Y"));
133 if (par_align == SCM_EOL)
134 par_align = self_align;
137 Interval ext (me->extent (me, a));
139 if (scm_is_number (self_align))
141 // Empty extent doesn't mean an error - we simply don't align such grobs.
142 // However, empty extent and non-empty stencil would be suspicious.
143 if (!ext.is_empty ())
144 x -= ext.linear_combination (scm_to_double (self_align));
145 else if (me->get_stencil ())
146 warning (me->name () + " has empty extent and non-empty stencil.");
149 if (scm_is_number (par_align))
151 // See comment above.
153 x += he.linear_combination (scm_to_double (par_align));
154 else if (him->get_stencil ())
155 warning (him->name () + " has empty extent and non-empty stencil.");
158 return scm_from_double (x);
162 Self_alignment_interface::set_aligned_on_parent (Grob *me, Axis a)
164 add_offset_callback (me,
165 (a == X_AXIS) ? aligned_on_x_parent_proc : aligned_on_y_parent_proc,
169 ADD_INTERFACE (Self_alignment_interface,
170 "Position this object on itself and/or on its parent. To this"
171 " end, the following functions are provided:\n"
174 "@item Self_alignment_interface::[xy]_aligned_on_self\n"
175 "Align self on reference point, using"
176 " @code{self-alignment-X} and @code{self-alignment-Y}."
177 "@item Self_alignment_interface::aligned_on_[xy]_parent\n"
178 "@item Self_alignment_interface::centered_on_[xy]_parent\n"
179 "Shift the object so its own reference point is centered on"
180 " the extent of the parent\n"
184 "parent-alignment-X "
185 "parent-alignment-Y "
188 "X-align-on-main-noteheads "