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