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