]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
5a0d16606c9faa6eecbd406b98641e061156f9b7
[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 (Accidental_interface::has_interface (e))
263             {
264               vector<Box> bs = Accidental_interface::accurate_boxes (e, common);
265               boxes.insert (boxes.end (), bs.begin (), bs.end ());
266             }
267           else
268             {
269               if (Note_column::has_interface (e->get_parent (X_AXIS))
270                   && to_boolean (me->get_property ("add-stem-support")))
271                 {
272                   note_column_map[e->get_parent (X_AXIS)].push_back (e);
273                   continue;
274                 }
275
276               Skyline_pair *sp = Skyline_pair::unsmob (e->get_property ("vertical-skylines"));
277               if (sp && a == Y_AXIS && !pure)
278                 {
279                   Skyline_pair copy = Skyline_pair (*sp);
280                   copy.shift (e->relative_coordinate (common[X_AXIS], X_AXIS));
281                   copy.raise (e->relative_coordinate (common[Y_AXIS], Y_AXIS));
282                   skyps.push_back (copy);
283                   continue;
284                 }
285               Box b;
286               for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
287                 b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
288
289               if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
290                 continue;
291
292               boxes.push_back (b);
293               min_h = minmax (dir, b[a][-dir], min_h);
294             }
295         }
296     }
297
298   // this loop ensures that parts of a note column will be in the same box
299   // pushes scripts and such over stems instead of just over heads
300   for (map<Grob *, vector<Grob *> >::iterator i = note_column_map.begin (); i != note_column_map.end (); i++)
301     {
302       Box big;
303       for (vsize j = 0; j < (*i).second.size (); j++)
304         {
305           Grob *e = (*i).second[j];
306           Box b;
307           for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
308             b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
309
310           if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
311             continue;
312
313           big.unite (b);
314         }
315       if (!big[X_AXIS].is_empty () && !big[Y_AXIS].is_empty ())
316         boxes.push_back (big);
317     }
318
319   Skyline dim (boxes, other_axis (a), dir);
320   if (skyps.size ())
321     {
322       Skyline_pair merged (skyps);
323       dim.merge (merged[dir]);
324     }
325   if (!boxes.size ())
326     dim.set_minimum_height (0.0);
327   else
328     dim.set_minimum_height (min_h);
329
330   if (include_staff)
331     {
332       Interval staff_extents;
333       common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
334       staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
335       dim.set_minimum_height (minmax (dir, min_h, staff_extents[dir]));
336     }
337
338   Real dist = dim.distance (my_dim, 0.1); // 0.1 m4g1c value...fix...
339   Real total_off = !isinf (dist) ? dir * dist : 0.0;
340
341   return finish_offset (me, dir, total_off, current_offset);
342 }
343
344 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
345 SCM
346 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
347 {
348   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
349 }
350
351 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
352 SCM
353 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
354 {
355   return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
356                                 true, scm_to_int (start), scm_to_int (end), 0);
357 }
358
359 /*
360   Position next to support, taking into account my own dimensions and padding.
361 */
362 SCM
363 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
364 {
365   Real r;
366   Real *current_off_ptr = 0;
367   if (scm_is_number (current_off_scm))
368     {
369       r = scm_to_double (current_off_scm);
370       current_off_ptr = &r;
371     }
372
373   return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
374 }
375
376 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
377 SCM
378 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
379 {
380   return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
381 }
382
383 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
384 SCM
385 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
386 {
387   return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
388 }
389
390 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
391 SCM
392 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
393 {
394   return axis_aligned_side_helper (smob, Y_AXIS, true,
395                                    scm_to_int (start),
396                                    scm_to_int (end),
397                                    cur_off);
398 }
399
400 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
401 SCM
402 Side_position_interface::calc_cross_staff (SCM smob)
403 {
404   Grob *me = unsmob_grob (smob);
405   extract_grob_set (me, "side-support-elements", elts);
406
407   for (vsize i = 0; i < elts.size (); i++)
408     if (to_boolean (elts[i]->get_property ("cross-staff")))
409       return SCM_BOOL_T;
410
411   Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
412   return scm_from_bool (common != me->get_parent (Y_AXIS));
413 }
414
415 SCM
416 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
417                                        Real *current_off)
418 {
419   Direction dir = get_grob_direction (me);
420   bool skyline = to_boolean (me->get_property ("use-skylines"));
421
422   Real o = scm_to_double (skyline && !pure
423                           ? skyline_side_position (me, a, pure, start, end, current_off)
424                           : general_side_position (me, a, true, true, pure, start, end, current_off));
425
426   /*
427     Maintain a minimum distance to the staff. This is similar to side
428     position with padding, but it will put adjoining objects on a row if
429     stuff sticks out of the staff a little.
430   */
431   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
432   if (staff && a == Y_AXIS)
433     {
434       if (to_boolean (me->get_property ("quantize-position")))
435         {
436           Grob *common = me->common_refpoint (staff, Y_AXIS);
437           Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
438           Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
439           Real ss = Staff_symbol::staff_space (staff);
440           Real position = 2 * (my_off + o - staff_off) / ss;
441           Real rounded = directed_round (position, dir);
442           Grob *head = me->get_parent (X_AXIS);
443
444           Interval staff_span = Staff_symbol::line_span (staff);
445           staff_span.widen (1);
446           if (staff_span.contains (position)
447               /* In case of a ledger lines, quantize even if we're outside the staff. */
448               || (Note_head::has_interface (head)
449
450                   && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
451             {
452               o += (rounded - position) * 0.5 * ss;
453               if (Staff_symbol_referencer::on_line (me, int (rounded)))
454                 o += dir * 0.5 * ss;
455             }
456         }
457       else if (scm_is_number (me->get_property ("staff-padding")) && dir)
458         {
459           Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
460
461           Real staff_padding
462             = Staff_symbol_referencer::staff_space (me)
463               * scm_to_double (me->get_property ("staff-padding"));
464
465           Grob *parent = me->get_parent (Y_AXIS);
466           Grob *common = me->common_refpoint (staff, Y_AXIS);
467           Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
468           Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
469           Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
470           Real diff = (dir * staff_extent[dir] + staff_padding
471                        - dir * (o + iv[-dir])
472                        + dir * (staff_position - parent_position));
473           o += dir * max (diff, 0.0);
474         }
475     }
476   return scm_from_double (o);
477 }
478
479 void
480 Side_position_interface::set_axis (Grob *me, Axis a)
481 {
482   if (!scm_is_number (me->get_property ("side-axis")))
483     {
484       me->set_property ("side-axis", scm_from_int (a));
485       chain_offset_callback (me,
486                              (a == X_AXIS)
487                              ? x_aligned_side_proc
488                              : y_aligned_side_proc,
489                              a);
490     }
491 }
492
493 Axis
494 Side_position_interface::get_axis (Grob *me)
495 {
496   if (scm_is_number (me->get_property ("side-axis")))
497     return Axis (scm_to_int (me->get_property ("side-axis")));
498
499   string msg = String_convert::form_string ("side-axis not set for grob %s.",
500                                             me->name ().c_str ());
501   me->programming_error (msg);
502   return NO_AXES;
503 }
504
505 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
506 SCM
507 Side_position_interface::move_to_extremal_staff (SCM smob)
508 {
509   Grob *me = unsmob_grob (smob);
510   System *sys = dynamic_cast<System *> (me->get_system ());
511   Direction dir = get_grob_direction (me);
512   if (dir != DOWN)
513     dir = UP;
514
515   Interval iv = me->extent (sys, X_AXIS);
516   iv.widen (1.0);
517   Grob *top_staff = sys->get_extremal_staff (dir, iv);
518
519   if (!top_staff)
520     return SCM_BOOL_F;
521
522   // Only move this grob if it is a direct child of the system.  We
523   // are not interested in moving marks from other staves to the top
524   // staff; we only want to move marks from the system to the top
525   // staff.
526   if (sys != me->get_parent (Y_AXIS))
527     return SCM_BOOL_F;
528
529   me->set_parent (top_staff, Y_AXIS);
530   me->flush_extent_cache (Y_AXIS);
531   Axis_group_interface::add_element (top_staff, me);
532
533   // Remove any cross-staff side-support dependencies
534   Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
535   if (ga)
536     {
537       vector<Grob *> const &elts = ga->array ();
538       vector<Grob *> new_elts;
539       for (vsize i = 0; i < elts.size (); ++i)
540         {
541           if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
542             new_elts.push_back (elts[i]);
543         }
544       ga->set_array (new_elts);
545     }
546   return SCM_BOOL_T;
547 }
548
549 ADD_INTERFACE (Side_position_interface,
550                "Position a victim object (this one) next to other objects"
551                " (the support).  The property @code{direction} signifies where"
552                " to put the victim object relative to the support (left or"
553                " right, up or down?)\n"
554                "\n"
555                "The routine also takes the size of the staff into account if"
556                " @code{staff-padding} is set.  If undefined, the staff symbol"
557                " is ignored.",
558
559                /* properties */
560                "add-stem-support "
561                "direction "
562                "minimum-space "
563                "padding "
564                "quantize-position "
565                "side-axis "
566                "side-support-elements "
567                "slur-padding "
568                "staff-padding "
569                "use-skylines "
570               );