]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
* scm/define-grobs.scm (all-grob-descriptions): use staff-padding
[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   return gh_double2scm (total_off);
111 }
112
113 /*
114   Cut & paste (ugh.)
115  */
116 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_refpoints,2);
117 SCM
118 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
119 {
120   Grob *me = unsmob_grob (smob);
121   Axis a = (Axis) gh_scm2int (axis);
122
123   return general_side_position (me, a, false); 
124 }
125
126
127
128
129 Real
130 directed_round (Real f, Direction d)
131 {
132   if (d < 0)
133     return floor (f);
134   else
135     return ceil (f);
136 }
137
138 /*
139   Callback that quantises in staff-spaces, rounding in the direction
140   of the elements "direction" elt property.
141
142   Only rounds when we're inside the staff, as determined by
143   Staff_symbol_referencer::staff_radius () */
144 MAKE_SCHEME_CALLBACK (Side_position_interface,quantised_position,2);
145 SCM
146 Side_position_interface::quantised_position (SCM element_smob, SCM)
147 {
148   Grob *me = unsmob_grob (element_smob);
149   
150   Direction d = Side_position_interface::get_direction (me);
151
152   Grob * stsym = Staff_symbol_referencer::get_staff_symbol (me);
153   if (stsym)
154     {
155       Real p = Staff_symbol_referencer::get_position (me);
156       Real rp = directed_round (p, d);
157       Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
158       int ip = int (rp);
159
160       if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
161         {
162           ip += d;
163           rp += d;
164         }
165
166       return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
167     }
168   return gh_double2scm (0.0);
169 }
170
171 /*
172   Position next to support, taking into account my own dimensions and padding.
173  */
174 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_side,2);
175 SCM
176 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
177 {
178   Grob *me = unsmob_grob (element_smob);
179   Axis a = (Axis) gh_scm2int (axis);
180   
181   Direction d = Side_position_interface::get_direction (me);
182   
183   Real o = gh_scm2double (aligned_on_support_extents (element_smob,axis));
184
185   Interval iv =  me->extent (me, a);
186
187   if (!iv.is_empty ())
188     {
189       if (!d)
190         {
191           programming_error ("Direction unknown, but aligned-side wanted.");
192           d = DOWN;
193         }
194       o += - iv[-d];
195     }
196   return gh_double2scm (o);
197 }
198
199 /*
200   Maintain a minimum distance to the staff. This is similar to side
201   position with padding, but it will put adjoining objects on a row if
202   stuff sticks out of the staff a little.
203  */
204 MAKE_SCHEME_CALLBACK (Side_position_interface,out_of_staff,2);
205 SCM
206 Side_position_interface::out_of_staff (SCM element_smob, SCM axis)
207 {
208   Grob *me = unsmob_grob (element_smob);
209   Axis a = (Axis) gh_scm2int (axis);
210
211   Grob * st = Staff_symbol_referencer::get_staff_symbol (me);
212
213   if (!st)
214     return gh_int2scm (0);
215
216   SCM scm_padding = me->get_grob_property ("staff-padding");
217   if (!gh_number_p (scm_padding))
218     return gh_int2scm (0);
219   
220   Real padding=
221     Staff_symbol_referencer::staff_space (me)
222     * gh_scm2double (scm_padding);
223   
224   Grob *common = me->common_refpoint (st, Y_AXIS);
225   Direction d = Side_position_interface::get_direction (me);
226   Interval staff_size = st->extent (common, Y_AXIS);
227   Interval me_ext = me->extent (common, a);
228   Real diff =  d*staff_size[d] + padding - d*me_ext[-d];
229   return gh_double2scm (d*  (diff >? 0));
230 }
231
232 void
233 Side_position_interface::add_staff_support (Grob*me)
234 {
235   Grob* st = Staff_symbol_referencer::get_staff_symbol (me);
236   if (st && get_axis (me) == Y_AXIS)
237     {
238       add_support (me,st);
239     }
240 }
241
242 void
243 Side_position_interface::set_axis (Grob*me, Axis a)
244 {
245   me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
246 }
247
248
249
250 // ugh. doesn't cactch all variants. 
251 Axis
252 Side_position_interface::get_axis (Grob*me)
253 {
254   if (me->has_offset_callback_b (Side_position_interface::aligned_side_proc, X_AXIS)
255       || me->has_offset_callback_b (Side_position_interface::aligned_side_proc , X_AXIS))
256     return X_AXIS;
257
258   
259   return Y_AXIS;
260 }
261
262 void
263 Side_position_interface::set_direction (Grob*me, Direction d)
264 {
265   me->set_grob_property ("direction", gh_int2scm (d));
266 }
267
268 void
269 Side_position_interface::set_minimum_space (Grob*me, Real m)
270 {
271   me->set_grob_property ("minimum-space", gh_double2scm (m));
272 }
273
274 void
275 Side_position_interface::set_padding (Grob*me, Real p)
276 {
277   me->set_grob_property ("padding", gh_double2scm (p));
278 }
279
280
281 bool
282 Side_position_interface::supported_b (Grob*me) 
283 {
284   SCM s = me->get_grob_property ("side-support-elements"); 
285   return gh_pair_p (s);
286 }
287
288
289
290
291 ADD_INTERFACE (Side_position_interface,"side-position-interface",
292                "Position a victim object (this one) next to other objects (the "
293                "support).  In this case, the property @code{direction} signifies where to put the  "
294                "victim object relative to the support (left or right, up or down?)\n\n "
295                "The @code{out_of_staff} routine puts objects at a distance of the staff. If the property "
296                "@code{staff-padding} is not defined, the routine doesn't do anything." 
297                ,
298                "staff-padding side-support-elements direction-source "
299                "direction side-relative-direction minimum-space padding");