]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
* lily/break-substitution.cc (fast_substitute_grob_array): do
[lilypond.git] / lily / align-interface.cc
1 /*
2   align-interface.cc -- implement Align_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "align-interface.hh"
10
11 #include "spanner.hh"
12 #include "item.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"
17
18 MAKE_SCHEME_CALLBACK (Align_interface, alignment_callback, 2);
19 SCM
20 Align_interface::alignment_callback (SCM element_smob, SCM axis)
21 {
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     {
27       Align_interface::align_elements_to_extents (par, ax);
28     }
29   return scm_make_real (0.0);
30 }
31
32 MAKE_SCHEME_CALLBACK (Align_interface, fixed_distance_alignment_callback, 2);
33 SCM
34 Align_interface::fixed_distance_alignment_callback (SCM element_smob, SCM axis)
35 {
36   Grob *me = unsmob_grob (element_smob);
37   Axis ax = (Axis)scm_to_int (axis);
38   Grob *par = me->get_parent (ax);
39   if (par && !to_boolean (par->get_property ("positioning-done")))
40     {
41       Align_interface::align_to_fixed_distance (par, ax);
42     }
43   return scm_make_real (0.0);
44 }
45
46 /*
47   merge with align-to-extents?
48 */
49 void
50 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
51 {
52   me->set_property ("positioning-done", SCM_BOOL_T);
53
54   SCM d = me->get_property ("stacking-dir");
55
56   Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
57   if (!stacking_dir)
58     stacking_dir = DOWN;
59
60   Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
61
62   extract_grob_set (me, "elements", elem_source);
63
64   Link_array<Grob> elems  (elem_source); // writable..
65   
66   Real where_f = 0;
67
68   Interval v;
69   v.set_empty ();
70   Array<Real> translates;
71
72   for (int j = elems.size (); j--;)
73     {
74       /*
75         This is not very elegant, in that we need special support for
76         hara-kiri. Unfortunately, the generic wiring of
77         force_hara_kiri_callback () (extent and offset callback) is
78         such that we might get into a loop if we call extent () or
79         offset () the elements.
80
81
82       */
83       if (a == Y_AXIS
84           && Hara_kiri_group_spanner::has_interface (elems[j]))
85         Hara_kiri_group_spanner::consider_suicide (elems[j]);
86
87       if (!elems[j]->is_live ())
88         elems.del (j);
89     }
90
91   for (int j = 0; j < elems.size (); j++)
92     {
93       where_f += stacking_dir * dy;
94       translates.push (where_f);
95       v.unite (Interval (where_f, where_f));
96     }
97
98   /*
99     TODO: support self-alignment-{Y, X}
100   */
101   for (int i = 0; i < translates.size (); i++)
102     {
103       elems[i]->translate_axis (translates[i] - v.center (), a);
104     }
105 }
106
107 /*
108   Hairy function to put elements where they should be. Can be tweaked
109   from the outside by setting extra-space in its
110   children
111
112   We assume that the children the refpoints of the children are still
113   found at 0.0 -- we will fuck up with thresholds if children's
114   extents are already moved to locations such as (-16, -8), since the
115   dy needed to put things in a row doesn't relate to the distances
116   between original refpoints.
117
118   TODO: maybe we should rethink and throw out thresholding altogether.
119   The original function has been taken over by
120   align_to_fixed_distance ().
121 */
122 void
123 Align_interface::align_elements_to_extents (Grob *me, Axis a)
124 {
125   Spanner *me_spanner = dynamic_cast<Spanner *> (me);
126   if (a == Y_AXIS
127       && me_spanner
128       && me_spanner->get_bound (LEFT)->break_status_dir () == CENTER)
129     {
130       me_spanner->warning (_("vertical alignment called before line-breaking. Only do cross-staff spanners with PianoStaff."));
131     }
132   
133   me->set_property ("positioning-done", SCM_BOOL_T);
134
135   SCM d = me->get_property ("stacking-dir");
136
137   Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
138   if (!stacking_dir)
139     stacking_dir = DOWN;
140
141   Interval threshold = robust_scm2interval (me->get_property ("threshold"),
142                                             Interval (0, Interval::infinity ()));
143
144   Array<Interval> dims;
145
146   Link_array<Grob> elems;
147
148   extract_grob_set (me, "elements", all_grobs);
149   for (int i = 0; i < all_grobs.size (); i++)
150     {
151       Interval y = all_grobs[i]->extent (me, a);
152       if (!y.is_empty ())
153         {
154           Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
155
156           elems.push (e);
157           dims.push (y);
158         }
159     }
160
161   /*
162     Read self-alignment-X and self-alignment-Y. This may seem like
163     code duplication. (and really: it is), but this is necessary to
164     prevent ugly cyclic dependencies that arise when you combine
165     self-alignment on a child with alignment of children.
166   */
167   static SCM prop_syms[2];
168
169   if (!prop_syms[0])
170     {
171       prop_syms[X_AXIS] = ly_symbol2scm ("self-alignment-X");
172       prop_syms[Y_AXIS] = ly_symbol2scm ("self-alignment-Y");
173     }
174
175   SCM align (me->internal_get_property (prop_syms[a]));
176
177   Array<Real> translates;
178   Interval total;
179   Real where_f = 0;
180
181   for (int j = 0; j < elems.size (); j++)
182     {
183       Real dy = -dims[j][-stacking_dir];
184       if (j)
185         dy += dims[j - 1][stacking_dir];
186
187       /*
188         we want dy to be > 0
189       */
190       dy *= stacking_dir;
191       if (j)
192         {
193           dy = min (max (dy, threshold[SMALLER]),
194                     threshold[BIGGER]);
195         }
196
197       where_f += stacking_dir * dy;
198       total.unite (dims[j] + where_f);
199       translates.push (where_f);
200     }
201
202   Real center_offset = 0.0;
203   /*
204     also move the grobs that were empty, to maintain spatial order.
205   */
206   Array<Real> all_translates;
207   if (translates.size ())
208     {
209       int i = 0;
210       int j = 0;
211       Real w = translates[0];
212       while (j < all_grobs.size ())
213         {
214           if (i < elems.size () && all_grobs[j] == elems[i])
215             {
216               w = translates[i++];
217             }
218           all_translates.push (w);
219           j++;
220         }
221
222       /*
223         FIXME: uncommenting freaks out the Y-alignment of
224         line-of-score.
225       */
226       if (scm_is_number (align))
227         center_offset = total.linear_combination (scm_to_double (align));
228
229       for (int j = 0; j < all_grobs.size (); j++)
230         all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
231     }
232 }
233 Axis
234 Align_interface::axis (Grob *me)
235 {
236   return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
237 }
238
239 void
240 Align_interface::add_element (Grob *me, Grob *element, SCM call_back)
241 {
242   element->add_offset_callback (call_back, Align_interface::axis (me));
243   Axis_group_interface::add_element (me, element);
244 }
245
246 void
247 Align_interface::set_axis (Grob *me, Axis a)
248 {
249   Axis_group_interface::set_axes (me, a, a);
250   SCM ga_scm = me->get_object ("elements");
251   Grob_array *ga = unsmob_grob_array (ga_scm);
252   if (!ga)
253     {
254       ga_scm = Grob_array::make_array ();
255       ga = unsmob_grob_array (ga_scm);
256       me->set_object ("elements", ga_scm);
257     }
258
259   ga->set_ordered (true);
260   
261 }
262
263 /*
264   Find Y-axis parent of G that has a #'forced-distance property. This
265   has the effect of finding the piano-staff given an object in that
266   piano staff.
267 */
268 Grob *
269 find_fixed_alignment_parent (Grob *g)
270 {
271   while (g)
272     {
273       if (scm_is_number (g->get_property ("forced-distance")))
274         return g;
275
276       g = g->get_parent (Y_AXIS);
277     }
278
279   return 0;
280 }
281
282 ADD_INTERFACE (Align_interface, "align-interface",
283                "Order grobs from top to bottom, left to right, right to left or bottom"
284                "to top.",
285                "forced-distance stacking-dir align-dir threshold positioning-done "
286                "elements axes");
287
288 struct Foobar
289 {
290   bool has_interface (Grob *);
291 };
292