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