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