]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
release: 1.3.132
[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 /*
17   This callback is set in the children of the align element. It does
18   not compute anything, but a side effect of a->do_side_processing ()
19   is that the elements are placed correctly.  */
20 MAKE_SCHEME_CALLBACK(Align_interface,alignment_callback,2);
21 SCM
22 Align_interface::alignment_callback (SCM element_smob, SCM axis)
23 {
24   Grob * me = unsmob_grob (element_smob);
25   Axis ax = (Axis )gh_scm2int (axis);
26   Grob * par = me->parent_l (ax);
27   if (par && !to_boolean (par->get_grob_property ("alignment-done")))
28     {
29       Align_interface::align_elements_to_extents (par, ax);
30     }
31   return gh_double2scm (0.0);
32 }
33
34 MAKE_SCHEME_CALLBACK(Align_interface,fixed_distance_alignment_callback,2);
35 SCM
36 Align_interface::fixed_distance_alignment_callback (SCM element_smob, SCM axis)
37 {
38   Grob * me = unsmob_grob (element_smob);
39   Axis ax = (Axis )gh_scm2int (axis);
40   Grob * par = me->parent_l (ax);
41   if (par && !to_boolean (par->get_grob_property ("alignment-done")))
42     {
43       Align_interface::align_to_fixed_distance (par, ax);
44     }
45   return gh_double2scm (0.0);
46 }
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   
56   Direction stacking_dir = gh_number_p(d) ? to_dir (d) : CENTER;
57   if (!stacking_dir)
58     stacking_dir = DOWN;
59
60
61   SCM force = me->get_grob_property ("forced-distance");
62
63   Real dy = 0.0;
64   if (gh_number_p (force))
65     {
66       dy = gh_scm2double (force);
67     }
68   
69   Link_array<Grob> elems
70     = Pointer_group_interface__extract_elements (  me, (Grob*) 0, "elements");
71   Real where_f=0;
72   for (int j=0 ;  j < elems.size(); j++) 
73     {
74       where_f += stacking_dir * dy;
75       elems[j]->translate_axis (where_f, a);
76     }
77 }
78
79 /*
80   Hairy function to put elements where they should be. Can be tweaked
81   from the outside by setting minimum-space and extra-space in its
82   children */
83 void
84 Align_interface::align_elements_to_extents (Grob * me, Axis a)
85 {
86   me->set_grob_property ("alignment-done", SCM_BOOL_T);
87   
88   SCM d =   me->get_grob_property ("stacking-dir");
89
90   
91   Direction stacking_dir = gh_number_p(d) ? to_dir (d) : CENTER;
92   if (!stacking_dir)
93     stacking_dir = DOWN;
94
95
96   
97   Interval threshold = Interval (0, Interval::infinity ());
98   SCM thr = me->get_grob_property ("threshold");
99   if (gh_pair_p (thr))
100     {
101       threshold[SMALLER] = gh_scm2double (gh_car (thr));
102       threshold[BIGGER] = gh_scm2double (gh_cdr (thr));      
103     }
104
105   
106   Array<Interval> dims;
107
108   Link_array<Grob> elems;
109   Link_array<Grob> all_grobs
110     = Pointer_group_interface__extract_elements (me, (Grob*) 0, "elements");
111   for (int i=0; i < all_grobs.size(); i++) 
112     {
113       Interval y = all_grobs[i]->extent(me, a);
114       if (!y.empty_b())
115         {
116           Grob *e =dynamic_cast<Grob*>(all_grobs[i]);
117
118           // todo: fucks up if item both in Halign & Valign. 
119           SCM min_dims = e->remove_grob_property ("minimum-space");
120           if (gh_pair_p (min_dims) &&
121               gh_number_p (gh_car (min_dims))
122               && gh_number_p (gh_cdr (min_dims)))
123             {
124               y.unite (ly_scm2interval (min_dims));
125             }
126           
127           SCM extra_dims = e->remove_grob_property ("extra-space");
128           if (gh_pair_p (extra_dims) &&
129               gh_number_p (gh_car (extra_dims))
130               && gh_number_p (gh_cdr (extra_dims)))
131             {
132               y[LEFT] += gh_scm2double (gh_car  (extra_dims));
133               y[RIGHT] += gh_scm2double (gh_cdr (extra_dims));
134             }
135
136           elems.push (e);
137           dims.push (y);          
138         }
139     }
140   
141  
142   /*
143     Read self-alignment-X and self-alignment-Y. This may seem like
144     code duplication. (and really: it is), but this is necessary to
145     prevent ugly cyclic dependencies that arise when you combine
146     self-alignment on a child with alignment of children.
147   */
148
149   String s ("self-alignment-");
150
151   s +=  (a == X_AXIS) ? "X" : "Y";
152
153   SCM align (me->get_grob_property (s.ch_C()));
154      
155   Array<Real> translates ;
156   Interval total;
157   Real where_f=0;
158   
159   for (int j=0 ;  j < elems.size(); j++) 
160     {
161       Real dy = 0.0;
162       dy = - stacking_dir * dims[j][-stacking_dir];
163       if (j)
164         dy += stacking_dir * dims[j-1][stacking_dir];
165
166       if (j)
167         {
168           dy = (dy >? threshold[SMALLER] )
169             <? threshold[BIGGER];
170         }
171
172       where_f += stacking_dir * dy;
173       total.unite ( dims[j] +   where_f);
174       translates.push (where_f);
175     }
176
177   
178
179   Grob * align_center = unsmob_grob (align);
180   Real center_offset = 0.0;
181   
182   /*
183     also move the grobs that were empty, to maintain spatial order. 
184    */
185   Array<Real> all_translates;
186   if (translates.size  ())
187     {
188       int i =0;
189       int j =0;
190       Real w = translates[0];
191       while (j  < all_grobs.size ())
192         {
193           if (i < elems.size () && all_grobs[j] == elems[i])
194             {
195               w = translates[i++];
196             }
197           if (all_grobs[j] == align_center)
198             center_offset = w;
199           all_translates .push (w);
200           j++;
201         }
202     }
203
204   if (isdir_b  (align))
205     {
206       center_offset = total.linear_combination (gh_scm2double (align));
207     }
208
209   for (int j = 0 ;  j < all_grobs.size (); j++)
210     all_grobs[j]->translate_axis (all_translates[j] - center_offset, a);
211 }
212
213 Axis
214 Align_interface::axis (Grob*me)
215 {
216   return  Axis (gh_scm2int (gh_car (me->get_grob_property ("axes"))));
217 }
218
219
220 /*
221   should  use generic Scm funcs.
222  */
223 int
224 Align_interface::get_count (Grob*me,Grob*s)
225 {
226   SCM e = me->get_grob_property ("elements");
227   int c =0;
228   while (gh_pair_p (e))
229     {
230       if (gh_car (e) == s->self_scm ())
231         break;
232       c++;
233       e = gh_cdr (e);
234     }
235   return c;
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
246 void
247 Align_interface::set_interface (Grob*me)
248 {
249   me->set_interface (ly_symbol2scm ("align-interface"));
250
251   Axis_group_interface::set_interface (me);
252 }
253
254 void
255 Align_interface::set_axis (Grob*me,Axis a)
256 {
257   Axis_group_interface::set_axes (me, a,a );
258 }
259
260 bool
261 Align_interface::has_interface (Grob*me)
262 {
263   return me && me->has_interface (ly_symbol2scm ("align-interface"));
264 }
265