2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--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 "break-align-interface.hh"
22 #include "align-interface.hh"
23 #include "axis-group-interface.hh"
24 #include "dimensions.hh"
25 #include "international.hh"
26 #include "output-def.hh"
27 #include "paper-column.hh"
28 #include "pointer-group-interface.hh"
29 #include "side-position-interface.hh"
36 This is tricky: we cannot modify 'elements, since callers are
37 iterating the same list. Reordering the list in-place, or resetting
38 'elements will skip elements in the loops of callers.
40 So we return the correct order as an array.
43 Break_alignment_interface::break_align_order (Item *me)
45 SCM order_vec = me->get_property ("break-align-orders");
46 if (!scm_is_vector (order_vec)
47 || scm_c_vector_length (order_vec) < 3)
50 SCM order = scm_vector_ref (order_vec,
51 scm_from_int (me->break_status_dir () + 1));
57 Break_alignment_interface::ordered_elements (Grob *grob)
59 Item *me = dynamic_cast<Item *> (grob);
60 extract_grob_set (me, "elements", elts);
62 SCM order = break_align_order (me);
64 if (scm_is_false (order))
67 vector<Grob *> writable_elts (elts);
69 Copy in order specified in BREAK-ALIGN-ORDER.
71 vector<Grob *> new_elts;
72 for (; scm_is_pair (order); order = scm_cdr (order))
74 SCM sym = scm_car (order);
76 for (vsize i = writable_elts.size (); i--;)
78 Grob *g = writable_elts[i];
79 if (g && sym == g->get_property ("break-align-symbol"))
81 new_elts.push_back (g);
82 writable_elts.erase (writable_elts.begin () + i);
91 Break_alignment_interface::add_element (Grob *me, Grob *toadd)
93 Align_interface::add_element (me, toadd);
96 MAKE_SCHEME_CALLBACK (Break_alignment_interface, calc_positioning_done, 1)
98 Break_alignment_interface::calc_positioning_done (SCM smob)
100 Grob *grob = unsmob<Grob> (smob);
101 Item *me = dynamic_cast<Item *> (grob);
103 me->set_property ("positioning-done", SCM_BOOL_T);
105 vector<Grob *> elems = ordered_elements (me);
106 vector<Interval> extents;
108 for (vsize i = 0; i < elems.size (); i++)
110 Interval y = elems[i]->extent (elems[i], X_AXIS);
111 extents.push_back (y);
115 while (idx < extents.size () && extents[idx].is_empty ())
118 vector<Real> offsets;
119 offsets.resize (elems.size ());
120 for (vsize i = 0; i < offsets.size (); i++)
123 Real extra_right_space = 0.0;
124 vsize edge_idx = VPOS;
125 while (idx < elems.size ())
127 vsize next_idx = idx + 1;
128 while (next_idx < elems.size ()
129 && extents[next_idx].is_empty ())
132 Grob *l = elems[idx];
135 if (next_idx < elems.size ())
141 Find the first grob with a space-alist entry.
143 extract_grob_set (l, "elements", elts);
145 for (vsize i = elts.size (); i--;)
150 && scm_is_eq (elt->get_property ("break-align-symbol"),
151 ly_symbol2scm ("left-edge")))
154 SCM l = elt->get_property ("space-alist");
162 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
165 We used to use #'cause to find out the symbol and the spacing
166 table, but that gets icky when that grob is suicided for some
171 extract_grob_set (r, "elements", elts);
172 for (vsize i = elts.size ();
173 !scm_is_symbol (rsym) && i--;)
176 rsym = elt->get_property ("break-align-symbol");
180 if (scm_is_eq (rsym, ly_symbol2scm ("left-edge")))
184 if (scm_is_symbol (rsym))
185 entry = scm_assq (rsym, alist);
187 bool entry_found = scm_is_pair (entry);
191 if (scm_is_symbol (rsym))
192 sym_string = ly_symbol2string (rsym);
195 if (unsmob<Grob> (l->get_property ("cause")))
196 orig_string = unsmob<Grob> (l->get_property ("cause"))->name ();
198 programming_error (to_string ("No spacing entry from %s to `%s'",
199 orig_string.c_str (),
200 sym_string.c_str ()));
204 SCM type = ly_symbol2scm ("extra-space");
208 entry = scm_cdr (entry);
210 distance = scm_to_double (scm_cdr (entry));
211 type = scm_car (entry);
216 if (scm_is_eq (type, ly_symbol2scm ("extra-space")))
217 offsets[next_idx] = extents[idx][RIGHT] + distance
218 - extents[next_idx][LEFT];
219 /* should probably junk minimum-space */
220 else if (scm_is_eq (type, ly_symbol2scm ("minimum-space")))
221 offsets[next_idx] = max (extents[idx][RIGHT], distance);
225 extra_right_space = distance;
226 if (idx + 1 < offsets.size ())
227 offsets[idx + 1] = extents[idx][RIGHT] + distance;
234 Interval total_extent;
236 Real alignment_off = 0.0;
237 for (vsize i = 0; i < offsets.size (); i++)
241 alignment_off = -here;
242 total_extent.unite (extents[i] + here);
245 if (total_extent.is_empty ())
248 if (me->break_status_dir () == LEFT)
249 alignment_off = -total_extent[RIGHT] - extra_right_space;
250 else if (edge_idx == VPOS)
251 alignment_off = -total_extent[LEFT];
253 here = alignment_off;
254 for (vsize i = 0; i < offsets.size (); i++)
257 elems[i]->translate_axis (here, X_AXIS);
263 MAKE_SCHEME_CALLBACK (Break_alignable_interface, self_align_callback, 1)
265 Break_alignable_interface::self_align_callback (SCM grob)
267 Grob *me = unsmob<Grob> (grob);
268 Item *alignment = dynamic_cast<Item *> (me->get_parent (X_AXIS));
269 if (!has_interface<Break_alignment_interface> (alignment))
270 return scm_from_int (0);
272 SCM symbol_list = me->get_property ("break-align-symbols");
273 vector<Grob *> elements = Break_alignment_interface::ordered_elements (alignment);
274 if (elements.size () == 0)
275 return scm_from_int (0);
277 int break_aligned_grob = -1;
278 for (; scm_is_pair (symbol_list); symbol_list = scm_cdr (symbol_list))
280 SCM sym = scm_car (symbol_list);
281 for (vsize i = 0; i < elements.size (); i++)
283 if (elements[i]->get_property ("break-align-symbol") == sym)
285 if (Item::break_visible (elements[i])
286 && !elements[i]->extent (elements[i], X_AXIS).is_empty ())
288 break_aligned_grob = i;
289 goto found_break_aligned_grob; /* ugh. need to break out of 2 loops */
291 else if (break_aligned_grob == -1)
292 break_aligned_grob = i;
297 found_break_aligned_grob:
298 if (break_aligned_grob == -1)
299 return scm_from_int (0);
301 Grob *alignment_parent = elements[break_aligned_grob];
302 Grob *common = me->common_refpoint (alignment_parent, X_AXIS);
303 Real anchor = robust_scm2double (alignment_parent->get_property ("break-align-anchor"), 0);
305 return scm_from_double (alignment_parent->relative_coordinate (common, X_AXIS)
306 - me->relative_coordinate (common, X_AXIS)
310 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_average_anchor, 1)
312 Break_aligned_interface::calc_average_anchor (SCM grob)
314 Grob *me = unsmob<Grob> (grob);
318 /* average the anchors of those children that have it set */
319 extract_grob_set (me, "elements", elts);
320 for (vsize i = 0; i < elts.size (); i++)
322 SCM anchor = elts[i]->get_property ("break-align-anchor");
323 if (scm_is_number (anchor))
326 avg += scm_to_double (anchor);
330 return scm_from_double (count > 0 ? avg / count : 0);
333 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_extent_aligned_anchor, 1)
335 Break_aligned_interface::calc_extent_aligned_anchor (SCM smob)
337 Grob *me = unsmob<Grob> (smob);
338 Real alignment = robust_scm2double (me->get_property ("break-align-anchor-alignment"), 0.0);
339 Interval iv = me->extent (me, X_AXIS);
341 if (isinf (iv[LEFT]) && isinf (iv[RIGHT])) /* avoid NaN */
342 return scm_from_double (0.0);
344 return scm_from_double (iv.linear_combination (alignment));
347 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_break_visibility, 1)
349 Break_aligned_interface::calc_break_visibility (SCM smob)
351 /* a BreakAlignGroup is break-visible if it has one element that is break-visible */
352 Grob *me = unsmob<Grob> (smob);
353 SCM ret = scm_c_make_vector (3, SCM_EOL);
354 extract_grob_set (me, "elements", elts);
355 for (int dir = 0; dir <= 2; dir++)
357 bool visible = false;
358 for (vsize i = 0; i < elts.size (); i++)
360 SCM vis = elts[i]->get_property ("break-visibility");
361 if (scm_is_vector (vis) && to_boolean (scm_c_vector_ref (vis, dir)))
364 scm_c_vector_set_x (ret, dir, scm_from_bool (visible));
369 ADD_INTERFACE (Break_alignment_interface,
370 "The object that performs break alignment.\n"
372 "Three interfaces deal specifically with break alignment:\n"
374 "@item break-alignment-interface (this one),\n"
375 "@item @ref{break-alignable-interface}, and\n"
376 "@item @ref{break-aligned-interface}.\n"
379 " Each of these interfaces supports grob properties that use"
380 " @w{@emph{break-align symbols}}, which are Scheme symbols that"
381 " are used to specify the alignment, ordering, and spacing of"
382 " certain notational elements (@q{breakable}@tie{}items)."
384 "@subsubheading Available break-align symbols:\n"
402 "break-align-orders "
405 ADD_INTERFACE (Break_alignable_interface,
406 "Object that is aligned on a break alignment.",
409 "break-align-symbols "
410 "non-break-align-symbols "
413 ADD_INTERFACE (Break_aligned_interface,
417 "break-align-anchor "
418 "break-align-anchor-alignment "
419 "break-align-symbol "