2 break-align-interface.cc -- implement Break_align_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "break-align-interface.hh"
13 #include "libc-extension.hh" // isinf
14 #include "pointer-group-interface.hh"
15 #include "self-alignment-interface.hh"
16 #include "side-position-interface.hh"
17 #include "axis-group-interface.hh"
19 #include "dimensions.hh"
20 #include "output-def.hh"
21 #include "paper-column.hh"
22 #include "align-interface.hh"
24 MAKE_SCHEME_CALLBACK (Break_align_interface, alignment_callback, 2);
26 Break_align_interface::alignment_callback (SCM element_smob, SCM axis)
28 Grob *me = unsmob_grob (element_smob);
29 Axis a = (Axis) scm_to_int (axis);
32 Grob *par = me->get_parent (a);
33 if (par && !to_boolean (par->get_property ("positioning-done")))
35 par->set_property ("positioning-done", SCM_BOOL_T);
36 Break_align_interface::do_alignment (par);
39 return scm_from_double (0);
42 MAKE_SCHEME_CALLBACK (Break_align_interface, self_align_callback, 2);
44 Break_align_interface::self_align_callback (SCM element_smob, SCM axis)
46 Grob *me = unsmob_grob (element_smob);
48 assert (scm_to_int (axis) == X_AXIS);
50 Item *item = dynamic_cast<Item *> (me);
51 Direction bsd = item->break_status_dir ();
54 me->set_property ("self-alignment-X", scm_from_int (RIGHT));
58 Force break alignment itself to be done first, in the case
60 return Self_alignment_interface::aligned_on_self (element_smob, axis);
64 This is tricky: we cannot modify 'elements, since callers are
65 iterating the same list. Reordering the list in-place, or resetting
66 'elements will skip elements in the loops of callers.
68 So we return the correct order as an array.
71 Break_align_interface::ordered_elements (Grob *grob)
73 Item *me = dynamic_cast<Item *> (grob);
74 extract_grob_set (me, "elements", elts);
76 SCM order_vec = me->get_property ("break-align-orders");
77 if (!scm_is_vector (order_vec)
78 || scm_c_vector_length (order_vec) < 3)
81 Link_array<Grob> writable_elts (elts);
82 SCM order = scm_vector_ref (order_vec,
83 scm_from_int (me->break_status_dir () + 1));
86 Copy in order specified in BREAK-ALIGN-ORDER.
88 Link_array<Grob> new_elts;
89 for (; scm_is_pair (order); order = scm_cdr (order))
91 SCM sym = scm_car (order);
93 for (int i = writable_elts.size(); i --; )
95 Grob *g = writable_elts[i];
96 if (g && sym == g->get_property ("break-align-symbol"))
99 writable_elts.del (i);
108 Break_align_interface::add_element (Grob *me, Grob *toadd)
110 Axis_group_interface::add_element (me, toadd);
114 Break_align_interface::do_alignment (Grob *grob)
116 Item *me = dynamic_cast<Item *> (grob);
118 Link_array<Grob> elems = ordered_elements (me);
119 Array<Interval> extents;
121 int last_nonempty = -1;
122 for (int i = 0; i < elems.size (); i++)
124 Interval y = elems[i]->extent (elems[i], X_AXIS);
131 while (idx < extents.size () && extents[idx].is_empty ())
135 offsets.set_size (elems.size ());
136 for (int i = 0; i < offsets.size ();i++)
139 Real extra_right_space = 0.0;
141 while (idx < elems.size ())
143 int next_idx = idx + 1;
144 while (next_idx < elems.size ()
145 && extents[next_idx].is_empty ())
148 Grob *l = elems[idx];
151 if (next_idx < elems.size ())
157 Find the first grob with a space-alist entry.
159 extract_grob_set (l, "elements", elts);
161 for (int i = elts.size(); i--; )
166 && elt->get_property ("break-align-symbol")
167 == ly_symbol2scm ("left-edge"))
170 SCM l = elt->get_property ("space-alist");
178 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
181 We used to use #'cause to find out the symbol and the spacing
182 table, but that gets icky when that grob is suicided for some
187 extract_grob_set (r, "elements", elts);
188 for (int i = elts.size();
189 !scm_is_symbol (rsym) && i--;)
192 rsym = elt->get_property ("break-align-symbol");
196 if (rsym == ly_symbol2scm ("left-edge"))
200 if (scm_is_symbol (rsym))
201 entry = scm_assq (rsym, alist);
203 bool entry_found = scm_is_pair (entry);
207 if (scm_is_symbol (rsym))
208 sym_string = ly_symbol2string (rsym);
211 if (unsmob_grob (l->get_property ("cause")))
212 orig_string = unsmob_grob (l->get_property ("cause"))->name ();
214 programming_error (_f ("No spacing entry from %s to `%s'",
215 orig_string.to_str0 (),
216 sym_string.to_str0 ()));
220 SCM type = ly_symbol2scm ("extra-space");
224 entry = scm_cdr (entry);
226 distance = scm_to_double (scm_cdr (entry));
227 type = scm_car (entry);
232 if (type == ly_symbol2scm ("extra-space"))
233 offsets[next_idx] = extents[idx][RIGHT] + distance
234 - extents[next_idx][LEFT];
235 /* should probably junk minimum-space */
236 else if (type == ly_symbol2scm ("minimum-space"))
237 offsets[next_idx] = max (extents[idx][RIGHT], distance);
241 extra_right_space = distance;
248 Interval total_extent;
250 Real alignment_off = 0.0;
251 for (int i = 0; i < offsets.size (); i++)
255 alignment_off = -here;
256 total_extent.unite (extents[i] + here);
259 if (total_extent.is_empty ())
262 if (me->break_status_dir () == LEFT)
264 alignment_off = -total_extent[RIGHT] - extra_right_space;
266 else if (edge_idx < 0)
267 alignment_off = -total_extent[LEFT];
269 here = alignment_off;
270 for (int i = 0; i < offsets.size (); i++)
273 elems[i]->translate_axis (here, X_AXIS);
277 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
278 "Items that are aligned in prefatory matter.\n"
280 "The spacing of these items is controlled by the @code{space-alist}\n"
281 "property. It contains a list @code{break-align-symbol}s with a specification\n"
282 "of the associated space. The space specification can be "
284 "@item (minimum-space . @var{spc}))\n"
285 " Pad space until the distance is @var{spc}\n"
286 "@item (fixed-space . @var{spc})\n"
287 " Set a fixed space\n"
288 "@item (semi-fixed-space . @var{spc})\n"
289 " Set a space. Half of it is fixed and half is stretchable. \n"
290 "(does not work at start of line. fixme)\n"
291 "@item (extra-space . @var{spc})\n"
292 " Add @var{spc} amount of space.\n"
295 "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
296 "the first note on a line, and the next note halfway a line.\n"
298 "Rules for this spacing are much more complicated than this. \n"
299 "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
300 "break-align-symbol space-alist");
302 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
303 "The object that performs break aligment. See @ref{break-aligned-interface}.",
304 "positioning-done break-align-orders");