]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
release: 1.4.2
[lilypond.git] / lily / side-position-interface.cc
1 /*   
2   staff-side.cc --  implement Staff_side_element
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include <math.h>               // ceil.
10
11 #include "side-position-interface.hh"
12 #include "debug.hh"
13 #include "warn.hh"
14 #include "dimensions.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "group-interface.hh"
17 #include "directional-element-interface.hh"
18
19 void
20 Side_position_interface::add_support (Grob*me, Grob*e)
21 {
22   Pointer_group_interface::add_element (me, "side-support-elements",e);
23 }
24
25
26
27 Direction
28 Side_position_interface::get_direction (Grob*me)
29 {
30   SCM d = me->get_grob_property ("direction");
31   if (isdir_b (d) && to_dir (d))
32     return to_dir (d);
33
34   Direction relative_dir = Direction (1);
35   SCM reldir = me->get_grob_property ("side-relative-direction");       // should use a lambda.
36   if (isdir_b (reldir))
37     {
38       relative_dir = to_dir (reldir);
39     }
40   
41   SCM other_elt = me->get_grob_property ("direction-source");
42   Grob * e = unsmob_grob (other_elt);
43   if (e)
44     {
45       return (Direction) (relative_dir * Directional_element_interface::get (e));
46     }
47   
48   return CENTER;
49 }
50   
51
52 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_extents, 2);
53 SCM
54 Side_position_interface::aligned_on_support_extents (SCM element_smob, SCM axis)
55 {
56   Grob *me = unsmob_grob (element_smob);
57   Axis a = (Axis) gh_scm2int (axis);
58
59   return general_side_position (me, a, true);
60 }
61
62
63 /*
64  Puts the element next to the support, optionally taking in
65  account the extent of the support.
66 */
67 SCM
68 Side_position_interface::general_side_position (Grob * me, Axis a, bool use_extents)
69 {
70   Grob *common = me->parent_l (a);
71
72   /*
73     As this is only used as a callback, this is called only once. We
74     could wipe SIDE-SUPPORT-ELEMENTS after we retrieve it to conserve
75     memory; however -- we should look more into benefits of such actions?
76
77     The benefit is small, it seems: total GC times taken don't
78     differ. Would this also hamper Generational GC ?
79     
80   */
81   SCM support = me->get_grob_property ("side-support-elements");
82     // me->remove_grob_property ("side-support-elements");
83   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
84     {
85       Grob * e  = unsmob_grob (gh_car (s));
86       if (e)
87         common = common->common_refpoint (e, a);
88     }
89   
90   Interval dim;
91   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
92     {
93       Grob * e  = unsmob_grob (gh_car (s));
94       if (e)
95         if (use_extents)
96           dim.unite (e->extent (common, a));
97         else
98           {
99             Real x = e->relative_coordinate (common, a);
100             dim.unite (Interval (x,x));
101           }
102     }
103
104   if (dim.empty_b ())
105     {
106       dim = Interval (0,0);
107     }
108
109   Direction dir = Side_position_interface::get_direction (me);
110     
111   Real off =  me->parent_l (a)->relative_coordinate (common, a);
112   SCM minimum = me->remove_grob_property ("minimum-space");
113
114   Real total_off = dim.linear_combination (dir) + off;
115   SCM padding = me->remove_grob_property ("padding");
116   if (gh_number_p (padding))
117     {
118       total_off += gh_scm2double (padding) * dir;
119     }
120
121   if (gh_number_p (minimum) 
122       && dir
123       && total_off * dir < gh_scm2double (minimum))
124     {
125       total_off = gh_scm2double (minimum) * dir;
126     }
127
128   if (fabs (total_off) > 100 CM)
129     programming_error ("Huh ? Improbable staff side dim.");
130
131   return gh_double2scm (total_off);
132 }
133
134 /*
135   Cut & paste (ugh.)
136  */
137 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_refpoints,2);
138 SCM
139 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
140 {
141   Grob *me = unsmob_grob (smob);
142   Axis a = (Axis) gh_scm2int (axis);
143
144   return  general_side_position (me, a, false); 
145 }
146
147
148 /**
149   callback that centers the element on itself
150  */
151 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_self,2);
152 SCM
153 Side_position_interface::aligned_on_self (SCM element_smob, SCM axis)
154 {
155   Grob *me = unsmob_grob (element_smob);
156   Axis a = (Axis) gh_scm2int (axis);
157   String s ("self-alignment-");
158
159   s += (a == X_AXIS) ? "X" : "Y";
160
161   SCM align (me->get_grob_property (s.ch_C ()));
162   if (gh_number_p (align))
163     {
164       Interval ext (me->extent (me,a));
165
166       if (ext.empty_b ())
167         {
168           programming_error ("I'm empty. Can't align on self");
169           return gh_double2scm (0.0);
170         }
171       else
172         {
173           return gh_double2scm (- ext.linear_combination (gh_scm2double (align)));
174         }
175     }
176   else if (unsmob_grob (align))
177     {
178       return gh_double2scm (- unsmob_grob (align)->relative_coordinate (me,  a));
179     }
180     return gh_double2scm (0.0);
181 }
182
183
184
185 Real
186 directed_round (Real f, Direction d)
187 {
188   if (d < 0)
189     return floor (f);
190   else
191     return ceil (f);
192 }
193
194 /*
195   Callback that quantises in staff-spaces, rounding in the direction
196   of the elements "direction" elt property.
197
198   Only rounds when we're inside the staff, as determined by
199   Staff_symbol_referencer::staff_radius () */
200 MAKE_SCHEME_CALLBACK (Side_position_interface,quantised_position,2);
201 SCM
202 Side_position_interface::quantised_position (SCM element_smob, SCM)
203 {
204   Grob *me = unsmob_grob (element_smob);
205   
206   
207   Direction d = Side_position_interface::get_direction (me);
208
209   if (Staff_symbol_referencer::has_interface (me))
210     {
211       Real p = Staff_symbol_referencer::position_f (me);
212       Real rp = directed_round (p, d);
213       Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
214       int ip = int (rp);
215
216       if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
217         {
218           ip += d;
219           rp += d;
220         }
221
222       return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
223     }
224   return gh_double2scm (0.0);
225 }
226
227 /*
228   Position next to support, taking into account my own dimensions and padding.
229  */
230 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_side,2);
231 SCM
232 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
233 {
234   Grob *me = unsmob_grob (element_smob);
235   Axis a = (Axis) gh_scm2int (axis);
236   
237   Direction d = Side_position_interface::get_direction (me);
238   
239   Real o = gh_scm2double (aligned_on_support_extents (element_smob,axis));
240
241   Interval iv =  me->extent (me, a);
242
243   if (!iv.empty_b ())
244     {
245       if (!d)
246         {
247           programming_error ("Direction unknown, but aligned-side wanted.");
248           d = DOWN;
249         }
250       o += - iv[-d];
251
252       SCM pad = me->get_grob_property ("padding");
253       if (gh_number_p (pad))
254         o += d *gh_scm2double (pad) ; 
255     }
256   return gh_double2scm (o);
257 }
258
259 /*
260   Position centered on parent.
261  */
262 MAKE_SCHEME_CALLBACK (Side_position_interface,centered_on_parent,2);
263 SCM
264 Side_position_interface::centered_on_parent (SCM element_smob, SCM axis)
265 {
266   Grob *me = unsmob_grob (element_smob);
267   Axis a = (Axis) gh_scm2int (axis);
268   Grob *him = me->parent_l (a);
269
270   return gh_double2scm (him->extent (him,a).center ());  
271 }
272
273
274 void
275 Side_position_interface::add_staff_support (Grob*me)
276 {
277   Grob* st = Staff_symbol_referencer::staff_symbol_l (me);
278   if (st)
279     {
280       add_support (me,st);
281     }
282 }
283
284 void
285 Side_position_interface::set_axis (Grob*me, Axis a)
286 {
287   me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
288 }
289
290
291
292 // ugh. doesn't cactch all variants. 
293 Axis
294 Side_position_interface::get_axis (Grob*me)
295 {
296   if (me->has_offset_callback_b (Side_position_interface::aligned_side_proc, X_AXIS)
297       || me->has_offset_callback_b (Side_position_interface::aligned_side_proc , X_AXIS))
298     return X_AXIS;
299
300   
301   return Y_AXIS;
302 }
303
304 void
305 Side_position_interface::set_direction (Grob*me, Direction d)
306 {
307   me->set_grob_property ("direction", gh_int2scm (d));
308 }
309
310 void
311 Side_position_interface::set_minimum_space (Grob*me, Real m)
312 {
313   me->set_grob_property ("minimum-space", gh_double2scm (m));
314 }
315
316 void
317 Side_position_interface::set_padding (Grob*me, Real p)
318 {
319   me->set_grob_property ("padding", gh_double2scm (p));
320 }
321
322 bool
323 Side_position_interface::has_interface (Grob*me) 
324 {
325   return me->has_interface (ly_symbol2scm ("side-position-interface"));
326 }
327
328 bool
329 Side_position_interface::supported_b (Grob*me) 
330 {
331   SCM s = me->get_grob_property ("side-support-elements"); 
332   return gh_pair_p (s);
333 }
334
335