2 break-align-interface.cc -- implement Break_align_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.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"
23 MAKE_SCHEME_CALLBACK (Break_align_interface, self_align_callback, 1);
25 Break_align_interface::self_align_callback (SCM smob)
27 Grob *me = unsmob_grob (smob);
29 Item *item = dynamic_cast<Item *> (me);
30 Direction bsd = item->break_status_dir ();
32 me->set_property ("self-alignment-X", scm_from_int (RIGHT));
35 Force break alignment itself to be done first, in the case
37 return Self_alignment_interface::aligned_on_self (me, X_AXIS);
41 This is tricky: we cannot modify 'elements, since callers are
42 iterating the same list. Reordering the list in-place, or resetting
43 'elements will skip elements in the loops of callers.
45 So we return the correct order as an array.
48 Break_align_interface::ordered_elements (Grob *grob)
50 Item *me = dynamic_cast<Item *> (grob);
51 extract_grob_set (me, "elements", elts);
53 SCM order_vec = me->get_property ("break-align-orders");
54 if (!scm_is_vector (order_vec)
55 || scm_c_vector_length (order_vec) < 3)
58 Link_array<Grob> writable_elts (elts);
59 SCM order = scm_vector_ref (order_vec,
60 scm_from_int (me->break_status_dir () + 1));
63 Copy in order specified in BREAK-ALIGN-ORDER.
65 Link_array<Grob> new_elts;
66 for (; scm_is_pair (order); order = scm_cdr (order))
68 SCM sym = scm_car (order);
70 for (int i = writable_elts.size (); i--;)
72 Grob *g = writable_elts[i];
73 if (g && sym == g->get_property ("break-align-symbol"))
76 writable_elts.del (i);
85 Break_align_interface::add_element (Grob *me, Grob *toadd)
87 Align_interface::add_element (me, toadd);
90 MAKE_SCHEME_CALLBACK(Break_align_interface, calc_positioning_done, 1)
92 Break_align_interface::calc_positioning_done (SCM smob)
94 Grob *grob = unsmob_grob (smob);
95 Item *me = dynamic_cast<Item *> (grob);
97 Link_array<Grob> elems = ordered_elements (me);
98 Array<Interval> extents;
100 int last_nonempty = -1;
101 for (int i = 0; i < elems.size (); i++)
103 Interval y = elems[i]->extent (elems[i], X_AXIS);
110 while (idx < extents.size () && extents[idx].is_empty ())
114 offsets.set_size (elems.size ());
115 for (int i = 0; i < offsets.size ();i++)
118 Real extra_right_space = 0.0;
120 while (idx < elems.size ())
122 int next_idx = idx + 1;
123 while (next_idx < elems.size ()
124 && extents[next_idx].is_empty ())
127 Grob *l = elems[idx];
130 if (next_idx < elems.size ())
136 Find the first grob with a space-alist entry.
138 extract_grob_set (l, "elements", elts);
140 for (int i = elts.size (); i--;)
145 && elt->get_property ("break-align-symbol")
146 == ly_symbol2scm ("left-edge"))
149 SCM l = elt->get_property ("space-alist");
157 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
160 We used to use #'cause to find out the symbol and the spacing
161 table, but that gets icky when that grob is suicided for some
166 extract_grob_set (r, "elements", elts);
167 for (int i = elts.size ();
168 !scm_is_symbol (rsym) && i--;)
171 rsym = elt->get_property ("break-align-symbol");
175 if (rsym == ly_symbol2scm ("left-edge"))
179 if (scm_is_symbol (rsym))
180 entry = scm_assq (rsym, alist);
182 bool entry_found = scm_is_pair (entry);
186 if (scm_is_symbol (rsym))
187 sym_string = ly_symbol2string (rsym);
190 if (unsmob_grob (l->get_property ("cause")))
191 orig_string = unsmob_grob (l->get_property ("cause"))->name ();
193 programming_error (_f ("No spacing entry from %s to `%s'",
194 orig_string.to_str0 (),
195 sym_string.to_str0 ()));
199 SCM type = ly_symbol2scm ("extra-space");
203 entry = scm_cdr (entry);
205 distance = scm_to_double (scm_cdr (entry));
206 type = scm_car (entry);
211 if (type == ly_symbol2scm ("extra-space"))
212 offsets[next_idx] = extents[idx][RIGHT] + distance
213 - extents[next_idx][LEFT];
214 /* should probably junk minimum-space */
215 else if (type == ly_symbol2scm ("minimum-space"))
216 offsets[next_idx] = max (extents[idx][RIGHT], distance);
219 extra_right_space = distance;
225 Interval total_extent;
227 Real alignment_off = 0.0;
228 for (int i = 0; i < offsets.size (); i++)
232 alignment_off = -here;
233 total_extent.unite (extents[i] + here);
236 if (total_extent.is_empty ())
239 if (me->break_status_dir () == LEFT)
240 alignment_off = -total_extent[RIGHT] - extra_right_space;
241 else if (edge_idx < 0)
242 alignment_off = -total_extent[LEFT];
244 here = alignment_off;
245 for (int i = 0; i < offsets.size (); i++)
248 elems[i]->translate_axis (here, X_AXIS);
254 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
255 "Items that are aligned in prefatory matter.\n"
257 "The spacing of these items is controlled by the @code{space-alist}\n"
258 "property. It contains a list @code{break-align-symbol}s with a specification\n"
259 "of the associated space. The space specification can be "
261 "@item (minimum-space . @var{spc}))\n"
262 " Pad space until the distance is @var{spc}\n"
263 "@item (fixed-space . @var{spc})\n"
264 " Set a fixed space\n"
265 "@item (semi-fixed-space . @var{spc})\n"
266 " Set a space. Half of it is fixed and half is stretchable. \n"
267 "(does not work at start of line. fixme)\n"
268 "@item (extra-space . @var{spc})\n"
269 " Add @var{spc} amount of space.\n"
272 "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
273 "the first note on a line, and the next note halfway a line.\n"
275 "Rules for this spacing are much more complicated than this. \n"
276 "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
279 "break-align-symbol "
283 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
284 "The object that performs break aligment. See @ref{break-aligned-interface}.",
288 "break-align-orders");