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