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);
162 if (me_spanner->get_bound (LEFT)->break_status_dir () == CENTER)
163 me->warning (_ ("vertical alignment called before line-breaking. "
164 "Only do cross-staff spanners with PianoStaff."));
166 SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
167 SCM extra_space_handle = scm_assoc (ly_symbol2scm ("alignment-extra-space"), details);
169 extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
170 ? scm_cdr (extra_space_handle)
175 me->set_property ("positioning-done", SCM_BOOL_T);
177 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
180 Interval threshold = robust_scm2interval (me->get_property ("threshold"),
181 Interval (0, Interval::infinity ()));
183 Array<Interval> dims;
185 Link_array<Grob> elems;
187 extract_grob_set (me, "elements", all_grobs);
188 for (int i = 0; i < all_grobs.size (); i++)
190 Interval y = all_grobs[i]->extent (me, a);
193 Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
201 Read self-alignment-X and self-alignment-Y. This may seem like
202 code duplication. (and really: it is), but this is necessary to
203 prevent ugly cyclic dependencies that arise when you combine
204 self-alignment on a child with alignment of children.
206 SCM align ((a == X_AXIS)
207 ? me->get_property ("self-alignment-X")
208 : me->get_property ("self-alignment-Y"));
210 Array<Real> translates;
214 for (int j = 0; j < elems.size (); j++)
216 Real dy = -dims[j][-stacking_dir];
218 dy += dims[j - 1][stacking_dir];
225 dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
227 where += stacking_dir * (dy + extra_space / elems.size ());
228 total.unite (dims[j] + where);
229 translates.push (where);
232 Real center_offset = 0.0;
235 also move the grobs that were empty, to maintain spatial order.
237 Array<Real> all_translates;
238 if (translates.size ())
242 Real w = translates[0];
243 while (j < all_grobs.size ())
245 if (i < elems.size () && all_grobs[j] == elems[i])
247 all_translates.push (w);
252 FIXME: uncommenting freaks out the Y-alignment of
255 if (scm_is_number (align))
256 center_offset = total.linear_combination (scm_to_double (align));
258 for (int j = 0; j < all_grobs.size (); j++)
259 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
263 Align_interface::axis (Grob *me)
265 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
269 Align_interface::add_element (Grob *me, Grob *element, SCM call_back)
271 element->add_offset_callback (call_back, Align_interface::axis (me));
272 Axis_group_interface::add_element (me, element);
276 Align_interface::set_axis (Grob *me, Axis a)
278 Axis_group_interface::set_axes (me, a, a);
279 SCM ga_scm = me->get_object ("elements");
280 Grob_array *ga = unsmob_grob_array (ga_scm);
283 ga_scm = Grob_array::make_array ();
284 ga = unsmob_grob_array (ga_scm);
285 me->set_object ("elements", ga_scm);
288 ga->set_ordered (true);
292 Find Y-axis parent of G that has a #'forced-distance property. This
293 has the effect of finding the piano-staff given an object in that
297 find_fixed_alignment_parent (Grob *g)
301 if (scm_is_number (g->get_property ("forced-distance")))
304 g = g->get_parent (Y_AXIS);
310 ADD_INTERFACE (Align_interface,
313 "Order grobs from top to bottom, left to right, right to left or bottom "
315 "For vertical alignments of staves, the @code{break-system-details} of "
316 "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
317 "Set @code{alignment-extra-space} to add extra space for staves. Set "
318 "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
333 bool has_interface (Grob *);