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