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