]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
* lily/breathing-sign.cc (offset_callback): idem.
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "align-interface.hh"
10 #include "spanner.hh"
11 #include "item.hh"
12 #include "axis-group-interface.hh"
13 #include "pointer-group-interface.hh"
14 #include "hara-kiri-group-spanner.hh"
15 #include "grob-array.hh"
16 #include "international.hh"
17 #include "warn.hh"
18
19 /*
20   TODO: for vertical spacing, should also include a rod & spring
21   scheme of sorts into this: the alignment should default to a certain
22   distance between element refpoints, unless bbox force a bigger
23   distance.
24  */
25
26 MAKE_SCHEME_CALLBACK (Align_interface, calc_positioning_done, 1);
27 SCM
28 Align_interface::calc_positioning_done (SCM smob)
29 {
30   Grob *me = unsmob_grob (smob);
31   SCM axis = scm_car (me->get_property ("axes"));
32   Axis ax = Axis (scm_to_int (axis));
33
34   SCM force = me->get_property ("forced-distance");
35   if (scm_is_number (force))
36     Align_interface::align_to_fixed_distance (me, ax);
37   else
38     Align_interface::align_elements_to_extents (me, ax);
39
40   return SCM_BOOL_T;
41 }
42
43 /*
44   merge with align-to-extents?
45 */
46 MAKE_SCHEME_CALLBACK(Align_interface, stretch_after_break, 1)
47 SCM
48 Align_interface::stretch_after_break (SCM grob)
49 {
50   Grob *me = unsmob_grob (grob);
51
52   Spanner *me_spanner = dynamic_cast<Spanner *> (me);
53   extract_grob_set (me, "elements", elems);
54
55   if (me_spanner && elems.size ())
56     {
57       Grob *common = common_refpoint_of_array (elems, me, Y_AXIS);
58
59       /* force position callbacks */
60       for (vsize i = 0; i < elems.size (); i++)
61         elems[i]->relative_coordinate (common, Y_AXIS);
62
63       SCM details =  me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
64       SCM extra_space_handle = scm_assoc (ly_symbol2scm ("fixed-alignment-extra-space"), details);
65       
66       Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
67                                             ? scm_cdr (extra_space_handle)
68                                             : SCM_EOL,
69                                             0.0);
70
71       Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
72                                                DOWN);
73       Real delta  = extra_space / elems.size() * stacking_dir;
74       for (vsize i = 0; i < elems.size (); i++)
75         elems[i]->translate_axis (i * delta, Y_AXIS);
76     }
77   
78   return SCM_UNSPECIFIED;
79 }
80
81 /*
82   merge with align-to-extents?
83 */
84 void
85 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
86 {
87   Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
88                                            DOWN);
89
90   Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
91
92   extract_grob_set (me, "elements", elem_source);
93
94   vector<Grob*> elems (elem_source); // writable..
95
96   Real where = 0;
97
98   Interval v;
99   v.set_empty ();
100   vector<Real> translates;
101
102   for (vsize j = elems.size (); j--;)
103     {
104       /*
105         This is not very elegant, in that we need special support for
106         hara-kiri. Unfortunately, the generic wiring of
107         force_hara_kiri_callback () (extent and offset callback) is
108         such that we might get into a loop if we call extent () or
109         offset () the elements.
110       */
111       if (a == Y_AXIS
112           && Hara_kiri_group_spanner::has_interface (elems[j]))
113         Hara_kiri_group_spanner::consider_suicide (elems[j]);
114
115       if (!elems[j]->is_live ())
116         elems.erase (elems.begin () + j);
117     }
118
119   for (vsize j = 0; j < elems.size (); j++)
120     {
121       where += stacking_dir * dy;
122       translates.push_back (where);
123       v.unite (Interval (where, where));
124     }
125
126   /*
127     TODO: support self-alignment-{Y, X}
128   */
129   for (vsize i = 0; i < translates.size (); i++)
130     elems[i]->translate_axis (translates[i] - v.center (), a);
131 }
132
133 /*
134   Hairy function to put elements where they should be. Can be tweaked
135   from the outside by setting extra-space in its
136   children
137
138   We assume that the children the refpoints of the children are still
139   found at 0.0 -- we will fuck up with thresholds if children's
140   extents are already moved to locations such as (-16, -8), since the
141   dy needed to put things in a row doesn't relate to the distances
142   between original refpoints.
143
144   TODO: maybe we should rethink and throw out thresholding altogether.
145   The original function has been taken over by
146   align_to_fixed_distance ().
147 */
148
149 vector<Real>
150 Align_interface::get_extents_aligned_translates (Grob *me,
151                                                  vector<Grob*> const &all_grobs,
152                                                  Axis a,
153                                                  bool pure, int start, int end)
154 {
155   Spanner *me_spanner = dynamic_cast<Spanner *> (me);
156
157
158   SCM line_break_details = SCM_EOL;
159   if (a == Y_AXIS && me_spanner)
160     {
161       line_break_details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
162
163       if (!me->get_system () && !pure)
164         me->warning (_ ("vertical alignment called before line-breaking.\n"
165                         "Only do cross-staff spanners with PianoStaff."));
166
167     }
168   
169   Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
170                                            DOWN);
171
172   Interval threshold = robust_scm2interval (me->get_property ("threshold"),
173                                             Interval (0, Interval::infinity ()));
174
175   vector<Interval> dims;
176   vector<Grob*> elems;
177
178   for (vsize i = 0; i < all_grobs.size (); i++)
179     {
180       Interval y = all_grobs[i]->maybe_pure_extent (all_grobs[i], a, pure, start, end);
181       if (!y.is_empty ())
182         {
183           Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
184
185           elems.push_back (e);
186           dims.push_back (y);
187         }
188     }
189
190   /*
191     Read self-alignment-X and self-alignment-Y. This may seem like
192     code duplication. (and really: it is), but this is necessary to
193     prevent ugly cyclic dependencies that arise when you combine
194     self-alignment on a child with alignment of children.
195   */
196   SCM align ((a == X_AXIS)
197              ? me->get_property ("self-alignment-X")
198              : me->get_property ("self-alignment-Y"));
199
200   Interval total;
201   Real where = 0;
202   Real extra_space = 0.0;
203   SCM extra_space_handle = scm_assq (ly_symbol2scm ("alignment-extra-space"), line_break_details);
204
205   extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
206                                    ? scm_cdr (extra_space_handle)
207                                    : SCM_EOL,
208                                    extra_space);
209   
210   vector<Real> translates;
211   for (vsize j = 0; j < elems.size (); j++)
212     {
213       Real dy = -dims[j][-stacking_dir];
214       if (j)
215         dy += dims[j - 1][stacking_dir];
216
217       /*
218         we want dy to be > 0
219       */
220       dy *= stacking_dir;
221       if (j)
222         dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
223
224       where += stacking_dir * (dy + extra_space / elems.size ());
225       total.unite (dims[j] + where);
226       translates.push_back (where);
227     }
228
229   SCM offsets_handle = scm_assq (ly_symbol2scm ("alignment-offsets"), line_break_details);
230   if (scm_is_pair (offsets_handle))
231     {
232       vsize i = 0;
233  
234       for (SCM s = scm_cdr (offsets_handle); scm_is_pair (s) && i < translates.size (); s = scm_cdr (s), i++)
235         {
236           if (scm_is_number (scm_car (s)))
237             translates[i] = scm_to_double (scm_car (s));
238         }
239     }
240
241   
242   Real center_offset = 0.0;
243   
244   /*
245     also move the grobs that were empty, to maintain spatial order.
246   */
247   vector<Real> all_translates;
248   if (translates.size ())
249     {
250       Real w = translates[0];
251
252       if (scm_is_number (align))
253         center_offset = total.linear_combination (scm_to_double (align));
254
255       for  (vsize i = 0, j = 0; j < all_grobs.size (); j++)
256         {
257           if (i < elems.size () && all_grobs[j] == elems[i])
258             w = translates[i++];
259           all_translates.push_back (w - center_offset);
260         }
261     }
262   return all_translates;
263 }
264
265 void
266 Align_interface::align_elements_to_extents (Grob *me, Axis a)
267 {
268   extract_grob_set (me, "elements", all_grobs);
269
270   vector<Real> translates = get_extents_aligned_translates (me, all_grobs, a, false, 0, 0);
271   if (translates.size ())
272       for (vsize j = 0; j < all_grobs.size (); j++)
273         all_grobs[j]->translate_axis (translates[j], a);
274 }
275
276 Real
277 Align_interface::get_pure_child_y_translation (Grob *me, Grob *ch, int start, int end)
278 {
279   extract_grob_set (me, "elements", all_grobs);
280   SCM dy_scm = me->get_property ("forced-distance");
281
282   if (scm_is_number (dy_scm))
283     {
284       Real dy = scm_to_double (dy_scm) * robust_scm2dir (me->get_property ("stacking-dir"), DOWN);
285       Real pos = 0;
286       for (vsize i = 0; i < all_grobs.size (); i++)
287         {
288           if (all_grobs[i] == ch)
289             return pos;
290           if (!Hara_kiri_group_spanner::has_interface (all_grobs[i])
291               || !Hara_kiri_group_spanner::request_suicide (all_grobs[i], start, end))
292             pos += dy;
293         }
294     }
295   else
296     {
297       vector<Real> translates = get_extents_aligned_translates (me, all_grobs, Y_AXIS, true, start, end);
298
299       if (translates.size ())
300         {
301           for (vsize i = 0; i < all_grobs.size (); i++)
302             if (all_grobs[i] == ch)
303               return translates[i];
304         }
305       else
306         return 0;
307     }
308
309   programming_error (_ ("tried to get a translation for something that isn't my child"));
310   return 0;
311 }
312
313 Axis
314 Align_interface::axis (Grob *me)
315 {
316   return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
317 }
318
319 void
320 Align_interface::add_element (Grob *me, Grob *element)
321 {
322   Axis a = Align_interface::axis (me);
323   SCM sym = axis_offset_symbol (a);
324   SCM proc = axis_parent_positioning (a);
325     
326   element->internal_set_property (sym, proc);
327   Axis_group_interface::add_element (me, element);
328 }
329
330 void
331 Align_interface::set_ordered (Grob *me)
332 {
333   SCM ga_scm = me->get_object ("elements");
334   Grob_array *ga = unsmob_grob_array (ga_scm);
335   if (!ga)
336     {
337       ga_scm = Grob_array::make_array ();
338       ga = unsmob_grob_array (ga_scm);
339       me->set_object ("elements", ga_scm);
340     }
341
342   ga->set_ordered (true);
343 }
344
345 /*
346   Find Y-axis parent of G that has a #'forced-distance property. This
347   has the effect of finding the piano-staff given an object in that
348   piano staff.
349 */
350 Grob *
351 find_fixed_alignment_parent (Grob *g)
352 {
353   while (g)
354     {
355       if (scm_is_number (g->get_property ("forced-distance")))
356         return g;
357
358       g = g->get_parent (Y_AXIS);
359     }
360
361   return 0;
362 }
363
364 ADD_INTERFACE (Align_interface,
365                "align-interface",
366                
367                "Order grobs from top to bottom, left to right, right to left or bottom "
368                "to top.  "
369                "For vertical alignments of staves, the @code{break-system-details} of "
370                "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
371                "Set @code{alignment-extra-space} to add extra space for staves. Set "
372                "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
373                ,
374                
375                /*
376                  properties
377                 */
378                "forced-distance "
379                "stacking-dir "
380                "align-dir "
381                "threshold "
382                "positioning-done "
383                "elements axes");
384
385 struct Foobar
386 {
387   bool has_interface (Grob *);
388 };
389