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