2 align-interface.cc -- implement Align_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include "align-interface.hh"
12 #include "group-interface.hh"
13 #include "axis-group-interface.hh"
14 #include "hara-kiri-group-spanner.hh"
15 #include "output-def.hh"
17 MAKE_SCHEME_CALLBACK (Align_interface,alignment_callback,2);
19 Align_interface::alignment_callback (SCM element_smob, SCM axis)
21 Grob * me = unsmob_grob (element_smob);
22 Axis ax = (Axis)scm_to_int (axis);
23 Grob * par = me->get_parent (ax);
24 if (par && !to_boolean (par->get_property ("positioning-done")))
26 Align_interface::align_elements_to_extents (par, ax);
28 return scm_make_real (0.0);
31 MAKE_SCHEME_CALLBACK (Align_interface,fixed_distance_alignment_callback,2);
33 Align_interface::fixed_distance_alignment_callback (SCM element_smob, SCM axis)
35 Grob * me = unsmob_grob (element_smob);
36 Axis ax = (Axis)scm_to_int (axis);
37 Grob * par = me->get_parent (ax);
38 if (par && !to_boolean (par->get_property ("positioning-done")))
40 Align_interface::align_to_fixed_distance (par, ax);
42 return scm_make_real (0.0);
46 merge with align-to-extents?
49 Align_interface::align_to_fixed_distance (Grob *me , Axis a)
51 me->set_property ("positioning-done", SCM_BOOL_T);
53 SCM d = me->get_property ("stacking-dir");
55 Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
59 Real dy = robust_scm2double (me->get_property ("forced-distance"),0.0);
61 Link_array<Grob> elems
62 = Pointer_group_interface__extract_grobs (me, (Grob*) 0, "elements");
68 Array<Real> translates;
70 for (int j= elems.size (); j--; )
73 This is not very elegant, in that we need special support for
74 hara-kiri. Unfortunately, the generic wiring of
75 force_hara_kiri_callback () (extent and offset callback) is
76 such that we might get into a loop if we call extent () or
77 offset () the elements.
82 && Hara_kiri_group_spanner::has_interface (elems[j]))
83 Hara_kiri_group_spanner::consider_suicide (elems[j]);
85 if (!elems[j]->is_live ())
89 for (int j =0; j < elems.size (); j++)
91 where_f += stacking_dir * dy;
92 translates.push (where_f);
93 v.unite (Interval (where_f, where_f));
97 TODO: support self-alignment-{Y,X}
99 for (int i = 0; i < translates.size (); i++)
101 elems[i]->translate_axis (translates[i] - v.center (), a);
106 Hairy function to put elements where they should be. Can be tweaked
107 from the outside by setting extra-space in its
110 We assume that the children the refpoints of the children are still
111 found at 0.0 -- we will fuck up with thresholds if children's
112 extents are already moved to locations such as (-16, -8), since the
113 dy needed to put things in a row doesn't relate to the distances
114 between original refpoints.
116 TODO: maybe we should rethink and throw out thresholding altogether.
117 The original function has been taken over by
118 align_to_fixed_distance ().
121 Align_interface::align_elements_to_extents (Grob * me, Axis a)
123 me->set_property ("positioning-done", SCM_BOOL_T);
125 SCM d = me->get_property ("stacking-dir");
127 Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
131 Interval threshold = robust_scm2interval (me->get_property ("threshold"),
132 Interval (0, Interval::infinity ()));
134 Array<Interval> dims;
136 Link_array<Grob> elems;
137 Link_array<Grob> all_grobs
138 = Pointer_group_interface__extract_grobs (me, (Grob*) 0, "elements");
139 for (int i=0; i < all_grobs.size (); i++)
141 Interval y = all_grobs[i]->extent (me, a);
144 Grob *e =dynamic_cast<Grob*> (all_grobs[i]);
153 Read self-alignment-X and self-alignment-Y. This may seem like
154 code duplication. (and really: it is), but this is necessary to
155 prevent ugly cyclic dependencies that arise when you combine
156 self-alignment on a child with alignment of children.
158 static SCM prop_syms[2];
162 prop_syms[X_AXIS] = ly_symbol2scm ("self-alignment-X");
163 prop_syms[Y_AXIS] = ly_symbol2scm ("self-alignment-Y");
166 SCM align (me->internal_get_property (prop_syms[a]));
168 Array<Real> translates ;
172 for (int j=0 ; j < elems.size (); j++)
174 Real dy = - dims[j][-stacking_dir];
176 dy += dims[j-1][stacking_dir];
185 dy = (dy >? threshold[SMALLER])
186 <? threshold[BIGGER];
189 where_f += stacking_dir * dy;
190 total.unite (dims[j] + where_f);
191 translates.push (where_f);
195 Real center_offset = 0.0;
197 also move the grobs that were empty, to maintain spatial order.
199 Array<Real> all_translates;
200 if (translates.size ())
204 Real w = translates[0];
205 while (j < all_grobs.size ())
207 if (i < elems.size () && all_grobs[j] == elems[i])
211 all_translates.push (w);
217 FIXME: uncommenting freaks out the Y-alignment of
220 if (scm_is_number (align))
221 center_offset = total.linear_combination (scm_to_double (align));
223 for (int j = 0 ; j < all_grobs.size (); j++)
224 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
228 Align_interface::axis (Grob*me)
230 return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
234 Align_interface::add_element (Grob*me,Grob* s, SCM cb)
236 s->add_offset_callback (cb, Align_interface::axis (me));
237 Axis_group_interface::add_element (me, s);
241 Align_interface::set_axis (Grob*me,Axis a)
243 Axis_group_interface::set_axes (me, a,a);
248 Find Y-axis parent of G that has a #'forced-distance property. This
249 has the effect of finding the piano-staff given an object in that
253 find_fixed_alignment_parent (Grob *g)
257 if (scm_is_number (g->get_property ("forced-distance")))
260 g = g->get_parent (Y_AXIS);
269 ADD_INTERFACE (Align_interface, "align-interface",
270 "Order grobs from top to bottom, left to right, right to left or bottom"
273 "forced-distance stacking-dir align-dir threshold positioning-done "
274 "center-element elements axes");
279 bool has_interface (Grob*);