1 /*
2 This file is part of LilyPond, the GNU music typesetter.
3
4 Copyright (C) 1998--2020 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 "accidental-interface.hh"
23 #include "accidental-placement.hh"
24 #include "axis-group-interface.hh"
25 #include "directional-element-interface.hh"
26 #include "grob.hh"
27 #include "grob-array.hh"
28 #include "international.hh"
29 #include "item.hh"
30 #include "main.hh"
31 #include "misc.hh"
32 #include "note-head.hh"
33 #include "note-column.hh"
34 #include "pointer-group-interface.hh"
35 #include "skyline-pair.hh"
36 #include "staff-grouper-interface.hh"
37 #include "staff-symbol-referencer.hh"
38 #include "staff-symbol.hh"
39 #include "stem.hh"
40 #include "string-convert.hh"
41 #include "system.hh"
42 #include "warn.hh"
43 #include "unpure-pure-container.hh"
44
45 #include <algorithm>
46 #include <cmath> // ceil.
47 #include <map>
48 #include <set>
49
50 using std::set;
51 using std::string;
52 using std::vector;
53
54 void
add_support(Grob * me,Grob * e)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 *>
get_support_set(Grob * me)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 = get_object (accs, "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
axis_aligned_side_helper(SCM smob,Axis a,bool pure,int start,int end,SCM current_off_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
x_aligned_side(SCM smob,SCM current_off)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
y_aligned_side(SCM smob,SCM current_off)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
pure_y_aligned_side(SCM smob,SCM start,SCM end,SCM cur_off)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
calc_cross_staff(SCM smob)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 (from_scm<bool> (get_property (elts[i], "cross-staff"))
158 && !is_scm<Direction> (get_property_data (elts[i], "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 (from_scm<bool> (get_property (elts[i], "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
aligned_side(Grob * me,Axis a,bool pure,int start,int end,Real * current_off)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 if (!dir)
189 {
190 // Should we even warn? Previously this caused an assertion
191 // failure, so this might not be well-supported behavior. But
192 // possibly this could be useful?
193 me->warning (_f ("%s needs a direction for side alignment",
194 me->name ().c_str ()));
195 // Too much of the remaining code does not do anything sensible
196 // without a direction, so we just take what we have and bail
197 return to_scm (current_off ? *current_off : 0.0);
198 }
199
200 set<Grob *> support = get_support_set (me);
201
202 Grob *common[2];
203 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
204 common[ax] = common_refpoint_of_array (support,
205 (ax == a
206 ? me->get_parent (ax)
207 : me),
208 ax);
209
210 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
211 bool quantize_position = from_scm<bool> (get_maybe_pure_property (me, "quantize-position", pure, start, end));
212 bool me_cross_staff = from_scm<bool> (get_property (me, "cross-staff"));
213
214 bool include_staff
215 = staff_symbol
216 && a == Y_AXIS
217 && scm_is_number (get_maybe_pure_property (me, "staff-padding", pure, start, end))
218 && !quantize_position;
219
220 if (include_staff)
221 common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
222
223 Skyline my_dim;
224 SCM skyp = get_maybe_pure_property (me, a == X_AXIS
225 ? "horizontal-skylines"
226 : "vertical-skylines",
227 pure,
228 start,
229 end);
230 if (unsmob<Skyline_pair> (skyp))
231 {
232 // for spanner pure heights, we don't know horizontal spacing,
233 // so a spanner can never have a meaningful x coordiante
234 // we just give it the parents' coordinate because its
235 // skyline will likely be of infinite width anyway
236 // and we don't want to prematurely trigger H spacing
237 Real xc;
238 if (a == X_AXIS)
239 xc = me->parent_relative (common[X_AXIS], X_AXIS);
240 else // Y_AXIS
241 {
242 if (!pure)
243 xc = me->relative_coordinate (common[X_AXIS], X_AXIS);
244 else
245 // Not safe to call X-offset callbacks here as that may
246 // trigger stem and beam direction, so just set to 0
247 xc = 0;
248 }
249
250 // same here, for X_AXIS spacing, if it's happening, it should only be
251 // before line breaking. because there is no thing as "pure" x spacing,
252 // we assume that it is all pure
253 Real yc = a == X_AXIS
254 ? me->pure_relative_y_coordinate (common[Y_AXIS], start, end)
255 : me->get_y_parent ()->maybe_pure_coordinate (common[Y_AXIS], Y_AXIS, pure, start, end);
256 Skyline_pair copy = *unsmob<Skyline_pair> (skyp);
257 copy.shift (a == X_AXIS ? yc : xc);
258 copy.raise (a == X_AXIS ? xc : yc);
259 my_dim = copy[-dir];
260 }
261 else
262 me->warning (_ ("cannot find skylines - strange alignment will follow"));
263
264 vector<Box> boxes;
265 vector<Skyline_pair> skyps;
266 set<Grob *>::iterator it;
267
268 for (it = support.begin (); it != support.end (); it++)
269 {
270 Grob *e = *it;
271
272 bool cross_staff = from_scm<bool> (get_property (e, "cross-staff"));
273 if (a == Y_AXIS
274 && !me_cross_staff // 'me' promised not to adapt to staff-spacing
275 && cross_staff) // but 'e' might move based on staff-pacing
276 continue; // so 'me' may not move in response to 'e'
277
278 if (a == Y_AXIS
279 && has_interface<Stem> (e))
280 {
281 // If called as 'pure' we may not force a stem to set its direction,
282 if (pure && !is_scm<Direction> (get_property_data (e, "direction")))
283 continue;
284 // There is no need to consider stems pointing away.
285 if (dir == -get_grob_direction (e))
286 continue;
287 }
288
289 if (e)
290 {
291 SCM sp = get_maybe_pure_property (e, a == X_AXIS
292 ? "horizontal-skylines"
293 : "vertical-skylines",
294 pure,
295 start,
296 end);
297
298 if (unsmob<Skyline_pair> (sp))
299 {
300 Real xc = pure && dynamic_cast<Spanner *> (e)
301 ? e->parent_relative (common[X_AXIS], X_AXIS)
302 : e->relative_coordinate (common[X_AXIS], X_AXIS);
303 // same logic as above
304 // we assume horizontal spacing is always pure
305 Real yc = a == X_AXIS
306 ? e->pure_relative_y_coordinate (common[Y_AXIS], start, end)
307 : e->maybe_pure_coordinate (common[Y_AXIS], Y_AXIS, pure, start, end);
308 Skyline_pair copy = *unsmob<Skyline_pair> (sp);
309 if (a == Y_AXIS
310 && has_interface<Stem> (e)
311 && from_scm<bool> (get_maybe_pure_property (me, "add-stem-support", pure, start, end)))
312 copy[dir].set_minimum_height (copy[dir].max_height ());
313 copy.shift (a == X_AXIS ? yc : xc);
314 copy.raise (a == X_AXIS ? xc : yc);
315 skyps.push_back (copy);
316 }
317 else { /* no warning*/ }
318 }
319 }
320
321 Skyline dim (boxes, other_axis (a), dir);
322 if (skyps.size ())
323 {
324 Skyline_pair merged (skyps);
325 dim.merge (merged[dir]);
326 }
327
328 if (include_staff)
329 {
330 Interval staff_extents;
331 common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
332 staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
333 dim.set_minimum_height (staff_extents[dir]);
334 }
335
336 // Sometimes, we want to side position for grobs but they
337 // don't position against anything. Some cases where this is true:
338 // - StanzaNumber if the supporting lyrics are hara-kiri'd
339 // SystemStartBracket
340 // InstrumentName
341 // In all these cases, we set the height of the support to 0.
342 // This becomes then like the self-alignment-interface with the
343 // caveat that there is padding added.
344 // TODO: if there is a grob that never has side-support-elements
345 // (like InstrumentName), why are we using this function? Isn't it
346 // overkill? A function like self-alignment-interface with padding
347 // works just fine.
348 // One could even imagine the two interfaces merged, as the only
349 // difference is that in self-alignment-interface we align on the parent
350 // where as here we align on a group of grobs.
351 if (dim.is_empty ())
352 {
353 dim = Skyline (dim.direction ());
354 dim.set_minimum_height (0.0);
355 }
356
357 Real ss = Staff_symbol_referencer::staff_space (me);
358 Real dist = dim.distance (my_dim, from_scm<double> (get_maybe_pure_property (me, "horizon-padding", pure, start, end), 0.0));
359 Real total_off = !std::isinf (dist) ? dir * dist : 0.0;
360
361 total_off += dir * ss * from_scm<double> (get_maybe_pure_property (me, "padding", pure, start, end), 0.0);
362
363 Real minimum_space = ss * from_scm<double> (get_maybe_pure_property (me, "minimum-space", pure, start, end), -1);
364
365 if (minimum_space >= 0
366 && dir
367 && total_off * dir < minimum_space)
368 total_off = minimum_space * dir;
369
370 if (current_off)
371 total_off = dir * std::max (dir * total_off,
372 dir * (*current_off));
373
374 /* FIXME: 1000 should relate to paper size. */
375 if (fabs (total_off) > 1000)
376 {
377 string msg
378 = String_convert::form_string ("Improbable offset for grob %s: %f",
379 me->name ().c_str (), total_off);
380
381 programming_error (msg);
382 if (strict_infinity_checking)
383 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
384 }
385
386 /*
387 Ensure 'staff-padding' from my refpoint to the staff. This is similar to
388 side-position with padding, but it will put adjoining objects on a row if
389 stuff sticks out of the staff a little.
390 */
391 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
392 if (staff && a == Y_AXIS)
393 {
394 if (quantize_position)
395 {
396 Grob *common = me->common_refpoint (staff, Y_AXIS);
397 Real my_off = me->get_y_parent ()->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
398 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
399 Real ss = Staff_symbol::staff_space (staff);
400 Real position = 2 * (my_off + total_off - staff_off) / ss;
401 Real rounded = directed_round (position, dir);
402 Grob *head = me->get_x_parent ();
403
404 Interval staff_span = Staff_symbol::line_span (staff);
405 staff_span.widen (1);
406 if (staff_span.contains (position)
407 /* If we are between notehead and staff, quantize for ledger lines. */
408 || (has_interface<Note_head> (head)
409 && dir * position < 0))
410 {
411 total_off += (rounded - position) * 0.5 * ss;
412 if (Staff_symbol_referencer::on_line (me, int (rounded)))
413 total_off += dir * 0.5 * ss;
414 }
415 }
416 else if (scm_is_number (get_maybe_pure_property (me, "staff-padding", pure, start, end)) && dir)
417 {
418 Real staff_padding
419 = Staff_symbol_referencer::staff_space (me)
420 * scm_to_double (get_maybe_pure_property (me, "staff-padding", pure, start, end));
421
422 Grob *parent = me->get_y_parent ();
423 Grob *common = me->common_refpoint (staff, Y_AXIS);
424 Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
425 Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
426 Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
427 Real diff = (dir * staff_extent[dir] + staff_padding
428 - dir * total_off
429 + dir * (staff_position - parent_position));
430 total_off += dir * std::max (diff, 0.0);
431 }
432 }
433 return to_scm (total_off);
434 }
435
436 void
set_axis(Grob * me,Axis a)437 Side_position_interface::set_axis (Grob *me, Axis a)
438 {
439 if (!scm_is_number (get_property (me, "side-axis")))
440 {
441 set_property (me, "side-axis", to_scm (a));
442 chain_offset_callback (me,
443 (a == X_AXIS)
444 ? x_aligned_side_proc
445 : Unpure_pure_container::make_smob (y_aligned_side_proc, pure_y_aligned_side_proc),
446 a);
447 }
448 }
449
450 Axis
get_axis(Grob * me)451 Side_position_interface::get_axis (Grob *me)
452 {
453 if (scm_is_number (get_property (me, "side-axis")))
454 return Axis (scm_to_int (get_property (me, "side-axis")));
455
456 if (scm_is_true (get_property (me, "stencil")))
457 me->programming_error (_f ("no side-axis setting found for grob %s.",
458 me->name ().c_str ()));
459 return NO_AXES;
460 }
461
462 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
463 SCM
move_to_extremal_staff(SCM smob)464 Side_position_interface::move_to_extremal_staff (SCM smob)
465 {
466 auto *const me = LY_ASSERT_SMOB (Grob, smob, 1);
467
468 Direction dir = get_grob_direction (me);
469 if (dir != DOWN)
470 dir = UP;
471
472 System *sys = me->get_system ();
473 Interval iv = me->extent (sys, X_AXIS);
474 iv.widen (1.0);
475
476 Grob *grouper = me->get_y_parent ();
477 if (has_interface<Staff_grouper_interface> (grouper))
478 ; // find the extremal staff of this group
479 else if (grouper == sys)
480 {
481 // find the extremal staff of the whole system
482 grouper = unsmob<Grob> (get_object (sys, "vertical-alignment"));
483 if (!grouper)
484 return SCM_BOOL_F;
485 }
486 else // do not move marks from other staves to the top staff
487 return SCM_BOOL_F;
488
489 // N.B. It's ugly to pass a VerticalAlignment to this staff-grouper function.
490 // Read the comments in the function for more detail.
491 Grob *top_staff = Staff_grouper_interface::get_extremal_staff (grouper, sys, dir, iv);
492 if (!top_staff)
493 return SCM_BOOL_F;
494
495 me->set_y_parent (top_staff);
496 me->flush_extent_cache (Y_AXIS);
497 Axis_group_interface::add_element (top_staff, me);
498
499 // Remove any cross-staff side-support dependencies
500 Grob_array *ga = unsmob<Grob_array> (get_object (me, "side-support-elements"));
501 if (ga)
502 {
503 vector<Grob *> const &elts = ga->array ();
504 vector<Grob *> new_elts;
505 for (vsize i = 0; i < elts.size (); ++i)
506 {
507 if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
508 new_elts.push_back (elts[i]);
509 }
510 ga->set_array (new_elts);
511 }
512 return SCM_BOOL_T;
513 }
514
515 ADD_INTERFACE (Side_position_interface,
516 "Position a victim object (this one) next to other objects"
517 " (the support). The property @code{direction} signifies where"
518 " to put the victim object relative to the support (left or"
519 " right, up or down?)\n"
520 "\n"
521 "The routine also takes the size of the staff into account if"
522 " @code{staff-padding} is set. If undefined, the staff symbol"
523 " is ignored.",
524
525 /* properties */
526 "add-stem-support "
527 "direction "
528 "minimum-space "
529 "horizon-padding "
530 "padding "
531 "quantize-position "
532 "side-axis "
533 "side-support-elements "
534 "slur-padding "
535 "staff-padding "
536 "use-skylines "
537 );
538