]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
new file.
[lilypond.git] / lily / side-position-interface.cc
1 /*   
2   side-position-interface.cc -- implement Side_position_interface
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "side-position-interface.hh"
11
12 #include <math.h>               // ceil.
13
14 #include "note-head.hh"
15 #include "warn.hh"
16 #include "warn.hh"
17 #include "dimensions.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "group-interface.hh"
20 #include "directional-element-interface.hh"
21 #include "staff-symbol-referencer.hh"
22 #include "string-convert.hh"
23
24 void
25 Side_position_interface::add_support (Grob*me, Grob*e)
26 {
27   Pointer_group_interface::add_grob (me, ly_symbol2scm ("side-support-elements"), e);
28 }
29
30 Direction
31 Side_position_interface::get_direction (Grob*me)
32 {
33   SCM d = me->get_property ("direction");
34   if (is_direction (d) && to_dir (d))
35     return to_dir (d);
36
37   Direction relative_dir = Direction (1);
38   SCM reldir = me->get_property ("side-relative-direction");    // should use a lambda.
39   if (is_direction (reldir))
40     {
41       relative_dir = to_dir (reldir);
42     }
43   
44   SCM other_elt = me->get_property ("direction-source");
45   Grob * e = unsmob_grob (other_elt);
46   if (e)
47     {
48       return (Direction) (relative_dir * get_grob_direction (e));
49     }
50   
51   return CENTER;
52 }
53   
54
55 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_on_support_extents, 2);
56 SCM
57 Side_position_interface::aligned_on_support_extents (SCM element_smob, SCM axis)
58 {
59   Grob *me = unsmob_grob (element_smob);
60   Axis a = (Axis) scm_to_int (axis);
61
62   return general_side_position (me, a, true);
63 }
64
65
66 /* Put the element next to the support, optionally taking in
67    account the extent of the support.  */
68 SCM
69 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents)
70 {
71   Real ss = Staff_symbol_referencer::staff_space (me);
72   SCM support = me->get_property ("side-support-elements");
73   Grob *common = common_refpoint_of_list (support, me->get_parent (a), a);
74   Grob * st = Staff_symbol_referencer::get_staff_symbol (me);
75   bool include_staff = (st
76                         && a == Y_AXIS
77                         && scm_is_number (me->get_property ("staff-padding")));
78
79   Interval dim;
80   if (include_staff)
81     {
82       common = st->common_refpoint (common, Y_AXIS);
83       dim = st->extent (common, Y_AXIS);
84     }
85     
86   for (SCM s = support; s != SCM_EOL; s = scm_cdr (s))
87     {
88       Grob *e = unsmob_grob (scm_car (s));
89       if (e)
90         if (use_extents)
91           dim.unite (e->extent (common, a));
92         else
93           {
94             Real x = e->relative_coordinate (common, a);
95             dim.unite (Interval (x, x));
96           }
97     }
98
99   if (dim.is_empty ())
100     dim = Interval (0, 0);
101
102   Direction dir = Side_position_interface::get_direction (me);
103     
104   Real off = me->get_parent (a)->relative_coordinate (common, a);
105   Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"),  -1);
106
107   Real total_off = dim.linear_combination (dir) - off;
108   total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
109
110   if (minimum_space >= 0
111       && dir
112       && total_off * dir < minimum_space)
113     total_off = minimum_space * dir;
114
115   /* FIXME: 100CM should relate to paper size.  */
116   if (fabs (total_off) > 100 CM)
117     {
118       String msg
119         =  String_convert::form_string ("Improbable offset for grob %s: %f%s",
120                                         me->name ().to_str0 (), total_off,
121                                         INTERNAL_UNIT);
122       
123       programming_error (msg);
124     }
125   return scm_make_real (total_off);
126 }
127
128 /*
129   Cut & paste (ugh.)
130  */
131 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_on_support_refpoints, 2);
132 SCM
133 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
134 {
135   Grob *me = unsmob_grob (smob);
136   Axis a = (Axis) scm_to_int (axis);
137
138   return general_side_position (me, a, false); 
139 }
140
141
142
143
144 Real
145 directed_round (Real f, Direction d)
146 {
147   if (d < 0)
148     return floor (f);
149   else
150     return ceil (f);
151 }
152
153 /*
154   Callback that quantises in staff-spaces, rounding in the direction
155   of the elements "direction" elt property.
156
157   Only rounds when we're inside the staff, as determined by
158   Staff_symbol_referencer::staff_radius () */
159 MAKE_SCHEME_CALLBACK (Side_position_interface, quantised_position, 2);
160 SCM
161 Side_position_interface::quantised_position (SCM element_smob, SCM)
162 {
163   Grob *me = unsmob_grob (element_smob);
164   
165   Direction d = Side_position_interface::get_direction (me);
166
167   Grob * stsym = Staff_symbol_referencer::get_staff_symbol (me);
168   if (stsym)
169     {
170       Real p = Staff_symbol_referencer::get_position (me);
171       Real rp = directed_round (p, d);
172       Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
173       int ip = int (rp);
174
175       Grob *head = me->get_parent (X_AXIS);
176         
177       if (Staff_symbol_referencer::on_staffline (me, ip)
178           && ((abs (ip) <= rad)
179               || (Note_head::has_interface (head)
180                   && sign (Staff_symbol_referencer::get_position (head))
181                   == -d)
182               ))
183         {
184           ip += d;
185           rp += d;
186         }
187       
188       return scm_make_real ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
189     }
190   return scm_make_real (0.0);
191 }
192
193 /*
194   Position next to support, taking into account my own dimensions and padding.
195  */
196 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_side, 2);
197 SCM
198 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
199 {
200   Grob *me = unsmob_grob (element_smob);
201   Axis a = (Axis) scm_to_int (axis);
202   
203   Direction d = Side_position_interface::get_direction (me);
204   
205   Real o = scm_to_double (aligned_on_support_extents (element_smob, axis));
206
207   Interval iv =  me->extent (me, a);
208
209   if (!iv.is_empty ())
210     {
211       if (!d)
212         {
213           programming_error ("Direction unknown, but aligned-side wanted.");
214           d = DOWN;
215         }
216       o += - iv[-d];
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   Grob * st = Staff_symbol_referencer::get_staff_symbol (me);
225   if (st && a == Y_AXIS
226       && scm_is_number (me->get_property ("staff-padding")))
227     {
228       Real padding =
229       Staff_symbol_referencer::staff_space (me)
230       * scm_to_double (me->get_property ("staff-padding"));
231   
232       Grob *common = me->common_refpoint (st, Y_AXIS);
233       
234       Interval staff_size = st->extent (common, Y_AXIS);
235       Real diff =  d*staff_size[d] + padding - d*(o + iv[-d]);
236       o += (d*  (diff >? 0));
237     }
238       
239   return scm_make_real (o);
240 }
241
242
243 void
244 Side_position_interface::set_axis (Grob*me, Axis a)
245 {
246   me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
247 }
248
249 // ugh. doesn't catch all variants. 
250 Axis
251 Side_position_interface::get_axis (Grob*me)
252 {
253   if (me->has_offset_callback (Side_position_interface::aligned_side_proc, X_AXIS)
254       || me->has_offset_callback (Side_position_interface::aligned_side_proc , X_AXIS))
255     return X_AXIS;
256   
257   return Y_AXIS;
258 }
259
260
261 ADD_INTERFACE (Side_position_interface, "side-position-interface",
262                "Position a victim object (this one) next to other objects (the "
263                "support).   The property @code{direction} signifies where to put the  "
264                "victim object relative to the support (left or right, up or down?)\n\n "
265                "The routine also takes the size the staff into account if "
266                "@code{staff-padding} is set. If undefined, the staff symbol is ignored." 
267                ,
268                "staff-padding side-support-elements direction-source "
269                "direction side-relative-direction minimum-space padding");