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