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"
10 #include "axis-group-interface.hh"
11 #include "grob-array.hh"
12 #include "hara-kiri-group-spanner.hh"
13 #include "international.hh"
15 #include "paper-column.hh"
16 #include "pointer-group-interface.hh"
18 #include "skyline-pair.hh"
23 TODO: for vertical spacing, should also include a rod & spring
24 scheme of sorts into this: the alignment should default to a certain
25 distance between element refpoints, unless bbox force a bigger
29 MAKE_SCHEME_CALLBACK (Align_interface, calc_positioning_done, 1);
31 Align_interface::calc_positioning_done (SCM smob)
33 Grob *me = unsmob_grob (smob);
35 me->set_property ("positioning-done", SCM_BOOL_T);
37 SCM axis = scm_car (me->get_property ("axes"));
38 Axis ax = Axis (scm_to_int (axis));
40 Align_interface::align_elements_to_extents (me, ax);
46 TODO: This belongs to the old two-pass spacing. Delete me.
48 MAKE_SCHEME_CALLBACK (Align_interface, stretch_after_break, 1)
50 Align_interface::stretch_after_break (SCM grob)
52 Grob *me = unsmob_grob (grob);
54 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
55 extract_grob_set (me, "elements", elems);
57 if (me_spanner && elems.size ())
59 Grob *common = common_refpoint_of_array (elems, me, Y_AXIS);
61 /* force position callbacks */
62 for (vsize i = 0; i < elems.size (); i++)
63 elems[i]->relative_coordinate (common, Y_AXIS);
65 SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
66 SCM extra_space_handle = scm_assoc (ly_symbol2scm ("fixed-alignment-extra-space"), details);
68 Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
69 ? scm_cdr (extra_space_handle)
73 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
75 Real delta = extra_space / elems.size () * stacking_dir;
76 for (vsize i = 0; i < elems.size (); i++)
77 elems[i]->translate_axis (i * delta, Y_AXIS);
80 return SCM_UNSPECIFIED;
83 /* for each grob, find its upper and lower skylines. If the grob has
84 an empty extent, delete it from the list instead. If the extent is
85 non-empty but there is no skyline available (or pure is true), just
86 create a flat skyline from the bounding box */
88 get_skylines (Grob *me,
89 vector<Grob*> *const elements,
91 bool pure, int start, int end,
92 vector<Skyline_pair> *const ret)
94 /* each child's skyline was calculated according to the common refpoint of its
95 elements. Here we need all the skylines to be positioned with respect to
96 a single refpoint, so we need the common refpoint of the common refpoints
97 of the elements of the children */
98 vector<Grob*> child_refpoints;
99 for (vsize i = 0; i < elements->size (); i++)
101 Grob *elt = (*elements)[i];
102 Grob *child_common = unsmob_grob ((a == Y_AXIS)
103 ? elt->get_object ("X-common")
104 : elt->get_object ("Y-common"));
108 extract_grob_set (elt, "elements", child_elts);
109 child_common = common_refpoint_of_array (child_elts, elt, other_axis (a));
112 child_refpoints.push_back (child_common);
115 Grob *other_common = common_refpoint_of_array (child_refpoints, me, other_axis (a));
117 for (vsize i = elements->size (); i--;)
119 Grob *g = (*elements)[i];
120 Skyline_pair skylines;
122 /* each skyline is calculated relative to (potentially) a different other_axis
123 coordinate. In order to compare the skylines effectively, we need to shift them
124 to some absolute reference point */
127 Skyline_pair *skys = Skyline_pair::unsmob (g->get_property (a == Y_AXIS
128 ? "vertical-skylines"
129 : "horizontal-skylines"));
133 /* this is perhaps an abuse of minimum-?-extent: maybe we should create
134 another property? But it seems that the only (current) use of
135 minimum-Y-extent is to separate vertically-aligned elements */
136 SCM min_extent = g->get_property (a == X_AXIS
137 ? ly_symbol2scm ("minimum-X-extent")
138 : ly_symbol2scm ("minimum-Y-extent"));
140 if (is_number_pair (min_extent))
143 Interval other_extent = g->extent (other_common, other_axis (a));
144 b[a] = ly_scm2interval (min_extent);
145 b[other_axis (a)] = other_extent;
146 if (!other_extent.is_empty ())
147 skylines.insert (b, 0, other_axis (a));
150 Real offset = child_refpoints[i]->relative_coordinate (other_common, other_axis (a));
151 skylines.shift (offset);
155 assert (a == Y_AXIS);
156 Interval extent = g->pure_height (g, start, end);
157 if (!extent.is_empty ())
161 b[other_axis (a)] = Interval (-infinity_f, infinity_f);
162 skylines.insert (b, 0, other_axis (a));
166 if (skylines.is_empty ())
167 elements->erase (elements->begin () + i);
169 ret->push_back (skylines);
175 Align_interface::get_extents_aligned_translates (Grob *me,
176 vector<Grob*> const &all_grobs,
178 bool pure, int start, int end)
180 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
183 SCM line_break_details = SCM_EOL;
184 if (a == Y_AXIS && me_spanner)
187 line_break_details = get_root_system (me)->column (start)->get_property ("line-break-system-details");
189 line_break_details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
191 if (!me->get_system () && !pure)
192 me->programming_error ("vertical alignment called before line-breaking");
195 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
198 vector<Grob*> elems (all_grobs); // writable copy
199 vector<Skyline_pair> skylines;
201 get_skylines (me, &elems, a, pure, start, end, &skylines);
204 /* TODO: extra-space stuff belongs to two-pass spacing. Delete me */
205 SCM extra_space_handle = scm_assq (ly_symbol2scm ("alignment-extra-space"), line_break_details);
206 Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
207 ? scm_cdr (extra_space_handle)
211 Real padding = robust_scm2double (me->get_property ("padding"), 0.0);
212 vector<Real> translates;
213 Skyline down_skyline (stacking_dir);
214 for (vsize j = 0; j < elems.size (); j++)
218 dy = skylines[j][-stacking_dir].max_height ();
221 down_skyline.merge (skylines[j-1][stacking_dir]);
222 dy = down_skyline.distance (skylines[j][-stacking_dir]);
225 if (isinf (dy)) /* if the skyline is empty, maybe max_height is infinity_f */
228 dy = max (0.0, dy + padding + extra_space / elems.size ());
229 down_skyline.raise (-stacking_dir * dy);
230 where += stacking_dir * dy;
231 translates.push_back (where);
234 SCM offsets_handle = scm_assq (ly_symbol2scm ("alignment-offsets"),
236 if (scm_is_pair (offsets_handle))
240 for (SCM s = scm_cdr (offsets_handle);
241 scm_is_pair (s) && i < translates.size (); s = scm_cdr (s), i++)
243 if (scm_is_number (scm_car (s)))
244 translates[i] = scm_to_double (scm_car (s));
248 vector<Real> all_translates;
250 if (!translates.empty ())
252 Real w = translates[0];
253 for (vsize i = 0, j = 0; j < all_grobs.size (); j++)
255 if (i < elems.size () && all_grobs[j] == elems[i])
257 all_translates.push_back (w);
260 return all_translates;
264 Align_interface::align_elements_to_extents (Grob *me, Axis a)
266 extract_grob_set (me, "elements", all_grobs);
268 vector<Real> translates = get_extents_aligned_translates (me, all_grobs, a, false, 0, 0);
269 if (translates.size ())
270 for (vsize j = 0; j < all_grobs.size (); j++)
271 all_grobs[j]->translate_axis (translates[j], a);
274 /* After we have already determined the y-offsets of our children, we may still
275 want to stretch them a little. */
277 Align_interface::stretch (Grob *me, Real amount, Axis a)
279 extract_grob_set (me, "elements", elts);
280 Real non_empty_elts = stretchable_children_count (me);
282 Direction dir = robust_scm2dir (me->get_property ("stacking-dir"), DOWN);
283 for (vsize i = 1; i < elts.size (); i++)
285 if (!elts[i]->extent (me, a).is_empty ()
286 && !to_boolean (elts[i]->get_property ("keep-fixed-while-stretching")))
287 offset += amount / non_empty_elts;
288 elts[i]->translate_axis (dir * offset, a);
290 me->flush_extent_cache (Y_AXIS);
294 Align_interface::get_pure_child_y_translation (Grob *me, Grob *ch, int start, int end)
296 extract_grob_set (me, "elements", all_grobs);
297 SCM dy_scm = me->get_property ("forced-distance");
299 if (scm_is_number (dy_scm))
301 Real dy = scm_to_double (dy_scm) * robust_scm2dir (me->get_property ("stacking-dir"), DOWN);
303 for (vsize i = 0; i < all_grobs.size (); i++)
305 if (all_grobs[i] == ch)
307 if (!Hara_kiri_group_spanner::has_interface (all_grobs[i])
308 || !Hara_kiri_group_spanner::request_suicide (all_grobs[i], start, end))
314 vector<Real> translates = get_extents_aligned_translates (me, all_grobs, Y_AXIS, true, start, end);
316 if (translates.size ())
318 for (vsize i = 0; i < all_grobs.size (); i++)
319 if (all_grobs[i] == ch)
320 return translates[i];
326 programming_error (_ ("tried to get a translation for something that is no child of mine"));
331 Align_interface::axis (Grob *me)
333 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
337 Align_interface::add_element (Grob *me, Grob *element)
339 Axis a = Align_interface::axis (me);
340 SCM sym = axis_offset_symbol (a);
341 SCM proc = axis_parent_positioning (a);
343 element->set_property (sym, proc);
344 Axis_group_interface::add_element (me, element);
348 Align_interface::set_ordered (Grob *me)
350 SCM ga_scm = me->get_object ("elements");
351 Grob_array *ga = unsmob_grob_array (ga_scm);
354 ga_scm = Grob_array::make_array ();
355 ga = unsmob_grob_array (ga_scm);
356 me->set_object ("elements", ga_scm);
359 ga->set_ordered (true);
363 Align_interface::stretchable_children_count (Grob const *me)
365 extract_grob_set (me, "elements", elts);
368 /* start at 1: we will never move the first child while stretching */
369 for (vsize i = 1; i < elts.size (); i++)
370 if (!to_boolean (elts[i]->get_property ("keep-fixed-while-stretching"))
371 && !elts[i]->extent (elts[i], Y_AXIS).is_empty ())
377 MAKE_SCHEME_CALLBACK (Align_interface, calc_max_stretch, 1)
379 Align_interface::calc_max_stretch (SCM smob)
381 Grob *me = unsmob_grob (smob);
382 Spanner *spanner_me = dynamic_cast<Spanner*> (me);
385 if (spanner_me && stretchable_children_count (me) > 0)
387 Paper_column *left = dynamic_cast<Paper_column*> (spanner_me->get_bound (LEFT));
388 Real height = me->extent (me, Y_AXIS).length ();
389 SCM line_break_details = left->get_property ("line-break-system-details");
390 SCM fixed_offsets = scm_assq (ly_symbol2scm ("alignment-offsets"),
393 /* if there are fixed offsets, we refuse to stretch */
394 if (fixed_offsets != SCM_BOOL_F)
397 ret = height * height / 80.0; /* why this, exactly? -- jneem */
399 return scm_from_double (ret);
402 ADD_INTERFACE (Align_interface,
403 "Order grobs from top to bottom, left to right, right to left"
404 " or bottom to top. For vertical alignments of staves, the"
405 " @code{break-system-details} of the left"
406 " @internalsref{NonMusicalPaperColumn} may be set to tune"
407 " vertical spacing. Set @code{alignment-extra-space} to add"
408 " extra space for staves. Set"
409 " @code{fixed-alignment-extra-space} to force staves in"
410 " @code{PianoStaff}s further apart.",