]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
* lily/side-position-interface.cc (out_of_staff): move contents of
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include <math.h>               // ceil.
10
11 #include "side-position-interface.hh"
12 #include "warn.hh"
13 #include "warn.hh"
14 #include "dimensions.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "group-interface.hh"
17 #include "directional-element-interface.hh"
18 #include "staff-symbol-referencer.hh"
19
20 void
21 Side_position_interface::add_support (Grob*me, Grob*e)
22 {
23   Pointer_group_interface::add_grob (me, ly_symbol2scm ("side-support-elements"), e);
24 }
25
26 Direction
27 Side_position_interface::get_direction (Grob*me)
28 {
29   SCM d = me->get_grob_property ("direction");
30   if (is_direction (d) && to_dir (d))
31     return to_dir (d);
32
33   Direction relative_dir = Direction (1);
34   SCM reldir = me->get_grob_property ("side-relative-direction");       // should use a lambda.
35   if (is_direction (reldir))
36     {
37       relative_dir = to_dir (reldir);
38     }
39   
40   SCM other_elt = me->get_grob_property ("direction-source");
41   Grob * e = unsmob_grob (other_elt);
42   if (e)
43     {
44       return (Direction) (relative_dir * get_grob_direction (e));
45     }
46   
47   return CENTER;
48 }
49   
50
51 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_extents, 2);
52 SCM
53 Side_position_interface::aligned_on_support_extents (SCM element_smob, SCM axis)
54 {
55   Grob *me = unsmob_grob (element_smob);
56   Axis a = (Axis) gh_scm2int (axis);
57
58   return general_side_position (me, a, true);
59 }
60
61
62 /*
63  Puts the element next to the support, optionally taking in
64  account the extent of the support.
65 */
66 SCM
67 Side_position_interface::general_side_position (Grob * me, Axis a, bool use_extents)
68 {
69   Real ss = Staff_symbol_referencer::staff_space (me);
70   SCM support = me->get_grob_property ("side-support-elements");
71   Grob *common = common_refpoint_of_list (support, me->get_parent (a), a);
72   
73   Interval dim;
74   for (SCM s = support; s != SCM_EOL; s = ly_cdr (s))
75     {
76       Grob * e  = unsmob_grob (ly_car (s));
77       if (e)
78         if (use_extents)
79           dim.unite (e->extent (common, a));
80         else
81           {
82             Real x = e->relative_coordinate (common, a);
83             dim.unite (Interval (x,x));
84           }
85     }
86
87   if (dim.is_empty ())
88     {
89       dim = Interval (0,0);
90     }
91
92   Direction dir = Side_position_interface::get_direction (me);
93     
94   Real off =  me->get_parent (a)->relative_coordinate (common, a);
95   Real  minimum_space = ss * robust_scm2double (me->get_grob_property ("minimum-space"),  -1);
96
97   Real total_off = dim.linear_combination (dir) - off;
98   total_off += dir * ss * robust_scm2double (me->get_grob_property ("padding"), 0);
99
100   if (minimum_space >= 0
101       && dir
102       && total_off * dir < minimum_space)
103     {
104       total_off = minimum_space * dir;
105     }
106
107   if (fabs (total_off) > 100 CM)
108     programming_error ("Huh ? Improbable staff side dim.");
109
110
111   
112   
113   return gh_double2scm (total_off);
114 }
115
116 /*
117   Cut & paste (ugh.)
118  */
119 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_refpoints,2);
120 SCM
121 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
122 {
123   Grob *me = unsmob_grob (smob);
124   Axis a = (Axis) gh_scm2int (axis);
125
126   return general_side_position (me, a, false); 
127 }
128
129
130
131
132 Real
133 directed_round (Real f, Direction d)
134 {
135   if (d < 0)
136     return floor (f);
137   else
138     return ceil (f);
139 }
140
141 /*
142   Callback that quantises in staff-spaces, rounding in the direction
143   of the elements "direction" elt property.
144
145   Only rounds when we're inside the staff, as determined by
146   Staff_symbol_referencer::staff_radius () */
147 MAKE_SCHEME_CALLBACK (Side_position_interface,quantised_position,2);
148 SCM
149 Side_position_interface::quantised_position (SCM element_smob, SCM)
150 {
151   Grob *me = unsmob_grob (element_smob);
152   
153   Direction d = Side_position_interface::get_direction (me);
154
155   Grob * stsym = Staff_symbol_referencer::get_staff_symbol (me);
156   if (stsym)
157     {
158       Real p = Staff_symbol_referencer::get_position (me);
159       Real rp = directed_round (p, d);
160       Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
161       int ip = int (rp);
162
163       if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
164         {
165           ip += d;
166           rp += d;
167         }
168
169       return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
170     }
171   return gh_double2scm (0.0);
172 }
173
174 /*
175   Position next to support, taking into account my own dimensions and padding.
176  */
177 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_side,2);
178 SCM
179 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
180 {
181   Grob *me = unsmob_grob (element_smob);
182   Axis a = (Axis) gh_scm2int (axis);
183   
184   Direction d = Side_position_interface::get_direction (me);
185   
186   Real o = gh_scm2double (aligned_on_support_extents (element_smob,axis));
187
188   Interval iv =  me->extent (me, a);
189
190   if (!iv.is_empty ())
191     {
192       if (!d)
193         {
194           programming_error ("Direction unknown, but aligned-side wanted.");
195           d = DOWN;
196         }
197       o += - iv[-d];
198     }
199
200   Grob * st = Staff_symbol_referencer::get_staff_symbol (me);
201   if (st && a == Y_AXIS
202       && gh_number_p (me->get_grob_property ("staff-padding")))
203     {
204       Real padding=
205       Staff_symbol_referencer::staff_space (me)
206       * gh_scm2double (me->get_grob_property ("staff-padding"));
207   
208       Grob *common = me->common_refpoint (st, Y_AXIS);
209       
210       Interval staff_size = st->extent (common, Y_AXIS);
211       Interval me_ext = me->extent (common, a);
212       Real diff =  d*staff_size[d] + padding - d*(o + iv[-d]);
213       o += (d*  (diff >? 0));
214     }
215       
216   return gh_double2scm (o);
217 }
218
219 /*
220   Maintain a minimum distance to the staff. This is similar to side
221   position with padding, but it will put adjoining objects on a row if
222   stuff sticks out of the staff a little.
223  */
224 MAKE_SCHEME_CALLBACK (Side_position_interface,out_of_staff,2);
225 SCM
226 Side_position_interface::out_of_staff (SCM element_smob, SCM axis)
227 {
228   return gh_double2scm (0);
229 }
230
231 void
232 Side_position_interface::add_staff_support (Grob*me)
233 {
234   Grob* st = Staff_symbol_referencer::get_staff_symbol (me);
235   if (st && get_axis (me) == Y_AXIS)
236     {
237       add_support (me,st);
238     }
239 }
240
241 void
242 Side_position_interface::set_axis (Grob*me, Axis a)
243 {
244   me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
245 }
246
247
248
249 // ugh. doesn't cactch all variants. 
250 Axis
251 Side_position_interface::get_axis (Grob*me)
252 {
253   if (me->has_offset_callback_b (Side_position_interface::aligned_side_proc, X_AXIS)
254       || me->has_offset_callback_b (Side_position_interface::aligned_side_proc , X_AXIS))
255     return X_AXIS;
256
257   
258   return Y_AXIS;
259 }
260
261 void
262 Side_position_interface::set_direction (Grob*me, Direction d)
263 {
264   me->set_grob_property ("direction", gh_int2scm (d));
265 }
266
267 void
268 Side_position_interface::set_minimum_space (Grob*me, Real m)
269 {
270   me->set_grob_property ("minimum-space", gh_double2scm (m));
271 }
272
273 void
274 Side_position_interface::set_padding (Grob*me, Real p)
275 {
276   me->set_grob_property ("padding", gh_double2scm (p));
277 }
278
279
280 bool
281 Side_position_interface::supported_b (Grob*me) 
282 {
283   SCM s = me->get_grob_property ("side-support-elements"); 
284   return gh_pair_p (s);
285 }
286
287
288
289
290 ADD_INTERFACE (Side_position_interface,"side-position-interface",
291                "Position a victim object (this one) next to other objects (the "
292                "support).  In this case, the property @code{direction} signifies where to put the  "
293                "victim object relative to the support (left or right, up or down?)\n\n "
294                "The @code{out_of_staff} routine puts objects at a distance of the staff. If the property "
295                "@code{staff-padding} is not defined, the routine doesn't do anything." 
296                ,
297                "staff-padding side-support-elements direction-source "
298                "direction side-relative-direction minimum-space padding");