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?
46 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
48 me->set_property ("positioning-done", SCM_BOOL_T);
50 SCM d = me->get_property ("stacking-dir");
52 Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
56 Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
58 extract_grob_set (me, "elements", elem_source);
60 Link_array<Grob> elems (elem_source); // writable..
66 Array<Real> translates;
68 for (int j = elems.size (); j--;)
71 This is not very elegant, in that we need special support for
72 hara-kiri. Unfortunately, the generic wiring of
73 force_hara_kiri_callback () (extent and offset callback) is
74 such that we might get into a loop if we call extent () or
75 offset () the elements.
80 && Hara_kiri_group_spanner::has_interface (elems[j]))
81 Hara_kiri_group_spanner::consider_suicide (elems[j]);
83 if (!elems[j]->is_live ())
87 for (int j = 0; j < elems.size (); j++)
89 where_f += stacking_dir * dy;
90 translates.push (where_f);
91 v.unite (Interval (where_f, where_f));
95 TODO: support self-alignment-{Y, X}
97 for (int i = 0; i < translates.size (); i++)
98 elems[i]->translate_axis (translates[i] - v.center (), a);
102 Hairy function to put elements where they should be. Can be tweaked
103 from the outside by setting extra-space in its
106 We assume that the children the refpoints of the children are still
107 found at 0.0 -- we will fuck up with thresholds if children's
108 extents are already moved to locations such as (-16, -8), since the
109 dy needed to put things in a row doesn't relate to the distances
110 between original refpoints.
112 TODO: maybe we should rethink and throw out thresholding altogether.
113 The original function has been taken over by
114 align_to_fixed_distance ().
117 Align_interface::align_elements_to_extents (Grob *me, Axis a)
119 Spanner *me_spanner = dynamic_cast<Spanner *> (me);
122 && me_spanner->get_bound (LEFT)->break_status_dir () == CENTER)
123 me_spanner->warning (_ ("vertical alignment called before line-breaking. Only do cross-staff spanners with PianoStaff."));
125 me->set_property ("positioning-done", SCM_BOOL_T);
127 SCM d = me->get_property ("stacking-dir");
129 Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
133 Interval threshold = robust_scm2interval (me->get_property ("threshold"),
134 Interval (0, Interval::infinity ()));
136 Array<Interval> dims;
138 Link_array<Grob> elems;
140 extract_grob_set (me, "elements", all_grobs);
141 for (int i = 0; i < all_grobs.size (); i++)
143 Interval y = all_grobs[i]->extent (me, a);
146 Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
154 Read self-alignment-X and self-alignment-Y. This may seem like
155 code duplication. (and really: it is), but this is necessary to
156 prevent ugly cyclic dependencies that arise when you combine
157 self-alignment on a child with alignment of children.
159 static SCM prop_syms[2];
163 prop_syms[X_AXIS] = ly_symbol2scm ("self-alignment-X");
164 prop_syms[Y_AXIS] = ly_symbol2scm ("self-alignment-Y");
167 SCM align (me->internal_get_property (prop_syms[a]));
169 Array<Real> translates;
173 for (int j = 0; j < elems.size (); j++)
175 Real dy = -dims[j][-stacking_dir];
177 dy += dims[j - 1][stacking_dir];
185 dy = min (max (dy, threshold[SMALLER]),
189 where_f += stacking_dir * dy;
190 total.unite (dims[j] + where_f);
191 translates.push (where_f);
194 Real center_offset = 0.0;
196 also move the grobs that were empty, to maintain spatial order.
198 Array<Real> all_translates;
199 if (translates.size ())
203 Real w = translates[0];
204 while (j < all_grobs.size ())
206 if (i < elems.size () && all_grobs[j] == elems[i])
208 all_translates.push (w);
213 FIXME: uncommenting freaks out the Y-alignment of
216 if (scm_is_number (align))
217 center_offset = total.linear_combination (scm_to_double (align));
219 for (int j = 0; j < all_grobs.size (); j++)
220 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
224 Align_interface::axis (Grob *me)
226 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
230 Align_interface::add_element (Grob *me, Grob *element, SCM call_back)
232 element->add_offset_callback (call_back, Align_interface::axis (me));
233 Axis_group_interface::add_element (me, element);
237 Align_interface::set_axis (Grob *me, Axis a)
239 Axis_group_interface::set_axes (me, a, a);
240 SCM ga_scm = me->get_object ("elements");
241 Grob_array *ga = unsmob_grob_array (ga_scm);
244 ga_scm = Grob_array::make_array ();
245 ga = unsmob_grob_array (ga_scm);
246 me->set_object ("elements", ga_scm);
249 ga->set_ordered (true);
253 Find Y-axis parent of G that has a #'forced-distance property. This
254 has the effect of finding the piano-staff given an object in that
258 find_fixed_alignment_parent (Grob *g)
262 if (scm_is_number (g->get_property ("forced-distance")))
265 g = g->get_parent (Y_AXIS);
271 ADD_INTERFACE (Align_interface, "align-interface",
272 "Order grobs from top to bottom, left to right, right to left or bottom"
274 "forced-distance stacking-dir align-dir threshold positioning-done "
279 bool has_interface (Grob *);