]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
release: 1.3.136
[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   SCM support = me->get_grob_property ("side-support-elements");
72   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
73     {
74       Grob * e  = unsmob_grob (gh_car (s));
75       if (e)
76         common = common->common_refpoint (e, a);
77     }
78   
79   Interval dim;
80   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
81     {
82       Grob * e  = unsmob_grob ( gh_car (s));
83       if (e)
84         if (use_extents)
85           dim.unite (e->extent (common, a));
86         else
87           {
88             Real x = e->relative_coordinate (common, a);
89             dim.unite (Interval (x,x));
90           }
91     }
92
93   if (dim.empty_b ())
94     {
95       dim = Interval(0,0);
96     }
97
98   Direction dir = Side_position_interface::get_direction (me);
99     
100   Real off =  me->parent_l (a)->relative_coordinate (common, a);
101   SCM minimum = me->remove_grob_property ("minimum-space");
102
103   Real total_off = dim.linear_combination (dir) + off;
104   SCM padding = me->remove_grob_property ("padding");
105   if (gh_number_p (padding))
106     {
107       total_off += gh_scm2double (padding) * dir;
108     }
109
110   if (gh_number_p (minimum) 
111       && dir
112       && total_off * dir < gh_scm2double (minimum))
113     {
114       total_off = gh_scm2double (minimum) * dir;
115     }
116
117   if (fabs (total_off) > 100 CM)
118     programming_error ("Huh ? Improbable staff side dim.");
119
120   return gh_double2scm (total_off);
121 }
122
123 /*
124   Cut & paste (ugh.)
125  */
126 MAKE_SCHEME_CALLBACK(Side_position_interface,aligned_on_support_refpoints,2);
127 SCM
128 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
129 {
130   Grob *me = unsmob_grob (smob);
131   Axis a = (Axis) gh_scm2int (axis);
132
133   return  general_side_position (me, a, false); 
134 }
135
136
137 /**
138   callback that centers the element on itself
139  */
140 MAKE_SCHEME_CALLBACK(Side_position_interface,aligned_on_self,2);
141 SCM
142 Side_position_interface::aligned_on_self (SCM element_smob, SCM axis)
143 {
144   Grob *me = unsmob_grob (element_smob);
145   Axis a = (Axis) gh_scm2int (axis);
146   String s ("self-alignment-");
147
148   s +=  (a == X_AXIS) ? "X" : "Y";
149
150   SCM align (me->get_grob_property (s.ch_C()));
151   if (gh_number_p (align))
152     {
153       Interval ext(me->extent (me,a));
154
155       if (ext.empty_b ())
156         {
157           programming_error ("I'm empty. Can't align on self");
158           return gh_double2scm (0.0);
159         }
160       else
161         {
162           return gh_double2scm (- ext.linear_combination (gh_scm2double (align)));
163         }
164     }
165   else if (unsmob_grob (align))
166     {
167       return gh_double2scm (- unsmob_grob (align)->relative_coordinate (me,  a));
168     }
169     return gh_double2scm (0.0);
170 }
171
172
173
174 Real
175 directed_round (Real f, Direction d)
176 {
177   if (d < 0)
178     return floor (f);
179   else
180     return ceil (f);
181 }
182
183 /*
184   Callback that quantises in staff-spaces, rounding in the direction
185   of the elements "direction" elt property.
186
187   Only rounds when we're inside the staff, as determined by
188   Staff_symbol_referencer::staff_radius() */
189 MAKE_SCHEME_CALLBACK(Side_position_interface,quantised_position,2);
190 SCM
191 Side_position_interface::quantised_position (SCM element_smob, SCM )
192 {
193   Grob *me = unsmob_grob (element_smob);
194   
195   
196   Direction d = Side_position_interface::get_direction (me);
197
198   if (Staff_symbol_referencer::has_interface (me))
199     {
200       Real p = Staff_symbol_referencer::position_f (me);
201       Real rp = directed_round (p, d);
202       Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
203       int ip = int  (rp);
204
205       if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
206         {
207           ip += d;
208           rp += d;
209         }
210
211       return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
212     }
213   return gh_double2scm (0.0);
214 }
215
216 /*
217   Position next to support, taking into account my own dimensions and padding.
218  */
219 MAKE_SCHEME_CALLBACK(Side_position_interface,aligned_side,2);
220 SCM
221 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
222 {
223   Grob *me = unsmob_grob (element_smob);
224   Axis a = (Axis) gh_scm2int (axis);
225   
226   Direction d = Side_position_interface::get_direction (me);
227   Real o = gh_scm2double (aligned_on_support_extents (element_smob,axis));
228
229   Interval iv =  me->extent (me, a);
230
231   if (!iv.empty_b ())
232     {
233       o += - iv[-d];
234
235       SCM pad = me->get_grob_property ("padding");
236       if (gh_number_p (pad))
237         o += d *gh_scm2double (pad) ; 
238     }
239   return gh_double2scm (o);
240 }
241
242 /*
243   Position centered on parent.
244  */
245 MAKE_SCHEME_CALLBACK(Side_position_interface,centered_on_parent,2);
246 SCM
247 Side_position_interface::centered_on_parent (SCM element_smob, SCM axis)
248 {
249   Grob *me = unsmob_grob (element_smob);
250   Axis a = (Axis) gh_scm2int (axis);
251   Grob *him = me->parent_l (a);
252
253   return gh_double2scm (him->extent (him,a).center ());  
254 }
255
256
257 void
258 Side_position_interface::add_staff_support (Grob*me)
259 {
260   Grob* st = Staff_symbol_referencer::staff_symbol_l (me);
261   if (st)
262     {
263       add_support (me,st);
264     }
265 }
266
267 void
268 Side_position_interface::set_axis (Grob*me, Axis a)
269 {
270   me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
271 }
272
273
274
275 // ugh. doesn't cactch all variants. 
276 Axis
277 Side_position_interface::get_axis (Grob*me)
278 {
279   if (me->has_offset_callback_b (Side_position_interface::aligned_side_proc, X_AXIS)
280       || me->has_offset_callback_b (Side_position_interface::aligned_side_proc , X_AXIS))
281     return X_AXIS;
282
283   
284   return Y_AXIS;
285 }
286
287 void
288 Side_position_interface::set_direction (Grob*me, Direction d)
289 {
290   me->set_grob_property ("direction", gh_int2scm (d));
291 }
292
293 void
294 Side_position_interface::set_minimum_space (Grob*me, Real m)
295 {
296   me->set_grob_property ("minimum-space", gh_double2scm (m));
297 }
298
299 void
300 Side_position_interface::set_padding (Grob*me, Real p)
301 {
302   me->set_grob_property ("padding", gh_double2scm (p));
303 }
304
305 bool
306 Side_position_interface::has_interface (Grob*me) 
307 {
308   return me->has_interface (ly_symbol2scm ("side-position-interface"));
309 }
310
311 bool
312 Side_position_interface::supported_b (Grob*me) 
313 {
314   SCM s = me->get_grob_property  ("side-support-elements"); 
315   return gh_pair_p(s);
316 }
317
318