]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
Merge branch 'master' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond
[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--2007 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 "system.hh"
18 #include "warn.hh"
19 #include "paper-column.hh"
20
21 /*
22   TODO: for vertical spacing, should also include a rod & spring
23   scheme of sorts into this: the alignment should default to a certain
24   distance between element refpoints, unless bbox force a bigger
25   distance.
26  */
27
28 MAKE_SCHEME_CALLBACK (Align_interface, calc_positioning_done, 1);
29 SCM
30 Align_interface::calc_positioning_done (SCM smob)
31 {
32   Grob *me = unsmob_grob (smob);
33   SCM axis = scm_car (me->get_property ("axes"));
34   Axis ax = Axis (scm_to_int (axis));
35
36   SCM force = me->get_property ("forced-distance");
37   if (scm_is_number (force))
38     Align_interface::align_to_fixed_distance (me, ax);
39   else
40     Align_interface::align_elements_to_extents (me, ax);
41
42   return SCM_BOOL_T;
43 }
44
45 /*
46   merge with align-to-extents?
47 */
48 MAKE_SCHEME_CALLBACK(Align_interface, stretch_after_break, 1)
49 SCM
50 Align_interface::stretch_after_break (SCM grob)
51 {
52   Grob *me = unsmob_grob (grob);
53
54   Spanner *me_spanner = dynamic_cast<Spanner *> (me);
55   extract_grob_set (me, "elements", elems);
56
57   if (me_spanner && elems.size ())
58     {
59       Grob *common = common_refpoint_of_array (elems, me, Y_AXIS);
60
61       /* force position callbacks */
62       for (vsize i = 0; i < elems.size (); i++)
63         elems[i]->relative_coordinate (common, Y_AXIS);
64
65       SCM details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
66       SCM extra_space_handle = scm_assoc (ly_symbol2scm ("fixed-alignment-extra-space"), details);
67       
68       Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
69                                             ? scm_cdr (extra_space_handle)
70                                             : SCM_EOL,
71                                             0.0);
72
73       Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
74                                                DOWN);
75       Real delta  = extra_space / elems.size() * stacking_dir;
76       for (vsize i = 0; i < elems.size (); i++)
77         elems[i]->translate_axis (i * delta, Y_AXIS);
78     }
79   
80   return SCM_UNSPECIFIED;
81 }
82
83 /*
84   merge with align-to-extents?
85 */
86 void
87 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
88 {
89   Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
90                                            DOWN);
91
92   Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
93
94   extract_grob_set (me, "elements", elem_source);
95
96   vector<Grob*> elems (elem_source); // writable..
97
98   Real where = 0;
99
100   Interval v;
101   v.set_empty ();
102   vector<Real> translates;
103
104   for (vsize j = elems.size (); j--;)
105     {
106       /*
107         This is not very elegant, in that we need special support for
108         hara-kiri. Unfortunately, the generic wiring of
109         force_hara_kiri_callback () (extent and offset callback) is
110         such that we might get into a loop if we call extent () or
111         offset () the elements.
112       */
113       if (a == Y_AXIS
114           && Hara_kiri_group_spanner::has_interface (elems[j]))
115         Hara_kiri_group_spanner::consider_suicide (elems[j]);
116
117       if (!elems[j]->is_live ())
118         elems.erase (elems.begin () + j);
119     }
120
121   for (vsize j = 0; j < elems.size (); j++)
122     {
123       where += stacking_dir * dy;
124       translates.push_back (where);
125       v.unite (Interval (where, where));
126     }
127
128   for (vsize i = 0; i < translates.size (); i++)
129     elems[i]->translate_axis (translates[i] - v.center (), a);
130 }
131
132 /* for each grob, find its upper and lower skylines. If the grob has
133    an empty extent, delete it from the list instead. If the extent is
134    non-empty but there is no skyline available (or pure is true), just
135    create a flat skyline from the bounding box */
136 static void
137 get_skylines (Grob *me,
138               vector<Grob*> *const elements,
139               Axis a,
140               bool pure, int start, int end,
141               vector<Skyline_pair> *const ret)
142 {
143   /* each child's skyline was calculated according to the common refpoint of its
144      elements. Here we need all the skylines to be positioned with respect to
145      a single refpoint, so we need the common refpoint of the common refpoints
146      of the elements of the children */
147   vector<Grob*> child_refpoints;
148   for (vsize i = 0; i < elements->size (); i++)
149     {
150       Grob *elt = (*elements)[i];
151       Grob *child_common = unsmob_grob ((a == Y_AXIS)
152                                         ? elt->get_object ("X-common")
153                                         : elt->get_object ("Y-common"));
154       
155       if (!child_common)
156         {
157           extract_grob_set (elt, "elements", child_elts);
158           child_common = common_refpoint_of_array (child_elts, elt, other_axis (a));
159         }
160       
161       child_refpoints.push_back (child_common);
162     }
163   Grob *common_refpoint = common_refpoint_of_array (child_refpoints, me, other_axis (a));
164   
165   for (vsize i = elements->size (); i--;)
166     {
167       Grob *g = (*elements)[i];
168       Interval extent = g->maybe_pure_extent (g, a, pure, start, end);
169       Interval other_extent = pure ? Interval (-infinity_f, infinity_f)
170         : g->extent (common_refpoint, other_axis (a));
171       Box b;
172       b[a] = extent;
173       b[other_axis (a)] = other_extent;
174
175       if (extent.is_empty ())
176         {
177           elements->erase (elements->begin () + i);
178           continue;
179         }
180
181       Skyline_pair skylines;
182       if (!pure
183           && Skyline_pair::unsmob (g->get_property ("skylines")))
184         skylines = *Skyline_pair::unsmob (g->get_property ("skylines"));
185       else
186         {
187           if (!pure)
188             programming_error ("no skylines for alignment-child\n");
189           
190           skylines = Skyline_pair (b, 0, other_axis (a));
191         }
192
193       /* each skyline is calculated relative to (potentially) a different other_axis
194          coordinate. In order to compare the skylines effectively, we need to shift them
195          to some absolute reference point */
196       if (!pure)
197         {
198           /* this is perhaps an abuse of minimum-?-extent: maybe we should create
199              another property? But it seems that the only (current) use of
200              minimum-Y-extent is to separate vertically-aligned elements */
201           SCM min_extent = g->get_property (a == X_AXIS ? "minimum-X-extent" : "minimum-Y-extent");
202           if (is_number_pair (min_extent))
203             {
204               b[a] = ly_scm2interval (min_extent);
205               skylines.insert (b, 0, other_axis (a));
206             }
207
208           Real offset = child_refpoints[i]->relative_coordinate (common_refpoint, other_axis (a));
209           skylines.shift (offset);
210         }
211
212
213       ret->push_back (skylines);
214     }
215   reverse (*ret);
216 }
217
218 vector<Real>
219 Align_interface::get_extents_aligned_translates (Grob *me,
220                                                  vector<Grob*> const &all_grobs,
221                                                  Axis a,
222                                                  bool pure, int start, int end)
223 {
224   Spanner *me_spanner = dynamic_cast<Spanner *> (me);
225
226
227   SCM line_break_details = SCM_EOL;
228   if (a == Y_AXIS && me_spanner)
229     {
230       if (pure)
231         line_break_details = get_root_system (me)->column (start)->get_property ("line-break-system-details");
232       else
233         line_break_details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
234
235       if (!me->get_system () && !pure)
236         me->warning (_ ("vertical alignment called before line-breaking.\n"
237                         "Only do cross-staff spanners with PianoStaff."));
238
239     }
240   
241   Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
242                                            DOWN);
243
244   vector<Grob*> elems (all_grobs); // writable copy
245   vector<Skyline_pair> skylines;
246
247   get_skylines (me, &elems, a, pure, start, end, &skylines);
248
249   Real where = 0;
250   SCM extra_space_handle = scm_assq (ly_symbol2scm ("alignment-extra-space"), line_break_details);
251   Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
252                                         ? scm_cdr (extra_space_handle)
253                                         : SCM_EOL,
254                                         0.0);
255
256   Real padding = robust_scm2double (me->get_property ("padding"), 0.0);
257   vector<Real> translates;
258   for (vsize j = 0; j < elems.size (); j++)
259     {
260       Real dy = 0;
261       if (j == 0)
262         dy = skylines[j][-stacking_dir].max_height ();
263       else
264         dy = skylines[j-1][stacking_dir].distance (skylines[j][-stacking_dir]);
265
266       where += stacking_dir * max (0.0, dy + padding + extra_space / elems.size ());
267       translates.push_back (where);
268     }
269
270   SCM offsets_handle = scm_assq (ly_symbol2scm ("alignment-offsets"),
271                                  line_break_details);
272   if (scm_is_pair (offsets_handle))
273     {
274       vsize i = 0;
275  
276       for (SCM s = scm_cdr (offsets_handle);
277            scm_is_pair (s) && i < translates.size (); s = scm_cdr (s), i++)
278         {
279           if (scm_is_number (scm_car (s)))
280             translates[i] = scm_to_double (scm_car (s));
281         }
282     }
283
284   vector<Real> all_translates;
285
286   if (!translates.empty ())
287     {
288       Real w = translates[0];
289       for  (vsize i = 0, j = 0; j < all_grobs.size (); j++)
290         {
291           if (i < elems.size () && all_grobs[j] == elems[i])
292             w = translates[i++];
293           all_translates.push_back (w);
294         }
295     }
296   return all_translates;
297 }
298
299 void
300 Align_interface::align_elements_to_extents (Grob *me, Axis a)
301 {
302   extract_grob_set (me, "elements", all_grobs);
303
304   vector<Real> translates = get_extents_aligned_translates (me, all_grobs, a, false, 0, 0);
305   if (translates.size ())
306     for (vsize j = 0; j < all_grobs.size (); j++)
307       all_grobs[j]->translate_axis (translates[j], a);
308 }
309
310 Real
311 Align_interface::get_pure_child_y_translation (Grob *me, Grob *ch, int start, int end)
312 {
313   extract_grob_set (me, "elements", all_grobs);
314   SCM dy_scm = me->get_property ("forced-distance");
315
316   if (scm_is_number (dy_scm))
317     {
318       Real dy = scm_to_double (dy_scm) * robust_scm2dir (me->get_property ("stacking-dir"), DOWN);
319       Real pos = 0;
320       for (vsize i = 0; i < all_grobs.size (); i++)
321         {
322           if (all_grobs[i] == ch)
323             return pos;
324           if (!Hara_kiri_group_spanner::has_interface (all_grobs[i])
325               || !Hara_kiri_group_spanner::request_suicide (all_grobs[i], start, end))
326             pos += dy;
327         }
328     }
329   else
330     {
331       vector<Real> translates = get_extents_aligned_translates (me, all_grobs, Y_AXIS, true, start, end);
332
333       if (translates.size ())
334         {
335           for (vsize i = 0; i < all_grobs.size (); i++)
336             if (all_grobs[i] == ch)
337               return translates[i];
338         }
339       else
340         return 0;
341     }
342
343   programming_error (_ ("tried to get a translation for something that is no child of mine"));
344   return 0;
345 }
346
347 Axis
348 Align_interface::axis (Grob *me)
349 {
350   return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
351 }
352
353 void
354 Align_interface::add_element (Grob *me, Grob *element)
355 {
356   Axis a = Align_interface::axis (me);
357   SCM sym = axis_offset_symbol (a);
358   SCM proc = axis_parent_positioning (a);
359     
360   element->set_property (sym, proc);
361   Axis_group_interface::add_element (me, element);
362 }
363
364 void
365 Align_interface::set_ordered (Grob *me)
366 {
367   SCM ga_scm = me->get_object ("elements");
368   Grob_array *ga = unsmob_grob_array (ga_scm);
369   if (!ga)
370     {
371       ga_scm = Grob_array::make_array ();
372       ga = unsmob_grob_array (ga_scm);
373       me->set_object ("elements", ga_scm);
374     }
375
376   ga->set_ordered (true);
377 }
378
379 /*
380   Find Y-axis parent of G that has a #'forced-distance property. This
381   has the effect of finding the piano-staff given an object in that
382   piano staff.
383 */
384 Grob *
385 find_fixed_alignment_parent (Grob *g)
386 {
387   while (g)
388     {
389       if (scm_is_number (g->get_property ("forced-distance")))
390         return g;
391
392       g = g->get_parent (Y_AXIS);
393     }
394
395   return 0;
396 }
397
398 ADD_INTERFACE (Align_interface,
399                
400                "Order grobs from top to bottom, left to right, right to left or bottom "
401                "to top.  "
402                "For vertical alignments of staves, the @code{break-system-details} of "
403                "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
404                "Set @code{alignment-extra-space} to add extra space for staves. Set "
405                "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
406                ,
407                
408                /*
409                  properties
410                 */
411                "align-dir "
412                "axes "
413                "elements "
414                "forced-distance "
415                "padding "
416                "positioning-done "
417                "stacking-dir "
418                "threshold "
419                );