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