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