]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
* lily/horizontal-bracket.cc (make_bracket): new function.
[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 0
163       /*
164         TODO: messes up for figured bass alignments. 
165        */
166       if (me_spanner->get_bound (LEFT)->break_status_dir () == CENTER)
167         me->warning (_ ("vertical alignment called before line-breaking. "
168                         "Only do cross-staff spanners with PianoStaff."));
169 #endif
170
171       SCM details =  me_spanner->get_bound (LEFT)->get_property ("line-break-system-details");
172       SCM extra_space_handle = scm_assoc (ly_symbol2scm ("alignment-extra-space"), details);
173
174       extra_space = robust_scm2double (scm_is_pair (extra_space_handle)
175                                        ? scm_cdr (extra_space_handle)
176                                        : SCM_EOL,
177                                        extra_space);
178     }
179   
180   me->set_property ("positioning-done", SCM_BOOL_T);
181   
182   Direction stacking_dir = robust_scm2dir (me->get_property ("stacking-dir"),
183                                                DOWN);
184
185   Interval threshold = robust_scm2interval (me->get_property ("threshold"),
186                                             Interval (0, Interval::infinity ()));
187
188   Array<Interval> dims;
189
190   Link_array<Grob> elems;
191
192   extract_grob_set (me, "elements", all_grobs);
193   for (int i = 0; i < all_grobs.size (); i++)
194     {
195       Interval y = all_grobs[i]->extent (me, a);
196       if (!y.is_empty ())
197         {
198           Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
199
200           elems.push (e);
201           dims.push (y);
202         }
203     }
204
205   /*
206     Read self-alignment-X and self-alignment-Y. This may seem like
207     code duplication. (and really: it is), but this is necessary to
208     prevent ugly cyclic dependencies that arise when you combine
209     self-alignment on a child with alignment of children.
210   */
211   SCM align ((a == X_AXIS)
212              ? me->get_property ("self-alignment-X")
213              : me->get_property ("self-alignment-Y"));
214
215   Array<Real> translates;
216   Interval total;
217   Real where = 0;
218
219   for (int j = 0; j < elems.size (); j++)
220     {
221       Real dy = -dims[j][-stacking_dir];
222       if (j)
223         dy += dims[j - 1][stacking_dir];
224
225       /*
226         we want dy to be > 0
227       */
228       dy *= stacking_dir;
229       if (j)
230         dy = min (max (dy, threshold[SMALLER]), threshold[BIGGER]);
231
232       where += stacking_dir * (dy + extra_space / elems.size ());
233       total.unite (dims[j] + where);
234       translates.push (where);
235     }
236
237   Real center_offset = 0.0;
238   
239   /*
240     also move the grobs that were empty, to maintain spatial order.
241   */
242   Array<Real> all_translates;
243   if (translates.size ())
244     {
245       int i = 0;
246       int j = 0;
247       Real w = translates[0];
248       while (j < all_grobs.size ())
249         {
250           if (i < elems.size () && all_grobs[j] == elems[i])
251             w = translates[i++];
252           all_translates.push (w);
253           j++;
254         }
255
256       /*
257         FIXME: uncommenting freaks out the Y-alignment of
258         line-of-score.
259       */
260       if (scm_is_number (align))
261         center_offset = total.linear_combination (scm_to_double (align));
262
263       for (int j = 0; j < all_grobs.size (); j++)
264         all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
265     }
266 }
267 Axis
268 Align_interface::axis (Grob *me)
269 {
270   return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
271 }
272
273 void
274 Align_interface::add_element (Grob *me, Grob *element, SCM call_back)
275 {
276   element->add_offset_callback (call_back, Align_interface::axis (me));
277   Axis_group_interface::add_element (me, element);
278 }
279
280 void
281 Align_interface::set_axis (Grob *me, Axis a)
282 {
283   Axis_group_interface::set_axes (me, a, a);
284   SCM ga_scm = me->get_object ("elements");
285   Grob_array *ga = unsmob_grob_array (ga_scm);
286   if (!ga)
287     {
288       ga_scm = Grob_array::make_array ();
289       ga = unsmob_grob_array (ga_scm);
290       me->set_object ("elements", ga_scm);
291     }
292
293   ga->set_ordered (true);
294 }
295
296 /*
297   Find Y-axis parent of G that has a #'forced-distance property. This
298   has the effect of finding the piano-staff given an object in that
299   piano staff.
300 */
301 Grob *
302 find_fixed_alignment_parent (Grob *g)
303 {
304   while (g)
305     {
306       if (scm_is_number (g->get_property ("forced-distance")))
307         return g;
308
309       g = g->get_parent (Y_AXIS);
310     }
311
312   return 0;
313 }
314
315 ADD_INTERFACE (Align_interface,
316                "align-interface",
317                
318                "Order grobs from top to bottom, left to right, right to left or bottom "
319                "to top.  "
320                "For vertical alignments of staves, the @code{break-system-details} of "
321                "the left @internalsref{NonMusicalPaperColumn} may be set to tune vertical spacing "
322                "Set @code{alignment-extra-space} to add extra space for staves. Set "
323                "@code{fixed-alignment-extra-space} to force staves in PianoStaves further apart."
324                ,
325                
326                /*
327                  properties
328                 */
329                "forced-distance "
330                "stacking-dir "
331                "align-dir "
332                "threshold "
333                "positioning-done "
334                "elements axes");
335
336 struct Foobar
337 {
338   bool has_interface (Grob *);
339 };
340