]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
release: 1.5.29
[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--2002 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 ("alignment-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 ("alignment-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 ("alignment-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   SCM force = me->get_grob_property ("forced-distance");
60
61   Real dy = 0.0;
62   if (gh_number_p (force))
63     {
64       dy = gh_scm2double (force);
65     }
66   
67   Link_array<Grob> elems
68     = Pointer_group_interface__extract_grobs (me, (Grob*) 0, "elements");
69
70   Real where_f=0;
71
72   Interval v;
73   v.set_empty ();
74   Array<Real> translates;
75   
76   for (int j= elems.size (); j--; ) 
77     {
78       /*
79         This is not very elegant, in that we need special support for
80         hara kiri. Unfortunately, the generic wiring of
81         force_hara_kiri_callback () (extent and offset callback) is
82         such that we might get into a loop if we call extent() or
83         offset() the elements.
84         
85          
86        */
87       if (a == Y_AXIS
88           && Hara_kiri_group_spanner::has_interface (elems[j]))
89         Hara_kiri_group_spanner::consider_suicide (elems[j]);
90
91       if (!ly_pair_p (elems[j]-> immutable_property_alist_))
92         elems.del(j);
93     }
94
95   for (int j =0; j < elems.size (); j++)
96     {
97       where_f += stacking_dir * dy;
98       translates.push (where_f);
99       v.unite (Interval (where_f, where_f));
100     }
101
102   /*
103     TODO: support self-alignment-{Y,X}
104    */
105   for (int i = 0; i < translates.size (); i++)
106     {
107       elems[i]->translate_axis (translates[i] - v.center (), a);
108     }
109 }
110
111 /*
112   Hairy function to put elements where they should be. Can be tweaked
113   from the outside by setting minimum-space and extra-space in its
114   children
115
116   We assume that the children the refpoints of the children are still
117   found at 0.0 -- we will fuck up with thresholds if children's
118   extents are already moved to locations such as (-16, -8), since the
119   dy needed to put things in a row doesn't relate to the distances
120   between original refpoints.
121
122   TODO: maybe we should rethink and throw out thresholding altogether.
123   The original function has been taken over by
124   align_to_fixed_distance().
125 */
126 void
127 Align_interface::align_elements_to_extents (Grob * me, Axis a)
128 {
129   me->set_grob_property ("alignment-done", SCM_BOOL_T);
130   
131   SCM d =   me->get_grob_property ("stacking-dir");
132
133   
134   Direction stacking_dir = gh_number_p (d) ? to_dir (d) : CENTER;
135   if (!stacking_dir)
136     stacking_dir = DOWN;
137
138
139   
140   Interval threshold = Interval (0, Interval::infinity ());
141   SCM thr = me->get_grob_property ("threshold");
142   if (gh_pair_p (thr))
143     {
144       threshold[SMALLER] = gh_scm2double (ly_car (thr));
145       threshold[BIGGER] = gh_scm2double (ly_cdr (thr));      
146     }
147
148   
149   Array<Interval> dims;
150
151   Link_array<Grob> elems;
152   Link_array<Grob> all_grobs
153     = Pointer_group_interface__extract_grobs (me, (Grob*) 0, "elements");
154   for (int i=0; i < all_grobs.size (); i++) 
155     {
156       Interval y = all_grobs[i]->extent (me, a);
157       if (!y.empty_b ())
158         {
159           Grob *e =dynamic_cast<Grob*> (all_grobs[i]);
160
161           // todo: fucks up if item both in Halign & Valign. 
162           SCM min_dims = e->remove_grob_property ("minimum-space");
163           if (gh_pair_p (min_dims) &&
164               gh_number_p (ly_car (min_dims))
165               && gh_number_p (ly_cdr (min_dims)))
166             {
167               y.unite (ly_scm2interval (min_dims));
168             }
169           
170           SCM extra_dims = e->remove_grob_property ("extra-space");
171           if (gh_pair_p (extra_dims) &&
172               gh_number_p (ly_car (extra_dims))
173               && gh_number_p (ly_cdr (extra_dims)))
174             {
175               y[LEFT] += gh_scm2double (ly_car (extra_dims));
176               y[RIGHT] += gh_scm2double (ly_cdr (extra_dims));
177             }
178
179           elems.push (e);
180           dims.push (y);          
181         }
182     }
183   
184  
185   /*
186     Read self-alignment-X and self-alignment-Y. This may seem like
187     code duplication. (and really: it is), but this is necessary to
188     prevent ugly cyclic dependencies that arise when you combine
189     self-alignment on a child with alignment of children.
190   */
191
192   String s ("self-alignment-");
193
194   s += (a == X_AXIS) ? "X" : "Y";
195
196   SCM align (me->get_grob_property (s.ch_C ()));
197      
198   Array<Real> translates ;
199   Interval total;
200   Real where_f=0;
201   
202   for (int j=0 ;  j < elems.size (); j++) 
203     {
204       Real dy = -  dims[j][-stacking_dir];
205       if (j)
206         dy += dims[j-1][stacking_dir];
207
208
209       /*
210         we want dy to be > 0
211        */
212       dy *= stacking_dir; 
213       if (j)
214         {
215           dy = (dy >? threshold[SMALLER])
216             <? threshold[BIGGER];
217         }
218
219       where_f += stacking_dir * dy;
220       total.unite (dims[j] +   where_f);
221       translates.push (where_f);
222     }
223
224   
225   Grob * align_center = unsmob_grob (align);
226   Real center_offset = 0.0;
227   
228   /*
229     also move the grobs that were empty, to maintain spatial order. 
230    */
231   Array<Real> all_translates;
232   if (translates.size ())
233     {
234       int i =0;
235       int j =0;
236       Real w = translates[0];
237       while (j  < all_grobs.size ())
238         {
239           if (i < elems.size () && all_grobs[j] == elems[i])
240             {
241               w = translates[i++];
242             }
243           if (all_grobs[j] == align_center)
244             center_offset = w;
245           all_translates .push (w);
246           j++;
247         }
248
249
250       /*
251         FIXME: uncommenting freaks out the Y-alignment of
252         line-of-score.
253        */
254       // Real align_param = isdir_b (align)  ? gh_scm2double (align) : 0.0;
255       
256       if (gh_number_p (align))
257         center_offset = total.linear_combination (gh_scm2double (align));
258
259       for (int j = 0 ;  j < all_grobs.size (); j++)
260         all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
261     }
262 }
263 Axis
264 Align_interface::axis (Grob*me)
265 {
266   return  Axis (gh_scm2int (ly_car (me->get_grob_property ("axes"))));
267 }
268
269
270 /*
271   should  use generic Scm funcs.
272  */
273 int
274 Align_interface::get_count (Grob*me,Grob*s)
275 {
276   SCM e = me->get_grob_property ("elements");
277   int c =0;
278   while (gh_pair_p (e))
279     {
280       if (ly_car (e) == s->self_scm ())
281         break;
282       c++;
283       e = ly_cdr (e);
284     }
285   return c;
286 }
287
288 void
289 Align_interface::add_element (Grob*me,Grob* s, SCM cb)
290 {
291   s->add_offset_callback (cb, Align_interface::axis (me));
292   Axis_group_interface::add_element (me, s);
293 }
294
295
296 void
297 Align_interface::set_interface (Grob*me)
298 {
299   me->set_interface (ly_symbol2scm ("align-interface"));
300
301   Axis_group_interface::set_interface (me);
302 }
303
304 void
305 Align_interface::set_axis (Grob*me,Axis a)
306 {
307   Axis_group_interface::set_axes (me, a,a);
308 }
309
310 bool
311 Align_interface::has_interface (Grob*me)
312 {
313   return me && me->has_interface (ly_symbol2scm ("align-interface"));
314 }
315