2 align-interface.cc -- implement Align_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "align-interface.hh"
12 #include "axis-group-interface.hh"
13 #include "pointer-group-interface.hh"
14 #include "hara-kiri-group-spanner.hh"
15 #include "grob-array.hh"
16 #include "international.hh"
20 TODO: for vertical spacing, should also include a rod & spring
21 scheme of sorts into this: the alignment should default to a certain
22 distance between element refpoints, unless bbox force a bigger
26 MAKE_SCHEME_CALLBACK (Align_interface, calc_positioning_done, 1);
28 Align_interface::calc_positioning_done (SCM smob)
30 Grob *me = unsmob_grob (smob);
31 SCM axis = scm_car (me->get_property ("axes"));
32 Axis ax = Axis (scm_to_int (axis));
34 SCM force = me->get_property ("forced-distance");
35 if (scm_is_number (force))
36 Align_interface::align_to_fixed_distance (me, ax);
38 Align_interface::align_elements_to_extents (me, ax);
44 merge with align-to-extents?
46 MAKE_SCHEME_CALLBACK(Align_interface, stretch_after_break, 1)
48 Align_interface::stretch_after_break (SCM grob)
50 Grob *me = unsmob_grob (grob);
52 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
53 extract_grob_set (me, "elements", elems);
55 if (me_spanner && elems.size ())
57 Grob *common = common_refpoint_of_array (elems, me, Y_AXIS);
59 /* force position callbacks */
60 for (vsize i = 0; i < elems.size (); i++)
61 elems[i]->relative_coordinate (common, Y_AXIS);
63 SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
64 SCM extra_space_handle = scm_assoc (ly_symbol2scm ("fixed-alignment-extra-space"), details);
66 Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
67 ? scm_cdr (extra_space_handle)
71 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
73 Real delta = extra_space / elems.size() * stacking_dir;
74 for (vsize i = 0; i < elems.size (); i++)
75 elems[i]->translate_axis (i * delta, Y_AXIS);
78 return SCM_UNSPECIFIED;
82 merge with align-to-extents?
85 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
87 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
90 Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
92 extract_grob_set (me, "elements", elem_source);
94 vector<Grob*> elems (elem_source); // writable..
100 vector<Real> translates;
102 for (vsize j = elems.size (); j--;)
105 This is not very elegant, in that we need special support for
106 hara-kiri. Unfortunately, the generic wiring of
107 force_hara_kiri_callback () (extent and offset callback) is
108 such that we might get into a loop if we call extent () or
109 offset () the elements.
112 && Hara_kiri_group_spanner::has_interface (elems[j]))
113 Hara_kiri_group_spanner::consider_suicide (elems[j]);
115 if (!elems[j]->is_live ())
116 elems.erase (elems.begin () + j);
119 for (vsize j = 0; j < elems.size (); j++)
121 where += stacking_dir * dy;
122 translates.push_back (where);
123 v.unite (Interval (where, where));
127 TODO: support self-alignment-{Y, X}
129 for (vsize i = 0; i < translates.size (); i++)
130 elems[i]->translate_axis (translates[i] - v.center (), a);
134 Hairy function to put elements where they should be. Can be tweaked
135 from the outside by setting extra-space in its
138 We assume that the children the refpoints of the children are still
139 found at 0.0 -- we will fuck up with thresholds if children's
140 extents are already moved to locations such as (-16, -8), since the
141 dy needed to put things in a row doesn't relate to the distances
142 between original refpoints.
144 TODO: maybe we should rethink and throw out thresholding altogether.
145 The original function has been taken over by
146 align_to_fixed_distance ().
150 Align_interface::get_extents_aligned_translates (Grob *me,
151 vector<Grob*> const &all_grobs,
153 bool pure, int start, int end)
155 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
158 SCM line_break_details = SCM_EOL;
159 if (a == Y_AXIS && me_spanner)
161 line_break_details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
163 if (!me->get_system () && !pure)
164 me->warning (_ ("vertical alignment called before line-breaking.\n"
165 "Only do cross-staff spanners with PianoStaff."));
169 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
172 Interval threshold = robust_scm2interval (me->get_property ("threshold"),
173 Interval (0, Interval::infinity ()));
175 vector<Interval> dims;
178 for (vsize i = 0; i < all_grobs.size (); i++)
180 Interval y = all_grobs[i]->maybe_pure_extent (all_grobs[i], a, pure, start, end);
183 Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
191 Read self-alignment-X and self-alignment-Y. This may seem like
192 code duplication. (and really: it is), but this is necessary to
193 prevent ugly cyclic dependencies that arise when you combine
194 self-alignment on a child with alignment of children.
196 SCM align ((a == X_AXIS)
197 ? me->get_property ("self-alignment-X")
198 : me->get_property ("self-alignment-Y"));
202 Real extra_space = 0.0;
203 SCM extra_space_handle = scm_assq (ly_symbol2scm ("alignment-extra-space"), line_break_details);
205 extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
206 ? scm_cdr (extra_space_handle)
210 Real padding = robust_scm2double (me->get_property ("padding"),
212 vector<Real> translates;
213 for (vsize j = 0; j < elems.size (); j++)
215 Real dy = -dims[j][-stacking_dir];
217 dy += dims[j - 1][stacking_dir];
224 dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
227 where += stacking_dir * (dy + padding + extra_space / elems.size ());
228 total.unite (dims[j] + where);
229 translates.push_back (where);
232 SCM offsets_handle = scm_assq (ly_symbol2scm ("alignment-offsets"),
234 if (scm_is_pair (offsets_handle))
238 for (SCM s = scm_cdr (offsets_handle);
239 scm_is_pair (s) && i < translates.size (); s = scm_cdr (s), i++)
241 if (scm_is_number (scm_car (s)))
242 translates[i] = scm_to_double (scm_car (s));
247 Real center_offset = 0.0;
250 also move the grobs that were empty, to maintain spatial order.
252 vector<Real> all_translates;
253 if (translates.size ())
255 Real w = translates[0];
257 if (scm_is_number (align))
258 center_offset = total.linear_combination (scm_to_double (align));
260 for (vsize i = 0, j = 0; j < all_grobs.size (); j++)
262 if (i < elems.size () && all_grobs[j] == elems[i])
264 all_translates.push_back (w - center_offset);
267 return all_translates;
271 Align_interface::align_elements_to_extents (Grob *me, Axis a)
273 extract_grob_set (me, "elements", all_grobs);
275 vector<Real> translates = get_extents_aligned_translates (me, all_grobs, a, false, 0, 0);
276 if (translates.size ())
277 for (vsize j = 0; j < all_grobs.size (); j++)
278 all_grobs[j]->translate_axis (translates[j], a);
282 Align_interface::get_pure_child_y_translation (Grob *me, Grob *ch, int start, int end)
284 extract_grob_set (me, "elements", all_grobs);
285 SCM dy_scm = me->get_property ("forced-distance");
287 if (scm_is_number (dy_scm))
289 Real dy = scm_to_double (dy_scm) * robust_scm2dir (me->get_property ("stacking-dir"), DOWN);
291 for (vsize i = 0; i < all_grobs.size (); i++)
293 if (all_grobs[i] == ch)
295 if (!Hara_kiri_group_spanner::has_interface (all_grobs[i])
296 || !Hara_kiri_group_spanner::request_suicide (all_grobs[i], start, end))
302 vector<Real> translates = get_extents_aligned_translates (me, all_grobs, Y_AXIS, true, start, end);
304 if (translates.size ())
306 for (vsize i = 0; i < all_grobs.size (); i++)
307 if (all_grobs[i] == ch)
308 return translates[i];
314 programming_error (_ ("tried to get a translation for something that isn't my child"));
319 Align_interface::axis (Grob *me)
321 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
325 Align_interface::add_element (Grob *me, Grob *element)
327 Axis a = Align_interface::axis (me);
328 SCM sym = axis_offset_symbol (a);
329 SCM proc = axis_parent_positioning (a);
331 element->set_property (sym, proc);
332 Axis_group_interface::add_element (me, element);
336 Align_interface::set_ordered (Grob *me)
338 SCM ga_scm = me->get_object ("elements");
339 Grob_array *ga = unsmob_grob_array (ga_scm);
342 ga_scm = Grob_array::make_array ();
343 ga = unsmob_grob_array (ga_scm);
344 me->set_object ("elements", ga_scm);
347 ga->set_ordered (true);
351 Find Y-axis parent of G that has a #'forced-distance property. This
352 has the effect of finding the piano-staff given an object in that
356 find_fixed_alignment_parent (Grob *g)
360 if (scm_is_number (g->get_property ("forced-distance")))
363 g = g->get_parent (Y_AXIS);
369 ADD_INTERFACE (Align_interface,
372 "Order grobs from top to bottom, left to right, right to left or bottom "
374 "For vertical alignments of staves, the @code{break-system-details} of "
375 "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
376 "Set @code{alignment-extra-space} to add extra space for staves. Set "
377 "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
395 bool has_interface (Grob *);