]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
Fixes issue 1951 (script accidental collision).
[lilypond.git] / lily / side-position-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "side-position-interface.hh"
21
22 #include <cmath>                // ceil.
23 #include <algorithm>
24
25 using namespace std;
26
27 #include "accidental-interface.hh"
28 #include "axis-group-interface.hh"
29 #include "directional-element-interface.hh"
30 #include "grob.hh"
31 #include "grob-array.hh"
32 #include "main.hh"
33 #include "misc.hh"
34 #include "note-head.hh"
35 #include "pointer-group-interface.hh"
36 #include "staff-symbol-referencer.hh"
37 #include "staff-symbol.hh"
38 #include "stem.hh"
39 #include "string-convert.hh"
40 #include "system.hh"
41 #include "warn.hh"
42
43 void
44 Side_position_interface::add_support (Grob *me, Grob *e)
45 {
46   Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
47 }
48
49 SCM
50 finish_offset (Grob *me, Direction dir, Real total_off, Real *current_offset)
51 {
52   Real ss = Staff_symbol_referencer::staff_space (me);
53   Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
54   total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
55
56   if (minimum_space >= 0
57       && dir
58       && total_off * dir < minimum_space)
59     total_off = minimum_space * dir;
60
61
62   if (current_offset)
63     total_off = dir * max (dir * total_off,
64                            dir * (*current_offset));
65
66   /* FIXME: 1000 should relate to paper size.  */
67   if (fabs (total_off) > 1000)
68     {
69       string msg
70         = String_convert::form_string ("Improbable offset for grob %s: %f",
71                                        me->name ().c_str (), total_off);
72
73       programming_error (msg);
74       if (strict_infinity_checking)
75         scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
76     }
77
78   return scm_from_double (total_off);
79 }
80
81 /* Put the element next to the support, optionally taking in
82    account the extent of the support.
83
84    Does not take into account the extent of ME.
85 */
86 SCM
87 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
88                                                 bool include_my_extent,
89                                                 bool pure, int start, int end,
90                                                 Real *current_offset)
91 {
92   extract_grob_set (me, "side-support-elements", support);
93
94   Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
95   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
96   bool include_staff
97     = staff_symbol
98       && a == Y_AXIS
99       && scm_is_number (me->get_property ("staff-padding"))
100       && !to_boolean (me->get_property ("quantize-position"));
101
102   Interval dim;
103   Interval staff_extents;
104   if (include_staff)
105     {
106       common = staff_symbol->common_refpoint (common, Y_AXIS);
107       staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
108
109       if (include_staff)
110         dim.unite (staff_extents);
111     }
112
113   Direction dir = get_grob_direction (me);
114
115   for (vsize i = 0; i < support.size (); i++)
116     {
117       Grob *e = support[i];
118
119       // In the case of a stem, we will find a note head as well
120       // ignoring the stem solves cyclic dependencies if the stem is
121       // attached to a cross-staff beam.
122       if (a == Y_AXIS
123           && Stem::has_interface (e)
124           && dir == - get_grob_direction (e))
125         continue;
126
127       if (e)
128         {
129           if (use_extents)
130             dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
131           else
132             {
133               Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
134               dim.unite (Interval (x, x));
135             }
136         }
137     }
138
139   if (dim.is_empty ())
140     dim = Interval (0, 0);
141
142   Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
143
144   Real total_off = dim.linear_combination (dir) - off;
145   if (include_my_extent)
146     {
147       Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
148       if (!iv.is_empty ())
149         {
150           if (!dir)
151             {
152               programming_error ("direction unknown, but aligned-side wanted");
153               dir = DOWN;
154             }
155           total_off += -iv[-dir];
156         }
157     }
158
159   return finish_offset (me, dir, total_off, current_offset);
160 }
161
162 SCM
163 Side_position_interface::skyline_side_position (Grob *me, Axis a,
164                                                 bool pure, int start, int end,
165                                                 Real *current_offset)
166 {
167   extract_grob_set (me, "side-support-elements", support);
168
169   Grob *common[2];
170   for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
171     common[ax] = common_refpoint_of_array (support, ax == a ? me->get_parent (ax) : me, ax);
172
173   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
174   Direction dir = get_grob_direction (me);
175
176   Box off;
177   Real my_min_h = dir == LEFT ? -infinity_f : infinity_f;
178   for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
179     {
180       if (ax == a)
181         off[ax] = me->get_parent (ax)->maybe_pure_coordinate (common[ax], ax, pure, start, end)
182                   + me->maybe_pure_extent (me, ax, pure, start, end);
183       else
184         off[ax] = me->maybe_pure_extent (common[ax], ax, pure, start, end);
185     }
186
187   if (off[X_AXIS].is_empty () || off[Y_AXIS].is_empty ())
188     return scm_from_double (0.0);
189
190   my_min_h = off[a][dir];
191
192   Real skyline_padding = 0.1;
193
194   Skyline my_dim (off, skyline_padding, other_axis (a), -dir);
195   my_dim.set_minimum_height (my_min_h);
196
197   bool include_staff
198     = staff_symbol
199       && a == Y_AXIS
200       && scm_is_number (me->get_property ("staff-padding"))
201       && !to_boolean (me->get_property ("quantize-position"));
202
203   vector<Box> boxes;
204   Real min_h = dir == LEFT ? infinity_f : -infinity_f;
205   for (vsize i = 0; i < support.size (); i++)
206     {
207       Grob *e = support[i];
208
209       // In the case of a stem, we will find a note head as well
210       // ignoring the stem solves cyclic dependencies if the stem is
211       // attached to a cross-staff beam.
212       if (a == Y_AXIS
213           && Stem::has_interface (e)
214           && dir == - get_grob_direction (e))
215         continue;
216
217       if (e)
218         {
219           if (Accidental_interface::has_interface (e))
220             {
221               vector<Box> bs = Accidental_interface::accurate_boxes (e, common);
222               boxes.insert (boxes.end (), bs.begin (), bs.end ());
223             }
224           else
225             {
226               Box b;
227               for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
228                 b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
229
230               if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
231                 continue;
232
233               boxes.push_back (b);
234               min_h = minmax (dir, b[a][-dir], min_h);
235             }
236         }
237     }
238
239   Skyline dim (boxes, skyline_padding, other_axis (a), dir);
240   if (!boxes.size ())
241     dim.set_minimum_height (0.0);
242   else
243     dim.set_minimum_height (min_h);
244
245   if (include_staff)
246     {
247       Interval staff_extents;
248       common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
249       staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
250       dim.set_minimum_height (minmax (dir, min_h, staff_extents[dir]));
251     }
252
253   Real total_off = dir * dim.distance (my_dim);
254   return finish_offset (me, dir, total_off, current_offset);
255 }
256
257 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
258 SCM
259 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
260 {
261   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
262 }
263
264 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
265 SCM
266 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
267 {
268   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
269                                 true, scm_to_int (start), scm_to_int (end), 0);
270 }
271
272 /*
273   Position next to support, taking into account my own dimensions and padding.
274 */
275 SCM
276 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
277 {
278   Real r;
279   Real *current_off_ptr = 0;
280   if (scm_is_number (current_off_scm))
281     {
282       r = scm_to_double (current_off_scm);
283       current_off_ptr = &r;
284     }
285
286   return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
287 }
288
289 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
290 SCM
291 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
292 {
293   return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
294 }
295
296 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
297 SCM
298 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
299 {
300   return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
301 }
302
303 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
304 SCM
305 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
306 {
307   return axis_aligned_side_helper (smob, Y_AXIS, true,
308                                    scm_to_int (start),
309                                    scm_to_int (end),
310                                    cur_off);
311 }
312
313 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
314 SCM
315 Side_position_interface::calc_cross_staff (SCM smob)
316 {
317   Grob *me = unsmob_grob (smob);
318   extract_grob_set (me, "side-support-elements", elts);
319
320   for (vsize i = 0; i < elts.size (); i++)
321     if (to_boolean (elts[i]->get_property ("cross-staff")))
322       return SCM_BOOL_T;
323
324   Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
325   return scm_from_bool (common != me->get_parent (Y_AXIS));
326 }
327
328 SCM
329 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
330                                        Real *current_off)
331 {
332   Direction dir = get_grob_direction (me);
333   bool skyline = to_boolean (me->get_property ("use-skylines"));
334
335   Real o = scm_to_double (skyline
336                           ? skyline_side_position (me, a, pure, start, end, current_off)
337                           : general_side_position (me, a, true, true, pure, start, end, current_off));
338
339   /*
340     Maintain a minimum distance to the staff. This is similar to side
341     position with padding, but it will put adjoining objects on a row if
342     stuff sticks out of the staff a little.
343   */
344   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
345   if (staff && a == Y_AXIS)
346     {
347       if (to_boolean (me->get_property ("quantize-position")))
348         {
349           Grob *common = me->common_refpoint (staff, Y_AXIS);
350           Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
351           Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
352           Real ss = Staff_symbol::staff_space (staff);
353           Real position = 2 * (my_off + o - staff_off) / ss;
354           Real rounded = directed_round (position, dir);
355           Grob *head = me->get_parent (X_AXIS);
356
357           if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1
358               /* In case of a ledger lines, quantize even if we're outside the staff. */
359               || (Note_head::has_interface (head)
360
361                   && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
362             {
363               o += (rounded - position) * 0.5 * ss;
364               if (Staff_symbol_referencer::on_line (me, int (rounded)))
365                 o += dir * 0.5 * ss;
366             }
367         }
368       else if (scm_is_number (me->get_property ("staff-padding")) && dir)
369         {
370           Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
371
372           Real staff_padding
373             = Staff_symbol_referencer::staff_space (me)
374               * scm_to_double (me->get_property ("staff-padding"));
375
376           Grob *parent = me->get_parent (Y_AXIS);
377           Grob *common = me->common_refpoint (staff, Y_AXIS);
378           Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
379           Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
380           Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
381           Real diff = (dir * staff_extent[dir] + staff_padding
382                        - dir * (o + iv[-dir])
383                        + dir * (staff_position - parent_position));
384           o += dir * max (diff, 0.0);
385         }
386     }
387   return scm_from_double (o);
388 }
389
390 void
391 Side_position_interface::set_axis (Grob *me, Axis a)
392 {
393   if (!scm_is_number (me->get_property ("side-axis")))
394     {
395       me->set_property ("side-axis", scm_from_int (a));
396       chain_offset_callback (me,
397                              (a == X_AXIS)
398                              ? x_aligned_side_proc
399                              : y_aligned_side_proc,
400                              a);
401     }
402 }
403
404 Axis
405 Side_position_interface::get_axis (Grob *me)
406 {
407   if (scm_is_number (me->get_property ("side-axis")))
408     return Axis (scm_to_int (me->get_property ("side-axis")));
409
410   string msg = String_convert::form_string ("side-axis not set for grob %s.",
411                                             me->name ().c_str ());
412   me->programming_error (msg);
413   return NO_AXES;
414 }
415
416 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
417 SCM
418 Side_position_interface::move_to_extremal_staff (SCM smob)
419 {
420   Grob *me = unsmob_grob (smob);
421   System *sys = dynamic_cast<System *> (me->get_system ());
422   Direction dir = get_grob_direction (me);
423   if (dir != DOWN)
424     dir = UP;
425
426   Interval iv = me->extent (sys, X_AXIS);
427   iv.widen (1.0);
428   Grob *top_staff = sys->get_extremal_staff (dir, iv);
429
430   if (!top_staff)
431     return SCM_BOOL_F;
432
433   // Only move this grob if it is a direct child of the system.  We
434   // are not interested in moving marks from other staves to the top
435   // staff; we only want to move marks from the system to the top
436   // staff.
437   if (sys != me->get_parent (Y_AXIS))
438     return SCM_BOOL_F;
439
440   me->set_parent (top_staff, Y_AXIS);
441   me->flush_extent_cache (Y_AXIS);
442   Axis_group_interface::add_element (top_staff, me);
443
444   // Remove any cross-staff side-support dependencies
445   Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
446   if (ga)
447     {
448       vector<Grob *> const &elts = ga->array ();
449       vector<Grob *> new_elts;
450       for (vsize i = 0; i < elts.size (); ++i)
451         {
452           if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
453             new_elts.push_back (elts[i]);
454         }
455       ga->set_array (new_elts);
456     }
457   return SCM_BOOL_T;
458 }
459
460 ADD_INTERFACE (Side_position_interface,
461                "Position a victim object (this one) next to other objects"
462                " (the support).  The property @code{direction} signifies where"
463                " to put the victim object relative to the support (left or"
464                " right, up or down?)\n"
465                "\n"
466                "The routine also takes the size of the staff into account if"
467                " @code{staff-padding} is set.  If undefined, the staff symbol"
468                " is ignored.",
469
470                /* properties */
471                "direction "
472                "minimum-space "
473                "padding "
474                "quantize-position "
475                "side-axis "
476                "side-support-elements "
477                "slur-padding "
478                "staff-padding "
479                "use-skylines "
480               );