]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
Merge branch 'translation' into staging
[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 #include <set>
25 #include <map>
26
27 using namespace std;
28
29 #include "accidental-interface.hh"
30 #include "accidental-placement.hh"
31 #include "axis-group-interface.hh"
32 #include "directional-element-interface.hh"
33 #include "grob.hh"
34 #include "grob-array.hh"
35 #include "main.hh"
36 #include "misc.hh"
37 #include "note-head.hh"
38 #include "note-column.hh"
39 #include "pointer-group-interface.hh"
40 #include "skyline-pair.hh"
41 #include "staff-symbol-referencer.hh"
42 #include "staff-symbol.hh"
43 #include "stem.hh"
44 #include "string-convert.hh"
45 #include "system.hh"
46 #include "warn.hh"
47
48 void
49 Side_position_interface::add_support (Grob *me, Grob *e)
50 {
51   Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
52 }
53
54 SCM
55 finish_offset (Grob *me, Direction dir, Real total_off, Real *current_offset)
56 {
57   Real ss = Staff_symbol_referencer::staff_space (me);
58   Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
59   total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
60
61   if (minimum_space >= 0
62       && dir
63       && total_off * dir < minimum_space)
64     total_off = minimum_space * dir;
65
66   if (current_offset)
67     total_off = dir * max (dir * total_off,
68                            dir * (*current_offset));
69
70   /* FIXME: 1000 should relate to paper size.  */
71   if (fabs (total_off) > 1000)
72     {
73       string msg
74         = String_convert::form_string ("Improbable offset for grob %s: %f",
75                                        me->name ().c_str (), total_off);
76
77       programming_error (msg);
78       if (strict_infinity_checking)
79         scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
80     }
81
82   return scm_from_double (total_off);
83 }
84
85 set<Grob *>
86 get_support_set (Grob *me)
87 {
88   // Only slightly kludgy heuristic...
89   // We want to make sure that all AccidentalPlacements'
90   // accidentals make it into the side support
91   extract_grob_set (me, "side-support-elements", proto_support);
92   set<Grob *> support;
93
94   for (vsize i = 0; i < proto_support.size (); i++)
95     {
96       if (Accidental_placement::has_interface (proto_support[i]))
97         {
98           Grob *accs = proto_support[i];
99           for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
100                acs = scm_cdr (acs))
101             for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
102               {
103                 Grob *a = unsmob_grob (scm_car (s));
104                 support.insert (a);
105               }
106         }
107       else
108         support.insert (proto_support[i]);
109     }
110   return support;
111 }
112
113 /* Put the element next to the support, optionally taking in
114    account the extent of the support.
115
116    Does not take into account the extent of ME.
117 */
118 SCM
119 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
120                                                 bool include_my_extent,
121                                                 bool pure, int start, int end,
122                                                 Real *current_offset)
123 {
124   set<Grob *> support = get_support_set (me);
125
126   Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
127   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
128   bool include_staff
129     = staff_symbol
130       && a == Y_AXIS
131       && scm_is_number (me->get_property ("staff-padding"))
132       && !to_boolean (me->get_property ("quantize-position"));
133
134   Interval dim;
135   Interval staff_extents;
136   if (include_staff)
137     {
138       common = staff_symbol->common_refpoint (common, Y_AXIS);
139       staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
140
141       if (include_staff)
142         dim.unite (staff_extents);
143     }
144
145   Direction dir = get_grob_direction (me);
146
147   set<Grob *>::iterator it;
148
149   for (it = support.begin (); it != support.end (); it++)
150     {
151       Grob *e = *it;
152
153       // In the case of a stem, we will find a note head as well
154       // ignoring the stem solves cyclic dependencies if the stem is
155       // attached to a cross-staff beam.
156       if (a == Y_AXIS
157           && Stem::has_interface (e)
158           && dir == - get_grob_direction (e))
159         continue;
160
161       if (e)
162         {
163           if (use_extents)
164             dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
165           else
166             {
167               Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
168               dim.unite (Interval (x, x));
169             }
170         }
171     }
172
173   if (dim.is_empty ())
174     dim = Interval (0, 0);
175
176   Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
177
178   Real total_off = dim.linear_combination (dir) - off;
179   if (include_my_extent)
180     {
181       Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
182       if (!iv.is_empty ())
183         {
184           if (!dir)
185             {
186               programming_error ("direction unknown, but aligned-side wanted");
187               dir = DOWN;
188             }
189           total_off += -iv[-dir];
190         }
191     }
192
193   return finish_offset (me, dir, total_off, current_offset);
194 }
195
196 SCM
197 Side_position_interface::skyline_side_position (Grob *me, Axis a,
198                                                 bool pure, int start, int end,
199                                                 Real *current_offset)
200 {
201   set<Grob *> support = get_support_set (me);
202
203   Grob *common[2];
204   for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
205     common[ax] = common_refpoint_of_array (support, ax == a ? me->get_parent (ax) : me, ax);
206
207   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
208   Direction dir = get_grob_direction (me);
209
210   Skyline my_dim;
211   Skyline_pair *sp = Skyline_pair::unsmob (me->get_property ("vertical-skylines"));
212   if (sp && a == Y_AXIS && !pure)
213     {
214       Skyline_pair copy = Skyline_pair (*sp);
215       copy.shift (me->relative_coordinate (common[X_AXIS], X_AXIS));
216       copy.raise (me->get_parent (Y_AXIS)->relative_coordinate (common[Y_AXIS], Y_AXIS));
217       my_dim = copy[-dir];
218     }
219   else
220     {
221       Box off;
222       for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
223         {
224           if (ax == a)
225             off[ax] = me->get_parent (ax)->maybe_pure_coordinate (common[ax], ax, pure, start, end)
226                       + me->maybe_pure_extent (me, ax, pure, start, end);
227           else
228             off[ax] = me->maybe_pure_extent (common[ax], ax, pure, start, end);
229         }
230
231       if (off[X_AXIS].is_empty () || off[Y_AXIS].is_empty ())
232         return scm_from_double (0.0);
233
234       my_dim = Skyline (off, other_axis (a), -dir);
235     }
236   bool include_staff
237     = staff_symbol
238       && a == Y_AXIS
239       && scm_is_number (me->get_property ("staff-padding"))
240       && !to_boolean (me->get_property ("quantize-position"));
241
242   vector<Box> boxes;
243   vector<Skyline_pair> skyps;
244   Real min_h = dir == LEFT ? infinity_f : -infinity_f;
245   set<Grob *>::iterator it;
246
247   map<Grob *, vector<Grob *> > note_column_map; // for parts of a note column
248   for (it = support.begin (); it != support.end (); it++)
249     {
250       Grob *e = *it;
251
252       // In the case of a stem, we will find a note head as well
253       // ignoring the stem solves cyclic dependencies if the stem is
254       // attached to a cross-staff beam.
255       if (a == Y_AXIS
256           && Stem::has_interface (e)
257           && dir == - get_grob_direction (e))
258         continue;
259
260       if (e)
261         {
262            if (Note_column::has_interface (e->get_parent (X_AXIS))
263                && to_boolean (me->get_property ("add-stem-support")))
264              {
265                note_column_map[e->get_parent (X_AXIS)].push_back (e);
266                continue;
267              }
268
269            Skyline_pair *sp = Skyline_pair::unsmob (e->get_property ("vertical-skylines"));
270            if (sp && a == Y_AXIS && !pure)
271              {
272                Skyline_pair copy = Skyline_pair (*sp);
273                copy.shift (e->relative_coordinate (common[X_AXIS], X_AXIS));
274                copy.raise (e->relative_coordinate (common[Y_AXIS], Y_AXIS));
275                skyps.push_back (copy);
276                continue;
277              }
278            Box b;
279            for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
280              b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
281
282            if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
283              continue;
284
285            boxes.push_back (b);
286            min_h = minmax (dir, b[a][-dir], min_h);
287         }
288     }
289
290   // this loop ensures that parts of a note column will be in the same box
291   // pushes scripts and such over stems instead of just over heads
292   for (map<Grob *, vector<Grob *> >::iterator i = note_column_map.begin (); i != note_column_map.end (); i++)
293     {
294       Box big;
295       for (vsize j = 0; j < (*i).second.size (); j++)
296         {
297           Grob *e = (*i).second[j];
298           Box b;
299           for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
300             b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
301
302           if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
303             continue;
304
305           big.unite (b);
306         }
307       if (!big[X_AXIS].is_empty () && !big[Y_AXIS].is_empty ())
308         boxes.push_back (big);
309     }
310
311   Skyline dim (boxes, other_axis (a), dir);
312   if (skyps.size ())
313     {
314       Skyline_pair merged (skyps);
315       dim.merge (merged[dir]);
316     }
317   if (!boxes.size ())
318     dim.set_minimum_height (0.0);
319   else
320     dim.set_minimum_height (min_h);
321
322   if (include_staff)
323     {
324       Interval staff_extents;
325       common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
326       staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
327       dim.set_minimum_height (minmax (dir, min_h, staff_extents[dir]));
328     }
329
330   Real dist = dim.distance (my_dim, 0.1); // 0.1 m4g1c value...fix...
331   Real total_off = !isinf (dist) ? dir * dist : 0.0;
332
333   return finish_offset (me, dir, total_off, current_offset);
334 }
335
336 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
337 SCM
338 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
339 {
340   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
341 }
342
343 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
344 SCM
345 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
346 {
347   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
348                                 true, scm_to_int (start), scm_to_int (end), 0);
349 }
350
351 /*
352   Position next to support, taking into account my own dimensions and padding.
353 */
354 SCM
355 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
356 {
357   Real r;
358   Real *current_off_ptr = 0;
359   if (scm_is_number (current_off_scm))
360     {
361       r = scm_to_double (current_off_scm);
362       current_off_ptr = &r;
363     }
364
365   return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
366 }
367
368 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
369 SCM
370 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
371 {
372   return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
373 }
374
375 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
376 SCM
377 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
378 {
379   return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
380 }
381
382 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
383 SCM
384 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
385 {
386   return axis_aligned_side_helper (smob, Y_AXIS, true,
387                                    scm_to_int (start),
388                                    scm_to_int (end),
389                                    cur_off);
390 }
391
392 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
393 SCM
394 Side_position_interface::calc_cross_staff (SCM smob)
395 {
396   Grob *me = unsmob_grob (smob);
397   extract_grob_set (me, "side-support-elements", elts);
398
399   for (vsize i = 0; i < elts.size (); i++)
400     if (to_boolean (elts[i]->get_property ("cross-staff")))
401       return SCM_BOOL_T;
402
403   Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
404   return scm_from_bool (common != me->get_parent (Y_AXIS));
405 }
406
407 SCM
408 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
409                                        Real *current_off)
410 {
411   Direction dir = get_grob_direction (me);
412   bool skyline = to_boolean (me->get_property ("use-skylines"));
413
414   Real o = scm_to_double (skyline && !pure
415                           ? skyline_side_position (me, a, pure, start, end, current_off)
416                           : general_side_position (me, a, true, true, pure, start, end, current_off));
417
418   /*
419     Maintain a minimum distance to the staff. This is similar to side
420     position with padding, but it will put adjoining objects on a row if
421     stuff sticks out of the staff a little.
422   */
423   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
424   if (staff && a == Y_AXIS)
425     {
426       if (to_boolean (me->get_property ("quantize-position")))
427         {
428           Grob *common = me->common_refpoint (staff, Y_AXIS);
429           Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
430           Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
431           Real ss = Staff_symbol::staff_space (staff);
432           Real position = 2 * (my_off + o - staff_off) / ss;
433           Real rounded = directed_round (position, dir);
434           Grob *head = me->get_parent (X_AXIS);
435
436           Interval staff_span = Staff_symbol::line_span (staff);
437           staff_span.widen (1);
438           if (staff_span.contains (position)
439               /* In case of a ledger lines, quantize even if we're outside the staff. */
440               || (Note_head::has_interface (head)
441
442                   && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
443             {
444               o += (rounded - position) * 0.5 * ss;
445               if (Staff_symbol_referencer::on_line (me, int (rounded)))
446                 o += dir * 0.5 * ss;
447             }
448         }
449       else if (scm_is_number (me->get_property ("staff-padding")) && dir)
450         {
451           Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
452
453           Real staff_padding
454             = Staff_symbol_referencer::staff_space (me)
455               * scm_to_double (me->get_property ("staff-padding"));
456
457           Grob *parent = me->get_parent (Y_AXIS);
458           Grob *common = me->common_refpoint (staff, Y_AXIS);
459           Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
460           Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
461           Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
462           Real diff = (dir * staff_extent[dir] + staff_padding
463                        - dir * (o + iv[-dir])
464                        + dir * (staff_position - parent_position));
465           o += dir * max (diff, 0.0);
466         }
467     }
468   return scm_from_double (o);
469 }
470
471 void
472 Side_position_interface::set_axis (Grob *me, Axis a)
473 {
474   if (!scm_is_number (me->get_property ("side-axis")))
475     {
476       me->set_property ("side-axis", scm_from_int (a));
477       chain_offset_callback (me,
478                              (a == X_AXIS)
479                              ? x_aligned_side_proc
480                              : y_aligned_side_proc,
481                              a);
482     }
483 }
484
485 Axis
486 Side_position_interface::get_axis (Grob *me)
487 {
488   if (scm_is_number (me->get_property ("side-axis")))
489     return Axis (scm_to_int (me->get_property ("side-axis")));
490
491   string msg = String_convert::form_string ("side-axis not set for grob %s.",
492                                             me->name ().c_str ());
493   me->programming_error (msg);
494   return NO_AXES;
495 }
496
497 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
498 SCM
499 Side_position_interface::move_to_extremal_staff (SCM smob)
500 {
501   Grob *me = unsmob_grob (smob);
502   System *sys = dynamic_cast<System *> (me->get_system ());
503   Direction dir = get_grob_direction (me);
504   if (dir != DOWN)
505     dir = UP;
506
507   Interval iv = me->extent (sys, X_AXIS);
508   iv.widen (1.0);
509   Grob *top_staff = sys->get_extremal_staff (dir, iv);
510
511   if (!top_staff)
512     return SCM_BOOL_F;
513
514   // Only move this grob if it is a direct child of the system.  We
515   // are not interested in moving marks from other staves to the top
516   // staff; we only want to move marks from the system to the top
517   // staff.
518   if (sys != me->get_parent (Y_AXIS))
519     return SCM_BOOL_F;
520
521   me->set_parent (top_staff, Y_AXIS);
522   me->flush_extent_cache (Y_AXIS);
523   Axis_group_interface::add_element (top_staff, me);
524
525   // Remove any cross-staff side-support dependencies
526   Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
527   if (ga)
528     {
529       vector<Grob *> const &elts = ga->array ();
530       vector<Grob *> new_elts;
531       for (vsize i = 0; i < elts.size (); ++i)
532         {
533           if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
534             new_elts.push_back (elts[i]);
535         }
536       ga->set_array (new_elts);
537     }
538   return SCM_BOOL_T;
539 }
540
541 ADD_INTERFACE (Side_position_interface,
542                "Position a victim object (this one) next to other objects"
543                " (the support).  The property @code{direction} signifies where"
544                " to put the victim object relative to the support (left or"
545                " right, up or down?)\n"
546                "\n"
547                "The routine also takes the size of the staff into account if"
548                " @code{staff-padding} is set.  If undefined, the staff symbol"
549                " is ignored.",
550
551                /* properties */
552                "add-stem-support "
553                "direction "
554                "minimum-space "
555                "padding "
556                "quantize-position "
557                "side-axis "
558                "side-support-elements "
559                "slur-padding "
560                "staff-padding "
561                "use-skylines "
562               );