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