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"
11 #include "axis-group-interface.hh"
12 #include "hara-kiri-group-spanner.hh"
13 #include "output-def.hh"
15 MAKE_SCHEME_CALLBACK (Align_interface, alignment_callback, 2);
17 Align_interface::alignment_callback (SCM element_smob, SCM axis)
19 Grob *me = unsmob_grob (element_smob);
20 Axis ax = (Axis)scm_to_int (axis);
21 Grob *par = me->get_parent (ax);
22 if (par && !to_boolean (par->get_property ("positioning-done")))
24 Align_interface::align_elements_to_extents (par, ax);
26 return scm_make_real (0.0);
29 MAKE_SCHEME_CALLBACK (Align_interface, fixed_distance_alignment_callback, 2);
31 Align_interface::fixed_distance_alignment_callback (SCM element_smob, SCM axis)
33 Grob *me = unsmob_grob (element_smob);
34 Axis ax = (Axis)scm_to_int (axis);
35 Grob *par = me->get_parent (ax);
36 if (par && !to_boolean (par->get_property ("positioning-done")))
38 Align_interface::align_to_fixed_distance (par, ax);
40 return scm_make_real (0.0);
44 merge with align-to-extents?
47 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
49 me->set_property ("positioning-done", SCM_BOOL_T);
51 SCM d = me->get_property ("stacking-dir");
53 Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
57 Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
59 Link_array<Grob> elems
60 = extract_grob_array (me, ly_symbol2scm ("elements"));
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++)
99 elems[i]->translate_axis (translates[i] - v.center (), a);
104 Hairy function to put elements where they should be. Can be tweaked
105 from the outside by setting extra-space in its
108 We assume that the children the refpoints of the children are still
109 found at 0.0 -- we will fuck up with thresholds if children's
110 extents are already moved to locations such as (-16, -8), since the
111 dy needed to put things in a row doesn't relate to the distances
112 between original refpoints.
114 TODO: maybe we should rethink and throw out thresholding altogether.
115 The original function has been taken over by
116 align_to_fixed_distance ().
119 Align_interface::align_elements_to_extents (Grob *me, Axis a)
121 me->set_property ("positioning-done", SCM_BOOL_T);
123 SCM d = me->get_property ("stacking-dir");
125 Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
129 Interval threshold = robust_scm2interval (me->get_property ("threshold"),
130 Interval (0, Interval::infinity ()));
132 Array<Interval> dims;
134 Link_array<Grob> elems;
135 Link_array<Grob> all_grobs
136 = extract_grob_array (me, ly_symbol2scm ("elements"));
137 for (int i = 0; i < all_grobs.size (); i++)
139 Interval y = all_grobs[i]->extent (me, a);
142 Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
150 Read self-alignment-X and self-alignment-Y. This may seem like
151 code duplication. (and really: it is), but this is necessary to
152 prevent ugly cyclic dependencies that arise when you combine
153 self-alignment on a child with alignment of children.
155 static SCM prop_syms[2];
159 prop_syms[X_AXIS] = ly_symbol2scm ("self-alignment-X");
160 prop_syms[Y_AXIS] = ly_symbol2scm ("self-alignment-Y");
163 SCM align (me->internal_get_property (prop_syms[a]));
165 Array<Real> translates;
169 for (int j = 0; j < elems.size (); j++)
171 Real dy = -dims[j][-stacking_dir];
173 dy += dims[j - 1][stacking_dir];
181 dy = (dy >? threshold[SMALLER])
182 <? threshold[BIGGER];
185 where_f += stacking_dir * dy;
186 total.unite (dims[j] + where_f);
187 translates.push (where_f);
190 Real center_offset = 0.0;
192 also move the grobs that were empty, to maintain spatial order.
194 Array<Real> all_translates;
195 if (translates.size ())
199 Real w = translates[0];
200 while (j < all_grobs.size ())
202 if (i < elems.size () && all_grobs[j] == elems[i])
206 all_translates.push (w);
211 FIXME: uncommenting freaks out the Y-alignment of
214 if (scm_is_number (align))
215 center_offset = total.linear_combination (scm_to_double (align));
217 for (int j = 0; j < all_grobs.size (); j++)
218 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
222 Align_interface::axis (Grob *me)
224 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
228 Align_interface::add_element (Grob *me, Grob *s, SCM cb)
230 s->add_offset_callback (cb, Align_interface::axis (me));
231 Axis_group_interface::add_element (me, s);
235 Align_interface::set_axis (Grob *me, Axis a)
237 Axis_group_interface::set_axes (me, a, a);
241 Find Y-axis parent of G that has a #'forced-distance property. This
242 has the effect of finding the piano-staff given an object in that
246 find_fixed_alignment_parent (Grob *g)
250 if (scm_is_number (g->get_property ("forced-distance")))
253 g = g->get_parent (Y_AXIS);
259 ADD_INTERFACE (Align_interface, "align-interface",
260 "Order grobs from top to bottom, left to right, right to left or bottom"
262 "forced-distance stacking-dir align-dir threshold positioning-done "
263 "center-element elements axes");
267 bool has_interface (Grob *);