]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
Merge branch 'master' of git://git.sv.gnu.org/lilypond
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "side-position-interface.hh"
10
11 #include <cmath>                // ceil.
12 #include <algorithm>
13 using namespace std;
14
15 #include "directional-element-interface.hh"
16 #include "grob.hh"
17 #include "main.hh"
18 #include "misc.hh"
19 #include "note-head.hh"
20 #include "pointer-group-interface.hh"
21 #include "staff-symbol-referencer.hh"
22 #include "staff-symbol.hh"
23 #include "stem.hh"
24 #include "string-convert.hh"
25 #include "warn.hh"
26
27 void
28 Side_position_interface::add_support (Grob *me, Grob *e)
29 {
30   Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
31 }
32
33 Direction
34 Side_position_interface::get_direction (Grob *me)
35 {
36   Direction relative_dir = Direction (1);
37   SCM reldir = me->get_property ("side-relative-direction");
38   if (is_direction (reldir))
39     relative_dir = to_dir (reldir);
40
41   SCM other_elt = me->get_object ("direction-source");
42   Grob *e = unsmob_grob (other_elt);
43   if (e)
44     return (Direction) (relative_dir * get_grob_direction (e));
45
46   return CENTER;
47 }
48
49 /* Put the element next to the support, optionally taking in
50    account the extent of the support.
51
52    Does not take into account the extent of ME.
53 */
54 SCM
55 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
56                                                 bool include_my_extent,
57                                                 bool pure, int start, int end,
58                                                 Real *current_offset)
59 {
60   Real ss = Staff_symbol_referencer::staff_space (me);
61
62   extract_grob_set (me, "side-support-elements", support);
63
64   Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
65   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
66   bool include_staff = 
67     staff_symbol
68     && a == Y_AXIS
69     && scm_is_number (me->get_property ("staff-padding"))
70     && !to_boolean (me->get_property ("quantize-position"));
71
72   Interval dim;
73   Interval staff_extents;
74   if (include_staff)
75     {
76       common = staff_symbol->common_refpoint (common, Y_AXIS);
77       staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
78
79       if (include_staff)
80         dim.unite (staff_extents);
81     }
82
83   Direction dir = get_grob_direction (me);
84
85   for (vsize i = 0; i < support.size (); i++)
86     {
87       Grob *e = support[i];
88
89       // In the case of a stem, we will find a note head as well
90       // ignoring the stem solves cyclic dependencies if the stem is
91       // attached to a cross-staff beam.
92       if (a == Y_AXIS
93           && Stem::has_interface (e)
94           && dir == - get_grob_direction (e))
95         continue;
96       
97       if (e)
98         if (use_extents)
99           dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
100         else
101           {
102             Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
103             dim.unite (Interval (x, x));
104           }
105     }
106
107   if (dim.is_empty ())
108     dim = Interval (0, 0);
109
110   Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
111   Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
112
113   Real total_off = dim.linear_combination (dir) - off;
114   total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
115
116   if (minimum_space >= 0
117       && dir
118       && total_off * dir < minimum_space)
119     total_off = minimum_space * dir;
120
121   if (include_my_extent)
122     {
123       Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
124       if (!iv.is_empty ())
125         {
126           if (!dir)
127             {
128               programming_error ("direction unknown, but aligned-side wanted");
129               dir = DOWN;
130             }
131           total_off += -iv[-dir];
132         }
133     }
134
135   if (current_offset)
136     total_off = dir * max (dir * total_off,
137                            dir * (*current_offset));
138   
139   
140   /* FIXME: 1000 should relate to paper size.  */
141   if (fabs (total_off) > 1000)
142     {
143       string msg
144         = String_convert::form_string ("Improbable offset for grob %s: %f",
145                                        me->name ().c_str (), total_off);
146
147       programming_error (msg);
148       if (strict_infinity_checking)
149         scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
150     }
151   return scm_from_double (total_off);
152 }
153
154
155 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
156 SCM
157 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
158 {
159   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0); 
160 }
161
162 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
163 SCM
164 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
165 {
166   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, 
167                                 true, scm_to_int (start), scm_to_int (end), 0); 
168 }
169
170
171 /*
172   Position next to support, taking into account my own dimensions and padding.
173 */
174 SCM
175 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
176 {
177   Real r;
178   Real *current_off_ptr = 0;
179   if (scm_is_number (current_off_scm))
180     {
181       r = scm_to_double (current_off_scm);
182       current_off_ptr = &r; 
183     }
184   
185   return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
186 }
187
188
189 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
190 SCM
191 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
192 {
193   return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
194 }
195
196 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
197 SCM
198 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
199 {
200   return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
201 }
202
203 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
204 SCM
205 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
206 {
207   return axis_aligned_side_helper (smob, Y_AXIS, true,
208                                    scm_to_int (start),
209                                    scm_to_int (end),
210                                    cur_off);
211 }
212
213 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
214 SCM
215 Side_position_interface::calc_cross_staff (SCM smob)
216 {
217   Grob *me = unsmob_grob (smob);
218   extract_grob_set (me, "side-support-elements", elts);
219
220   Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
221   return scm_from_bool (common != me->get_parent (Y_AXIS));
222 }
223
224 SCM
225 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
226                                        Real *current_off)
227 {
228   Direction dir = get_grob_direction (me);
229
230   Real o = scm_to_double (general_side_position (me, a, true, true, pure, start, end, current_off));
231
232   /*
233     Maintain a minimum distance to the staff. This is similar to side
234     position with padding, but it will put adjoining objects on a row if
235     stuff sticks out of the staff a little.
236   */
237   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
238   if (staff && a == Y_AXIS)
239     {
240       if (to_boolean (me->get_property ("quantize-position")))
241         {
242           Grob *common = me->common_refpoint (staff, Y_AXIS);
243           Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
244           Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
245           Real ss = Staff_symbol::staff_space (staff);
246           Real position = 2 * (my_off + o - staff_off) / ss;
247           Real rounded = directed_round (position, dir);
248           Grob *head = me->get_parent (X_AXIS);
249
250           if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1 
251               || (Note_head::has_interface (head)
252                   && sign (Staff_symbol_referencer::get_position (head)) == - dir))
253             {
254               o += (rounded - position) * 0.5 * ss;
255               if (Staff_symbol_referencer::on_line (me, int (rounded)))
256                 o += dir * 0.5 * ss;
257             }
258         }
259       else if (scm_is_number (me->get_property ("staff-padding")) && dir)
260         {
261           Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
262           
263           Real padding
264             = Staff_symbol_referencer::staff_space (me)
265             * scm_to_double (me->get_property ("staff-padding"));
266
267           Grob *common = me->common_refpoint (staff, Y_AXIS);
268
269           Interval staff_size = staff->maybe_pure_extent (common, Y_AXIS, pure, start, end);
270           Real diff = dir*staff_size[dir] + padding - dir * (o + iv[-dir]);
271           o += dir * max (diff, 0.0);
272         }
273     }
274   return scm_from_double (o);
275 }
276
277 void
278 Side_position_interface::set_axis (Grob *me, Axis a)
279 {
280   if (!scm_is_number (me->get_property ("side-axis")))
281     {
282       me->set_property ("side-axis", scm_from_int (a));
283       chain_offset_callback (me,
284                              (a==X_AXIS)
285                              ? x_aligned_side_proc
286                              : y_aligned_side_proc,
287                              a);
288     }
289 }
290 Axis
291 Side_position_interface::get_axis (Grob *me)
292 {
293   if (scm_is_number (me->get_property ("side-axis")))
294     return Axis (scm_to_int (me->get_property ("side-axis")));
295   
296   me->programming_error ("side-axis not set.");
297   return NO_AXES;
298 }
299
300 ADD_INTERFACE (Side_position_interface,
301                "Position a victim object (this one) next to other objects (the "
302                "support).   The property @code{direction} signifies where to put the  "
303                "victim object relative to the support (left or right, up or down?)\n\n "
304                "The routine also takes the size the staff into account if "
305                "@code{staff-padding} is set. If undefined, the staff symbol is ignored.",
306
307                /* properties */
308                "direction "
309                "direction-source "
310                "minimum-space "
311                "padding "
312                "quantize-position "
313                "side-axis "
314                "side-relative-direction "
315                "side-support-elements "
316                "slur-padding "
317                "staff-padding "
318                );