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