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