2 align-interface.cc -- implement Align_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2007 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"
19 #include "paper-column.hh"
22 TODO: for vertical spacing, should also include a rod & spring
23 scheme of sorts into this: the alignment should default to a certain
24 distance between element refpoints, unless bbox force a bigger
28 MAKE_SCHEME_CALLBACK (Align_interface, calc_positioning_done, 1);
30 Align_interface::calc_positioning_done (SCM smob)
32 Grob *me = unsmob_grob (smob);
34 me->set_property ("positioning-done", SCM_BOOL_T);
36 SCM axis = scm_car (me->get_property ("axes"));
37 Axis ax = Axis (scm_to_int (axis));
39 Align_interface::align_elements_to_extents (me, ax);
45 TODO: This belongs to the old two-pass spacing. Delete me.
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;
82 /* for each grob, find its upper and lower skylines. If the grob has
83 an empty extent, delete it from the list instead. If the extent is
84 non-empty but there is no skyline available (or pure is true), just
85 create a flat skyline from the bounding box */
87 get_skylines (Grob *me,
88 vector<Grob*> *const elements,
90 bool pure, int start, int end,
91 vector<Skyline_pair> *const ret)
93 /* each child's skyline was calculated according to the common refpoint of its
94 elements. Here we need all the skylines to be positioned with respect to
95 a single refpoint, so we need the common refpoint of the common refpoints
96 of the elements of the children */
97 vector<Grob*> child_refpoints;
98 for (vsize i = 0; i < elements->size (); i++)
100 Grob *elt = (*elements)[i];
101 Grob *child_common = unsmob_grob ((a == Y_AXIS)
102 ? elt->get_object ("X-common")
103 : elt->get_object ("Y-common"));
107 extract_grob_set (elt, "elements", child_elts);
108 child_common = common_refpoint_of_array (child_elts, elt, other_axis (a));
111 child_refpoints.push_back (child_common);
113 Grob *common_refpoint = common_refpoint_of_array (child_refpoints, me, other_axis (a));
115 for (vsize i = elements->size (); i--;)
117 Grob *g = (*elements)[i];
118 Skyline_pair skylines;
120 /* each skyline is calculated relative to (potentially) a different other_axis
121 coordinate. In order to compare the skylines effectively, we need to shift them
122 to some absolute reference point */
125 Skyline_pair *skys = Skyline_pair::unsmob (g->get_property ("skylines"));
129 programming_error ("no skylines for alignment-child\n");
131 /* this is perhaps an abuse of minimum-?-extent: maybe we should create
132 another property? But it seems that the only (current) use of
133 minimum-Y-extent is to separate vertically-aligned elements */
134 SCM min_extent = g->get_property (a == X_AXIS ? "minimum-X-extent" : "minimum-Y-extent");
136 if (is_number_pair (min_extent))
139 Interval other_extent = g->extent (common_refpoint, other_axis (a));
140 b[a] = ly_scm2interval (min_extent);
141 b[other_axis (a)] = other_extent;
142 if (!other_extent.is_empty ())
143 skylines.insert (b, 0, other_axis (a));
146 Real offset = child_refpoints[i]->relative_coordinate (common_refpoint, other_axis (a));
147 skylines.shift (offset);
151 assert (a == Y_AXIS);
152 Interval extent = g->pure_height (g, start, end);
153 if (!extent.is_empty ())
157 b[other_axis (a)] = Interval (-infinity_f, infinity_f);
158 skylines.insert (b, 0, other_axis (a));
162 if (skylines.is_empty ())
163 elements->erase (elements->begin () + i);
165 ret->push_back (skylines);
171 Align_interface::get_extents_aligned_translates (Grob *me,
172 vector<Grob*> const &all_grobs,
174 bool pure, int start, int end)
176 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
179 SCM line_break_details = SCM_EOL;
180 if (a == Y_AXIS && me_spanner)
183 line_break_details = get_root_system (me)->column (start)->get_property ("line-break-system-details");
185 line_break_details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
187 if (!me->get_system () && !pure)
188 me->warning (_ ("vertical alignment called before line-breaking.\n"
189 "Only do cross-staff spanners with PianoStaff."));
193 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
196 vector<Grob*> elems (all_grobs); // writable copy
197 vector<Skyline_pair> skylines;
199 get_skylines (me, &elems, a, pure, start, end, &skylines);
202 /* TODO: extra-space stuff belongs to two-pass spacing. Delete me */
203 SCM extra_space_handle = scm_assq (ly_symbol2scm ("alignment-extra-space"), line_break_details);
204 Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
205 ? scm_cdr (extra_space_handle)
209 Real padding = robust_scm2double (me->get_property ("padding"), 0.0);
210 vector<Real> translates;
211 for (vsize j = 0; j < elems.size (); j++)
215 dy = skylines[j][-stacking_dir].max_height ();
217 dy = skylines[j-1][stacking_dir].distance (skylines[j][-stacking_dir]);
219 where += stacking_dir * max (0.0, dy + padding + extra_space / elems.size ());
220 translates.push_back (where);
223 SCM offsets_handle = scm_assq (ly_symbol2scm ("alignment-offsets"),
225 if (scm_is_pair (offsets_handle))
229 for (SCM s = scm_cdr (offsets_handle);
230 scm_is_pair (s) && i < translates.size (); s = scm_cdr (s), i++)
232 if (scm_is_number (scm_car (s)))
233 translates[i] = scm_to_double (scm_car (s));
237 vector<Real> all_translates;
239 if (!translates.empty ())
241 Real w = translates[0];
242 for (vsize i = 0, j = 0; j < all_grobs.size (); j++)
244 if (i < elems.size () && all_grobs[j] == elems[i])
246 all_translates.push_back (w);
249 return all_translates;
253 Align_interface::align_elements_to_extents (Grob *me, Axis a)
255 extract_grob_set (me, "elements", all_grobs);
257 vector<Real> translates = get_extents_aligned_translates (me, all_grobs, a, false, 0, 0);
258 if (translates.size ())
259 for (vsize j = 0; j < all_grobs.size (); j++)
260 all_grobs[j]->translate_axis (translates[j], a);
263 /* After we have already determined the y-offsets of our children, we may still
264 want to stretch them a little. */
266 Align_interface::stretch (Grob *me, Real amount, Axis a)
268 extract_grob_set (me, "elements", elts);
269 Real non_empty_elts = stretchable_children_count (me);
271 Direction dir = robust_scm2dir (me->get_property ("stacking-dir"), DOWN);
272 for (vsize i = 0; i < elts.size (); i++)
274 elts[i]->translate_axis (dir * offset, a);
275 if (!elts[i]->extent (me, a).is_empty ()
276 && !to_boolean (elts[i]->get_property ("keep-fixed-while-stretching")))
277 offset += amount / non_empty_elts;
279 me->flush_extent_cache (Y_AXIS);
283 Align_interface::get_pure_child_y_translation (Grob *me, Grob *ch, int start, int end)
285 extract_grob_set (me, "elements", all_grobs);
286 SCM dy_scm = me->get_property ("forced-distance");
288 if (scm_is_number (dy_scm))
290 Real dy = scm_to_double (dy_scm) * robust_scm2dir (me->get_property ("stacking-dir"), DOWN);
292 for (vsize i = 0; i < all_grobs.size (); i++)
294 if (all_grobs[i] == ch)
296 if (!Hara_kiri_group_spanner::has_interface (all_grobs[i])
297 || !Hara_kiri_group_spanner::request_suicide (all_grobs[i], start, end))
303 vector<Real> translates = get_extents_aligned_translates (me, all_grobs, Y_AXIS, true, start, end);
305 if (translates.size ())
307 for (vsize i = 0; i < all_grobs.size (); i++)
308 if (all_grobs[i] == ch)
309 return translates[i];
315 programming_error (_ ("tried to get a translation for something that is no child of mine"));
320 Align_interface::axis (Grob *me)
322 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
326 Align_interface::add_element (Grob *me, Grob *element)
328 Axis a = Align_interface::axis (me);
329 SCM sym = axis_offset_symbol (a);
330 SCM proc = axis_parent_positioning (a);
332 element->set_property (sym, proc);
333 Axis_group_interface::add_element (me, element);
337 Align_interface::set_ordered (Grob *me)
339 SCM ga_scm = me->get_object ("elements");
340 Grob_array *ga = unsmob_grob_array (ga_scm);
343 ga_scm = Grob_array::make_array ();
344 ga = unsmob_grob_array (ga_scm);
345 me->set_object ("elements", ga_scm);
348 ga->set_ordered (true);
352 Align_interface::stretchable_children_count (Grob const *me)
354 extract_grob_set (me, "elements", elts);
357 /* start at 1: we will never move the first child while stretching */
358 for (vsize i = 1; i < elts.size (); i++)
359 if (!to_boolean (elts[i]->get_property ("keep-fixed-while-stretching"))
360 && !elts[i]->extent (elts[i], Y_AXIS).is_empty ())
366 MAKE_SCHEME_CALLBACK (Align_interface, calc_max_stretch, 1)
368 Align_interface::calc_max_stretch (SCM smob)
370 Grob *me = unsmob_grob (smob);
371 Spanner *spanner_me = dynamic_cast<Spanner*> (me);
374 if (spanner_me && stretchable_children_count (me) > 0)
376 Paper_column *left = dynamic_cast<Paper_column*> (spanner_me->get_bound (LEFT));
377 Real height = me->extent (me, Y_AXIS).length ();
378 SCM line_break_details = left->get_property ("line-break-system-details");
379 SCM fixed_offsets = scm_assq (ly_symbol2scm ("alignment-offsets"),
382 /* if there are fixed offsets, we refuse to stretch */
383 if (fixed_offsets != SCM_BOOL_F)
386 ret = height * height / 80.0; /* why this, exactly? -- jneem */
388 return scm_from_double (ret);
392 Find Y-axis parent of G that has a #'forced-distance property. This
393 has the effect of finding the piano-staff given an object in that
396 FIXME: piano staves no longer have forced-distance. The code that
397 relies on this function (in line-spanner) is broken.
400 find_fixed_alignment_parent (Grob *g)
403 programming_error ("deprecated. We don't use forced-distance anymore");
407 ADD_INTERFACE (Align_interface,
409 "Order grobs from top to bottom, left to right, right to left or bottom "
411 "For vertical alignments of staves, the @code{break-system-details} of "
412 "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
413 "Set @code{alignment-extra-space} to add extra space for staves. Set "
414 "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."