]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
Fixes issue 2005 - scripts move unnecessarily away from accidentals.
[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   for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
178     {
179       if (ax == a)
180         off[ax] = me->get_parent (ax)->maybe_pure_coordinate (common[ax], ax, pure, start, end)
181                   + me->maybe_pure_extent (me, ax, pure, start, end);
182       else
183         off[ax] = me->maybe_pure_extent (common[ax], ax, pure, start, end);
184     }
185
186   if (off[X_AXIS].is_empty () || off[Y_AXIS].is_empty ())
187     return scm_from_double (0.0);
188
189   Real skyline_padding = 0.1;
190
191   Skyline my_dim (off, skyline_padding, other_axis (a), -dir);
192
193   bool include_staff
194     = staff_symbol
195       && a == Y_AXIS
196       && scm_is_number (me->get_property ("staff-padding"))
197       && !to_boolean (me->get_property ("quantize-position"));
198
199   vector<Box> boxes;
200   Real min_h = dir == LEFT ? infinity_f : -infinity_f;
201   for (vsize i = 0; i < support.size (); i++)
202     {
203       Grob *e = support[i];
204
205       // In the case of a stem, we will find a note head as well
206       // ignoring the stem solves cyclic dependencies if the stem is
207       // attached to a cross-staff beam.
208       if (a == Y_AXIS
209           && Stem::has_interface (e)
210           && dir == - get_grob_direction (e))
211         continue;
212
213       if (e)
214         {
215           if (Accidental_interface::has_interface (e))
216             {
217               vector<Box> bs = Accidental_interface::accurate_boxes (e, common);
218               boxes.insert (boxes.end (), bs.begin (), bs.end ());
219             }
220           else
221             {
222               Box b;
223               for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
224                 b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
225
226               if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
227                 continue;
228
229               boxes.push_back (b);
230               min_h = minmax (dir, b[a][-dir], min_h);
231             }
232         }
233     }
234
235   Skyline dim (boxes, skyline_padding, other_axis (a), dir);
236   if (!boxes.size ())
237     dim.set_minimum_height (0.0);
238   else
239     dim.set_minimum_height (min_h);
240
241   if (include_staff)
242     {
243       Interval staff_extents;
244       common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
245       staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
246       dim.set_minimum_height (minmax (dir, min_h, staff_extents[dir]));
247     }
248
249   Real total_off = dir * dim.distance (my_dim);
250   return finish_offset (me, dir, total_off, current_offset);
251 }
252
253 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
254 SCM
255 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
256 {
257   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
258 }
259
260 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
261 SCM
262 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
263 {
264   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
265                                 true, scm_to_int (start), scm_to_int (end), 0);
266 }
267
268 /*
269   Position next to support, taking into account my own dimensions and padding.
270 */
271 SCM
272 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
273 {
274   Real r;
275   Real *current_off_ptr = 0;
276   if (scm_is_number (current_off_scm))
277     {
278       r = scm_to_double (current_off_scm);
279       current_off_ptr = &r;
280     }
281
282   return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
283 }
284
285 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
286 SCM
287 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
288 {
289   return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
290 }
291
292 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
293 SCM
294 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
295 {
296   return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
297 }
298
299 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
300 SCM
301 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
302 {
303   return axis_aligned_side_helper (smob, Y_AXIS, true,
304                                    scm_to_int (start),
305                                    scm_to_int (end),
306                                    cur_off);
307 }
308
309 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
310 SCM
311 Side_position_interface::calc_cross_staff (SCM smob)
312 {
313   Grob *me = unsmob_grob (smob);
314   extract_grob_set (me, "side-support-elements", elts);
315
316   for (vsize i = 0; i < elts.size (); i++)
317     if (to_boolean (elts[i]->get_property ("cross-staff")))
318       return SCM_BOOL_T;
319
320   Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
321   return scm_from_bool (common != me->get_parent (Y_AXIS));
322 }
323
324 SCM
325 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
326                                        Real *current_off)
327 {
328   Direction dir = get_grob_direction (me);
329   bool skyline = to_boolean (me->get_property ("use-skylines"));
330
331   Real o = scm_to_double (skyline
332                           ? skyline_side_position (me, a, pure, start, end, current_off)
333                           : general_side_position (me, a, true, true, pure, start, end, current_off));
334
335   /*
336     Maintain a minimum distance to the staff. This is similar to side
337     position with padding, but it will put adjoining objects on a row if
338     stuff sticks out of the staff a little.
339   */
340   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
341   if (staff && a == Y_AXIS)
342     {
343       if (to_boolean (me->get_property ("quantize-position")))
344         {
345           Grob *common = me->common_refpoint (staff, Y_AXIS);
346           Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
347           Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
348           Real ss = Staff_symbol::staff_space (staff);
349           Real position = 2 * (my_off + o - staff_off) / ss;
350           Real rounded = directed_round (position, dir);
351           Grob *head = me->get_parent (X_AXIS);
352
353           if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1
354               /* In case of a ledger lines, quantize even if we're outside the staff. */
355               || (Note_head::has_interface (head)
356
357                   && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
358             {
359               o += (rounded - position) * 0.5 * ss;
360               if (Staff_symbol_referencer::on_line (me, int (rounded)))
361                 o += dir * 0.5 * ss;
362             }
363         }
364       else if (scm_is_number (me->get_property ("staff-padding")) && dir)
365         {
366           Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
367
368           Real staff_padding
369             = Staff_symbol_referencer::staff_space (me)
370               * scm_to_double (me->get_property ("staff-padding"));
371
372           Grob *parent = me->get_parent (Y_AXIS);
373           Grob *common = me->common_refpoint (staff, Y_AXIS);
374           Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
375           Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
376           Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
377           Real diff = (dir * staff_extent[dir] + staff_padding
378                        - dir * (o + iv[-dir])
379                        + dir * (staff_position - parent_position));
380           o += dir * max (diff, 0.0);
381         }
382     }
383   return scm_from_double (o);
384 }
385
386 void
387 Side_position_interface::set_axis (Grob *me, Axis a)
388 {
389   if (!scm_is_number (me->get_property ("side-axis")))
390     {
391       me->set_property ("side-axis", scm_from_int (a));
392       chain_offset_callback (me,
393                              (a == X_AXIS)
394                              ? x_aligned_side_proc
395                              : y_aligned_side_proc,
396                              a);
397     }
398 }
399
400 Axis
401 Side_position_interface::get_axis (Grob *me)
402 {
403   if (scm_is_number (me->get_property ("side-axis")))
404     return Axis (scm_to_int (me->get_property ("side-axis")));
405
406   string msg = String_convert::form_string ("side-axis not set for grob %s.",
407                                             me->name ().c_str ());
408   me->programming_error (msg);
409   return NO_AXES;
410 }
411
412 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
413 SCM
414 Side_position_interface::move_to_extremal_staff (SCM smob)
415 {
416   Grob *me = unsmob_grob (smob);
417   System *sys = dynamic_cast<System *> (me->get_system ());
418   Direction dir = get_grob_direction (me);
419   if (dir != DOWN)
420     dir = UP;
421
422   Interval iv = me->extent (sys, X_AXIS);
423   iv.widen (1.0);
424   Grob *top_staff = sys->get_extremal_staff (dir, iv);
425
426   if (!top_staff)
427     return SCM_BOOL_F;
428
429   // Only move this grob if it is a direct child of the system.  We
430   // are not interested in moving marks from other staves to the top
431   // staff; we only want to move marks from the system to the top
432   // staff.
433   if (sys != me->get_parent (Y_AXIS))
434     return SCM_BOOL_F;
435
436   me->set_parent (top_staff, Y_AXIS);
437   me->flush_extent_cache (Y_AXIS);
438   Axis_group_interface::add_element (top_staff, me);
439
440   // Remove any cross-staff side-support dependencies
441   Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
442   if (ga)
443     {
444       vector<Grob *> const &elts = ga->array ();
445       vector<Grob *> new_elts;
446       for (vsize i = 0; i < elts.size (); ++i)
447         {
448           if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
449             new_elts.push_back (elts[i]);
450         }
451       ga->set_array (new_elts);
452     }
453   return SCM_BOOL_T;
454 }
455
456 ADD_INTERFACE (Side_position_interface,
457                "Position a victim object (this one) next to other objects"
458                " (the support).  The property @code{direction} signifies where"
459                " to put the victim object relative to the support (left or"
460                " right, up or down?)\n"
461                "\n"
462                "The routine also takes the size of the staff into account if"
463                " @code{staff-padding} is set.  If undefined, the staff symbol"
464                " is ignored.",
465
466                /* properties */
467                "direction "
468                "minimum-space "
469                "padding "
470                "quantize-position "
471                "side-axis "
472                "side-support-elements "
473                "slur-padding "
474                "staff-padding "
475                "use-skylines "
476               );