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