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