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 "align-interface.hh"
13 #include "axis-group-interface.hh"
14 #include "dimensions.hh"
15 #include "international.hh"
16 #include "output-def.hh"
17 #include "paper-column.hh"
18 #include "pointer-group-interface.hh"
19 #include "self-alignment-interface.hh"
20 #include "side-position-interface.hh"
24 MAKE_SCHEME_CALLBACK (Break_align_interface, self_align_callback, 1);
26 Break_align_interface::self_align_callback (SCM smob)
28 Grob *me = unsmob_grob (smob);
30 Item *item = dynamic_cast<Item *> (me);
31 Direction bsd = item->break_status_dir ();
33 me->set_property ("self-alignment-X", scm_from_int (RIGHT));
36 Force break alignment itself to be done first, in the case
38 return Self_alignment_interface::aligned_on_self (me, X_AXIS);
42 This is tricky: we cannot modify 'elements, since callers are
43 iterating the same list. Reordering the list in-place, or resetting
44 'elements will skip elements in the loops of callers.
46 So we return the correct order as an array.
49 Break_align_interface::ordered_elements (Grob *grob)
51 Item *me = dynamic_cast<Item *> (grob);
52 extract_grob_set (me, "elements", elts);
54 SCM order_vec = me->get_property ("break-align-orders");
55 if (!scm_is_vector (order_vec)
56 || scm_c_vector_length (order_vec) < 3)
59 Link_array<Grob> writable_elts (elts);
60 SCM order = scm_vector_ref (order_vec,
61 scm_from_int (me->break_status_dir () + 1));
64 Copy in order specified in BREAK-ALIGN-ORDER.
66 Link_array<Grob> new_elts;
67 for (; scm_is_pair (order); order = scm_cdr (order))
69 SCM sym = scm_car (order);
71 for (int i = writable_elts.size (); i--;)
73 Grob *g = writable_elts[i];
74 if (g && sym == g->get_property ("break-align-symbol"))
77 writable_elts.del (i);
86 Break_align_interface::add_element (Grob *me, Grob *toadd)
88 Align_interface::add_element (me, toadd);
91 MAKE_SCHEME_CALLBACK(Break_align_interface, calc_positioning_done, 1)
93 Break_align_interface::calc_positioning_done (SCM smob)
95 Grob *grob = unsmob_grob (smob);
96 Item *me = dynamic_cast<Item *> (grob);
98 Link_array<Grob> elems = ordered_elements (me);
99 Array<Interval> extents;
101 int last_nonempty = -1;
102 for (int i = 0; i < elems.size (); i++)
104 Interval y = elems[i]->extent (elems[i], X_AXIS);
111 while (idx < extents.size () && extents[idx].is_empty ())
115 offsets.set_size (elems.size ());
116 for (int i = 0; i < offsets.size ();i++)
119 Real extra_right_space = 0.0;
121 while (idx < elems.size ())
123 int next_idx = idx + 1;
124 while (next_idx < elems.size ()
125 && extents[next_idx].is_empty ())
128 Grob *l = elems[idx];
131 if (next_idx < elems.size ())
137 Find the first grob with a space-alist entry.
139 extract_grob_set (l, "elements", elts);
141 for (int i = elts.size (); i--;)
146 && elt->get_property ("break-align-symbol")
147 == ly_symbol2scm ("left-edge"))
150 SCM l = elt->get_property ("space-alist");
158 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
161 We used to use #'cause to find out the symbol and the spacing
162 table, but that gets icky when that grob is suicided for some
167 extract_grob_set (r, "elements", elts);
168 for (int i = elts.size ();
169 !scm_is_symbol (rsym) && i--;)
172 rsym = elt->get_property ("break-align-symbol");
176 if (rsym == ly_symbol2scm ("left-edge"))
180 if (scm_is_symbol (rsym))
181 entry = scm_assq (rsym, alist);
183 bool entry_found = scm_is_pair (entry);
186 std::string sym_string;
187 if (scm_is_symbol (rsym))
188 sym_string = ly_symbol2string (rsym);
190 std::string orig_string;
191 if (unsmob_grob (l->get_property ("cause")))
192 orig_string = unsmob_grob (l->get_property ("cause"))->name ();
194 programming_error (_f ("No spacing entry from %s to `%s'",
195 orig_string.c_str (),
196 sym_string.c_str ()));
200 SCM type = ly_symbol2scm ("extra-space");
204 entry = scm_cdr (entry);
206 distance = scm_to_double (scm_cdr (entry));
207 type = scm_car (entry);
212 if (type == ly_symbol2scm ("extra-space"))
213 offsets[next_idx] = extents[idx][RIGHT] + distance
214 - extents[next_idx][LEFT];
215 /* should probably junk minimum-space */
216 else if (type == ly_symbol2scm ("minimum-space"))
217 offsets[next_idx] = max (extents[idx][RIGHT], distance);
220 extra_right_space = distance;
226 Interval total_extent;
228 Real alignment_off = 0.0;
229 for (int i = 0; i < offsets.size (); i++)
233 alignment_off = -here;
234 total_extent.unite (extents[i] + here);
237 if (total_extent.is_empty ())
240 if (me->break_status_dir () == LEFT)
241 alignment_off = -total_extent[RIGHT] - extra_right_space;
242 else if (edge_idx < 0)
243 alignment_off = -total_extent[LEFT];
245 here = alignment_off;
246 for (int i = 0; i < offsets.size (); i++)
249 elems[i]->translate_axis (here, X_AXIS);
255 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
256 "Items that are aligned in prefatory matter.\n"
258 "The spacing of these items is controlled by the @code{space-alist}\n"
259 "property. It contains a list @code{break-align-symbol}s with a specification\n"
260 "of the associated space. The space specification can be "
262 "@item (minimum-space . @var{spc}))\n"
263 " Pad space until the distance is @var{spc}\n"
264 "@item (fixed-space . @var{spc})\n"
265 " Set a fixed space\n"
266 "@item (semi-fixed-space . @var{spc})\n"
267 " Set a space. Half of it is fixed and half is stretchable. \n"
268 "(does not work at start of line. fixme)\n"
269 "@item (extra-space . @var{spc})\n"
270 " Add @var{spc} amount of space.\n"
273 "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
274 "the first note on a line, and the next note halfway a line.\n"
276 "Rules for this spacing are much more complicated than this. \n"
277 "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
280 "break-align-symbol "
284 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
285 "The object that performs break aligment. See @ref{break-aligned-interface}.",
289 "break-align-orders");