]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
Makes all side-positioning based on skylines instead of boxes.
[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 "international.hh"
36 #include "item.hh"
37 #include "main.hh"
38 #include "misc.hh"
39 #include "note-head.hh"
40 #include "note-column.hh"
41 #include "pointer-group-interface.hh"
42 #include "skyline-pair.hh"
43 #include "staff-symbol-referencer.hh"
44 #include "staff-symbol.hh"
45 #include "stem.hh"
46 #include "string-convert.hh"
47 #include "system.hh"
48 #include "warn.hh"
49
50 void
51 Side_position_interface::add_support (Grob *me, Grob *e)
52 {
53   Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
54 }
55
56 void
57 Side_position_interface::recursive_add_support (Grob *me, Grob *e)
58 {
59   Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
60   extract_grob_set (e, "side-support-elements", sse);
61   for (vsize i = 0; i < sse.size (); i++)
62     recursive_add_support (me, sse[i]);
63 }
64
65 set<Grob *>
66 get_support_set (Grob *me)
67 {
68   // Only slightly kludgy heuristic...
69   // We want to make sure that all AccidentalPlacements'
70   // accidentals make it into the side support
71   extract_grob_set (me, "side-support-elements", proto_support);
72   set<Grob *> support;
73
74   for (vsize i = 0; i < proto_support.size (); i++)
75     {
76       if (Accidental_placement::has_interface (proto_support[i]))
77         {
78           Grob *accs = proto_support[i];
79           for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
80                acs = scm_cdr (acs))
81             for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
82               {
83                 Grob *a = unsmob_grob (scm_car (s));
84                 support.insert (a);
85               }
86         }
87       else
88         support.insert (proto_support[i]);
89     }
90   return support;
91 }
92
93 /*
94   Position next to support, taking into account my own dimensions and padding.
95 */
96 SCM
97 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
98 {
99   Real r;
100   Real *current_off_ptr = 0;
101   if (scm_is_number (current_off_scm))
102     {
103       r = scm_to_double (current_off_scm);
104       current_off_ptr = &r;
105     }
106
107   Grob *me = unsmob_grob (smob);
108   // We will only ever want widths of spanners after line breaking
109   // so we can set pure to false
110   if (dynamic_cast<Spanner *> (me) && a == X_AXIS)
111     pure = false;
112
113   return Side_position_interface::aligned_side (me, a, pure, start, end, current_off_ptr);
114 }
115
116 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
117 SCM
118 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
119 {
120   // Because horizontal skylines need vertical heights, we'd trigger
121   // unpure calculations too soon if this were called before line breaking.
122   // So, we always use pure heights.  Given that horizontal skylines are
123   // almost always used before line breaking anyway, this doesn't cause
124   // problems.
125   return axis_aligned_side_helper (smob, X_AXIS, true, 0, 0, current_off);
126 }
127
128 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
129 SCM
130 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
131 {
132   return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
133 }
134
135 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
136 SCM
137 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
138 {
139   return axis_aligned_side_helper (smob, Y_AXIS, true,
140                                    scm_to_int (start),
141                                    scm_to_int (end),
142                                    cur_off);
143 }
144
145 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
146 SCM
147 Side_position_interface::calc_cross_staff (SCM smob)
148 {
149   Grob *me = unsmob_grob (smob);
150   extract_grob_set (me, "side-support-elements", elts);
151 // Commented out because of cross staff issues
152 // Direction for cross staff stems depends on the spacing of staves,
153 // which depends on the inclusion of cross-staff side position grobs,
154 // which need the direction for positioning.  So the get_grob_direction call
155 // may lead to circular dependencies.
156 // #if 0
157   Direction my_dir = get_grob_direction (me) ;
158
159   // if a cross-staff grob is pointing in a different direction than
160   // that of an aligning element, we assume that the alignment
161   // of said element will not be influenced the cross-staffitude
162   // of the grob and thus we do not mark the aligning element
163   // as cross-staff
164   for (vsize i = 0; i < elts.size (); i++)
165     if (to_boolean (elts[i]->get_property ("cross-staff"))
166          && my_dir == get_grob_direction (elts[i]))
167       return SCM_BOOL_T;
168 //#endif
169 #if 0
170   for (vsize i = 0; i < elts.size (); i++)
171     if (to_boolean (elts[i]->get_property ("cross-staff")))
172       return SCM_BOOL_T;
173 #endif
174   Grob *myvag = Grob::get_vertical_axis_group (me);
175   for (vsize i = 0; i < elts.size (); i++)
176     if (myvag != Grob::get_vertical_axis_group (elts[i]))
177       return SCM_BOOL_T;
178
179   return SCM_BOOL_F;
180 }
181
182 // long function - each stage is clearly marked
183
184 SCM
185 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
186                                        Real *current_off)
187 {//printf (" %s\n", me->name ().c_str ());
188   Direction dir = get_grob_direction (me);
189
190   set<Grob *> support = get_support_set (me);
191
192   Grob *common[2];
193   for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
194     common[ax] = common_refpoint_of_array (support,
195                                            (ax == a
196                                            ? me->get_parent (ax)
197                                            : me),
198                                            ax);
199
200   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
201
202   bool include_staff
203     = staff_symbol
204       && a == Y_AXIS
205       && scm_is_number (me->get_property ("staff-padding"))
206       && !to_boolean (me->get_property ("quantize-position"));
207
208   if (include_staff)
209     common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
210
211   Skyline my_dim;
212   Skyline_pair *skyp = Skyline_pair::unsmob (
213                          me->get_maybe_pure_property (a == X_AXIS
214                                                       ? "horizontal-skylines"
215                                                       : "vertical-skylines",
216                                                       pure,
217                                                       start,
218                                                       end));
219   if (skyp)
220     {
221       // for spanner pure heights, we don't know horizontal spacing,
222       // so a spanner can never have a meaningful x coordiante
223       // we just give it the parents' coordinate because its
224       // skyline will likely be of infinite width anyway
225       // and we don't want to prematurely trigger H spacing
226       Real xc = a == X_AXIS || (pure && dynamic_cast<Spanner *> (me))
227                 ? me->get_parent (X_AXIS)->relative_coordinate (common[X_AXIS], X_AXIS)
228                 : me->relative_coordinate (common[X_AXIS], X_AXIS);
229       // same here, for X_AXIS spacing, if it's happening, it should only be
230       // before line breaking.  because there is no thing as "pure" x spacing,
231       // we assume that it is all pure
232       Real yc = a == X_AXIS
233                 ? me->pure_relative_y_coordinate (common[Y_AXIS], start, end)
234                 : me->get_parent (Y_AXIS)->maybe_pure_coordinate (common[Y_AXIS], Y_AXIS, pure, start, end);
235       Skyline_pair copy = Skyline_pair (*skyp);
236       copy.shift (a == X_AXIS ? yc : xc);
237       copy.raise (a == X_AXIS ? xc : yc);
238       my_dim = copy[-dir];
239     }
240   else
241     me->warning ("cannot find skylines - strange alignment will follow");
242
243
244   vector<Box> boxes;
245   vector<Skyline_pair> skyps;
246   set<Grob *>::iterator it;
247   Real max_raise = -dir * infinity_f;
248   bool aligns_to_cross_staff = false;
249
250   for (it = support.begin (); it != support.end (); it++)
251     {
252       Grob *e = *it;
253 //printf ("    %s\n", e->name ().c_str ());
254       // In the case of a stem, we will find a note head as well
255       // ignoring the stem solves cyclic dependencies if the stem is
256       // attached to a cross-staff beam.
257       if (a == Y_AXIS
258           && Stem::has_interface (e)
259           && dir == - get_grob_direction (e))
260         continue;
261
262       if (e)
263         {
264
265            bool cross_staff = to_boolean (e->get_property ("cross-staff"));
266
267            Skyline_pair *sp = Skyline_pair::unsmob
268              (e->get_maybe_pure_property (a == X_AXIS
269                                           ? "horizontal-skylines"
270                                           : "vertical-skylines",
271                                           pure || cross_staff,
272                                           start,
273                                           end));
274
275            aligns_to_cross_staff |= cross_staff;
276            if (sp)
277              {
278                Real xc = pure && dynamic_cast<Spanner *> (e)
279                          ? e->get_parent (X_AXIS)->relative_coordinate (common[X_AXIS], X_AXIS)
280                          : e->relative_coordinate (common[X_AXIS], X_AXIS);
281                // same logic as above
282                // we assume horizontal spacing is always pure
283                Real yc = a == X_AXIS
284                          ? e->pure_relative_y_coordinate (common[Y_AXIS], start, end)
285                          : e->maybe_pure_coordinate (common[Y_AXIS], Y_AXIS, pure, start, end);
286                Skyline_pair copy = Skyline_pair (*sp);
287                if (a == Y_AXIS
288                    && Stem::has_interface (e)
289                    && to_boolean (me->get_property ("add-stem-support")))
290                  copy[dir].set_minimum_height (copy[dir].max_height ());
291                copy.shift (a == X_AXIS ? yc : xc);
292                copy.raise (a == X_AXIS ? xc : yc);
293                max_raise = minmax (dir, max_raise, a == X_AXIS ? xc : yc);
294                skyps.push_back (copy);
295              }
296            else { /* no warning*/ }
297         }
298     }
299
300   Skyline dim (boxes, other_axis (a), dir);
301   if (skyps.size ())
302     {
303       Skyline_pair merged (skyps);
304       dim.merge (merged[dir]);
305     }
306
307   if (include_staff)
308     {
309       Interval staff_extents;
310       common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
311       staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
312       dim.set_minimum_height (staff_extents[dir]);
313     }
314
315   // this seems kinda kludgy, as there is no apparent logic to it
316   // however, it is a holdover from the previous code and
317   // necessary for the InstrumentName grob
318   // TODO: find a better way to deal with this...
319   if (dim.is_empty ())
320     {
321       dim = Skyline (dim.direction ());
322       dim.set_minimum_height (0.0);
323     }
324
325   // Ditto - seems kludgy, but this time logic of SystemStartBrackets
326   if (my_dim.is_empty ())
327     {
328       my_dim = Skyline (my_dim.direction ());
329       my_dim.set_minimum_height (isinf (max_raise) ? 0.0 : max_raise);
330     }
331
332   // Many cross-staff grobs do not have good height estimations.
333   // We give the grob the best chance of not colliding by shifting
334   // it to the maximum height in the case of cross-staff alignment.
335   // This means, in other words, that the old way things were done
336   // (using boxes instead of skylines) is just reactivated for
337   // alignment to cross-staff grobs.
338   if (aligns_to_cross_staff)
339     dim.set_minimum_height (dim.max_height ());
340
341   Real ss = Staff_symbol_referencer::staff_space (me);
342   Real dist = dim.distance (my_dim, robust_scm2double (me->get_property ("horizon-padding"), 0.0));
343   Real total_off = !isinf (dist) ? dir * dist : 0.0;
344
345   total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0.0);
346
347   Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
348
349   if (minimum_space >= 0
350       && dir
351       && total_off * dir < minimum_space)
352     total_off = minimum_space * dir;
353
354   if (current_off)
355     total_off = dir * max (dir * total_off,
356                            dir * (*current_off));
357
358   /* FIXME: 1000 should relate to paper size.  */
359   if (fabs (total_off) > 1000)
360     {
361       string msg
362         = String_convert::form_string ("Improbable offset for grob %s: %f",
363                                        me->name ().c_str (), total_off);
364
365       programming_error (msg);
366       if (strict_infinity_checking)
367         scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
368     }
369
370   /*
371     Maintain a minimum distance to the staff. This is similar to side
372     position with padding, but it will put adjoining objects on a row if
373     stuff sticks out of the staff a little.
374   */
375   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
376   if (staff && a == Y_AXIS)
377     {
378       if (to_boolean (me->get_property ("quantize-position")))
379         {
380           Grob *common = me->common_refpoint (staff, Y_AXIS);
381           Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
382           Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
383           Real ss = Staff_symbol::staff_space (staff);
384           Real position = 2 * (my_off + total_off - staff_off) / ss;
385           Real rounded = directed_round (position, dir);
386           Grob *head = me->get_parent (X_AXIS);
387
388           Interval staff_span = Staff_symbol::line_span (staff);
389           staff_span.widen (1);
390           if (staff_span.contains (position)
391               /* In case of a ledger lines, quantize even if we're outside the staff. */
392               || (Note_head::has_interface (head)
393
394                   && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
395             {
396               total_off += (rounded - position) * 0.5 * ss;
397               if (Staff_symbol_referencer::on_line (me, int (rounded)))
398                 total_off += dir * 0.5 * ss;
399             }
400         }
401       else if (scm_is_number (me->get_property ("staff-padding")) && dir)
402         {
403           Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
404
405           Real staff_padding
406             = Staff_symbol_referencer::staff_space (me)
407               * scm_to_double (me->get_property ("staff-padding"));
408
409           Grob *parent = me->get_parent (Y_AXIS);
410           Grob *common = me->common_refpoint (staff, Y_AXIS);
411           Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
412           Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
413           Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
414           Real diff = (dir * staff_extent[dir] + staff_padding
415                        - dir * (total_off + iv[-dir])
416                        + dir * (staff_position - parent_position));
417           total_off += dir * max (diff, 0.0);
418         }
419     }
420   return scm_from_double (total_off);
421 }
422
423 void
424 Side_position_interface::set_axis (Grob *me, Axis a)
425 {
426   if (!scm_is_number (me->get_property ("side-axis")))
427     {
428       me->set_property ("side-axis", scm_from_int (a));
429       chain_offset_callback (me,
430                              (a == X_AXIS)
431                              ? x_aligned_side_proc
432                              : y_aligned_side_proc,
433                              a);
434     }
435 }
436
437 Axis
438 Side_position_interface::get_axis (Grob *me)
439 {
440   if (scm_is_number (me->get_property ("side-axis")))
441     return Axis (scm_to_int (me->get_property ("side-axis")));
442
443   string msg = String_convert::form_string ("side-axis not set for grob %s.",
444                                             me->name ().c_str ());
445   me->programming_error (msg);
446   return NO_AXES;
447 }
448
449 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
450 SCM
451 Side_position_interface::move_to_extremal_staff (SCM smob)
452 {
453   Grob *me = unsmob_grob (smob);
454   System *sys = dynamic_cast<System *> (me->get_system ());
455   Direction dir = get_grob_direction (me);
456   if (dir != DOWN)
457     dir = UP;
458
459   Interval iv = me->extent (sys, X_AXIS);
460   iv.widen (1.0);
461   Grob *top_staff = sys->get_extremal_staff (dir, iv);
462
463   if (!top_staff)
464     return SCM_BOOL_F;
465
466   // Only move this grob if it is a direct child of the system.  We
467   // are not interested in moving marks from other staves to the top
468   // staff; we only want to move marks from the system to the top
469   // staff.
470   if (sys != me->get_parent (Y_AXIS))
471     return SCM_BOOL_F;
472
473   me->set_parent (top_staff, Y_AXIS);
474   me->flush_extent_cache (Y_AXIS);
475   Axis_group_interface::add_element (top_staff, me);
476
477   // Remove any cross-staff side-support dependencies
478   Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
479   if (ga)
480     {
481       vector<Grob *> const &elts = ga->array ();
482       vector<Grob *> new_elts;
483       for (vsize i = 0; i < elts.size (); ++i)
484         {
485           if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
486             new_elts.push_back (elts[i]);
487         }
488       ga->set_array (new_elts);
489     }
490   return SCM_BOOL_T;
491 }
492
493 ADD_INTERFACE (Side_position_interface,
494                "Position a victim object (this one) next to other objects"
495                " (the support).  The property @code{direction} signifies where"
496                " to put the victim object relative to the support (left or"
497                " right, up or down?)\n"
498                "\n"
499                "The routine also takes the size of the staff into account if"
500                " @code{staff-padding} is set.  If undefined, the staff symbol"
501                " is ignored.",
502
503                /* properties */
504                "add-stem-support "
505                "direction "
506                "minimum-space "
507                "horizon-padding "
508                "padding "
509                "quantize-position "
510                "side-axis "
511                "side-support-elements "
512                "slur-padding "
513                "staff-padding "
514                "use-skylines "
515               );