]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
* input/regression/alignment-vertical-spacing.ly: new file.
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.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
17 /*
18   TODO: for vertical spacing, should also include a rod & spring
19   scheme of sorts into this: the alignment should default to a certain
20   distance between element refpoints, unless bbox force a bigger
21   distance.
22  */
23
24 MAKE_SCHEME_CALLBACK (Align_interface, alignment_callback, 2);
25 SCM
26 Align_interface::alignment_callback (SCM element_smob, SCM axis)
27 {
28   Grob *me = unsmob_grob (element_smob);
29   Axis ax = (Axis)scm_to_int (axis);
30   Grob *par = me->get_parent (ax);
31   if (par && !to_boolean (par->get_property ("positioning-done")))
32     Align_interface::align_elements_to_extents (par, ax);
33   return scm_from_double (0.0);
34 }
35
36 MAKE_SCHEME_CALLBACK (Align_interface, fixed_distance_alignment_callback, 2);
37 SCM
38 Align_interface::fixed_distance_alignment_callback (SCM element_smob, SCM axis)
39 {
40   Grob *me = unsmob_grob (element_smob);
41   Axis ax = (Axis)scm_to_int (axis);
42   Grob *par = me->get_parent (ax);
43   if (par && !to_boolean (par->get_property ("positioning-done")))
44     Align_interface::align_to_fixed_distance (par, ax);
45   return scm_from_double (0.0);
46 }
47
48 /*
49   merge with align-to-extents?
50 */
51 MAKE_SCHEME_CALLBACK(Align_interface, stretch_after_break, 1)
52 SCM
53 Align_interface::stretch_after_break (SCM grob)
54 {
55   Grob *me = unsmob_grob (grob);
56
57   Spanner *me_spanner = dynamic_cast<Spanner *> (me);
58   extract_grob_set (me, "elements", elems);
59   if (me_spanner && elems.size ())
60     {
61       Grob *common = common_refpoint_of_array (elems, me, Y_AXIS);
62
63       /* force position callbacks */
64       for (int i = 0; i < elems.size (); i++)
65         elems[i]->relative_coordinate (common, Y_AXIS);
66
67       SCM details =  me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
68       SCM extra_space_handle = scm_assoc (ly_symbol2scm ("fixed-alignment-extra-space"), details);
69       
70       Real extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
71                                             ? scm_cdr (extra_space_handle)
72                                             : SCM_EOL,
73                                             0.0);
74
75       Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
76                                                DOWN);
77       Real delta  = extra_space / elems.size() * stacking_dir;
78       for (int i = 0; i < elems.size (); i++)
79         elems[i]->translate_axis (i * delta, Y_AXIS);
80     }
81   
82   return SCM_UNSPECIFIED;
83 }
84
85 /*
86   merge with align-to-extents?
87 */
88 void
89 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
90 {
91   me->set_property ("positioning-done", SCM_BOOL_T);
92
93   Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
94                                            DOWN);
95
96   Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
97
98   extract_grob_set (me, "elements", elem_source);
99
100   Link_array<Grob> elems (elem_source); // writable..
101
102   Real where_f = 0;
103
104   Interval v;
105   v.set_empty ();
106   Array<Real> translates;
107
108   for (int j = elems.size (); j--;)
109     {
110       /*
111         This is not very elegant, in that we need special support for
112         hara-kiri. Unfortunately, the generic wiring of
113         force_hara_kiri_callback () (extent and offset callback) is
114         such that we might get into a loop if we call extent () or
115         offset () the elements.
116       */
117       if (a == Y_AXIS
118           && Hara_kiri_group_spanner::has_interface (elems[j]))
119         Hara_kiri_group_spanner::consider_suicide (elems[j]);
120
121       if (!elems[j]->is_live ())
122         elems.del (j);
123     }
124
125   for (int j = 0; j < elems.size (); j++)
126     {
127       where_f += stacking_dir * dy;
128       translates.push (where_f);
129       v.unite (Interval (where_f, where_f));
130     }
131
132   /*
133     TODO: support self-alignment-{Y, X}
134   */
135   for (int i = 0; i < translates.size (); i++)
136     elems[i]->translate_axis (translates[i] - v.center (), a);
137 }
138
139 /*
140   Hairy function to put elements where they should be. Can be tweaked
141   from the outside by setting extra-space in its
142   children
143
144   We assume that the children the refpoints of the children are still
145   found at 0.0 -- we will fuck up with thresholds if children's
146   extents are already moved to locations such as (-16, -8), since the
147   dy needed to put things in a row doesn't relate to the distances
148   between original refpoints.
149
150   TODO: maybe we should rethink and throw out thresholding altogether.
151   The original function has been taken over by
152   align_to_fixed_distance ().
153 */
154 void
155 Align_interface::align_elements_to_extents (Grob *me, Axis a)
156 {
157   Real extra_space = 0.0;
158   Spanner *me_spanner = dynamic_cast<Spanner *> (me);
159   if (a == Y_AXIS
160       && me_spanner)
161     {
162       if (me_spanner->get_bound (LEFT)->break_status_dir () == CENTER)
163         me->warning (_ ("vertical alignment called before line-breaking. "
164                         "Only do cross-staff spanners with PianoStaff."));
165
166       SCM details =  me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
167       SCM extra_space_handle = scm_assoc (ly_symbol2scm ("alignment-extra-space"), details);
168
169       extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
170                                        ? scm_cdr (extra_space_handle)
171                                        : SCM_EOL,
172                                        extra_space);
173     }
174   
175   me->set_property ("positioning-done", SCM_BOOL_T);
176   
177   Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
178                                                DOWN);
179
180   Interval threshold = robust_scm2interval (me->get_property ("threshold"),
181                                             Interval (0, Interval::infinity ()));
182
183   Array<Interval> dims;
184
185   Link_array<Grob> elems;
186
187   extract_grob_set (me, "elements", all_grobs);
188   for (int i = 0; i < all_grobs.size (); i++)
189     {
190       Interval y = all_grobs[i]->extent (me, a);
191       if (!y.is_empty ())
192         {
193           Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
194
195           elems.push (e);
196           dims.push (y);
197         }
198     }
199
200   /*
201     Read self-alignment-X and self-alignment-Y. This may seem like
202     code duplication. (and really: it is), but this is necessary to
203     prevent ugly cyclic dependencies that arise when you combine
204     self-alignment on a child with alignment of children.
205   */
206   SCM align ((a == X_AXIS)
207              ? me->get_property ("self-alignment-X")
208              : me->get_property ("self-alignment-Y"));
209
210   Array<Real> translates;
211   Interval total;
212   Real where = 0;
213
214   for (int j = 0; j < elems.size (); j++)
215     {
216       Real dy = -dims[j][-stacking_dir];
217       if (j)
218         dy += dims[j - 1][stacking_dir];
219
220       /*
221         we want dy to be > 0
222       */
223       dy *= stacking_dir;
224       if (j)
225         dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
226
227       where += stacking_dir * (dy + extra_space / elems.size ());
228       total.unite (dims[j] + where);
229       translates.push (where);
230     }
231
232   Real center_offset = 0.0;
233   
234   /*
235     also move the grobs that were empty, to maintain spatial order.
236   */
237   Array<Real> all_translates;
238   if (translates.size ())
239     {
240       int i = 0;
241       int j = 0;
242       Real w = translates[0];
243       while (j < all_grobs.size ())
244         {
245           if (i < elems.size () && all_grobs[j] == elems[i])
246             w = translates[i++];
247           all_translates.push (w);
248           j++;
249         }
250
251       /*
252         FIXME: uncommenting freaks out the Y-alignment of
253         line-of-score.
254       */
255       if (scm_is_number (align))
256         center_offset = total.linear_combination (scm_to_double (align));
257
258       for (int j = 0; j < all_grobs.size (); j++)
259         all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
260     }
261 }
262 Axis
263 Align_interface::axis (Grob *me)
264 {
265   return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
266 }
267
268 void
269 Align_interface::add_element (Grob *me, Grob *element, SCM call_back)
270 {
271   element->add_offset_callback (call_back, Align_interface::axis (me));
272   Axis_group_interface::add_element (me, element);
273 }
274
275 void
276 Align_interface::set_axis (Grob *me, Axis a)
277 {
278   Axis_group_interface::set_axes (me, a, a);
279   SCM ga_scm = me->get_object ("elements");
280   Grob_array *ga = unsmob_grob_array (ga_scm);
281   if (!ga)
282     {
283       ga_scm = Grob_array::make_array ();
284       ga = unsmob_grob_array (ga_scm);
285       me->set_object ("elements", ga_scm);
286     }
287
288   ga->set_ordered (true);
289 }
290
291 /*
292   Find Y-axis parent of G that has a #'forced-distance property. This
293   has the effect of finding the piano-staff given an object in that
294   piano staff.
295 */
296 Grob *
297 find_fixed_alignment_parent (Grob *g)
298 {
299   while (g)
300     {
301       if (scm_is_number (g->get_property ("forced-distance")))
302         return g;
303
304       g = g->get_parent (Y_AXIS);
305     }
306
307   return 0;
308 }
309
310 ADD_INTERFACE (Align_interface,
311                "align-interface",
312                
313                "Order grobs from top to bottom, left to right, right to left or bottom "
314                "to top.  "
315                "For vertical alignments of staves, the @code{break-system-details} of "
316                "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
317                "Set @code{alignment-extra-space} to add extra space for staves. Set "
318                "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
319                ,
320                
321                /*
322                  properties
323                 */
324                "forced-distance "
325                "stacking-dir "
326                "align-dir "
327                "threshold "
328                "positioning-done "
329                "elements axes");
330
331 struct Foobar
332 {
333   bool has_interface (Grob *);
334 };
335