]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
release: 1.3.96
[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::add_element (me, "side-support-elements",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           dim.unite (e->extent (common, a));
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 (a)->relative_coordinate (common, a);
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 gh_double2scm (total_off);
105 }
106
107 /**
108   callback that centers the element on itself
109  */
110 MAKE_SCHEME_CALLBACK(Side_position,aligned_on_self,2);
111 SCM
112 Side_position::aligned_on_self (SCM element_smob, SCM axis)
113 {
114   Score_element *me = unsmob_element (element_smob);
115   Axis a = (Axis) gh_scm2int (axis);
116   String s ("self-alignment-");
117
118   s +=  (a == X_AXIS) ? "X" : "Y";
119
120   SCM align (me->get_elt_property (s.ch_C()));
121   if (gh_number_p (align))
122     {
123       Interval ext(me->extent (me,a));
124
125       if (ext.empty_b ())
126         {
127           programming_error ("I'm empty. Can't align on self");
128           return gh_double2scm (0.0);
129         }
130       else
131         {
132           Real lambda = (0.5 - gh_scm2double (align) / 2.0);
133           return gh_double2scm (- (lambda * ext[LEFT] + (1 - lambda) * ext[RIGHT]));
134         }
135     }
136   else if (unsmob_element (align))
137     {
138       return gh_double2scm (- unsmob_element (align)->relative_coordinate (me,  a));
139     }
140     return gh_double2scm (0.0);
141 }
142
143
144
145 Real
146 directed_round (Real f, Direction d)
147 {
148   if (d < 0)
149     return floor (f);
150   else
151     return ceil (f);
152 }
153
154 /*
155   Callback that quantises in staff-spaces, rounding in the direction
156   of the elements "direction" elt property.
157
158   Only rounds when we're inside the staff, as determined by
159   Staff_symbol_referencer::staff_radius() */
160 MAKE_SCHEME_CALLBACK(Side_position,quantised_position,2);
161 SCM
162 Side_position::quantised_position (SCM element_smob, SCM )
163 {
164   Score_element *me = unsmob_element (element_smob);
165   
166   
167   Direction d = Side_position::get_direction (me);
168
169   if (Staff_symbol_referencer::has_interface (me))
170     {
171       Real p = Staff_symbol_referencer::position_f (me);
172       Real rp = directed_round (p, d);
173       Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
174       int ip = int  (rp);
175
176       if (abs (ip) < rad && Staff_symbol_referencer::on_staffline (me,ip))
177         {
178           ip += d;
179           rp += d;
180         }
181
182       return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
183     }
184   return gh_double2scm (0.0);
185 }
186
187 /*
188   Position next to support, taking into account my own dimensions and padding.
189  */
190 MAKE_SCHEME_CALLBACK(Side_position,aligned_side,2);
191 SCM
192 Side_position::aligned_side (SCM element_smob, SCM axis)
193 {
194   Score_element *me = unsmob_element (element_smob);
195   Axis a = (Axis) gh_scm2int (axis);
196   
197   Direction d = Side_position::get_direction (me);
198   Real o = gh_scm2double (side_position (element_smob,axis));
199
200   Interval iv =  me->extent (me, a);
201
202   if (!iv.empty_b ())
203     {
204       o += - iv[-d];
205
206       SCM pad = me->get_elt_property ("padding");
207       if (gh_number_p (pad))
208         o += d *gh_scm2double (pad) ; 
209     }
210   return gh_double2scm (o);
211 }
212
213 /*
214   Position centered on parent.
215  */
216 MAKE_SCHEME_CALLBACK(Side_position,centered_on_parent,2);
217 SCM
218 Side_position::centered_on_parent (SCM element_smob, SCM axis)
219 {
220   Score_element *me = unsmob_element (element_smob);
221   Axis a = (Axis) gh_scm2int (axis);
222   Score_element *him = me->parent_l (a);
223
224   return gh_double2scm (him->extent (him,a).center ());  
225 }
226
227
228 void
229 Side_position::add_staff_support (Score_element*me)
230 {
231   Score_element* st = Staff_symbol_referencer::staff_symbol_l (me);
232   if (st)
233     {
234       add_support (me,st);
235     }
236 }
237
238 void
239 Side_position::set_axis (Score_element*me, Axis a)
240 {
241   me->add_offset_callback (Side_position::aligned_side_proc, a);
242 }
243
244
245
246 // ugh. doesn't cactch all variants. 
247 Axis
248 Side_position::get_axis (Score_element*me)
249 {
250   if (me->has_offset_callback_b (Side_position::aligned_side_proc, X_AXIS)
251       || me->has_offset_callback_b (Side_position::aligned_side_proc , X_AXIS))
252     return X_AXIS;
253
254   
255   return Y_AXIS;
256 }
257
258 void
259 Side_position::set_direction (Score_element*me, Direction d)
260 {
261   me->set_elt_property ("direction", gh_int2scm (d));
262 }
263
264 void
265 Side_position::set_minimum_space (Score_element*me, Real m)
266 {
267   me->set_elt_property ("minimum-space", gh_double2scm (m));
268 }
269
270 void
271 Side_position::set_padding (Score_element*me, Real p)
272 {
273   me->set_elt_property ("padding", gh_double2scm (p));
274 }
275
276 bool
277 Side_position::has_interface (Score_element*me) 
278 {
279   return me->has_interface (ly_symbol2scm ("side-position-interface"));
280 }
281
282 bool
283 Side_position::supported_b (Score_element*me) 
284 {
285   SCM s = me->get_elt_property  ("side-support-elements"); 
286   return gh_pair_p(s);
287 }
288
289