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"
19 TODO: for vertical spacing, should also include a rod & spring
20 scheme of sorts into this: the alignment should default to a certain
21 distance between element refpoints, unless bbox force a bigger
25 MAKE_SCHEME_CALLBACK (Align_interface, calc_positioning_done, 1);
27 Align_interface::calc_positioning_done (SCM smob)
29 Grob *me = unsmob_grob (smob);
30 SCM axis = scm_car (me->get_property ("axes"));
31 Axis ax = Axis (scm_to_int (axis));
33 SCM force = me->get_property ("forced-distance");
34 if (scm_is_number (force))
35 Align_interface::align_to_fixed_distance (me, ax);
37 Align_interface::align_elements_to_extents (me, ax);
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);
54 if (me_spanner && elems.size ())
56 Grob *common = common_refpoint_of_array (elems, me, Y_AXIS);
58 /* force position callbacks */
59 for (vsize i = 0; i < elems.size (); i++)
60 elems[i]->relative_coordinate (common, Y_AXIS);
62 SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
63 SCM extra_space_handle = scm_assoc (ly_symbol2scm ("fixed-alignment-extra-space"), details);
65 Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
66 ? scm_cdr (extra_space_handle)
70 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
72 Real delta = extra_space / elems.size() * stacking_dir;
73 for (vsize 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 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
89 Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
91 extract_grob_set (me, "elements", elem_source);
93 vector<Grob*> elems (elem_source); // writable..
99 vector<Real> translates;
101 for (vsize j = elems.size (); j--;)
104 This is not very elegant, in that we need special support for
105 hara-kiri. Unfortunately, the generic wiring of
106 force_hara_kiri_callback () (extent and offset callback) is
107 such that we might get into a loop if we call extent () or
108 offset () the elements.
111 && Hara_kiri_group_spanner::has_interface (elems[j]))
112 Hara_kiri_group_spanner::consider_suicide (elems[j]);
114 if (!elems[j]->is_live ())
115 elems.erase (elems.begin () + j);
118 for (vsize j = 0; j < elems.size (); j++)
120 where_f += stacking_dir * dy;
121 translates.push_back (where_f);
122 v.unite (Interval (where_f, where_f));
126 TODO: support self-alignment-{Y, X}
128 for (vsize i = 0; i < translates.size (); i++)
129 elems[i]->translate_axis (translates[i] - v.center (), a);
133 Hairy function to put elements where they should be. Can be tweaked
134 from the outside by setting extra-space in its
137 We assume that the children the refpoints of the children are still
138 found at 0.0 -- we will fuck up with thresholds if children's
139 extents are already moved to locations such as (-16, -8), since the
140 dy needed to put things in a row doesn't relate to the distances
141 between original refpoints.
143 TODO: maybe we should rethink and throw out thresholding altogether.
144 The original function has been taken over by
145 align_to_fixed_distance ().
149 Align_interface::align_elements_to_extents (Grob *me, Axis a)
151 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
154 SCM line_break_details = SCM_EOL;
155 if (a == Y_AXIS && me_spanner)
157 line_break_details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
159 if (!me->get_system ())
160 me->warning (_ ("vertical alignment called before line-breaking.\n"
161 "Only do cross-staff spanners with PianoStaff."));
165 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
168 Interval threshold = robust_scm2interval (me->get_property ("threshold"),
169 Interval (0, Interval::infinity ()));
171 vector<Interval> dims;
174 extract_grob_set (me, "elements", all_grobs);
175 for (vsize i = 0; i < all_grobs.size (); i++)
177 Interval y = all_grobs[i]->extent (me, a);
180 Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
188 Read self-alignment-X and self-alignment-Y. This may seem like
189 code duplication. (and really: it is), but this is necessary to
190 prevent ugly cyclic dependencies that arise when you combine
191 self-alignment on a child with alignment of children.
193 SCM align ((a == X_AXIS)
194 ? me->get_property ("self-alignment-X")
195 : me->get_property ("self-alignment-Y"));
199 Real extra_space = 0.0;
200 SCM extra_space_handle = scm_assq (ly_symbol2scm ("alignment-extra-space"), line_break_details);
202 extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
203 ? scm_cdr (extra_space_handle)
207 vector<Real> translates;
208 for (vsize j = 0; j < elems.size (); j++)
210 Real dy = -dims[j][-stacking_dir];
212 dy += dims[j - 1][stacking_dir];
219 dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
221 where += stacking_dir * (dy + extra_space / elems.size ());
222 total.unite (dims[j] + where);
223 translates.push_back (where);
226 SCM offsets_handle = scm_assq (ly_symbol2scm ("alignment-offsets"), line_break_details);
227 if (scm_is_pair (offsets_handle))
231 for (SCM s = scm_cdr (offsets_handle); scm_is_pair (s) && i < translates.size (); s = scm_cdr (s), i++)
233 if (scm_is_number (scm_car (s)))
234 translates[i] = scm_to_double (scm_car (s));
239 Real center_offset = 0.0;
242 also move the grobs that were empty, to maintain spatial order.
244 vector<Real> all_translates;
245 if (translates.size ())
247 Real w = translates[0];
248 for (vsize i = 0, j = 0; j < all_grobs.size (); j++)
250 if (i < elems.size () && all_grobs[j] == elems[i])
252 all_translates.push_back (w);
256 FIXME: uncommenting freaks out the Y-alignment of
259 if (scm_is_number (align))
260 center_offset = total.linear_combination (scm_to_double (align));
262 for (vsize j = 0; j < all_grobs.size (); j++)
263 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
267 Align_interface::axis (Grob *me)
269 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
273 Align_interface::add_element (Grob *me, Grob *element)
275 Axis a = Align_interface::axis (me);
276 SCM sym = axis_offset_symbol (a);
277 SCM proc = axis_parent_positioning (a);
279 element->internal_set_property (sym, proc);
280 Axis_group_interface::add_element (me, element);
284 Align_interface::set_ordered (Grob *me)
286 SCM ga_scm = me->get_object ("elements");
287 Grob_array *ga = unsmob_grob_array (ga_scm);
290 ga_scm = Grob_array::make_array ();
291 ga = unsmob_grob_array (ga_scm);
292 me->set_object ("elements", ga_scm);
295 ga->set_ordered (true);
299 Find Y-axis parent of G that has a #'forced-distance property. This
300 has the effect of finding the piano-staff given an object in that
304 find_fixed_alignment_parent (Grob *g)
308 if (scm_is_number (g->get_property ("forced-distance")))
311 g = g->get_parent (Y_AXIS);
317 ADD_INTERFACE (Align_interface,
320 "Order grobs from top to bottom, left to right, right to left or bottom "
322 "For vertical alignments of staves, the @code{break-system-details} of "
323 "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
324 "Set @code{alignment-extra-space} to add extra space for staves. Set "
325 "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
340 bool has_interface (Grob *);