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