]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
736ecb9096a51a48111595b51d938b74c896189a
[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   /*
127     TODO: support self-alignment-{Y, X}
128   */
129   for (vsize i = 0; i < translates.size (); i++)
130     elems[i]->translate_axis (translates[i] - v.center (), a);
131 }
132
133 /*
134   Hairy function to put elements where they should be. Can be tweaked
135   from the outside by setting extra-space in its
136   children
137
138   We assume that the children the refpoints of the children are still
139   found at 0.0 -- we will fuck up with thresholds if children's
140   extents are already moved to locations such as (-16, -8), since the
141   dy needed to put things in a row doesn't relate to the distances
142   between original refpoints.
143
144   TODO: maybe we should rethink and throw out thresholding altogether.
145   The original function has been taken over by
146   align_to_fixed_distance ().
147 */
148
149 vector<Real>
150 Align_interface::get_extents_aligned_translates (Grob *me,
151                                                  vector<Grob*> const &all_grobs,
152                                                  Axis a,
153                                                  bool pure, int start, int end)
154 {
155   Spanner *me_spanner = dynamic_cast<Spanner *> (me);
156
157
158   SCM line_break_details = SCM_EOL;
159   if (a == Y_AXIS && me_spanner)
160     {
161       line_break_details = me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
162
163       if (!me->get_system () && !pure)
164         me->warning (_ ("vertical alignment called before line-breaking.\n"
165                         "Only do cross-staff spanners with PianoStaff."));
166
167     }
168   
169   Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
170                                            DOWN);
171
172   Interval threshold = robust_scm2interval (me->get_property ("threshold"),
173                                             Interval (0, Interval::infinity ()));
174
175   vector<Interval> dims;
176   vector<Grob*> elems;
177
178   for (vsize i = 0; i < all_grobs.size (); i++)
179     {
180       Interval y = all_grobs[i]->maybe_pure_extent (all_grobs[i], a, pure, start, end);
181       if (!y.is_empty ())
182         {
183           Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
184
185           elems.push_back (e);
186           dims.push_back (y);
187         }
188     }
189
190   /*
191     Read self-alignment-X and self-alignment-Y. This may seem like
192     code duplication. (and really: it is), but this is necessary to
193     prevent ugly cyclic dependencies that arise when you combine
194     self-alignment on a child with alignment of children.
195   */
196   SCM align ((a == X_AXIS)
197              ? me->get_property ("self-alignment-X")
198              : me->get_property ("self-alignment-Y"));
199
200   Interval total;
201   Real where = 0;
202   Real extra_space = 0.0;
203   SCM extra_space_handle = scm_assq (ly_symbol2scm ("alignment-extra-space"), line_break_details);
204
205   extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
206                                    ? scm_cdr (extra_space_handle)
207                                    : SCM_EOL,
208                                    extra_space);
209
210   Real padding = robust_scm2double (me->get_property ("padding"),
211                                     0.0);
212   vector<Real> translates;
213   for (vsize j = 0; j < elems.size (); j++)
214     {
215       Real dy = -dims[j][-stacking_dir];
216       if (j)
217         dy += dims[j - 1][stacking_dir];
218
219       /*
220         we want dy to be > 0
221       */
222       dy *= stacking_dir;
223       if (j)
224         dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
225
226
227       where += stacking_dir * (dy + padding + extra_space / elems.size ());
228       total.unite (dims[j] + where);
229       translates.push_back (where);
230     }
231
232   SCM offsets_handle = scm_assq (ly_symbol2scm ("alignment-offsets"),
233                                  line_break_details);
234   if (scm_is_pair (offsets_handle))
235     {
236       vsize i = 0;
237  
238       for (SCM s = scm_cdr (offsets_handle);
239            scm_is_pair (s) && i < translates.size (); s = scm_cdr (s), i++)
240         {
241           if (scm_is_number (scm_car (s)))
242             translates[i] = scm_to_double (scm_car (s));
243         }
244     }
245
246   
247   Real center_offset = 0.0;
248   
249   /*
250     also move the grobs that were empty, to maintain spatial order.
251   */
252   vector<Real> all_translates;
253   if (translates.size ())
254     {
255       Real w = translates[0];
256
257       if (scm_is_number (align))
258         center_offset = total.linear_combination (scm_to_double (align));
259
260       for  (vsize i = 0, j = 0; j < all_grobs.size (); j++)
261         {
262           if (i < elems.size () && all_grobs[j] == elems[i])
263             w = translates[i++];
264           all_translates.push_back (w - center_offset);
265         }
266     }
267   return all_translates;
268 }
269
270 void
271 Align_interface::align_elements_to_extents (Grob *me, Axis a)
272 {
273   extract_grob_set (me, "elements", all_grobs);
274
275   vector<Real> translates = get_extents_aligned_translates (me, all_grobs, a, false, 0, 0);
276   if (translates.size ())
277       for (vsize j = 0; j < all_grobs.size (); j++)
278         all_grobs[j]->translate_axis (translates[j], a);
279 }
280
281 Real
282 Align_interface::get_pure_child_y_translation (Grob *me, Grob *ch, int start, int end)
283 {
284   extract_grob_set (me, "elements", all_grobs);
285   SCM dy_scm = me->get_property ("forced-distance");
286
287   if (scm_is_number (dy_scm))
288     {
289       Real dy = scm_to_double (dy_scm) * robust_scm2dir (me->get_property ("stacking-dir"), DOWN);
290       Real pos = 0;
291       for (vsize i = 0; i < all_grobs.size (); i++)
292         {
293           if (all_grobs[i] == ch)
294             return pos;
295           if (!Hara_kiri_group_spanner::has_interface (all_grobs[i])
296               || !Hara_kiri_group_spanner::request_suicide (all_grobs[i], start, end))
297             pos += dy;
298         }
299     }
300   else
301     {
302       vector<Real> translates = get_extents_aligned_translates (me, all_grobs, Y_AXIS, true, start, end);
303
304       if (translates.size ())
305         {
306           for (vsize i = 0; i < all_grobs.size (); i++)
307             if (all_grobs[i] == ch)
308               return translates[i];
309         }
310       else
311         return 0;
312     }
313
314   programming_error (_ ("tried to get a translation for something that isn't my child"));
315   return 0;
316 }
317
318 Axis
319 Align_interface::axis (Grob *me)
320 {
321   return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
322 }
323
324 void
325 Align_interface::add_element (Grob *me, Grob *element)
326 {
327   Axis a = Align_interface::axis (me);
328   SCM sym = axis_offset_symbol (a);
329   SCM proc = axis_parent_positioning (a);
330     
331   element->set_property (sym, proc);
332   Axis_group_interface::add_element (me, element);
333 }
334
335 void
336 Align_interface::set_ordered (Grob *me)
337 {
338   SCM ga_scm = me->get_object ("elements");
339   Grob_array *ga = unsmob_grob_array (ga_scm);
340   if (!ga)
341     {
342       ga_scm = Grob_array::make_array ();
343       ga = unsmob_grob_array (ga_scm);
344       me->set_object ("elements", ga_scm);
345     }
346
347   ga->set_ordered (true);
348 }
349
350 /*
351   Find Y-axis parent of G that has a #'forced-distance property. This
352   has the effect of finding the piano-staff given an object in that
353   piano staff.
354 */
355 Grob *
356 find_fixed_alignment_parent (Grob *g)
357 {
358   while (g)
359     {
360       if (scm_is_number (g->get_property ("forced-distance")))
361         return g;
362
363       g = g->get_parent (Y_AXIS);
364     }
365
366   return 0;
367 }
368
369 ADD_INTERFACE (Align_interface,
370                "align-interface",
371                
372                "Order grobs from top to bottom, left to right, right to left or bottom "
373                "to top.  "
374                "For vertical alignments of staves, the @code{break-system-details} of "
375                "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
376                "Set @code{alignment-extra-space} to add extra space for staves. Set "
377                "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
378                ,
379                
380                /*
381                  properties
382                 */
383                "align-dir "
384                "axes "
385                "elements "
386                "forced-distance "
387                "padding "
388                "positioning-done "
389                "stacking-dir "
390                "threshold "
391                );
392
393 struct Foobar
394 {
395   bool has_interface (Grob *);
396 };
397