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