]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
Use a `define-builtin-markup-command' macro for builtin markups, which
[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;
161       b[a] = extent;
162       b[other_axis (a)] = other_extent;
163
164       if (extent.is_empty ())
165         {
166           elements->erase (elements->begin () + i);
167           continue;
168         }
169
170       Skyline_pair skylines;
171       if (!pure
172           && Skyline_pair::unsmob (g->get_property ("skylines")))
173         skylines = *Skyline_pair::unsmob (g->get_property ("skylines"));
174       else
175         {
176           if (!pure)
177             programming_error ("no skylines for alignment-child\n");
178           
179           skylines = Skyline_pair (b, 0, other_axis (a));
180         }
181
182       /* each skyline is calculated relative to (potentially) a different other_axis
183          coordinate. In order to compare the skylines effectively, we need to shift them
184          to some absolute reference point */
185       if (!pure)
186         {
187           /* this is perhaps an abuse of minimum-?-extent: maybe we should create
188              another property? But it seems that the only (current) use of
189              minimum-Y-extent is to separate vertically-aligned elements */
190           SCM min_extent = g->get_property (a == X_AXIS ? "minimum-X-extent" : "minimum-Y-extent");
191           if (is_number_pair (min_extent))
192             {
193               b[a] = ly_scm2interval (min_extent);
194               skylines.insert (b, 0, other_axis (a));
195             }
196
197           Real offset = child_refpoints[i]->relative_coordinate (common_refpoint, other_axis (a));
198           skylines.shift (offset);
199         }
200
201
202       ret->push_back (skylines);
203     }
204   reverse (*ret);
205 }
206
207 vector<Real>
208 Align_interface::get_extents_aligned_translates (Grob *me,
209                                                  vector<Grob*> const &all_grobs,
210                                                  Axis a,
211                                                  bool pure, int start, int end)
212 {
213   Spanner *me_spanner = dynamic_cast<Spanner *> (me);
214
215
216   SCM line_break_details = SCM_EOL;
217   if (a == Y_AXIS && me_spanner)
218     {
219       line_break_details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
220
221       if (!me->get_system () && !pure)
222         me->warning (_ ("vertical alignment called before line-breaking.\n"
223                         "Only do cross-staff spanners with PianoStaff."));
224
225     }
226   
227   Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
228                                            DOWN);
229
230   vector<Grob*> elems (all_grobs); // writable copy
231   vector<Skyline_pair> skylines;
232
233   get_skylines (me, &elems, a, pure, start, end, &skylines);
234
235   Real where = 0;
236   SCM extra_space_handle = scm_assq (ly_symbol2scm ("alignment-extra-space"), line_break_details);
237   Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
238                                         ? scm_cdr (extra_space_handle)
239                                         : SCM_EOL,
240                                         0.0);
241
242   Real padding = robust_scm2double (me->get_property ("padding"), 0.0);
243   vector<Real> translates;
244   for (vsize j = 0; j < elems.size (); j++)
245     {
246       Real dy = 0;
247       if (j == 0)
248         dy = skylines[j][-stacking_dir].max_height ();
249       else
250         dy = skylines[j-1][stacking_dir].distance (skylines[j][-stacking_dir]);
251
252       where += stacking_dir * max (0.0, dy + padding + extra_space / elems.size ());
253       translates.push_back (where);
254     }
255
256   SCM offsets_handle = scm_assq (ly_symbol2scm ("alignment-offsets"),
257                                  line_break_details);
258   if (scm_is_pair (offsets_handle))
259     {
260       vsize i = 0;
261  
262       for (SCM s = scm_cdr (offsets_handle);
263            scm_is_pair (s) && i < translates.size (); s = scm_cdr (s), i++)
264         {
265           if (scm_is_number (scm_car (s)))
266             translates[i] = scm_to_double (scm_car (s));
267         }
268     }
269
270   vector<Real> all_translates;
271
272   if (!translates.empty ())
273     {
274       Real w = translates[0];
275       for  (vsize i = 0, j = 0; j < all_grobs.size (); j++)
276         {
277           if (i < elems.size () && all_grobs[j] == elems[i])
278             w = translates[i++];
279           all_translates.push_back (w);
280         }
281     }
282   return all_translates;
283 }
284
285 void
286 Align_interface::align_elements_to_extents (Grob *me, Axis a)
287 {
288   extract_grob_set (me, "elements", all_grobs);
289
290   vector<Real> translates = get_extents_aligned_translates (me, all_grobs, a, false, 0, 0);
291   if (translates.size ())
292     for (vsize j = 0; j < all_grobs.size (); j++)
293       all_grobs[j]->translate_axis (translates[j], a);
294 }
295
296 Real
297 Align_interface::get_pure_child_y_translation (Grob *me, Grob *ch, int start, int end)
298 {
299   extract_grob_set (me, "elements", all_grobs);
300   SCM dy_scm = me->get_property ("forced-distance");
301
302   if (scm_is_number (dy_scm))
303     {
304       Real dy = scm_to_double (dy_scm) * robust_scm2dir (me->get_property ("stacking-dir"), DOWN);
305       Real pos = 0;
306       for (vsize i = 0; i < all_grobs.size (); i++)
307         {
308           if (all_grobs[i] == ch)
309             return pos;
310           if (!Hara_kiri_group_spanner::has_interface (all_grobs[i])
311               || !Hara_kiri_group_spanner::request_suicide (all_grobs[i], start, end))
312             pos += dy;
313         }
314     }
315   else
316     {
317       vector<Real> translates = get_extents_aligned_translates (me, all_grobs, Y_AXIS, true, start, end);
318
319       if (translates.size ())
320         {
321           for (vsize i = 0; i < all_grobs.size (); i++)
322             if (all_grobs[i] == ch)
323               return translates[i];
324         }
325       else
326         return 0;
327     }
328
329   programming_error (_ ("tried to get a translation for something that isn't my child"));
330   return 0;
331 }
332
333 Axis
334 Align_interface::axis (Grob *me)
335 {
336   return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
337 }
338
339 void
340 Align_interface::add_element (Grob *me, Grob *element)
341 {
342   Axis a = Align_interface::axis (me);
343   SCM sym = axis_offset_symbol (a);
344   SCM proc = axis_parent_positioning (a);
345     
346   element->set_property (sym, proc);
347   Axis_group_interface::add_element (me, element);
348 }
349
350 void
351 Align_interface::set_ordered (Grob *me)
352 {
353   SCM ga_scm = me->get_object ("elements");
354   Grob_array *ga = unsmob_grob_array (ga_scm);
355   if (!ga)
356     {
357       ga_scm = Grob_array::make_array ();
358       ga = unsmob_grob_array (ga_scm);
359       me->set_object ("elements", ga_scm);
360     }
361
362   ga->set_ordered (true);
363 }
364
365 /*
366   Find Y-axis parent of G that has a #'forced-distance property. This
367   has the effect of finding the piano-staff given an object in that
368   piano staff.
369 */
370 Grob *
371 find_fixed_alignment_parent (Grob *g)
372 {
373   while (g)
374     {
375       if (scm_is_number (g->get_property ("forced-distance")))
376         return g;
377
378       g = g->get_parent (Y_AXIS);
379     }
380
381   return 0;
382 }
383
384 ADD_INTERFACE (Align_interface,
385                
386                "Order grobs from top to bottom, left to right, right to left or bottom "
387                "to top.  "
388                "For vertical alignments of staves, the @code{break-system-details} of "
389                "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
390                "Set @code{alignment-extra-space} to add extra space for staves. Set "
391                "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
392                ,
393                
394                /*
395                  properties
396                 */
397                "align-dir "
398                "axes "
399                "elements "
400                "forced-distance "
401                "padding "
402                "positioning-done "
403                "stacking-dir "
404                "threshold "
405                );