2 align-interface.cc -- implement Align_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.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, alignment_callback, 2);
26 Align_interface::alignment_callback (SCM element_smob, SCM axis)
28 Grob *me = unsmob_grob (element_smob);
29 Axis ax = (Axis)scm_to_int (axis);
30 Grob *par = me->get_parent (ax);
31 if (par && !to_boolean (par->get_property ("positioning-done")))
32 Align_interface::align_elements_to_extents (par, ax);
33 return scm_from_double (0.0);
36 MAKE_SCHEME_CALLBACK (Align_interface, fixed_distance_alignment_callback, 2);
38 Align_interface::fixed_distance_alignment_callback (SCM element_smob, SCM axis)
40 Grob *me = unsmob_grob (element_smob);
41 Axis ax = (Axis)scm_to_int (axis);
42 Grob *par = me->get_parent (ax);
43 if (par && !to_boolean (par->get_property ("positioning-done")))
44 Align_interface::align_to_fixed_distance (par, ax);
45 return scm_from_double (0.0);
49 merge with align-to-extents?
51 MAKE_SCHEME_CALLBACK(Align_interface, stretch_after_break, 1)
53 Align_interface::stretch_after_break (SCM grob)
55 Grob *me = unsmob_grob (grob);
57 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
58 extract_grob_set (me, "elements", elems);
59 if (me_spanner && elems.size ())
61 Grob *common = common_refpoint_of_array (elems, me, Y_AXIS);
63 /* force position callbacks */
64 for (int i = 0; i < elems.size (); i++)
65 elems[i]->relative_coordinate (common, Y_AXIS);
67 SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
68 SCM extra_space_handle = scm_assoc (ly_symbol2scm ("fixed-alignment-extra-space"), details);
70 Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
71 ? scm_cdr (extra_space_handle)
75 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
77 Real delta = extra_space / elems.size() * stacking_dir;
78 for (int i = 0; i < elems.size (); i++)
79 elems[i]->translate_axis (i * delta, Y_AXIS);
82 return SCM_UNSPECIFIED;
86 merge with align-to-extents?
89 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
91 me->set_property ("positioning-done", SCM_BOOL_T);
93 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
96 Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
98 extract_grob_set (me, "elements", elem_source);
100 Link_array<Grob> elems (elem_source); // writable..
106 Array<Real> translates;
108 for (int j = elems.size (); j--;)
111 This is not very elegant, in that we need special support for
112 hara-kiri. Unfortunately, the generic wiring of
113 force_hara_kiri_callback () (extent and offset callback) is
114 such that we might get into a loop if we call extent () or
115 offset () the elements.
118 && Hara_kiri_group_spanner::has_interface (elems[j]))
119 Hara_kiri_group_spanner::consider_suicide (elems[j]);
121 if (!elems[j]->is_live ())
125 for (int j = 0; j < elems.size (); j++)
127 where_f += stacking_dir * dy;
128 translates.push (where_f);
129 v.unite (Interval (where_f, where_f));
133 TODO: support self-alignment-{Y, X}
135 for (int i = 0; i < translates.size (); i++)
136 elems[i]->translate_axis (translates[i] - v.center (), a);
140 Hairy function to put elements where they should be. Can be tweaked
141 from the outside by setting extra-space in its
144 We assume that the children the refpoints of the children are still
145 found at 0.0 -- we will fuck up with thresholds if children's
146 extents are already moved to locations such as (-16, -8), since the
147 dy needed to put things in a row doesn't relate to the distances
148 between original refpoints.
150 TODO: maybe we should rethink and throw out thresholding altogether.
151 The original function has been taken over by
152 align_to_fixed_distance ().
155 Align_interface::align_elements_to_extents (Grob *me, Axis a)
157 Real extra_space = 0.0;
158 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
164 TODO: messes up for figured bass alignments.
166 if (me_spanner->get_bound (LEFT)->break_status_dir () == CENTER)
167 me->warning (_ ("vertical alignment called before line-breaking. "
168 "Only do cross-staff spanners with PianoStaff."));
171 SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
172 SCM extra_space_handle = scm_assoc (ly_symbol2scm ("alignment-extra-space"), details);
174 extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
175 ? scm_cdr (extra_space_handle)
180 me->set_property ("positioning-done", SCM_BOOL_T);
182 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
185 Interval threshold = robust_scm2interval (me->get_property ("threshold"),
186 Interval (0, Interval::infinity ()));
188 Array<Interval> dims;
190 Link_array<Grob> elems;
192 extract_grob_set (me, "elements", all_grobs);
193 for (int i = 0; i < all_grobs.size (); i++)
195 Interval y = all_grobs[i]->extent (me, a);
198 Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
206 Read self-alignment-X and self-alignment-Y. This may seem like
207 code duplication. (and really: it is), but this is necessary to
208 prevent ugly cyclic dependencies that arise when you combine
209 self-alignment on a child with alignment of children.
211 SCM align ((a == X_AXIS)
212 ? me->get_property ("self-alignment-X")
213 : me->get_property ("self-alignment-Y"));
215 Array<Real> translates;
219 for (int j = 0; j < elems.size (); j++)
221 Real dy = -dims[j][-stacking_dir];
223 dy += dims[j - 1][stacking_dir];
230 dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
232 where += stacking_dir * (dy + extra_space / elems.size ());
233 total.unite (dims[j] + where);
234 translates.push (where);
237 Real center_offset = 0.0;
240 also move the grobs that were empty, to maintain spatial order.
242 Array<Real> all_translates;
243 if (translates.size ())
247 Real w = translates[0];
248 while (j < all_grobs.size ())
250 if (i < elems.size () && all_grobs[j] == elems[i])
252 all_translates.push (w);
257 FIXME: uncommenting freaks out the Y-alignment of
260 if (scm_is_number (align))
261 center_offset = total.linear_combination (scm_to_double (align));
263 for (int j = 0; j < all_grobs.size (); j++)
264 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
268 Align_interface::axis (Grob *me)
270 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
274 Align_interface::add_element (Grob *me, Grob *element, SCM call_back)
276 element->add_offset_callback (call_back, Align_interface::axis (me));
277 Axis_group_interface::add_element (me, element);
281 Align_interface::set_axis (Grob *me, Axis a)
283 Axis_group_interface::set_axes (me, a, a);
284 SCM ga_scm = me->get_object ("elements");
285 Grob_array *ga = unsmob_grob_array (ga_scm);
288 ga_scm = Grob_array::make_array ();
289 ga = unsmob_grob_array (ga_scm);
290 me->set_object ("elements", ga_scm);
293 ga->set_ordered (true);
297 Find Y-axis parent of G that has a #'forced-distance property. This
298 has the effect of finding the piano-staff given an object in that
302 find_fixed_alignment_parent (Grob *g)
306 if (scm_is_number (g->get_property ("forced-distance")))
309 g = g->get_parent (Y_AXIS);
315 ADD_INTERFACE (Align_interface,
318 "Order grobs from top to bottom, left to right, right to left or bottom "
320 "For vertical alignments of staves, the @code{break-system-details} of "
321 "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
322 "Set @code{alignment-extra-space} to add extra space for staves. Set "
323 "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
338 bool has_interface (Grob *);