]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
release: 1.3.39
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include <math.h>               // ceil.
10
11 #include "side-position-interface.hh"
12 #include "staff-symbol.hh"
13 #include "debug.hh"
14 #include "warn.hh"
15 #include "dimensions.hh"
16 #include "dimension-cache.hh"
17 #include "staff-symbol-referencer.hh"
18
19 Side_position_interface::Side_position_interface (Score_element const *e)
20 {
21   elt_l_ = (Score_element*)e;
22 }
23
24
25 void
26 Side_position_interface::add_support (Score_element*e)
27 {
28   SCM sup = elt_l_->get_elt_property ("side-support");
29   elt_l_->set_elt_property ("side-support",
30                             gh_cons (e->self_scm_,sup));
31 }
32
33
34
35 Direction
36 Side_position_interface::get_direction () const
37 {
38   SCM d = elt_l_->get_elt_property ("direction");
39   if (isdir_b (d))
40     return to_dir (d) ? to_dir (d) : DOWN;
41
42   Direction relative_dir = UP;
43   SCM reldir = elt_l_->get_elt_property ("side-relative-direction");    // should use a lambda.
44   if (isdir_b (reldir))
45     {
46       relative_dir = to_dir (reldir);
47     }
48   
49   SCM other_elt = elt_l_->get_elt_property ("direction-source");
50   Score_element * e = unsmob_element(other_elt);
51   if (e)
52     {
53       return (Direction)(relative_dir * Side_position_interface (e).get_direction ());
54     }
55   
56   return DOWN;
57 }
58   
59 /*
60    Callback that does the aligning. Puts the element next to the support
61  */
62
63 Real
64 Side_position_interface::side_position (Dimension_cache const * c)
65 {
66   Score_element * me =  (c->element_l ());
67
68   Interval dim;
69   Axis  axis = c->axis ();
70   Score_element *common = me->parent_l (axis);
71   SCM support = me->get_elt_property ("side-support");
72   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
73     {
74       Score_element * e  = unsmob_element ( gh_car (s));
75       if (e)
76         common = common->common_refpoint (e, axis);
77     }
78   
79   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
80     {
81
82       Score_element * e  = unsmob_element ( gh_car (s));
83       if (e)
84         {
85           Real coord = e->relative_coordinate (common, axis);
86
87           dim.unite (coord + e->extent (axis));
88         }
89     }
90
91   if (dim.empty_b ())
92     {
93       dim = Interval(0,0);
94     }
95
96   Direction dir = Side_position_interface (me).get_direction ();
97     
98   Real off =  me->parent_l (axis)->relative_coordinate (common, axis);
99   SCM minimum = me->remove_elt_property ("minimum-space");
100
101   Real total_off = dim[dir] + off;
102   SCM padding = me->remove_elt_property ("padding");
103   if (gh_number_p (padding))
104     {
105       total_off += gh_scm2double (padding) * dir;
106     }
107   if (gh_number_p (minimum) && total_off * dir < gh_scm2double (minimum))
108     {
109       total_off = gh_scm2double (minimum) * dir;
110     }
111   if (fabs (total_off) > 100 CM)
112     programming_error ("Huh ? Improbable staff side dim.");
113
114   return total_off;
115 }
116
117 /**
118   callback that centers the element on itself
119  */
120 Real
121 Side_position_interface::aligned_on_self (Dimension_cache const *c)
122 {
123   String s ("self-alignment-");
124   Axis ax = c->axis ();
125   s +=  (ax == X_AXIS) ? "X" : "Y";
126   Score_element *elm = c->element_l ();
127   SCM align (elm->get_elt_property (s));
128   if (isdir_b (align))
129     {
130       Direction d = to_dir (align);
131       Interval ext(elm->extent (ax));
132
133       if (ext.empty_b ())
134         {
135           programming_error ("I'm empty. Can't align on self");
136           return 0.0;
137         }
138       else if (d)
139         {
140           return - ext[d];
141         }
142       return - ext.center ();
143     }
144   else
145     return 0.0;
146 }
147
148 Real
149 directed_round (Real f, Direction d)
150 {
151   if (d < 0)
152     return floor (f);
153   else
154     return ceil (f);
155 }
156
157 /*
158   Callback that quantises in staff-spaces, rounding in the direction
159   of the elements "direction" elt property. */
160 Real
161 Side_position_interface::quantised_position (Dimension_cache const *c)
162 {
163   Score_element * me =  (c->element_l ());
164   Side_position_interface s(me);
165   Direction d = s.get_direction ();
166   Staff_symbol_referencer_interface si (me);
167
168   if (si.has_interface_b ())
169     {
170       Real p = si.position_f ();
171       Real rp = directed_round (p, d);
172
173       int ip = int  (rp);
174       if ((ip % 2) == 0)
175         {
176           ip += d;
177           rp += d;
178         }
179
180       return (rp - p) * si.staff_space () / 2.0;
181     }
182   return 0.0;
183 }
184
185 /*
186   Position next to support, taking into account my own dimensions and padding.
187  */
188 Real
189 Side_position_interface::aligned_side (Dimension_cache const *c)
190 {
191   Score_element * me =  (c->element_l ());
192   Side_position_interface s(me);
193   Direction d = s.get_direction ();
194   Axis ax = c->axis ();
195   Real o = side_position (c);
196
197   Interval iv =  me->extent (ax);
198
199   if (!iv.empty_b ())
200     {
201       o += - iv[-d];
202
203       SCM pad = me->get_elt_property ("padding");
204       if (gh_number_p (pad))
205         o += d *gh_scm2double (pad) ; 
206     }
207   return o;
208 }
209
210 /*
211   Position centered on parent.
212  */
213 Real
214 Side_position_interface::centered_on_parent (Dimension_cache const *c)
215 {
216
217   Score_element *me = c->element_l ();
218   Axis a = c->axis ();
219   Score_element *him = me->parent_l (a);
220
221   return him->extent (a).center ();  
222 }
223
224
225 void
226 Side_position_interface::add_staff_support ()
227 {
228   Staff_symbol_referencer_interface si (elt_l_);
229   if (si.staff_symbol_l ())
230     {
231       add_support (si.staff_symbol_l ());
232     }
233 }
234
235 void
236 Side_position_interface::set_axis (Axis a)
237 {
238   // prop transparent ? 
239   if (elt_l_->get_elt_property ("side-support") == SCM_UNDEFINED)
240     elt_l_->set_elt_property ("side-support" ,SCM_EOL);
241
242   if (!elt_l_->has_offset_callback_b (aligned_side, a))
243     elt_l_->add_offset_callback (aligned_side, a);
244 }
245
246
247 void
248 Side_position_interface::set_quantised (Axis a)
249 {
250   elt_l_->add_offset_callback (quantised_position, a);
251 }
252
253 Axis
254 Side_position_interface::get_axis () const
255 {
256   if (elt_l_->has_offset_callback_b (&side_position, X_AXIS)
257       || elt_l_->has_offset_callback_b (&aligned_side , X_AXIS))
258     return X_AXIS;
259
260   
261   return Y_AXIS;
262 }
263
264 void
265 Side_position_interface::set_direction (Direction d) 
266 {
267   elt_l_->set_elt_property ("direction", gh_int2scm (d));
268 }
269
270 void
271 Side_position_interface::set_minimum_space (Real m)
272 {
273   elt_l_->set_elt_property ("minimum-space", gh_double2scm (m));
274 }
275
276 void
277 Side_position_interface::set_padding (Real p)
278 {
279   elt_l_->set_elt_property ("padding", gh_double2scm (p));
280 }
281
282 bool
283 Side_position_interface::has_interface_b () const
284 {
285   return elt_l_->get_elt_property ("side-support") != SCM_UNDEFINED;
286 }
287
288 bool
289 Side_position_interface::supported_b () const
290 {
291   SCM s =elt_l_->get_elt_property  ("side-support"); 
292   return s != SCM_UNDEFINED && s != SCM_EOL;
293 }
294
295
296 Side_position_interface
297 side_position (Score_element* e)
298 {
299   Side_position_interface si (e);
300   return si;
301 }