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 "pointer-group-interface.hh"
14 #include "self-alignment-interface.hh"
15 #include "side-position-interface.hh"
16 #include "axis-group-interface.hh"
18 #include "dimensions.hh"
19 #include "output-def.hh"
20 #include "paper-column.hh"
21 #include "align-interface.hh"
23 MAKE_SCHEME_CALLBACK (Break_align_interface, alignment_callback, 2);
25 Break_align_interface::alignment_callback (SCM element_smob, SCM axis)
27 Grob *me = unsmob_grob (element_smob);
28 Axis a = (Axis) scm_to_int (axis);
31 Grob *par = me->get_parent (a);
32 if (par && !to_boolean (par->get_property ("positioning-done")))
34 par->set_property ("positioning-done", SCM_BOOL_T);
35 Break_align_interface::do_alignment (par);
38 return scm_from_double (0);
41 MAKE_SCHEME_CALLBACK (Break_align_interface, self_align_callback, 2);
43 Break_align_interface::self_align_callback (SCM element_smob, SCM axis)
45 Grob *me = unsmob_grob (element_smob);
47 assert (scm_to_int (axis) == X_AXIS);
49 Item *item = dynamic_cast<Item *> (me);
50 Direction bsd = item->break_status_dir ();
52 me->set_property ("self-alignment-X", scm_from_int (RIGHT));
55 Force break alignment itself to be done first, in the case
57 return Self_alignment_interface::aligned_on_self (element_smob, axis);
61 This is tricky: we cannot modify 'elements, since callers are
62 iterating the same list. Reordering the list in-place, or resetting
63 'elements will skip elements in the loops of callers.
65 So we return the correct order as an array.
68 Break_align_interface::ordered_elements (Grob *grob)
70 Item *me = dynamic_cast<Item *> (grob);
71 extract_grob_set (me, "elements", elts);
73 SCM order_vec = me->get_property ("break-align-orders");
74 if (!scm_is_vector (order_vec)
75 || scm_c_vector_length (order_vec) < 3)
78 Link_array<Grob> writable_elts (elts);
79 SCM order = scm_vector_ref (order_vec,
80 scm_from_int (me->break_status_dir () + 1));
83 Copy in order specified in BREAK-ALIGN-ORDER.
85 Link_array<Grob> new_elts;
86 for (; scm_is_pair (order); order = scm_cdr (order))
88 SCM sym = scm_car (order);
90 for (int i = writable_elts.size (); i--;)
92 Grob *g = writable_elts[i];
93 if (g && sym == g->get_property ("break-align-symbol"))
96 writable_elts.del (i);
105 Break_align_interface::add_element (Grob *me, Grob *toadd)
107 Axis_group_interface::add_element (me, toadd);
111 Break_align_interface::do_alignment (Grob *grob)
113 Item *me = dynamic_cast<Item *> (grob);
115 Link_array<Grob> elems = ordered_elements (me);
116 Array<Interval> extents;
118 int last_nonempty = -1;
119 for (int i = 0; i < elems.size (); i++)
121 Interval y = elems[i]->extent (elems[i], X_AXIS);
128 while (idx < extents.size () && extents[idx].is_empty ())
132 offsets.set_size (elems.size ());
133 for (int i = 0; i < offsets.size ();i++)
136 Real extra_right_space = 0.0;
138 while (idx < elems.size ())
140 int next_idx = idx + 1;
141 while (next_idx < elems.size ()
142 && extents[next_idx].is_empty ())
145 Grob *l = elems[idx];
148 if (next_idx < elems.size ())
154 Find the first grob with a space-alist entry.
156 extract_grob_set (l, "elements", elts);
158 for (int i = elts.size (); i--;)
163 && elt->get_property ("break-align-symbol")
164 == ly_symbol2scm ("left-edge"))
167 SCM l = elt->get_property ("space-alist");
175 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
178 We used to use #'cause to find out the symbol and the spacing
179 table, but that gets icky when that grob is suicided for some
184 extract_grob_set (r, "elements", elts);
185 for (int i = elts.size ();
186 !scm_is_symbol (rsym) && i--;)
189 rsym = elt->get_property ("break-align-symbol");
193 if (rsym == ly_symbol2scm ("left-edge"))
197 if (scm_is_symbol (rsym))
198 entry = scm_assq (rsym, alist);
200 bool entry_found = scm_is_pair (entry);
204 if (scm_is_symbol (rsym))
205 sym_string = ly_symbol2string (rsym);
208 if (unsmob_grob (l->get_property ("cause")))
209 orig_string = unsmob_grob (l->get_property ("cause"))->name ();
211 programming_error (_f ("No spacing entry from %s to `%s'",
212 orig_string.to_str0 (),
213 sym_string.to_str0 ()));
217 SCM type = ly_symbol2scm ("extra-space");
221 entry = scm_cdr (entry);
223 distance = scm_to_double (scm_cdr (entry));
224 type = scm_car (entry);
229 if (type == ly_symbol2scm ("extra-space"))
230 offsets[next_idx] = extents[idx][RIGHT] + distance
231 - extents[next_idx][LEFT];
232 /* should probably junk minimum-space */
233 else if (type == ly_symbol2scm ("minimum-space"))
234 offsets[next_idx] = max (extents[idx][RIGHT], distance);
237 extra_right_space = distance;
243 Interval total_extent;
245 Real alignment_off = 0.0;
246 for (int i = 0; i < offsets.size (); i++)
250 alignment_off = -here;
251 total_extent.unite (extents[i] + here);
254 if (total_extent.is_empty ())
257 if (me->break_status_dir () == LEFT)
258 alignment_off = -total_extent[RIGHT] - extra_right_space;
259 else if (edge_idx < 0)
260 alignment_off = -total_extent[LEFT];
262 here = alignment_off;
263 for (int i = 0; i < offsets.size (); i++)
266 elems[i]->translate_axis (here, X_AXIS);
270 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
271 "Items that are aligned in prefatory matter.\n"
273 "The spacing of these items is controlled by the @code{space-alist}\n"
274 "property. It contains a list @code{break-align-symbol}s with a specification\n"
275 "of the associated space. The space specification can be "
277 "@item (minimum-space . @var{spc}))\n"
278 " Pad space until the distance is @var{spc}\n"
279 "@item (fixed-space . @var{spc})\n"
280 " Set a fixed space\n"
281 "@item (semi-fixed-space . @var{spc})\n"
282 " Set a space. Half of it is fixed and half is stretchable. \n"
283 "(does not work at start of line. fixme)\n"
284 "@item (extra-space . @var{spc})\n"
285 " Add @var{spc} amount of space.\n"
288 "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
289 "the first note on a line, and the next note halfway a line.\n"
291 "Rules for this spacing are much more complicated than this. \n"
292 "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
293 "break-align-symbol space-alist");
295 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
296 "The object that performs break aligment. See @ref{break-aligned-interface}.",
297 "positioning-done break-align-orders");