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