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