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"
21 TODO: for vertical spacing, should also include a rod & spring
22 scheme of sorts into this: the alignment should default to a certain
23 distance between element refpoints, unless bbox force a bigger
27 MAKE_SCHEME_CALLBACK (Align_interface, calc_positioning_done, 1);
29 Align_interface::calc_positioning_done (SCM smob)
31 Grob *me = unsmob_grob (smob);
32 SCM axis = scm_car (me->get_property ("axes"));
33 Axis ax = Axis (scm_to_int (axis));
35 SCM force = me->get_property ("forced-distance");
36 if (scm_is_number (force))
37 Align_interface::align_to_fixed_distance (me, ax);
39 Align_interface::align_elements_to_extents (me, ax);
45 merge with align-to-extents?
47 MAKE_SCHEME_CALLBACK(Align_interface, stretch_after_break, 1)
49 Align_interface::stretch_after_break (SCM grob)
51 Grob *me = unsmob_grob (grob);
53 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
54 extract_grob_set (me, "elements", elems);
56 if (me_spanner && elems.size ())
58 Grob *common = common_refpoint_of_array (elems, me, Y_AXIS);
60 /* force position callbacks */
61 for (vsize i = 0; i < elems.size (); i++)
62 elems[i]->relative_coordinate (common, Y_AXIS);
64 SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
65 SCM extra_space_handle = scm_assoc (ly_symbol2scm ("fixed-alignment-extra-space"), details);
67 Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
68 ? scm_cdr (extra_space_handle)
72 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
74 Real delta = extra_space / elems.size() * stacking_dir;
75 for (vsize i = 0; i < elems.size (); i++)
76 elems[i]->translate_axis (i * delta, Y_AXIS);
79 return SCM_UNSPECIFIED;
83 merge with align-to-extents?
86 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
88 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
91 Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
93 extract_grob_set (me, "elements", elem_source);
95 vector<Grob*> elems (elem_source); // writable..
101 vector<Real> translates;
103 for (vsize j = elems.size (); j--;)
106 This is not very elegant, in that we need special support for
107 hara-kiri. Unfortunately, the generic wiring of
108 force_hara_kiri_callback () (extent and offset callback) is
109 such that we might get into a loop if we call extent () or
110 offset () the elements.
113 && Hara_kiri_group_spanner::has_interface (elems[j]))
114 Hara_kiri_group_spanner::consider_suicide (elems[j]);
116 if (!elems[j]->is_live ())
117 elems.erase (elems.begin () + j);
120 for (vsize j = 0; j < elems.size (); j++)
122 where += stacking_dir * dy;
123 translates.push_back (where);
124 v.unite (Interval (where, where));
127 for (vsize i = 0; i < translates.size (); i++)
128 elems[i]->translate_axis (translates[i] - v.center (), a);
131 /* for each grob, find its upper and lower skylines. If the grob has
132 an empty extent, delete it from the list instead. If the extent is
133 non-empty but there is no skyline available (or pure is true), just
134 create a flat skyline from the bounding box */
136 get_skylines (Grob *me,
137 vector<Grob*> *const elements,
139 bool pure, int start, int end,
140 vector<Skyline_pair> *const ret)
142 /* each child's skyline was calculated according to the common refpoint of its
143 elements. Here we need all the skylines to be positioned with respect to
144 a single refpoint, so we need the common refpoint of the common refpoints
145 of the elements of the children */
146 vector<Grob*> child_refpoints;
147 for (vsize i = 0; i < elements->size (); i++)
149 Grob *elt = (*elements)[i];
150 Grob *child_common = 0;
153 should consider whether to restrict to Y_AXIS.
156 child_common = unsmob_grob (elt->get_object ("Y-common"));
158 child_common = unsmob_grob (elt->get_object ("X-common"));
162 extract_grob_set (elt, "elements", child_elts);
163 child_common = common_refpoint_of_array (child_elts, elt, other_axis (a));
166 child_refpoints.push_back (child_common);
168 Grob *common_refpoint = common_refpoint_of_array (child_refpoints, me, other_axis (a));
170 for (vsize i = elements->size (); i--;)
172 Grob *g = (*elements)[i];
173 Interval extent = g->maybe_pure_extent (g, a, pure, start, end);
174 Interval other_extent = pure ? Interval (-infinity_f, infinity_f)
175 : g->extent (common_refpoint, other_axis (a));
178 b[other_axis (a)] = other_extent;
180 if (extent.is_empty ())
182 elements->erase (elements->begin () + i);
186 Skyline_pair skylines;
188 && Skyline_pair::unsmob (g->get_property ("skylines")))
189 skylines = *Skyline_pair::unsmob (g->get_property ("skylines"));
193 programming_error ("no skylines for alignment-child\n");
195 skylines = Skyline_pair (b, 0, other_axis (a));
198 /* each skyline is calculated relative to (potentially) a different other_axis
199 coordinate. In order to compare the skylines effectively, we need to shift them
200 to some absolute reference point */
203 /* this is perhaps an abuse of minimum-?-extent: maybe we should create
204 another property? But it seems that the only (current) use of
205 minimum-Y-extent is to separate vertically-aligned elements */
206 SCM min_extent = g->get_property (a == X_AXIS ? "minimum-X-extent" : "minimum-Y-extent");
207 if (is_number_pair (min_extent))
209 b[a] = ly_scm2interval (min_extent);
210 skylines.insert (b, 0, other_axis (a));
213 Real offset = child_refpoints[i]->relative_coordinate (common_refpoint, other_axis (a));
214 skylines.shift (offset);
218 ret->push_back (skylines);
224 Align_interface::get_extents_aligned_translates (Grob *me,
225 vector<Grob*> const &all_grobs,
227 bool pure, int start, int end)
229 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
232 SCM line_break_details = SCM_EOL;
233 if (a == Y_AXIS && me_spanner)
236 line_break_details = get_root_system (me)->column (start)->get_property ("line-break-system-details");
238 line_break_details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
240 if (!me->get_system () && !pure)
241 me->warning (_ ("vertical alignment called before line-breaking.\n"
242 "Only do cross-staff spanners with PianoStaff."));
246 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
249 vector<Grob*> elems (all_grobs); // writable copy
250 vector<Skyline_pair> skylines;
252 get_skylines (me, &elems, a, pure, start, end, &skylines);
255 SCM extra_space_handle = scm_assq (ly_symbol2scm ("alignment-extra-space"), line_break_details);
256 Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
257 ? scm_cdr (extra_space_handle)
261 Real padding = robust_scm2double (me->get_property ("padding"), 0.0);
262 vector<Real> translates;
263 for (vsize j = 0; j < elems.size (); j++)
267 dy = skylines[j][-stacking_dir].max_height ();
269 dy = skylines[j-1][stacking_dir].distance (skylines[j][-stacking_dir]);
271 where += stacking_dir * max (0.0, dy + padding + extra_space / elems.size ());
272 translates.push_back (where);
275 SCM offsets_handle = scm_assq (ly_symbol2scm ("alignment-offsets"),
277 if (scm_is_pair (offsets_handle))
281 for (SCM s = scm_cdr (offsets_handle);
282 scm_is_pair (s) && i < translates.size (); s = scm_cdr (s), i++)
284 if (scm_is_number (scm_car (s)))
285 translates[i] = scm_to_double (scm_car (s));
289 vector<Real> all_translates;
291 if (!translates.empty ())
293 Real w = translates[0];
294 for (vsize i = 0, j = 0; j < all_grobs.size (); j++)
296 if (i < elems.size () && all_grobs[j] == elems[i])
298 all_translates.push_back (w);
301 return all_translates;
305 Align_interface::align_elements_to_extents (Grob *me, Axis a)
307 extract_grob_set (me, "elements", all_grobs);
309 vector<Real> translates = get_extents_aligned_translates (me, all_grobs, a, false, 0, 0);
310 if (translates.size ())
311 for (vsize j = 0; j < all_grobs.size (); j++)
312 all_grobs[j]->translate_axis (translates[j], a);
316 Align_interface::get_pure_child_y_translation (Grob *me, Grob *ch, int start, int end)
318 extract_grob_set (me, "elements", all_grobs);
319 SCM dy_scm = me->get_property ("forced-distance");
321 if (scm_is_number (dy_scm))
323 Real dy = scm_to_double (dy_scm) * robust_scm2dir (me->get_property ("stacking-dir"), DOWN);
325 for (vsize i = 0; i < all_grobs.size (); i++)
327 if (all_grobs[i] == ch)
329 if (!Hara_kiri_group_spanner::has_interface (all_grobs[i])
330 || !Hara_kiri_group_spanner::request_suicide (all_grobs[i], start, end))
336 vector<Real> translates = get_extents_aligned_translates (me, all_grobs, Y_AXIS, true, start, end);
338 if (translates.size ())
340 for (vsize i = 0; i < all_grobs.size (); i++)
341 if (all_grobs[i] == ch)
342 return translates[i];
348 programming_error (_ ("tried to get a translation for something that is no child of mine"));
353 Align_interface::axis (Grob *me)
355 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
359 Align_interface::add_element (Grob *me, Grob *element)
361 Axis a = Align_interface::axis (me);
362 SCM sym = axis_offset_symbol (a);
363 SCM proc = axis_parent_positioning (a);
365 element->set_property (sym, proc);
366 Axis_group_interface::add_element (me, element);
370 Align_interface::set_ordered (Grob *me)
372 SCM ga_scm = me->get_object ("elements");
373 Grob_array *ga = unsmob_grob_array (ga_scm);
376 ga_scm = Grob_array::make_array ();
377 ga = unsmob_grob_array (ga_scm);
378 me->set_object ("elements", ga_scm);
381 ga->set_ordered (true);
385 Find Y-axis parent of G that has a #'forced-distance property. This
386 has the effect of finding the piano-staff given an object in that
390 find_fixed_alignment_parent (Grob *g)
394 if (scm_is_number (g->get_property ("forced-distance")))
397 g = g->get_parent (Y_AXIS);
403 ADD_INTERFACE (Align_interface,
405 "Order grobs from top to bottom, left to right, right to left or bottom "
407 "For vertical alignments of staves, the @code{break-system-details} of "
408 "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
409 "Set @code{alignment-extra-space} to add extra space for staves. Set "
410 "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."