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 vector<Real> translates;
211 for (vsize j = 0; j < elems.size (); j++)
213 Real dy = -dims[j][-stacking_dir];
215 dy += dims[j - 1][stacking_dir];
222 dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
224 where += stacking_dir * (dy + extra_space / elems.size ());
225 total.unite (dims[j] + where);
226 translates.push_back (where);
229 SCM offsets_handle = scm_assq (ly_symbol2scm ("alignment-offsets"), line_break_details);
230 if (scm_is_pair (offsets_handle))
234 for (SCM s = scm_cdr (offsets_handle); scm_is_pair (s) && i < translates.size (); s = scm_cdr (s), i++)
236 if (scm_is_number (scm_car (s)))
237 translates[i] = scm_to_double (scm_car (s));
242 Real center_offset = 0.0;
245 also move the grobs that were empty, to maintain spatial order.
247 vector<Real> all_translates;
248 if (translates.size ())
250 Real w = translates[0];
252 if (scm_is_number (align))
253 center_offset = total.linear_combination (scm_to_double (align));
255 for (vsize i = 0, j = 0; j < all_grobs.size (); j++)
257 if (i < elems.size () && all_grobs[j] == elems[i])
259 all_translates.push_back (w - center_offset);
262 return all_translates;
266 Align_interface::align_elements_to_extents (Grob *me, Axis a)
268 extract_grob_set (me, "elements", all_grobs);
270 vector<Real> translates = get_extents_aligned_translates (me, all_grobs, a, false, 0, 0);
271 if (translates.size ())
272 for (vsize j = 0; j < all_grobs.size (); j++)
273 all_grobs[j]->translate_axis (translates[j], a);
277 Align_interface::get_pure_child_y_translation (Grob *me, Grob *ch, int start, int end)
279 extract_grob_set (me, "elements", all_grobs);
280 SCM dy_scm = me->get_property ("forced-distance");
282 if (scm_is_number (dy_scm))
284 Real dy = scm_to_double (dy_scm) * robust_scm2dir (me->get_property ("stacking-dir"), DOWN);
286 for (vsize i = 0; i < all_grobs.size (); i++)
288 if (all_grobs[i] == ch)
290 if (!Hara_kiri_group_spanner::has_interface (all_grobs[i])
291 || !Hara_kiri_group_spanner::request_suicide (all_grobs[i], start, end))
297 vector<Real> translates = get_extents_aligned_translates (me, all_grobs, Y_AXIS, true, start, end);
299 if (translates.size ())
301 for (vsize i = 0; i < all_grobs.size (); i++)
302 if (all_grobs[i] == ch)
303 return translates[i];
309 programming_error (_ ("tried to get a translation for something that isn't my child"));
314 Align_interface::axis (Grob *me)
316 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
320 Align_interface::add_element (Grob *me, Grob *element)
322 Axis a = Align_interface::axis (me);
323 SCM sym = axis_offset_symbol (a);
324 SCM proc = axis_parent_positioning (a);
326 element->internal_set_property (sym, proc);
327 Axis_group_interface::add_element (me, element);
331 Align_interface::set_ordered (Grob *me)
333 SCM ga_scm = me->get_object ("elements");
334 Grob_array *ga = unsmob_grob_array (ga_scm);
337 ga_scm = Grob_array::make_array ();
338 ga = unsmob_grob_array (ga_scm);
339 me->set_object ("elements", ga_scm);
342 ga->set_ordered (true);
346 Find Y-axis parent of G that has a #'forced-distance property. This
347 has the effect of finding the piano-staff given an object in that
351 find_fixed_alignment_parent (Grob *g)
355 if (scm_is_number (g->get_property ("forced-distance")))
358 g = g->get_parent (Y_AXIS);
364 ADD_INTERFACE (Align_interface,
367 "Order grobs from top to bottom, left to right, right to left or bottom "
369 "For vertical alignments of staves, the @code{break-system-details} of "
370 "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
371 "Set @code{alignment-extra-space} to add extra space for staves. Set "
372 "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
387 bool has_interface (Grob *);