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