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