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