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