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"
18 TODO: for vertical spacing, should also include a rod & spring
19 scheme of sorts into this: the alignment should default to a certain
20 distance between element refpoints, unless bbox force a bigger
24 MAKE_SCHEME_CALLBACK (Align_interface, calc_positioning_done, 1);
26 Align_interface::calc_positioning_done (SCM smob)
28 Grob *me = unsmob_grob (smob);
29 SCM axis = scm_car (me->get_property ("axes"));
30 Axis ax = (Axis)scm_to_int (axis);
32 SCM force = me->get_property ("forced-distance");
33 if (scm_is_number (force))
34 Align_interface::align_to_fixed_distance (me, ax);
36 Align_interface::align_elements_to_extents (me, ax);
42 merge with align-to-extents?
44 MAKE_SCHEME_CALLBACK(Align_interface, stretch_after_break, 1)
46 Align_interface::stretch_after_break (SCM grob)
48 Grob *me = unsmob_grob (grob);
50 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
51 extract_grob_set (me, "elements", elems);
52 if (me_spanner && elems.size ())
54 Grob *common = common_refpoint_of_array (elems, me, Y_AXIS);
56 /* force position callbacks */
57 for (int i = 0; i < elems.size (); i++)
58 elems[i]->relative_coordinate (common, Y_AXIS);
60 SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
61 SCM extra_space_handle = scm_assoc (ly_symbol2scm ("fixed-alignment-extra-space"), details);
63 Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
64 ? scm_cdr (extra_space_handle)
68 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
70 Real delta = extra_space / elems.size() * stacking_dir;
71 for (int i = 0; i < elems.size (); i++)
72 elems[i]->translate_axis (i * delta, Y_AXIS);
75 return SCM_UNSPECIFIED;
79 merge with align-to-extents?
82 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
84 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
87 Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
89 extract_grob_set (me, "elements", elem_source);
91 Link_array<Grob> elems (elem_source); // writable..
97 Array<Real> translates;
99 for (int j = elems.size (); j--;)
102 This is not very elegant, in that we need special support for
103 hara-kiri. Unfortunately, the generic wiring of
104 force_hara_kiri_callback () (extent and offset callback) is
105 such that we might get into a loop if we call extent () or
106 offset () the elements.
109 && Hara_kiri_group_spanner::has_interface (elems[j]))
110 Hara_kiri_group_spanner::consider_suicide (elems[j]);
112 if (!elems[j]->is_live ())
116 for (int j = 0; j < elems.size (); j++)
118 where_f += stacking_dir * dy;
119 translates.push (where_f);
120 v.unite (Interval (where_f, where_f));
124 TODO: support self-alignment-{Y, X}
126 for (int i = 0; i < translates.size (); i++)
127 elems[i]->translate_axis (translates[i] - v.center (), a);
131 Hairy function to put elements where they should be. Can be tweaked
132 from the outside by setting extra-space in its
135 We assume that the children the refpoints of the children are still
136 found at 0.0 -- we will fuck up with thresholds if children's
137 extents are already moved to locations such as (-16, -8), since the
138 dy needed to put things in a row doesn't relate to the distances
139 between original refpoints.
141 TODO: maybe we should rethink and throw out thresholding altogether.
142 The original function has been taken over by
143 align_to_fixed_distance ().
147 Align_interface::align_elements_to_extents (Grob *me, Axis a)
149 Real extra_space = 0.0;
150 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
156 TODO: messes up for figured bass alignments.
158 if (me_spanner->get_bound (LEFT)->break_status_dir () == CENTER)
159 me->warning (_ ("vertical alignment called before line-breaking. "
160 "Only do cross-staff spanners with PianoStaff."));
163 SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
164 SCM extra_space_handle = scm_assoc (ly_symbol2scm ("alignment-extra-space"), details);
166 extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
167 ? scm_cdr (extra_space_handle)
172 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
175 Interval threshold = robust_scm2interval (me->get_property ("threshold"),
176 Interval (0, Interval::infinity ()));
178 Array<Interval> dims;
180 Link_array<Grob> elems;
182 extract_grob_set (me, "elements", all_grobs);
183 for (int i = 0; i < all_grobs.size (); i++)
185 Interval y = all_grobs[i]->extent (me, a);
188 Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
196 Read self-alignment-X and self-alignment-Y. This may seem like
197 code duplication. (and really: it is), but this is necessary to
198 prevent ugly cyclic dependencies that arise when you combine
199 self-alignment on a child with alignment of children.
201 SCM align ((a == X_AXIS)
202 ? me->get_property ("self-alignment-X")
203 : me->get_property ("self-alignment-Y"));
205 Array<Real> translates;
209 for (int j = 0; j < elems.size (); j++)
211 Real dy = -dims[j][-stacking_dir];
213 dy += dims[j - 1][stacking_dir];
220 dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
222 where += stacking_dir * (dy + extra_space / elems.size ());
223 total.unite (dims[j] + where);
224 translates.push (where);
227 Real center_offset = 0.0;
230 also move the grobs that were empty, to maintain spatial order.
232 Array<Real> all_translates;
233 if (translates.size ())
237 Real w = translates[0];
238 while (j < all_grobs.size ())
240 if (i < elems.size () && all_grobs[j] == elems[i])
242 all_translates.push (w);
247 FIXME: uncommenting freaks out the Y-alignment of
250 if (scm_is_number (align))
251 center_offset = total.linear_combination (scm_to_double (align));
253 for (int j = 0; j < all_grobs.size (); j++)
254 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
258 Align_interface::axis (Grob *me)
260 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
264 Align_interface::add_element (Grob *me, Grob *element)
266 Axis a = Align_interface::axis (me);
267 SCM sym = axis_offset_symbol (a);
268 SCM proc = axis_parent_positioning (a);
270 element->internal_set_property (sym, proc);
271 Axis_group_interface::add_element (me, element);
275 Align_interface::set_ordered (Grob *me)
277 SCM ga_scm = me->get_object ("elements");
278 Grob_array *ga = unsmob_grob_array (ga_scm);
281 ga_scm = Grob_array::make_array ();
282 ga = unsmob_grob_array (ga_scm);
283 me->set_object ("elements", ga_scm);
286 ga->set_ordered (true);
290 Find Y-axis parent of G that has a #'forced-distance property. This
291 has the effect of finding the piano-staff given an object in that
295 find_fixed_alignment_parent (Grob *g)
299 if (scm_is_number (g->get_property ("forced-distance")))
302 g = g->get_parent (Y_AXIS);
308 ADD_INTERFACE (Align_interface,
311 "Order grobs from top to bottom, left to right, right to left or bottom "
313 "For vertical alignments of staves, the @code{break-system-details} of "
314 "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
315 "Set @code{alignment-extra-space} to add extra space for staves. Set "
316 "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
331 bool has_interface (Grob *);