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