1 /*
2 This file is part of LilyPond, the GNU music typesetter.
3
4 Copyright (C) 1997--2021 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 "break-align-interface.hh"
21
22 #include "align-interface.hh"
23 #include "axis-group-interface.hh"
24 #include "dimensions.hh"
25 #include "international.hh"
26 #include "ly-scm-list.hh"
27 #include "output-def.hh"
28 #include "paper-column.hh"
29 #include "pointer-group-interface.hh"
30 #include "side-position-interface.hh"
31 #include "warn.hh"
32
33 using std::string;
34 using std::vector;
35
36 /*
37 This is tricky: we cannot modify 'elements, since callers are
38 iterating the same list. Reordering the list in-place, or resetting
39 'elements will skip elements in the loops of callers.
40
41 So we return the correct order as an array.
42 */
43 SCM
break_align_order(Item * me)44 Break_alignment_interface::break_align_order (Item *me)
45 {
46 if (!me)
47 return SCM_BOOL_F;
48
49 SCM order_vec = get_property (me, "break-align-orders");
50 if (!scm_is_vector (order_vec)
51 || scm_c_vector_length (order_vec) < 3)
52 return SCM_BOOL_F;
53
54 SCM order = scm_vector_ref (order_vec,
55 to_scm (me->break_status_dir ().to_index ()));
56
57 return order;
58 }
59
60 vector<Grob *>
ordered_elements(Item * me)61 Break_alignment_interface::ordered_elements (Item *me)
62 {
63 extract_grob_set (me, "elements", elts);
64
65 SCM order = break_align_order (me);
66
67 if (scm_is_false (order))
68 return elts;
69
70 vector<Grob *> writable_elts (elts);
71 /*
72 Copy in order specified in BREAK-ALIGN-ORDER.
73 */
74 vector<Grob *> new_elts;
75 for (; scm_is_pair (order); order = scm_cdr (order))
76 {
77 SCM sym = scm_car (order);
78
79 for (vsize i = writable_elts.size (); i--;)
80 {
81 Grob *g = writable_elts[i];
82 if (g && scm_is_eq (sym, get_property (g, "break-align-symbol")))
83 {
84 new_elts.push_back (g);
85 writable_elts.erase (writable_elts.begin () + i);
86 }
87 }
88 }
89
90 return new_elts;
91 }
92
93 void
add_element(Item * me,Item * toadd)94 Break_alignment_interface::add_element (Item *me, Item *toadd)
95 {
96 Align_interface::add_element (me, toadd);
97 }
98
99 MAKE_SCHEME_CALLBACK (Break_alignment_interface, calc_positioning_done, 1)
100 SCM
calc_positioning_done(SCM smob)101 Break_alignment_interface::calc_positioning_done (SCM smob)
102 {
103 auto *const me = unsmob<Item> (smob);
104
105 set_property (me, "positioning-done", SCM_BOOL_T);
106
107 vector<Grob *> elems = ordered_elements (me);
108 vector<Interval> extents;
109
110 for (vsize i = 0; i < elems.size (); i++)
111 {
112 Interval y = elems[i]->extent (elems[i], X_AXIS);
113 extents.push_back (y);
114 }
115
116 vsize idx = 0;
117 while (idx < extents.size () && extents[idx].is_empty ())
118 idx++;
119
120 vector<Real> offsets;
121 offsets.resize (elems.size ());
122 for (vsize i = 0; i < offsets.size (); i++)
123 offsets[i] = 0.0;
124
125 Real extra_right_space = 0.0;
126 vsize edge_idx = VPOS;
127 while (idx < elems.size ())
128 {
129 vsize next_idx = idx + 1;
130 while (next_idx < elems.size ()
131 && extents[next_idx].is_empty ())
132 next_idx++;
133
134 Grob *l = elems[idx];
135 Grob *r = 0;
136
137 if (next_idx < elems.size ())
138 r = elems[next_idx];
139
140 SCM alist = SCM_EOL;
141
142 /*
143 Find the first grob with a space-alist entry.
144 */
145 extract_grob_set (l, "elements", elts);
146
147 for (vsize i = elts.size (); i--;)
148 {
149 Grob *elt = elts[i];
150
151 if (edge_idx == VPOS
152 && scm_is_eq (get_property (elt, "break-align-symbol"),
153 ly_symbol2scm ("left-edge")))
154 edge_idx = idx;
155
156 SCM l = get_property (elt, "space-alist");
157 if (scm_is_pair (l))
158 {
159 alist = l;
160 break;
161 }
162 }
163
164 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
165
166 /*
167 We used to use #'cause to find out the symbol and the spacing
168 table, but that gets icky when that grob is suicided for some
169 reason.
170 */
171 if (r)
172 {
173 extract_grob_set (r, "elements", elts);
174 for (vsize i = elts.size ();
175 !scm_is_symbol (rsym) && i--;)
176 {
177 Grob *elt = elts[i];
178 rsym = get_property (elt, "break-align-symbol");
179 }
180 }
181
182 if (scm_is_eq (rsym, ly_symbol2scm ("left-edge")))
183 edge_idx = next_idx;
184
185 SCM entry = SCM_EOL;
186 if (scm_is_symbol (rsym))
187 entry = scm_assq (rsym, alist);
188
189 bool entry_found = scm_is_pair (entry);
190 if (!entry_found)
191 {
192 string sym_string;
193 if (scm_is_symbol (rsym))
194 sym_string = ly_symbol2string (rsym);
195
196 string orig_string;
197 if (unsmob<Grob> (get_property (l, "cause")))
198 orig_string = unsmob<Grob> (get_property (l, "cause"))->name ();
199
200 programming_error (to_string ("No spacing entry from %s to `%s'",
201 orig_string.c_str (),
202 sym_string.c_str ()));
203 }
204
205 Real distance = 1.0;
206 SCM type = ly_symbol2scm ("extra-space");
207
208 if (entry_found)
209 {
210 entry = scm_cdr (entry);
211
212 distance = scm_to_double (scm_cdr (entry));
213 type = scm_car (entry);
214 }
215
216 if (r)
217 {
218 if (scm_is_eq (type, ly_symbol2scm ("extra-space")))
219 offsets[next_idx] = extents[idx][RIGHT] + distance
220 - extents[next_idx][LEFT];
221 /* should probably junk minimum-space */
222 else if (scm_is_eq (type, ly_symbol2scm ("minimum-space")))
223 offsets[next_idx] = std::max (extents[idx][RIGHT], distance);
224 }
225 else
226 {
227 extra_right_space = distance;
228 if (idx + 1 < offsets.size ())
229 offsets[idx + 1] = extents[idx][RIGHT] + distance;
230 }
231
232 idx = next_idx;
233 }
234
235 Real here = 0.0;
236 Interval total_extent;
237
238 Real alignment_off = 0.0;
239 for (vsize i = 0; i < offsets.size (); i++)
240 {
241 here += offsets[i];
242 if (i == edge_idx)
243 alignment_off = -here;
244 total_extent.unite (extents[i] + here);
245 }
246
247 if (total_extent.is_empty ())
248 return SCM_BOOL_T;
249
250 if (me->break_status_dir () == LEFT)
251 alignment_off = -total_extent[RIGHT] - extra_right_space;
252 else if (edge_idx == VPOS)
253 alignment_off = -total_extent[LEFT];
254
255 here = alignment_off;
256 for (vsize i = 0; i < offsets.size (); i++)
257 {
258 here += offsets[i];
259 elems[i]->translate_axis (here, X_AXIS);
260 }
261
262 return SCM_BOOL_T;
263 }
264
265 MAKE_SCHEME_CALLBACK (Break_alignable_interface, find_parent, 1)
266 SCM
find_parent(SCM grob)267 Break_alignable_interface::find_parent (SCM grob)
268 {
269 auto *const me = LY_ASSERT_SMOB (Grob, grob, 1);
270 auto *const alignment_parent = find_parent (me);
271 return alignment_parent ? alignment_parent->self_scm () : SCM_BOOL_F;
272 }
273
274 Item *
find_parent(Grob * me)275 Break_alignable_interface::find_parent (Grob *me)
276 {
277 auto *const alignment = dynamic_cast<Item *> (me->get_x_parent ());
278 if (!has_interface<Break_alignment_interface> (alignment))
279 return nullptr;
280
281 auto elements = Break_alignment_interface::ordered_elements (alignment);
282 if (elements.empty ())
283 return nullptr;
284
285 Item *break_aligned_grob = nullptr;
286 SCM symbol_list = get_property (me, "break-align-symbols");
287 for (SCM sym : as_ly_scm_list (symbol_list))
288 {
289 for (auto *g : elements)
290 {
291 if (scm_is_eq (sym, get_property (g, "break-align-symbol")))
292 {
293 // Someone would have to do something unusual in Scheme to get a
294 // Spanner here.
295 if (auto *it = dynamic_cast<Item *> (g))
296 {
297 if (it->break_visible ()
298 // TODO SCM: simplify syntax?
299 && !it->extent (it, X_AXIS).is_empty ())
300 {
301 return it;
302 }
303 else if (!break_aligned_grob)
304 break_aligned_grob = it;
305 }
306 }
307 }
308 }
309
310 return break_aligned_grob;
311 }
312
313 MAKE_SCHEME_CALLBACK (Break_alignable_interface, self_align_callback, 1)
314 SCM
self_align_callback(SCM grob)315 Break_alignable_interface::self_align_callback (SCM grob)
316 {
317 auto *const me = LY_ASSERT_SMOB (Grob, grob, 1);
318 auto *const alignment_parent = find_parent (me);
319 if (!alignment_parent)
320 return to_scm (0);
321
322 Grob *common = me->common_refpoint (alignment_parent, X_AXIS);
323 Real anchor = from_scm<double> (get_property (alignment_parent, "break-align-anchor"), 0);
324
325 return to_scm (alignment_parent->relative_coordinate (common, X_AXIS)
326 - me->relative_coordinate (common, X_AXIS)
327 + anchor);
328 }
329
330 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_average_anchor, 1)
331 SCM
calc_average_anchor(SCM grob)332 Break_aligned_interface::calc_average_anchor (SCM grob)
333 {
334 Grob *me = unsmob<Grob> (grob);
335 Real avg = 0.0;
336 int count = 0;
337
338 /* average the anchors of those children that have it set */
339 extract_grob_set (me, "elements", elts);
340 for (vsize i = 0; i < elts.size (); i++)
341 {
342 SCM anchor = get_property (elts[i], "break-align-anchor");
343 if (scm_is_number (anchor))
344 {
345 count++;
346 avg += scm_to_double (anchor);
347 }
348 }
349
350 return to_scm (count > 0 ? avg / count : 0);
351 }
352
353 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_joint_anchor_alignment, 1)
354 SCM
calc_joint_anchor_alignment(SCM grob)355 Break_aligned_interface::calc_joint_anchor_alignment (SCM grob)
356 {
357 auto *const me = LY_ASSERT_SMOB (Grob, grob, 1);
358 return to_scm (calc_joint_anchor_alignment (me));
359 }
360
361 Direction
calc_joint_anchor_alignment(Grob * me)362 Break_aligned_interface::calc_joint_anchor_alignment (Grob *me)
363 {
364 // If all elements with non-zero alignment agree in sign, return that
365 // direction. Otherwise, return center. Just enough thought has been put
366 // into this algorithm to serve our immediate needs.
367 auto direction = CENTER;
368
369 extract_grob_set (me, "elements", elts);
370 for (vsize i = 0; i < elts.size (); i++)
371 {
372 SCM s = get_property (elts[i], "break-align-anchor-alignment");
373 double alignment = from_scm<double> (s, 0.0);
374 if (alignment < 0)
375 {
376 if (direction > CENTER)
377 return CENTER; // conflict
378 direction = LEFT;
379 }
380 else if (alignment > 0)
381 {
382 if (direction < CENTER)
383 return CENTER; // conflict
384 direction = RIGHT;
385 }
386 }
387
388 return direction;
389 }
390
391 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_extent_aligned_anchor, 1)
392 SCM
calc_extent_aligned_anchor(SCM smob)393 Break_aligned_interface::calc_extent_aligned_anchor (SCM smob)
394 {
395 Grob *me = unsmob<Grob> (smob);
396 Real alignment = from_scm<double> (get_property (me, "break-align-anchor-alignment"), 0.0);
397 Interval iv = me->extent (me, X_AXIS);
398
399 if (std::isinf (iv[LEFT]) && std::isinf (iv[RIGHT])) /* avoid NaN */
400 return to_scm (0.0);
401
402 return to_scm (iv.linear_combination (alignment));
403 }
404
405 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_break_visibility, 1)
406 SCM
calc_break_visibility(SCM smob)407 Break_aligned_interface::calc_break_visibility (SCM smob)
408 {
409 /* a BreakAlignGroup is break-visible if it has one element that is break-visible */
410 Grob *me = unsmob<Grob> (smob);
411 SCM ret = scm_c_make_vector (3, SCM_EOL);
412 extract_grob_set (me, "elements", elts);
413 for (int dir = 0; dir <= 2; dir++)
414 {
415 bool visible = false;
416 for (vsize i = 0; i < elts.size (); i++)
417 {
418 SCM vis = get_property (elts[i], "break-visibility");
419 if (scm_is_vector (vis) && from_scm<bool> (scm_c_vector_ref (vis, dir)))
420 visible = true;
421 }
422 scm_c_vector_set_x (ret, dir, scm_from_bool (visible));
423 }
424 return ret;
425 }
426
427 ADD_INTERFACE (Break_alignment_interface,
428 "The object that performs break alignment.\n"
429 "\n"
430 "Three interfaces deal specifically with break alignment:\n"
431 "@enumerate\n"
432 "@item break-alignment-interface (this one),\n"
433 "@item @ref{break-alignable-interface}, and\n"
434 "@item @ref{break-aligned-interface}.\n"
435 "@end enumerate\n"
436 "\n"
437 " Each of these interfaces supports grob properties that use"
438 " @w{@emph{break-align symbols}}, which are Scheme symbols that"
439 " are used to specify the alignment, ordering, and spacing of"
440 " certain notational elements (@q{breakable}@tie{}items)."
441 "\n"
442 "@subsubheading Available break-align symbols:\n"
443 "\n"
444 "@example\n"
445 "ambitus\n"
446 "breathing-sign\n"
447 "clef\n"
448 "cue-clef\n"
449 "cue-end-clef\n"
450 "custos\n"
451 "key-cancellation\n"
452 "key-signature\n"
453 "left-edge\n"
454 "staff-bar\n"
455 "time-signature\n"
456 "@end example",
457
458 /* properties */
459 "positioning-done "
460 "break-align-orders "
461 );
462
463 ADD_INTERFACE (Break_alignable_interface,
464 "Object that is aligned on a break alignment.",
465
466 /* properties */
467 "break-align-symbols "
468 "non-break-align-symbols "
469 );
470
471 ADD_INTERFACE (Break_aligned_interface,
472 "Breakable items.",
473
474 /* properties */
475 "break-align-anchor "
476 "break-align-anchor-alignment "
477 "break-align-symbol "
478 "space-alist "
479 );
480