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"
13 #include "axis-group-interface.hh"
14 #include "pointer-group-interface.hh"
15 #include "hara-kiri-group-spanner.hh"
16 #include "grob-array.hh"
18 MAKE_SCHEME_CALLBACK (Align_interface, alignment_callback, 2);
20 Align_interface::alignment_callback (SCM element_smob, SCM axis)
22 Grob *me = unsmob_grob (element_smob);
23 Axis ax = (Axis)scm_to_int (axis);
24 Grob *par = me->get_parent (ax);
25 if (par && !to_boolean (par->get_property ("positioning-done")))
26 Align_interface::align_elements_to_extents (par, ax);
27 return scm_from_double (0.0);
30 MAKE_SCHEME_CALLBACK (Align_interface, fixed_distance_alignment_callback, 2);
32 Align_interface::fixed_distance_alignment_callback (SCM element_smob, SCM axis)
34 Grob *me = unsmob_grob (element_smob);
35 Axis ax = (Axis)scm_to_int (axis);
36 Grob *par = me->get_parent (ax);
37 if (par && !to_boolean (par->get_property ("positioning-done")))
38 Align_interface::align_to_fixed_distance (par, ax);
39 return scm_from_double (0.0);
43 merge with align-to-extents?
45 MAKE_SCHEME_CALLBACK(Align_interface, stretch_after_break, 1)
47 Align_interface::stretch_after_break (SCM grob)
49 Grob *me = unsmob_grob (grob);
51 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
52 extract_grob_set (me, "elements", elems);
53 if (me_spanner && elems.size ())
55 Grob *common = common_refpoint_of_array (elems, me, Y_AXIS);
57 /* force position callbacks */
58 for (int i = 0; i < elems.size (); i++)
59 elems[i]->relative_coordinate (common, Y_AXIS);
61 SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
62 SCM extra_space_handle = scm_assoc (ly_symbol2scm ("fixed-alignment-extra-space"), details);
64 Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
65 ? scm_cdr (extra_space_handle)
69 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
72 Real delta = extra_space / elems.size();
73 for (int i = 0; i < elems.size (); i++)
74 elems[i]->translate_axis (i * delta, Y_AXIS);
77 return SCM_UNSPECIFIED;
81 merge with align-to-extents?
84 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
86 me->set_property ("positioning-done", SCM_BOOL_T);
88 SCM d = me->get_property ("stacking-dir");
90 Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
94 Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
96 extract_grob_set (me, "elements", elem_source);
98 Link_array<Grob> elems (elem_source); // writable..
104 Array<Real> translates;
106 for (int j = elems.size (); j--;)
109 This is not very elegant, in that we need special support for
110 hara-kiri. Unfortunately, the generic wiring of
111 force_hara_kiri_callback () (extent and offset callback) is
112 such that we might get into a loop if we call extent () or
113 offset () the elements.
116 && Hara_kiri_group_spanner::has_interface (elems[j]))
117 Hara_kiri_group_spanner::consider_suicide (elems[j]);
119 if (!elems[j]->is_live ())
123 for (int j = 0; j < elems.size (); j++)
125 where_f += stacking_dir * dy;
126 translates.push (where_f);
127 v.unite (Interval (where_f, where_f));
131 TODO: support self-alignment-{Y, X}
133 for (int i = 0; i < translates.size (); i++)
134 elems[i]->translate_axis (translates[i] - v.center (), a);
138 Hairy function to put elements where they should be. Can be tweaked
139 from the outside by setting extra-space in its
142 We assume that the children the refpoints of the children are still
143 found at 0.0 -- we will fuck up with thresholds if children's
144 extents are already moved to locations such as (-16, -8), since the
145 dy needed to put things in a row doesn't relate to the distances
146 between original refpoints.
148 TODO: maybe we should rethink and throw out thresholding altogether.
149 The original function has been taken over by
150 align_to_fixed_distance ().
153 Align_interface::align_elements_to_extents (Grob *me, Axis a)
155 Real extra_space = 0.0;
156 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
160 if (me_spanner->get_bound (LEFT)->break_status_dir () == CENTER)
161 me->warning (_ ("vertical alignment called before line-breaking. "
162 "Only do cross-staff spanners with PianoStaff."));
164 SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
165 SCM extra_space_handle = scm_assoc (ly_symbol2scm ("alignment-extra-space"), details);
167 extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
168 ? scm_cdr (extra_space_handle)
173 me->set_property ("positioning-done", SCM_BOOL_T);
175 SCM d = me->get_property ("stacking-dir");
177 Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
181 Interval threshold = robust_scm2interval (me->get_property ("threshold"),
182 Interval (0, Interval::infinity ()));
184 Array<Interval> dims;
186 Link_array<Grob> elems;
188 extract_grob_set (me, "elements", all_grobs);
189 for (int i = 0; i < all_grobs.size (); i++)
191 Interval y = all_grobs[i]->extent (me, a);
194 Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
202 Read self-alignment-X and self-alignment-Y. This may seem like
203 code duplication. (and really: it is), but this is necessary to
204 prevent ugly cyclic dependencies that arise when you combine
205 self-alignment on a child with alignment of children.
207 SCM align ((a == X_AXIS)
208 ? me->get_property ("self-alignment-X")
209 : me->get_property ("self-alignment-Y"));
211 Array<Real> translates;
215 for (int j = 0; j < elems.size (); j++)
217 Real dy = -dims[j][-stacking_dir];
219 dy += dims[j - 1][stacking_dir];
226 dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
228 where += stacking_dir * (dy + extra_space / elems.size ());
229 total.unite (dims[j] + where);
230 translates.push (where);
233 Real center_offset = 0.0;
236 also move the grobs that were empty, to maintain spatial order.
238 Array<Real> all_translates;
239 if (translates.size ())
243 Real w = translates[0];
244 while (j < all_grobs.size ())
246 if (i < elems.size () && all_grobs[j] == elems[i])
248 all_translates.push (w);
253 FIXME: uncommenting freaks out the Y-alignment of
256 if (scm_is_number (align))
257 center_offset = total.linear_combination (scm_to_double (align));
259 for (int j = 0; j < all_grobs.size (); j++)
260 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
264 Align_interface::axis (Grob *me)
266 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
270 Align_interface::add_element (Grob *me, Grob *element, SCM call_back)
272 element->add_offset_callback (call_back, Align_interface::axis (me));
273 Axis_group_interface::add_element (me, element);
277 Align_interface::set_axis (Grob *me, Axis a)
279 Axis_group_interface::set_axes (me, a, a);
280 SCM ga_scm = me->get_object ("elements");
281 Grob_array *ga = unsmob_grob_array (ga_scm);
284 ga_scm = Grob_array::make_array ();
285 ga = unsmob_grob_array (ga_scm);
286 me->set_object ("elements", ga_scm);
289 ga->set_ordered (true);
293 Find Y-axis parent of G that has a #'forced-distance property. This
294 has the effect of finding the piano-staff given an object in that
298 find_fixed_alignment_parent (Grob *g)
302 if (scm_is_number (g->get_property ("forced-distance")))
305 g = g->get_parent (Y_AXIS);
311 ADD_INTERFACE (Align_interface, "align-interface",
312 "Order grobs from top to bottom, left to right, right to left or bottom "
327 bool has_interface (Grob *);