2 align-interface.cc -- implement Align_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2002 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 "paper-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)gh_scm2int (axis);
23 Grob * par = me->get_parent (ax);
24 if (par && !to_boolean (par->get_grob_property ("alignment-done")))
26 Align_interface::align_elements_to_extents (par, ax);
28 return gh_double2scm (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)gh_scm2int (axis);
37 Grob * par = me->get_parent (ax);
38 if (par && !to_boolean (par->get_grob_property ("alignment-done")))
40 Align_interface::align_to_fixed_distance (par, ax);
42 return gh_double2scm (0.0);
46 merge with align-to-extents?
49 Align_interface::align_to_fixed_distance (Grob *me , Axis a)
51 me->set_grob_property ("alignment-done", SCM_BOOL_T);
53 SCM d = me->get_grob_property ("stacking-dir");
55 Direction stacking_dir = gh_number_p (d) ? to_dir (d) : CENTER;
59 SCM force = me->get_grob_property ("forced-distance");
62 if (gh_number_p (force))
64 dy = gh_scm2double (force);
67 Link_array<Grob> elems
68 = Pointer_group_interface__extract_grobs (me, (Grob*) 0, "elements");
74 Array<Real> translates;
76 for (int j= elems.size (); j--; )
79 This is not very elegant, in that we need special support for
80 hara kiri. Unfortunately, the generic wiring of
81 force_hara_kiri_callback () (extent and offset callback) is
82 such that we might get into a loop if we call extent() or
83 offset() the elements.
88 && Hara_kiri_group_spanner::has_interface (elems[j]))
89 Hara_kiri_group_spanner::consider_suicide (elems[j]);
91 if (!ly_pair_p (elems[j]-> immutable_property_alist_))
95 for (int j =0; j < elems.size (); j++)
97 where_f += stacking_dir * dy;
98 translates.push (where_f);
99 v.unite (Interval (where_f, where_f));
103 TODO: support self-alignment-{Y,X}
105 for (int i = 0; i < translates.size (); i++)
107 elems[i]->translate_axis (translates[i] - v.center (), a);
112 Hairy function to put elements where they should be. Can be tweaked
113 from the outside by setting minimum-space-pair and extra-space in its
116 We assume that the children the refpoints of the children are still
117 found at 0.0 -- we will fuck up with thresholds if children's
118 extents are already moved to locations such as (-16, -8), since the
119 dy needed to put things in a row doesn't relate to the distances
120 between original refpoints.
122 TODO: maybe we should rethink and throw out thresholding altogether.
123 The original function has been taken over by
124 align_to_fixed_distance().
127 Align_interface::align_elements_to_extents (Grob * me, Axis a)
129 me->set_grob_property ("alignment-done", SCM_BOOL_T);
131 SCM d = me->get_grob_property ("stacking-dir");
133 Direction stacking_dir = gh_number_p (d) ? to_dir (d) : CENTER;
137 Interval threshold = Interval (0, Interval::infinity ());
138 SCM thr = me->get_grob_property ("threshold");
141 threshold[SMALLER] = gh_scm2double (ly_car (thr));
142 threshold[BIGGER] = gh_scm2double (ly_cdr (thr));
146 Array<Interval> dims;
148 Link_array<Grob> elems;
149 Link_array<Grob> all_grobs
150 = Pointer_group_interface__extract_grobs (me, (Grob*) 0, "elements");
151 for (int i=0; i < all_grobs.size (); i++)
153 Interval y = all_grobs[i]->extent (me, a);
156 Grob *e =dynamic_cast<Grob*> (all_grobs[i]);
158 // todo: fucks up if item both in Halign & Valign.
159 SCM min_dims = e->remove_grob_property ("minimum-space-pair");
160 if (gh_pair_p (min_dims) &&
161 gh_number_p (ly_car (min_dims))
162 && gh_number_p (ly_cdr (min_dims)))
164 y.unite (ly_scm2interval (min_dims));
167 SCM extra_dims = e->remove_grob_property ("extra-space");
168 if (gh_pair_p (extra_dims) &&
169 gh_number_p (ly_car (extra_dims))
170 && gh_number_p (ly_cdr (extra_dims)))
172 y[LEFT] += gh_scm2double (ly_car (extra_dims));
173 y[RIGHT] += gh_scm2double (ly_cdr (extra_dims));
183 Read self-alignment-X and self-alignment-Y. This may seem like
184 code duplication. (and really: it is), but this is necessary to
185 prevent ugly cyclic dependencies that arise when you combine
186 self-alignment on a child with alignment of children.
189 String s ("self-alignment-");
191 s += (a == X_AXIS) ? "X" : "Y";
193 SCM align (me->get_grob_property (s.ch_C ()));
195 Array<Real> translates ;
199 for (int j=0 ; j < elems.size (); j++)
201 Real dy = - dims[j][-stacking_dir];
203 dy += dims[j-1][stacking_dir];
212 dy = (dy >? threshold[SMALLER])
213 <? threshold[BIGGER];
216 where_f += stacking_dir * dy;
217 total.unite (dims[j] + where_f);
218 translates.push (where_f);
222 Grob * align_center = unsmob_grob (align);
223 Real center_offset = 0.0;
226 also move the grobs that were empty, to maintain spatial order.
228 Array<Real> all_translates;
229 if (translates.size ())
233 Real w = translates[0];
234 while (j < all_grobs.size ())
236 if (i < elems.size () && all_grobs[j] == elems[i])
240 if (all_grobs[j] == align_center)
242 all_translates .push (w);
248 FIXME: uncommenting freaks out the Y-alignment of
251 // Real align_param = ly_dir_p (align) ? gh_scm2double (align) : 0.0;
253 if (gh_number_p (align))
254 center_offset = total.linear_combination (gh_scm2double (align));
256 for (int j = 0 ; j < all_grobs.size (); j++)
257 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
261 Align_interface::axis (Grob*me)
263 return Axis (gh_scm2int (ly_car (me->get_grob_property ("axes"))));
267 Align_interface::add_element (Grob*me,Grob* s, SCM cb)
269 s->add_offset_callback (cb, Align_interface::axis (me));
270 Axis_group_interface::add_element (me, s);
274 Align_interface::set_axis (Grob*me,Axis a)
276 Axis_group_interface::set_axes (me, a,a);
280 Align_interface::has_interface (Grob*me)
282 return me && me->has_interface (ly_symbol2scm ("align-interface"));
286 ADD_INTERFACE (Align_interface, "align-interface",
287 " Order grobs top to bottom/left to right/right to left etc.",
288 "forced-distance stacking-dir align-dir threshold alignment-done center-element elements axes");
292 ADD_INTERFACE (Foobar, "aligned-interface",
293 "read by align-interface",
294 "minimum-space-pair extra-space");