]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
* lily/align-interface.cc (align_elements_to_extents): warn if
[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
11 #include "spanner.hh"
12 #include "item.hh"
13 #include "axis-group-interface.hh"
14 #include "hara-kiri-group-spanner.hh"
15
16 MAKE_SCHEME_CALLBACK (Align_interface, alignment_callback, 2);
17 SCM
18 Align_interface::alignment_callback (SCM element_smob, SCM axis)
19 {
20   Grob *me = unsmob_grob (element_smob);
21   Axis ax = (Axis)scm_to_int (axis);
22   Grob *par = me->get_parent (ax);
23   if (par && !to_boolean (par->get_property ("positioning-done")))
24     {
25       Align_interface::align_elements_to_extents (par, ax);
26     }
27   return scm_make_real (0.0);
28 }
29
30 MAKE_SCHEME_CALLBACK (Align_interface, fixed_distance_alignment_callback, 2);
31 SCM
32 Align_interface::fixed_distance_alignment_callback (SCM element_smob, SCM axis)
33 {
34   Grob *me = unsmob_grob (element_smob);
35   Axis ax = (Axis)scm_to_int (axis);
36   Grob *par = me->get_parent (ax);
37   if (par && !to_boolean (par->get_property ("positioning-done")))
38     {
39       Align_interface::align_to_fixed_distance (par, ax);
40     }
41   return scm_make_real (0.0);
42 }
43
44 /*
45   merge with align-to-extents?
46 */
47 void
48 Align_interface::align_to_fixed_distance (Grob *me, Axis a)
49 {
50   me->set_property ("positioning-done", SCM_BOOL_T);
51
52   SCM d = me->get_property ("stacking-dir");
53
54   Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
55   if (!stacking_dir)
56     stacking_dir = DOWN;
57
58   Real dy = robust_scm2double (me->get_property ("forced-distance"), 0.0);
59
60   Link_array<Grob> elems
61     = extract_grob_array (me, ly_symbol2scm ("elements"));
62
63   Real where_f = 0;
64
65   Interval v;
66   v.set_empty ();
67   Array<Real> translates;
68
69   for (int j = elems.size (); j--;)
70     {
71       /*
72         This is not very elegant, in that we need special support for
73         hara-kiri. Unfortunately, the generic wiring of
74         force_hara_kiri_callback () (extent and offset callback) is
75         such that we might get into a loop if we call extent () or
76         offset () the elements.
77
78
79       */
80       if (a == Y_AXIS
81           && Hara_kiri_group_spanner::has_interface (elems[j]))
82         Hara_kiri_group_spanner::consider_suicide (elems[j]);
83
84       if (!elems[j]->is_live ())
85         elems.del (j);
86     }
87
88   for (int j = 0; j < elems.size (); j++)
89     {
90       where_f += stacking_dir * dy;
91       translates.push (where_f);
92       v.unite (Interval (where_f, where_f));
93     }
94
95   /*
96     TODO: support self-alignment-{Y, X}
97   */
98   for (int i = 0; i < translates.size (); i++)
99     {
100       elems[i]->translate_axis (translates[i] - v.center (), a);
101     }
102 }
103
104 /*
105   Hairy function to put elements where they should be. Can be tweaked
106   from the outside by setting extra-space in its
107   children
108
109   We assume that the children the refpoints of the children are still
110   found at 0.0 -- we will fuck up with thresholds if children's
111   extents are already moved to locations such as (-16, -8), since the
112   dy needed to put things in a row doesn't relate to the distances
113   between original refpoints.
114
115   TODO: maybe we should rethink and throw out thresholding altogether.
116   The original function has been taken over by
117   align_to_fixed_distance ().
118 */
119 void
120 Align_interface::align_elements_to_extents (Grob *me, Axis a)
121 {
122   Spanner *me_spanner = dynamic_cast<Spanner *> (me);
123   if (a == Y_AXIS
124       && me_spanner
125       && me_spanner->get_bound (LEFT)->break_status_dir () == CENTER)
126     {
127       me_spanner->warning (_("vertical alignment called before line-breaking. Only do cross-staff spanners with PianoStaff."));
128     }
129   
130   me->set_property ("positioning-done", SCM_BOOL_T);
131
132   SCM d = me->get_property ("stacking-dir");
133
134   Direction stacking_dir = scm_is_number (d) ? to_dir (d) : CENTER;
135   if (!stacking_dir)
136     stacking_dir = DOWN;
137
138   Interval threshold = robust_scm2interval (me->get_property ("threshold"),
139                                             Interval (0, Interval::infinity ()));
140
141   Array<Interval> dims;
142
143   Link_array<Grob> elems;
144   Link_array<Grob> all_grobs
145     = extract_grob_array (me, ly_symbol2scm ("elements"));
146   for (int i = 0; i < all_grobs.size (); i++)
147     {
148       Interval y = all_grobs[i]->extent (me, a);
149       if (!y.is_empty ())
150         {
151           Grob *e = dynamic_cast<Grob *> (all_grobs[i]);
152
153           elems.push (e);
154           dims.push (y);
155         }
156     }
157
158   /*
159     Read self-alignment-X and self-alignment-Y. This may seem like
160     code duplication. (and really: it is), but this is necessary to
161     prevent ugly cyclic dependencies that arise when you combine
162     self-alignment on a child with alignment of children.
163   */
164   static SCM prop_syms[2];
165
166   if (!prop_syms[0])
167     {
168       prop_syms[X_AXIS] = ly_symbol2scm ("self-alignment-X");
169       prop_syms[Y_AXIS] = ly_symbol2scm ("self-alignment-Y");
170     }
171
172   SCM align (me->internal_get_property (prop_syms[a]));
173
174   Array<Real> translates;
175   Interval total;
176   Real where_f = 0;
177
178   for (int j = 0; j < elems.size (); j++)
179     {
180       Real dy = -dims[j][-stacking_dir];
181       if (j)
182         dy += dims[j - 1][stacking_dir];
183
184       /*
185         we want dy to be > 0
186       */
187       dy *= stacking_dir;
188       if (j)
189         {
190           dy = min (max (dy, threshold[SMALLER]),
191                     threshold[BIGGER]);
192         }
193
194       where_f += stacking_dir * dy;
195       total.unite (dims[j] + where_f);
196       translates.push (where_f);
197     }
198
199   Real center_offset = 0.0;
200   /*
201     also move the grobs that were empty, to maintain spatial order.
202   */
203   Array<Real> all_translates;
204   if (translates.size ())
205     {
206       int i = 0;
207       int j = 0;
208       Real w = translates[0];
209       while (j < all_grobs.size ())
210         {
211           if (i < elems.size () && all_grobs[j] == elems[i])
212             {
213               w = translates[i++];
214             }
215           all_translates.push (w);
216           j++;
217         }
218
219       /*
220         FIXME: uncommenting freaks out the Y-alignment of
221         line-of-score.
222       */
223       if (scm_is_number (align))
224         center_offset = total.linear_combination (scm_to_double (align));
225
226       for (int j = 0; j < all_grobs.size (); j++)
227         all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
228     }
229 }
230 Axis
231 Align_interface::axis (Grob *me)
232 {
233   return Axis (scm_to_int (scm_car (me->get_property ("axes"))));
234 }
235
236 void
237 Align_interface::add_element (Grob *me, Grob *element, SCM call_back)
238 {
239   element->add_offset_callback (call_back, Align_interface::axis (me));
240   Axis_group_interface::add_element (me, element);
241 }
242
243 void
244 Align_interface::set_axis (Grob *me, Axis a)
245 {
246   Axis_group_interface::set_axes (me, a, a);
247 }
248
249 /*
250   Find Y-axis parent of G that has a #'forced-distance property. This
251   has the effect of finding the piano-staff given an object in that
252   piano staff.
253 */
254 Grob *
255 find_fixed_alignment_parent (Grob *g)
256 {
257   while (g)
258     {
259       if (scm_is_number (g->get_property ("forced-distance")))
260         return g;
261
262       g = g->get_parent (Y_AXIS);
263     }
264
265   return 0;
266 }
267
268 ADD_INTERFACE (Align_interface, "align-interface",
269                "Order grobs from top to bottom, left to right, right to left or bottom"
270                "to top.",
271                "forced-distance stacking-dir align-dir threshold positioning-done "
272                "center-element elements axes");
273
274 struct Foobar
275 {
276   bool has_interface (Grob *);
277 };
278