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