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