2 align-interface.cc -- implement Align_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2001 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 "paper-def.hh"
17 This callback is set in the children of the align element. It does
18 not compute anything, but a side effect of a->do_side_processing ()
19 is that the elements are placed correctly. */
20 MAKE_SCHEME_CALLBACK (Align_interface,alignment_callback,2);
22 Align_interface::alignment_callback (SCM element_smob, SCM axis)
24 Grob * me = unsmob_grob (element_smob);
25 Axis ax = (Axis)gh_scm2int (axis);
26 Grob * par = me->parent_l (ax);
27 if (par && !to_boolean (par->get_grob_property ("alignment-done")))
29 Align_interface::align_elements_to_extents (par, ax);
31 return gh_double2scm (0.0);
34 MAKE_SCHEME_CALLBACK (Align_interface,fixed_distance_alignment_callback,2);
36 Align_interface::fixed_distance_alignment_callback (SCM element_smob, SCM axis)
38 Grob * me = unsmob_grob (element_smob);
39 Axis ax = (Axis)gh_scm2int (axis);
40 Grob * par = me->parent_l (ax);
41 if (par && !to_boolean (par->get_grob_property ("alignment-done")))
43 Align_interface::align_to_fixed_distance (par, ax);
45 return gh_double2scm (0.0);
49 merge with align-to-extents?
52 Align_interface::align_to_fixed_distance (Grob *me , Axis a)
54 me->set_grob_property ("alignment-done", SCM_BOOL_T);
56 SCM d = me->get_grob_property ("stacking-dir");
59 Direction stacking_dir = gh_number_p (d) ? to_dir (d) : CENTER;
64 SCM force = me->get_grob_property ("forced-distance");
67 if (gh_number_p (force))
69 dy = gh_scm2double (force);
72 Link_array<Grob> elems
73 = Pointer_group_interface__extract_elements (me, (Grob*) 0, "elements");
79 Array<Real> translates;
81 for (int j=0 ; j < elems.size (); j++)
83 where_f += stacking_dir * dy;
84 translates.push (where_f);
85 v.unite (Interval (where_f, where_f));
89 TODO: support self-alignment-{Y,X}
91 for (int i = 0; i < translates.size (); i++)
93 elems[i]->translate_axis (translates[i] - v.center (), a);
98 Hairy function to put elements where they should be. Can be tweaked
99 from the outside by setting minimum-space and extra-space in its
102 We assume that the children the refpoints of the children are still
103 found at 0.0 -- we will fuck up with thresholds if children's
104 extents are already moved to locations such as (-16, -8), since the
105 dy needed to put things in a row doesn't relate to the distances
106 between original refpoints.
108 TODO: maybe we should rethink and throw out thresholding altogether.
109 The original function has been taken over by
110 align_to_fixed_distance().
113 Align_interface::align_elements_to_extents (Grob * me, Axis a)
115 me->set_grob_property ("alignment-done", SCM_BOOL_T);
117 SCM d = me->get_grob_property ("stacking-dir");
120 Direction stacking_dir = gh_number_p (d) ? to_dir (d) : CENTER;
126 Interval threshold = Interval (0, Interval::infinity ());
127 SCM thr = me->get_grob_property ("threshold");
130 threshold[SMALLER] = gh_scm2double (gh_car (thr));
131 threshold[BIGGER] = gh_scm2double (gh_cdr (thr));
135 Array<Interval> dims;
137 Link_array<Grob> elems;
138 Link_array<Grob> all_grobs
139 = Pointer_group_interface__extract_elements (me, (Grob*) 0, "elements");
140 for (int i=0; i < all_grobs.size (); i++)
142 Interval y = all_grobs[i]->extent (me, a);
145 Grob *e =dynamic_cast<Grob*> (all_grobs[i]);
147 // todo: fucks up if item both in Halign & Valign.
148 SCM min_dims = e->remove_grob_property ("minimum-space");
149 if (gh_pair_p (min_dims) &&
150 gh_number_p (gh_car (min_dims))
151 && gh_number_p (gh_cdr (min_dims)))
153 y.unite (ly_scm2interval (min_dims));
156 SCM extra_dims = e->remove_grob_property ("extra-space");
157 if (gh_pair_p (extra_dims) &&
158 gh_number_p (gh_car (extra_dims))
159 && gh_number_p (gh_cdr (extra_dims)))
161 y[LEFT] += gh_scm2double (gh_car (extra_dims));
162 y[RIGHT] += gh_scm2double (gh_cdr (extra_dims));
172 Read self-alignment-X and self-alignment-Y. This may seem like
173 code duplication. (and really: it is), but this is necessary to
174 prevent ugly cyclic dependencies that arise when you combine
175 self-alignment on a child with alignment of children.
178 String s ("self-alignment-");
180 s += (a == X_AXIS) ? "X" : "Y";
182 SCM align (me->get_grob_property (s.ch_C ()));
184 Array<Real> translates ;
188 for (int j=0 ; j < elems.size (); j++)
190 Real dy = - dims[j][-stacking_dir];
192 dy += dims[j-1][stacking_dir];
201 dy = (dy >? threshold[SMALLER])
202 <? threshold[BIGGER];
205 where_f += stacking_dir * dy;
206 total.unite (dims[j] + where_f);
207 translates.push (where_f);
211 Grob * align_center = unsmob_grob (align);
212 Real center_offset = 0.0;
215 also move the grobs that were empty, to maintain spatial order.
217 Array<Real> all_translates;
218 if (translates.size ())
222 Real w = translates[0];
223 while (j < all_grobs.size ())
225 if (i < elems.size () && all_grobs[j] == elems[i])
229 if (all_grobs[j] == align_center)
231 all_translates .push (w);
237 FIXME: uncommenting freaks out the Y-alignment of
240 // Real align_param = isdir_b (align) ? gh_scm2double (align) : 0.0;
242 if (gh_number_p (align))
243 center_offset = total.linear_combination (gh_scm2double (align));
245 for (int j = 0 ; j < all_grobs.size (); j++)
246 all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
250 Align_interface::axis (Grob*me)
252 return Axis (gh_scm2int (gh_car (me->get_grob_property ("axes"))));
257 should use generic Scm funcs.
260 Align_interface::get_count (Grob*me,Grob*s)
262 SCM e = me->get_grob_property ("elements");
264 while (gh_pair_p (e))
266 if (gh_car (e) == s->self_scm ())
275 Align_interface::add_element (Grob*me,Grob* s, SCM cb)
277 s->add_offset_callback (cb, Align_interface::axis (me));
278 Axis_group_interface::add_element (me, s);
283 Align_interface::set_interface (Grob*me)
285 me->set_interface (ly_symbol2scm ("align-interface"));
287 Axis_group_interface::set_interface (me);
291 Align_interface::set_axis (Grob*me,Axis a)
293 Axis_group_interface::set_axes (me, a,a);
297 Align_interface::has_interface (Grob*me)
299 return me && me->has_interface (ly_symbol2scm ("align-interface"));