2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2012 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"
37 #include "note-head.hh"
38 #include "note-column.hh"
39 #include "pointer-group-interface.hh"
40 #include "skyline-pair.hh"
41 #include "staff-symbol-referencer.hh"
42 #include "staff-symbol.hh"
44 #include "string-convert.hh"
49 Side_position_interface::add_support (Grob *me, Grob *e)
51 Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
55 finish_offset (Grob *me, Direction dir, Real total_off, Real *current_offset)
57 Real ss = Staff_symbol_referencer::staff_space (me);
58 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
59 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
61 if (minimum_space >= 0
63 && total_off * dir < minimum_space)
64 total_off = minimum_space * dir;
67 total_off = dir * max (dir * total_off,
68 dir * (*current_offset));
70 /* FIXME: 1000 should relate to paper size. */
71 if (fabs (total_off) > 1000)
74 = String_convert::form_string ("Improbable offset for grob %s: %f",
75 me->name ().c_str (), total_off);
77 programming_error (msg);
78 if (strict_infinity_checking)
79 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
82 return scm_from_double (total_off);
86 get_support_set (Grob *me)
88 // Only slightly kludgy heuristic...
89 // We want to make sure that all AccidentalPlacements'
90 // accidentals make it into the side support
91 extract_grob_set (me, "side-support-elements", proto_support);
94 for (vsize i = 0; i < proto_support.size (); i++)
96 if (Accidental_placement::has_interface (proto_support[i]))
98 Grob *accs = proto_support[i];
99 for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
101 for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
103 Grob *a = unsmob_grob (scm_car (s));
108 support.insert (proto_support[i]);
113 /* Put the element next to the support, optionally taking in
114 account the extent of the support.
116 Does not take into account the extent of ME.
119 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
120 bool include_my_extent,
121 bool pure, int start, int end,
122 Real *current_offset)
124 set<Grob *> support = get_support_set (me);
126 Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
127 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
131 && scm_is_number (me->get_property ("staff-padding"))
132 && !to_boolean (me->get_property ("quantize-position"));
135 Interval staff_extents;
138 common = staff_symbol->common_refpoint (common, Y_AXIS);
139 staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
142 dim.unite (staff_extents);
145 Direction dir = get_grob_direction (me);
147 set<Grob *>::iterator it;
149 for (it = support.begin (); it != support.end (); it++)
153 // In the case of a stem, we will find a note head as well
154 // ignoring the stem solves cyclic dependencies if the stem is
155 // attached to a cross-staff beam.
157 && Stem::has_interface (e)
158 && dir == - get_grob_direction (e))
164 dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
167 Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
168 dim.unite (Interval (x, x));
174 dim = Interval (0, 0);
176 Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
178 Real total_off = dim.linear_combination (dir) - off;
179 if (include_my_extent)
181 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
186 programming_error ("direction unknown, but aligned-side wanted");
189 total_off += -iv[-dir];
193 return finish_offset (me, dir, total_off, current_offset);
197 Side_position_interface::skyline_side_position (Grob *me, Axis a,
198 bool pure, int start, int end,
199 Real *current_offset)
201 set<Grob *> support = get_support_set (me);
204 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
205 common[ax] = common_refpoint_of_array (support, ax == a ? me->get_parent (ax) : me, ax);
207 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
208 Direction dir = get_grob_direction (me);
211 Skyline_pair *sp = Skyline_pair::unsmob (me->get_property ("vertical-skylines"));
212 if (sp && a == Y_AXIS && !pure)
214 Skyline_pair copy = Skyline_pair (*sp);
215 copy.shift (me->relative_coordinate (common[X_AXIS], X_AXIS));
216 copy.raise (me->get_parent (Y_AXIS)->relative_coordinate (common[Y_AXIS], Y_AXIS));
222 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
225 off[ax] = me->get_parent (ax)->maybe_pure_coordinate (common[ax], ax, pure, start, end)
226 + me->maybe_pure_extent (me, ax, pure, start, end);
228 off[ax] = me->maybe_pure_extent (common[ax], ax, pure, start, end);
231 if (off[X_AXIS].is_empty () || off[Y_AXIS].is_empty ())
232 return scm_from_double (0.0);
234 my_dim = Skyline (off, other_axis (a), -dir);
239 && scm_is_number (me->get_property ("staff-padding"))
240 && !to_boolean (me->get_property ("quantize-position"));
243 vector<Skyline_pair> skyps;
244 Real min_h = dir == LEFT ? infinity_f : -infinity_f;
245 set<Grob *>::iterator it;
247 map<Grob *, vector<Grob *> > note_column_map; // for parts of a note column
248 for (it = support.begin (); it != support.end (); it++)
252 // In the case of a stem, we will find a note head as well
253 // ignoring the stem solves cyclic dependencies if the stem is
254 // attached to a cross-staff beam.
256 && Stem::has_interface (e)
257 && dir == - get_grob_direction (e))
262 if (Note_column::has_interface (e->get_parent (X_AXIS))
263 && to_boolean (me->get_property ("add-stem-support")))
265 note_column_map[e->get_parent (X_AXIS)].push_back (e);
269 Skyline_pair *sp = Skyline_pair::unsmob (e->get_property ("vertical-skylines"));
270 if (sp && a == Y_AXIS && !pure)
272 Skyline_pair copy = Skyline_pair (*sp);
273 copy.shift (e->relative_coordinate (common[X_AXIS], X_AXIS));
274 copy.raise (e->relative_coordinate (common[Y_AXIS], Y_AXIS));
275 skyps.push_back (copy);
279 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
280 b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
282 if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
286 min_h = minmax (dir, b[a][-dir], min_h);
290 // this loop ensures that parts of a note column will be in the same box
291 // pushes scripts and such over stems instead of just over heads
292 for (map<Grob *, vector<Grob *> >::iterator i = note_column_map.begin (); i != note_column_map.end (); i++)
295 for (vsize j = 0; j < (*i).second.size (); j++)
297 Grob *e = (*i).second[j];
299 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
300 b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
302 if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
307 if (!big[X_AXIS].is_empty () && !big[Y_AXIS].is_empty ())
308 boxes.push_back (big);
311 Skyline dim (boxes, other_axis (a), dir);
314 Skyline_pair merged (skyps);
315 dim.merge (merged[dir]);
318 dim.set_minimum_height (0.0);
320 dim.set_minimum_height (min_h);
324 Interval staff_extents;
325 common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
326 staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
327 dim.set_minimum_height (minmax (dir, min_h, staff_extents[dir]));
330 Real dist = dim.distance (my_dim, 0.1); // 0.1 m4g1c value...fix...
331 Real total_off = !isinf (dist) ? dir * dist : 0.0;
333 return finish_offset (me, dir, total_off, current_offset);
336 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
338 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
340 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
343 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
345 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
347 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
348 true, scm_to_int (start), scm_to_int (end), 0);
352 Position next to support, taking into account my own dimensions and padding.
355 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
358 Real *current_off_ptr = 0;
359 if (scm_is_number (current_off_scm))
361 r = scm_to_double (current_off_scm);
362 current_off_ptr = &r;
365 return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
368 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
370 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
372 return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
375 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
377 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
379 return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
382 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
384 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
386 return axis_aligned_side_helper (smob, Y_AXIS, true,
392 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
394 Side_position_interface::calc_cross_staff (SCM smob)
396 Grob *me = unsmob_grob (smob);
397 extract_grob_set (me, "side-support-elements", elts);
399 for (vsize i = 0; i < elts.size (); i++)
400 if (to_boolean (elts[i]->get_property ("cross-staff")))
403 Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
404 return scm_from_bool (common != me->get_parent (Y_AXIS));
408 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
411 Direction dir = get_grob_direction (me);
412 bool skyline = to_boolean (me->get_property ("use-skylines"));
414 Real o = scm_to_double (skyline && !pure
415 ? skyline_side_position (me, a, pure, start, end, current_off)
416 : general_side_position (me, a, true, true, pure, start, end, current_off));
419 Maintain a minimum distance to the staff. This is similar to side
420 position with padding, but it will put adjoining objects on a row if
421 stuff sticks out of the staff a little.
423 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
424 if (staff && a == Y_AXIS)
426 if (to_boolean (me->get_property ("quantize-position")))
428 Grob *common = me->common_refpoint (staff, Y_AXIS);
429 Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
430 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
431 Real ss = Staff_symbol::staff_space (staff);
432 Real position = 2 * (my_off + o - staff_off) / ss;
433 Real rounded = directed_round (position, dir);
434 Grob *head = me->get_parent (X_AXIS);
436 Interval staff_span = Staff_symbol::line_span (staff);
437 staff_span.widen (1);
438 if (staff_span.contains (position)
439 /* In case of a ledger lines, quantize even if we're outside the staff. */
440 || (Note_head::has_interface (head)
442 && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
444 o += (rounded - position) * 0.5 * ss;
445 if (Staff_symbol_referencer::on_line (me, int (rounded)))
449 else if (scm_is_number (me->get_property ("staff-padding")) && dir)
451 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
454 = Staff_symbol_referencer::staff_space (me)
455 * scm_to_double (me->get_property ("staff-padding"));
457 Grob *parent = me->get_parent (Y_AXIS);
458 Grob *common = me->common_refpoint (staff, Y_AXIS);
459 Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
460 Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
461 Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
462 Real diff = (dir * staff_extent[dir] + staff_padding
463 - dir * (o + iv[-dir])
464 + dir * (staff_position - parent_position));
465 o += dir * max (diff, 0.0);
468 return scm_from_double (o);
472 Side_position_interface::set_axis (Grob *me, Axis a)
474 if (!scm_is_number (me->get_property ("side-axis")))
476 me->set_property ("side-axis", scm_from_int (a));
477 chain_offset_callback (me,
479 ? x_aligned_side_proc
480 : y_aligned_side_proc,
486 Side_position_interface::get_axis (Grob *me)
488 if (scm_is_number (me->get_property ("side-axis")))
489 return Axis (scm_to_int (me->get_property ("side-axis")));
491 string msg = String_convert::form_string ("side-axis not set for grob %s.",
492 me->name ().c_str ());
493 me->programming_error (msg);
497 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
499 Side_position_interface::move_to_extremal_staff (SCM smob)
501 Grob *me = unsmob_grob (smob);
502 System *sys = dynamic_cast<System *> (me->get_system ());
503 Direction dir = get_grob_direction (me);
507 Interval iv = me->extent (sys, X_AXIS);
509 Grob *top_staff = sys->get_extremal_staff (dir, iv);
514 // Only move this grob if it is a direct child of the system. We
515 // are not interested in moving marks from other staves to the top
516 // staff; we only want to move marks from the system to the top
518 if (sys != me->get_parent (Y_AXIS))
521 me->set_parent (top_staff, Y_AXIS);
522 me->flush_extent_cache (Y_AXIS);
523 Axis_group_interface::add_element (top_staff, me);
525 // Remove any cross-staff side-support dependencies
526 Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
529 vector<Grob *> const &elts = ga->array ();
530 vector<Grob *> new_elts;
531 for (vsize i = 0; i < elts.size (); ++i)
533 if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
534 new_elts.push_back (elts[i]);
536 ga->set_array (new_elts);
541 ADD_INTERFACE (Side_position_interface,
542 "Position a victim object (this one) next to other objects"
543 " (the support). The property @code{direction} signifies where"
544 " to put the victim object relative to the support (left or"
545 " right, up or down?)\n"
547 "The routine also takes the size of the staff into account if"
548 " @code{staff-padding} is set. If undefined, the staff symbol"
558 "side-support-elements "