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