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>
10 #include "break-align-interface.hh"
12 #include "pointer-group-interface.hh"
13 #include "self-alignment-interface.hh"
14 #include "side-position-interface.hh"
15 #include "axis-group-interface.hh"
17 #include "dimensions.hh"
18 #include "output-def.hh"
19 #include "paper-column.hh"
20 #include "align-interface.hh"
22 MAKE_SCHEME_CALLBACK (Break_align_interface, alignment_callback, 2);
24 Break_align_interface::alignment_callback (SCM element_smob, SCM axis)
26 Grob *me = unsmob_grob (element_smob);
27 Axis a = (Axis) scm_to_int (axis);
30 Grob *par = me->get_parent (a);
31 if (par && !to_boolean (par->get_property ("positioning-done")))
33 par->set_property ("positioning-done", SCM_BOOL_T);
34 Break_align_interface::do_alignment (par);
37 return scm_from_double (0);
40 MAKE_SCHEME_CALLBACK (Break_align_interface, self_align_callback, 2);
42 Break_align_interface::self_align_callback (SCM element_smob, SCM axis)
44 Grob *me = unsmob_grob (element_smob);
46 assert (scm_to_int (axis) == X_AXIS);
48 Item *item = dynamic_cast<Item *> (me);
49 Direction bsd = item->break_status_dir ();
51 me->set_property ("self-alignment-X", scm_from_int (RIGHT));
54 Force break alignment itself to be done first, in the case
56 return Self_alignment_interface::aligned_on_self (element_smob, axis);
60 This is tricky: we cannot modify 'elements, since callers are
61 iterating the same list. Reordering the list in-place, or resetting
62 'elements will skip elements in the loops of callers.
64 So we return the correct order as an array.
67 Break_align_interface::ordered_elements (Grob *grob)
69 Item *me = dynamic_cast<Item *> (grob);
70 extract_grob_set (me, "elements", elts);
72 SCM order_vec = me->get_property ("break-align-orders");
73 if (!scm_is_vector (order_vec)
74 || scm_c_vector_length (order_vec) < 3)
77 Link_array<Grob> writable_elts (elts);
78 SCM order = scm_vector_ref (order_vec,
79 scm_from_int (me->break_status_dir () + 1));
82 Copy in order specified in BREAK-ALIGN-ORDER.
84 Link_array<Grob> new_elts;
85 for (; scm_is_pair (order); order = scm_cdr (order))
87 SCM sym = scm_car (order);
89 for (int i = writable_elts.size (); i--;)
91 Grob *g = writable_elts[i];
92 if (g && sym == g->get_property ("break-align-symbol"))
95 writable_elts.del (i);
104 Break_align_interface::add_element (Grob *me, Grob *toadd)
106 Axis_group_interface::add_element (me, toadd);
110 Break_align_interface::do_alignment (Grob *grob)
112 Item *me = dynamic_cast<Item *> (grob);
114 Link_array<Grob> elems = ordered_elements (me);
115 Array<Interval> extents;
117 int last_nonempty = -1;
118 for (int i = 0; i < elems.size (); i++)
120 Interval y = elems[i]->extent (elems[i], X_AXIS);
127 while (idx < extents.size () && extents[idx].is_empty ())
131 offsets.set_size (elems.size ());
132 for (int i = 0; i < offsets.size ();i++)
135 Real extra_right_space = 0.0;
137 while (idx < elems.size ())
139 int next_idx = idx + 1;
140 while (next_idx < elems.size ()
141 && extents[next_idx].is_empty ())
144 Grob *l = elems[idx];
147 if (next_idx < elems.size ())
153 Find the first grob with a space-alist entry.
155 extract_grob_set (l, "elements", elts);
157 for (int i = elts.size (); i--;)
162 && elt->get_property ("break-align-symbol")
163 == ly_symbol2scm ("left-edge"))
166 SCM l = elt->get_property ("space-alist");
174 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
177 We used to use #'cause to find out the symbol and the spacing
178 table, but that gets icky when that grob is suicided for some
183 extract_grob_set (r, "elements", elts);
184 for (int i = elts.size ();
185 !scm_is_symbol (rsym) && i--;)
188 rsym = elt->get_property ("break-align-symbol");
192 if (rsym == ly_symbol2scm ("left-edge"))
196 if (scm_is_symbol (rsym))
197 entry = scm_assq (rsym, alist);
199 bool entry_found = scm_is_pair (entry);
203 if (scm_is_symbol (rsym))
204 sym_string = ly_symbol2string (rsym);
207 if (unsmob_grob (l->get_property ("cause")))
208 orig_string = unsmob_grob (l->get_property ("cause"))->name ();
210 programming_error (_f ("No spacing entry from %s to `%s'",
211 orig_string.to_str0 (),
212 sym_string.to_str0 ()));
216 SCM type = ly_symbol2scm ("extra-space");
220 entry = scm_cdr (entry);
222 distance = scm_to_double (scm_cdr (entry));
223 type = scm_car (entry);
228 if (type == ly_symbol2scm ("extra-space"))
229 offsets[next_idx] = extents[idx][RIGHT] + distance
230 - extents[next_idx][LEFT];
231 /* should probably junk minimum-space */
232 else if (type == ly_symbol2scm ("minimum-space"))
233 offsets[next_idx] = max (extents[idx][RIGHT], distance);
236 extra_right_space = distance;
242 Interval total_extent;
244 Real alignment_off = 0.0;
245 for (int i = 0; i < offsets.size (); i++)
249 alignment_off = -here;
250 total_extent.unite (extents[i] + here);
253 if (total_extent.is_empty ())
256 if (me->break_status_dir () == LEFT)
257 alignment_off = -total_extent[RIGHT] - extra_right_space;
258 else if (edge_idx < 0)
259 alignment_off = -total_extent[LEFT];
261 here = alignment_off;
262 for (int i = 0; i < offsets.size (); i++)
265 elems[i]->translate_axis (here, X_AXIS);
269 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
270 "Items that are aligned in prefatory matter.\n"
272 "The spacing of these items is controlled by the @code{space-alist}\n"
273 "property. It contains a list @code{break-align-symbol}s with a specification\n"
274 "of the associated space. The space specification can be "
276 "@item (minimum-space . @var{spc}))\n"
277 " Pad space until the distance is @var{spc}\n"
278 "@item (fixed-space . @var{spc})\n"
279 " Set a fixed space\n"
280 "@item (semi-fixed-space . @var{spc})\n"
281 " Set a space. Half of it is fixed and half is stretchable. \n"
282 "(does not work at start of line. fixme)\n"
283 "@item (extra-space . @var{spc})\n"
284 " Add @var{spc} amount of space.\n"
287 "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
288 "the first note on a line, and the next note halfway a line.\n"
290 "Rules for this spacing are much more complicated than this. \n"
291 "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
292 "break-align-symbol space-alist");
294 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
295 "The object that performs break aligment. See @ref{break-aligned-interface}.",
296 "positioning-done break-align-orders");