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")))
27 Align_interface::align_elements_to_extents (par, ax);
29 return scm_make_real (0.0);
32 MAKE_SCHEME_CALLBACK (Align_interface, fixed_distance_alignment_callback, 2);
34 Align_interface::fixed_distance_alignment_callback (SCM element_smob, SCM axis)
36 Grob *me = unsmob_grob (element_smob);
37 Axis ax = (Axis)scm_to_int (axis);
38 Grob *par = me->get_parent (ax);
39 if (par && !to_boolean (par->get_property ("positioning-done")))
41 Align_interface::align_to_fixed_distance (par, ax);
43 return scm_make_real (0.0);
47 merge with align-to-extents?
50 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
52 me->set_property ("positioning-done", SCM_BOOL_T);
54 SCM d = me->get_property ("stacking-dir");
56 Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
60 Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
62 extract_grob_set (me, "elements", elem_source);
64 Link_array<Grob> elems (elem_source); // writable..
70 Array<Real> translates;
72 for (int j = elems.size (); j--;)
75 This is not very elegant, in that we need special support for
76 hara-kiri. Unfortunately, the generic wiring of
77 force_hara_kiri_callback () (extent and offset callback) is
78 such that we might get into a loop if we call extent () or
79 offset () the elements.
84 && Hara_kiri_group_spanner::has_interface (elems[j]))
85 Hara_kiri_group_spanner::consider_suicide (elems[j]);
87 if (!elems[j]->is_live ())
91 for (int j = 0; j < elems.size (); j++)
93 where_f += stacking_dir * dy;
94 translates.push (where_f);
95 v.unite (Interval (where_f, where_f));
99 TODO: support self-alignment-{Y, X}
101 for (int i = 0; i < translates.size (); i++)
103 elems[i]->translate_axis (translates[i] - v.center (), a);
108 Hairy function to put elements where they should be. Can be tweaked
109 from the outside by setting extra-space in its
112 We assume that the children the refpoints of the children are still
113 found at 0.0 -- we will fuck up with thresholds if children's
114 extents are already moved to locations such as (-16, -8), since the
115 dy needed to put things in a row doesn't relate to the distances
116 between original refpoints.
118 TODO: maybe we should rethink and throw out thresholding altogether.
119 The original function has been taken over by
120 align_to_fixed_distance ().
123 Align_interface::align_elements_to_extents (Grob *me, Axis a)
125 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
128 && me_spanner->get_bound (LEFT)->break_status_dir () == CENTER)
130 me_spanner->warning (_("vertical alignment called before line-breaking. Only do cross-staff spanners with PianoStaff."));
133 me->set_property ("positioning-done", SCM_BOOL_T);
135 SCM d = me->get_property ("stacking-dir");
137 Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
141 Interval threshold = robust_scm2interval (me->get_property ("threshold"),
142 Interval (0, Interval::infinity ()));
144 Array<Interval> dims;
146 Link_array<Grob> elems;
148 extract_grob_set (me, "elements", all_grobs);
149 for (int i = 0; i < all_grobs.size (); i++)
151 Interval y = all_grobs[i]->extent (me, a);
154 Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
162 Read self-alignment-X and self-alignment-Y. This may seem like
163 code duplication. (and really: it is), but this is necessary to
164 prevent ugly cyclic dependencies that arise when you combine
165 self-alignment on a child with alignment of children.
167 static SCM prop_syms[2];
171 prop_syms[X_AXIS] = ly_symbol2scm ("self-alignment-X");
172 prop_syms[Y_AXIS] = ly_symbol2scm ("self-alignment-Y");
175 SCM align (me->internal_get_property (prop_syms[a]));
177 Array<Real> translates;
181 for (int j = 0; j < elems.size (); j++)
183 Real dy = -dims[j][-stacking_dir];
185 dy += dims[j - 1][stacking_dir];
193 dy = min (max (dy, threshold[SMALLER]),
197 where_f += stacking_dir * dy;
198 total.unite (dims[j] + where_f);
199 translates.push (where_f);
202 Real center_offset = 0.0;
204 also move the grobs that were empty, to maintain spatial order.
206 Array<Real> all_translates;
207 if (translates.size ())
211 Real w = translates[0];
212 while (j < all_grobs.size ())
214 if (i < elems.size () && all_grobs[j] == elems[i])
218 all_translates.push (w);
223 FIXME: uncommenting freaks out the Y-alignment of
226 if (scm_is_number (align))
227 center_offset = total.linear_combination (scm_to_double (align));
229 for (int j = 0; j < all_grobs.size (); j++)
230 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
234 Align_interface::axis (Grob *me)
236 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
240 Align_interface::add_element (Grob *me, Grob *element, SCM call_back)
242 element->add_offset_callback (call_back, Align_interface::axis (me));
243 Axis_group_interface::add_element (me, element);
247 Align_interface::set_axis (Grob *me, Axis a)
249 Axis_group_interface::set_axes (me, a, a);
250 SCM ga_scm = me->get_object ("elements");
251 Grob_array *ga = unsmob_grob_array (ga_scm);
254 ga_scm = Grob_array::make_array ();
255 ga = unsmob_grob_array (ga_scm);
256 me->set_object ("elements", ga_scm);
259 ga->set_ordered (true);
264 Find Y-axis parent of G that has a #'forced-distance property. This
265 has the effect of finding the piano-staff given an object in that
269 find_fixed_alignment_parent (Grob *g)
273 if (scm_is_number (g->get_property ("forced-distance")))
276 g = g->get_parent (Y_AXIS);
282 ADD_INTERFACE (Align_interface, "align-interface",
283 "Order grobs from top to bottom, left to right, right to left or bottom"
285 "forced-distance stacking-dir align-dir threshold positioning-done "
290 bool has_interface (Grob *);