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