]> git.donarmstrong.com Git - lilypond.git/blob - lily/align-interface.cc
release: 1.3.131
[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_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_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   Real where_f=0;
143   Array<Real> translates ;
144   for (int j=0 ;  j < elems.size(); j++) 
145     {
146       Real dy = 0.0;
147       dy = - stacking_dir * dims[j][-stacking_dir];
148       if (j)
149         dy += stacking_dir * dims[j-1][stacking_dir];
150       
151       if (j)
152         {
153           dy = (dy >? threshold[SMALLER] )
154             <? threshold[BIGGER];
155         }
156
157       where_f += stacking_dir * dy;
158       translates.push (where_f);
159     }
160
161   /*
162     also move the grobs that were empty, to maintain spatial order. 
163    */
164   if (translates.size  ())
165     {
166       int i =0;
167       int j =0;
168       Real w = translates[0];
169       while (j  < all_grobs.size ())
170         {
171           if (i < elems.size () && all_grobs[j] == elems[i])
172             {
173               w = translates[i++];
174             }
175           all_grobs[j]->translate_axis (w, a);
176
177           j++;
178         }
179     }
180 }
181
182 Axis
183 Align_interface::axis (Grob*me)
184 {
185   return  Axis (gh_scm2int (gh_car (me->get_grob_property ("axes"))));
186 }
187
188
189 /*
190   should  use generic Scm funcs.
191  */
192 int
193 Align_interface::get_count (Grob*me,Grob*s)
194 {
195   SCM e = me->get_grob_property ("elements");
196   int c =0;
197   while (gh_pair_p (e))
198     {
199       if (gh_car (e) == s->self_scm ())
200         break;
201       c++;
202       e = gh_cdr (e);
203     }
204   return c;
205 }
206
207 void
208 Align_interface::add_element (Grob*me,Grob* s, SCM cb)
209 {
210   s->add_offset_callback (cb, Align_interface::axis (me));
211   Axis_group_interface::add_element (me, s);
212 }
213
214
215 void
216 Align_interface::set_interface (Grob*me)
217 {
218   me->set_interface (ly_symbol2scm ("align-interface"));
219
220   Axis_group_interface::set_interface (me);
221 }
222
223 void
224 Align_interface::set_axis (Grob*me,Axis a)
225 {
226   Axis_group_interface::set_axes (me, a,a );
227 }
228
229 bool
230 Align_interface::has_interface (Grob*me)
231 {
232   return me && me->has_interface (ly_symbol2scm ("align-interface"));
233 }
234