2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "side-position-interface.hh"
22 #include <cmath> // ceil.
29 #include "accidental-interface.hh"
30 #include "accidental-placement.hh"
31 #include "axis-group-interface.hh"
32 #include "directional-element-interface.hh"
34 #include "grob-array.hh"
35 #include "international.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"
46 #include "string-convert.hh"
49 #include "unpure-pure-container.hh"
52 Side_position_interface::add_support (Grob *me, Grob *e)
54 Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
58 get_support_set (Grob *me)
60 // Only slightly kludgy heuristic...
61 // We want to make sure that all AccidentalPlacements'
62 // accidentals make it into the side support
63 extract_grob_set (me, "side-support-elements", proto_support);
66 for (vsize i = 0; i < proto_support.size (); i++)
68 if (Accidental_placement::has_interface (proto_support[i]))
70 Grob *accs = proto_support[i];
71 for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
73 for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
75 Grob *a = unsmob<Grob> (scm_car (s));
80 support.insert (proto_support[i]);
86 Position next to support, taking into account my own dimensions and padding.
89 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
92 Real *current_off_ptr = 0;
93 if (scm_is_number (current_off_scm))
95 r = scm_to_double (current_off_scm);
99 Grob *me = unsmob<Grob> (smob);
100 // We will only ever want widths of spanners after line breaking
101 // so we can set pure to false
102 if (dynamic_cast<Spanner *> (me) && a == X_AXIS)
105 return Side_position_interface::aligned_side (me, a, pure, start, end, current_off_ptr);
108 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
110 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
112 // Because horizontal skylines need vertical heights, we'd trigger
113 // unpure calculations too soon if this were called before line breaking.
114 // So, we always use pure heights. Given that horizontal skylines are
115 // almost always used before line breaking anyway, this doesn't cause
117 return axis_aligned_side_helper (smob, X_AXIS, true, 0, 0, current_off);
120 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
122 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
124 return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
127 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
129 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
131 return axis_aligned_side_helper (smob, Y_AXIS, true,
137 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
139 Side_position_interface::calc_cross_staff (SCM smob)
141 Grob *me = unsmob<Grob> (smob);
142 extract_grob_set (me, "side-support-elements", elts);
144 Direction my_dir = get_grob_direction (me) ;
146 for (vsize i = 0; i < elts.size (); i++)
149 If 'me' is placed relative to any cross-staff element with a
150 'direction callback defined, the placement of 'me' is likely
151 to depend on staff-spacing, thus 'me' should be considered
154 if (to_boolean (elts[i]->get_property ("cross-staff"))
155 && !is_direction (elts[i]->get_property_data ("direction")))
159 If elts[i] is cross-staff and is pointing in the same
160 direction as 'me', we assume that the alignment
161 of 'me' is influenced the cross-staffitude of elts[i]
162 and thus we mark 'me' as cross-staff.
164 if (to_boolean (elts[i]->get_property ("cross-staff"))
165 && my_dir == get_grob_direction (elts[i]))
169 Grob *myvag = Grob::get_vertical_axis_group (me);
170 for (vsize i = 0; i < elts.size (); i++)
171 if (myvag != Grob::get_vertical_axis_group (elts[i]))
177 // long function - each stage is clearly marked
180 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
183 Direction dir = get_grob_direction (me);
185 set<Grob *> support = get_support_set (me);
188 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
189 common[ax] = common_refpoint_of_array (support,
191 ? me->get_parent (ax)
195 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
196 bool quantize_position = to_boolean (me->get_maybe_pure_property ("quantize-position", pure, start, end));
197 bool me_cross_staff = to_boolean (me->get_property ("cross-staff"));
202 && scm_is_number (me->get_maybe_pure_property ("staff-padding", pure, start, end))
203 && !quantize_position;
206 common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
209 SCM skyp = me->get_maybe_pure_property (a == X_AXIS
210 ? "horizontal-skylines"
211 : "vertical-skylines",
215 if (unsmob<Skyline_pair> (skyp))
217 // for spanner pure heights, we don't know horizontal spacing,
218 // so a spanner can never have a meaningful x coordiante
219 // we just give it the parents' coordinate because its
220 // skyline will likely be of infinite width anyway
221 // and we don't want to prematurely trigger H spacing
222 Real xc = a == X_AXIS || (pure && dynamic_cast<Spanner *> (me))
223 ? me->get_parent (X_AXIS)->relative_coordinate (common[X_AXIS], X_AXIS)
224 : me->relative_coordinate (common[X_AXIS], X_AXIS);
225 // same here, for X_AXIS spacing, if it's happening, it should only be
226 // before line breaking. because there is no thing as "pure" x spacing,
227 // we assume that it is all pure
228 Real yc = a == X_AXIS
229 ? me->pure_relative_y_coordinate (common[Y_AXIS], start, end)
230 : me->get_parent (Y_AXIS)->maybe_pure_coordinate (common[Y_AXIS], Y_AXIS, pure, start, end);
231 Skyline_pair copy = *unsmob<Skyline_pair> (skyp);
232 copy.shift (a == X_AXIS ? yc : xc);
233 copy.raise (a == X_AXIS ? xc : yc);
237 me->warning ("cannot find skylines - strange alignment will follow");
241 vector<Skyline_pair> skyps;
242 set<Grob *>::iterator it;
244 for (it = support.begin (); it != support.end (); it++)
248 bool cross_staff = to_boolean (e->get_property ("cross-staff"));
250 && !me_cross_staff // 'me' promised not to adapt to staff-spacing
251 && cross_staff) // but 'e' might move based on staff-pacing
252 continue; // so 'me' may not move in response to 'e'
255 && Stem::has_interface (e))
257 // If called as 'pure' we may not force a stem to set its direction,
258 if (pure && !is_direction (e->get_property_data ("direction")))
260 // There is no need to consider stems pointing away.
261 if (dir == -get_grob_direction (e))
267 SCM sp = e->get_maybe_pure_property (a == X_AXIS
268 ? "horizontal-skylines"
269 : "vertical-skylines",
274 if (unsmob<Skyline_pair> (sp))
276 Real xc = pure && dynamic_cast<Spanner *> (e)
277 ? e->get_parent (X_AXIS)->relative_coordinate (common[X_AXIS], X_AXIS)
278 : e->relative_coordinate (common[X_AXIS], X_AXIS);
279 // same logic as above
280 // we assume horizontal spacing is always pure
281 Real yc = a == X_AXIS
282 ? e->pure_relative_y_coordinate (common[Y_AXIS], start, end)
283 : e->maybe_pure_coordinate (common[Y_AXIS], Y_AXIS, pure, start, end);
284 Skyline_pair copy = *unsmob<Skyline_pair> (sp);
286 && Stem::has_interface (e)
287 && to_boolean (me->get_maybe_pure_property ("add-stem-support", pure, start, end)))
288 copy[dir].set_minimum_height (copy[dir].max_height ());
289 copy.shift (a == X_AXIS ? yc : xc);
290 copy.raise (a == X_AXIS ? xc : yc);
291 skyps.push_back (copy);
293 else { /* no warning*/ }
297 Skyline dim (boxes, other_axis (a), dir);
300 Skyline_pair merged (skyps);
301 dim.merge (merged[dir]);
306 Interval staff_extents;
307 common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
308 staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
309 dim.set_minimum_height (staff_extents[dir]);
312 // Sometimes, we want to side position for grobs but they
313 // don't position against anything. Some cases where this is true:
314 // - StanzaNumber if the supporting lyrics are hara-kiri'd
315 // SystemStartBracket
317 // In all these cases, we set the height of the support to 0.
318 // This becomes then like the self-alignment-interface with the
319 // caveat that there is padding added.
320 // TODO: if there is a grob that never has side-support-elements
321 // (like InstrumentName), why are we using this function? Isn't it
322 // overkill? A function like self-alignment-interface with padding
324 // One could even imagine the two interfaces merged, as the only
325 // difference is that in self-alignment-interface we align on the parent
326 // where as here we align on a group of grobs.
329 dim = Skyline (dim.direction ());
330 dim.set_minimum_height (0.0);
333 Real ss = Staff_symbol_referencer::staff_space (me);
334 Real dist = dim.distance (my_dim, robust_scm2double (me->get_maybe_pure_property ("horizon-padding", pure, start, end), 0.0));
335 Real total_off = !isinf (dist) ? dir * dist : 0.0;
337 total_off += dir * ss * robust_scm2double (me->get_maybe_pure_property ("padding", pure, start, end), 0.0);
339 Real minimum_space = ss * robust_scm2double (me->get_maybe_pure_property ("minimum-space", pure, start, end), -1);
341 if (minimum_space >= 0
343 && total_off * dir < minimum_space)
344 total_off = minimum_space * dir;
347 total_off = dir * max (dir * total_off,
348 dir * (*current_off));
350 /* FIXME: 1000 should relate to paper size. */
351 if (fabs (total_off) > 1000)
354 = String_convert::form_string ("Improbable offset for grob %s: %f",
355 me->name ().c_str (), total_off);
357 programming_error (msg);
358 if (strict_infinity_checking)
359 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
363 Ensure 'staff-padding' from my refpoint to the staff. This is similar to
364 side-position with padding, but it will put adjoining objects on a row if
365 stuff sticks out of the staff a little.
367 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
368 if (staff && a == Y_AXIS)
370 if (quantize_position)
372 Grob *common = me->common_refpoint (staff, Y_AXIS);
373 Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
374 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
375 Real ss = Staff_symbol::staff_space (staff);
376 Real position = 2 * (my_off + total_off - staff_off) / ss;
377 Real rounded = directed_round (position, dir);
378 Grob *head = me->get_parent (X_AXIS);
380 Interval staff_span = Staff_symbol::line_span (staff);
381 staff_span.widen (1);
382 if (staff_span.contains (position)
383 /* If we are between notehead and staff, quantize for ledger lines. */
384 || (Note_head::has_interface (head)
385 && dir * position < 0))
387 total_off += (rounded - position) * 0.5 * ss;
388 if (Staff_symbol_referencer::on_line (me, int (rounded)))
389 total_off += dir * 0.5 * ss;
392 else if (scm_is_number (me->get_maybe_pure_property ("staff-padding", pure, start, end)) && dir)
395 = Staff_symbol_referencer::staff_space (me)
396 * scm_to_double (me->get_maybe_pure_property ("staff-padding", pure, start, end));
398 Grob *parent = me->get_parent (Y_AXIS);
399 Grob *common = me->common_refpoint (staff, Y_AXIS);
400 Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
401 Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
402 Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
403 Real diff = (dir * staff_extent[dir] + staff_padding
405 + dir * (staff_position - parent_position));
406 total_off += dir * max (diff, 0.0);
409 return scm_from_double (total_off);
413 Side_position_interface::set_axis (Grob *me, Axis a)
415 if (!scm_is_number (me->get_property ("side-axis")))
417 me->set_property ("side-axis", scm_from_int (a));
418 chain_offset_callback (me,
420 ? x_aligned_side_proc
421 : Unpure_pure_container::make_smob (y_aligned_side_proc, pure_y_aligned_side_proc),
427 Side_position_interface::get_axis (Grob *me)
429 if (scm_is_number (me->get_property ("side-axis")))
430 return Axis (scm_to_int (me->get_property ("side-axis")));
432 string msg = String_convert::form_string ("side-axis not set for grob %s.",
433 me->name ().c_str ());
434 me->programming_error (msg);
438 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
440 Side_position_interface::move_to_extremal_staff (SCM smob)
442 Grob *me = unsmob<Grob> (smob);
443 System *sys = dynamic_cast<System *> (me->get_system ());
444 Direction dir = get_grob_direction (me);
448 Interval iv = me->extent (sys, X_AXIS);
450 Grob *top_staff = sys->get_extremal_staff (dir, iv);
455 // Only move this grob if it is a direct child of the system. We
456 // are not interested in moving marks from other staves to the top
457 // staff; we only want to move marks from the system to the top
459 if (sys != me->get_parent (Y_AXIS))
462 me->set_parent (top_staff, Y_AXIS);
463 me->flush_extent_cache (Y_AXIS);
464 Axis_group_interface::add_element (top_staff, me);
466 // Remove any cross-staff side-support dependencies
467 Grob_array *ga = unsmob<Grob_array> (me->get_object ("side-support-elements"));
470 vector<Grob *> const &elts = ga->array ();
471 vector<Grob *> new_elts;
472 for (vsize i = 0; i < elts.size (); ++i)
474 if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
475 new_elts.push_back (elts[i]);
477 ga->set_array (new_elts);
482 ADD_INTERFACE (Side_position_interface,
483 "Position a victim object (this one) next to other objects"
484 " (the support). The property @code{direction} signifies where"
485 " to put the victim object relative to the support (left or"
486 " right, up or down?)\n"
488 "The routine also takes the size of the staff into account if"
489 " @code{staff-padding} is set. If undefined, the staff symbol"
500 "side-support-elements "