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 (vsize 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 (vsize 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 std::vector<Real> translates;
99 for (vsize 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 ())
113 elems.erase (elems.begin () + j);
116 for (vsize j = 0; j < elems.size (); j++)
118 where_f += stacking_dir * dy;
119 translates.push_back (where_f);
120 v.unite (Interval (where_f, where_f));
124 TODO: support self-alignment-{Y, X}
126 for (vsize 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 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
152 SCM line_break_details = SCM_EOL;
153 if (a == Y_AXIS && me_spanner)
154 line_break_details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
156 Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
159 Interval threshold = robust_scm2interval (me->get_property ("threshold"),
160 Interval (0, Interval::infinity ()));
162 std::vector<Interval> dims;
163 Link_array<Grob> elems;
165 extract_grob_set (me, "elements", all_grobs);
166 for (vsize i = 0; i < all_grobs.size (); i++)
168 Interval y = all_grobs[i]->extent (me, a);
171 Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
179 Read self-alignment-X and self-alignment-Y. This may seem like
180 code duplication. (and really: it is), but this is necessary to
181 prevent ugly cyclic dependencies that arise when you combine
182 self-alignment on a child with alignment of children.
184 SCM align ((a == X_AXIS)
185 ? me->get_property ("self-alignment-X")
186 : me->get_property ("self-alignment-Y"));
190 Real extra_space = 0.0;
191 SCM extra_space_handle = scm_assq (ly_symbol2scm ("alignment-extra-space"), line_break_details);
193 extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
194 ? scm_cdr (extra_space_handle)
198 std::vector<Real> translates;
199 for (vsize j = 0; j < elems.size (); j++)
201 Real dy = -dims[j][-stacking_dir];
203 dy += dims[j - 1][stacking_dir];
210 dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
212 where += stacking_dir * (dy + extra_space / elems.size ());
213 total.unite (dims[j] + where);
214 translates.push_back (where);
217 SCM offsets_handle = scm_assq (ly_symbol2scm ("alignment-offsets"), line_break_details);
218 if (scm_is_pair (offsets_handle))
222 for (SCM s = scm_cdr (offsets_handle); scm_is_pair (s) && i < translates.size (); s = scm_cdr (s), i++)
224 if (scm_is_number (scm_car (s)))
225 translates[i] = scm_to_double (scm_car (s));
230 Real center_offset = 0.0;
233 also move the grobs that were empty, to maintain spatial order.
235 std::vector<Real> all_translates;
236 if (translates.size ())
238 Real w = translates[0];
239 for (vsize i = 0, j = 0; j < all_grobs.size (); j++)
241 if (i < elems.size () && all_grobs[j] == elems[i])
243 all_translates.push_back (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 (vsize 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 *);