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::break_align_order (Item *me)
51 SCM order_vec = me->get_property ("break-align-orders");
52 if (!scm_is_vector (order_vec)
53 || scm_c_vector_length (order_vec) < 3)
56 SCM order = scm_vector_ref (order_vec,
57 scm_from_int (me->break_status_dir () + 1));
65 Break_align_interface::ordered_elements (Grob *grob)
67 Item *me = dynamic_cast<Item *> (grob);
68 extract_grob_set (me, "elements", elts);
71 SCM order = break_align_order (me);
73 if (order == SCM_BOOL_F)
76 vector<Grob*> writable_elts (elts);
78 Copy in order specified in BREAK-ALIGN-ORDER.
80 vector<Grob*> new_elts;
81 for (; scm_is_pair (order); order = scm_cdr (order))
83 SCM sym = scm_car (order);
85 for (vsize i = writable_elts.size (); i--;)
87 Grob *g = writable_elts[i];
88 if (g && sym == g->get_property ("break-align-symbol"))
90 new_elts.push_back (g);
91 writable_elts.erase (writable_elts.begin () + i);
100 Break_align_interface::add_element (Grob *me, Grob *toadd)
102 Align_interface::add_element (me, toadd);
105 MAKE_SCHEME_CALLBACK(Break_align_interface, calc_positioning_done, 1)
107 Break_align_interface::calc_positioning_done (SCM smob)
109 Grob *grob = unsmob_grob (smob);
110 Item *me = dynamic_cast<Item *> (grob);
112 vector<Grob*> elems = ordered_elements (me);
113 vector<Interval> extents;
115 int last_nonempty = -1;
116 for (vsize i = 0; i < elems.size (); i++)
118 Interval y = elems[i]->extent (elems[i], X_AXIS);
119 extents.push_back (y);
125 while (idx < extents.size () && extents[idx].is_empty ())
128 vector<Real> offsets;
129 offsets.resize (elems.size ());
130 for (vsize i = 0; i < offsets.size ();i++)
133 Real extra_right_space = 0.0;
134 vsize edge_idx = VPOS;
135 while (idx < elems.size ())
137 vsize next_idx = idx + 1;
138 while (next_idx < elems.size ()
139 && extents[next_idx].is_empty ())
142 Grob *l = elems[idx];
145 if (next_idx < elems.size ())
151 Find the first grob with a space-alist entry.
153 extract_grob_set (l, "elements", elts);
155 for (vsize i = elts.size (); i--;)
160 && (elt->get_property ("break-align-symbol")
161 == ly_symbol2scm ("left-edge")))
164 SCM l = elt->get_property ("space-alist");
172 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
175 We used to use #'cause to find out the symbol and the spacing
176 table, but that gets icky when that grob is suicided for some
181 extract_grob_set (r, "elements", elts);
182 for (vsize i = elts.size ();
183 !scm_is_symbol (rsym) && i--;)
186 rsym = elt->get_property ("break-align-symbol");
190 if (rsym == ly_symbol2scm ("left-edge"))
194 if (scm_is_symbol (rsym))
195 entry = scm_assq (rsym, alist);
197 bool entry_found = scm_is_pair (entry);
201 if (scm_is_symbol (rsym))
202 sym_string = ly_symbol2string (rsym);
205 if (unsmob_grob (l->get_property ("cause")))
206 orig_string = unsmob_grob (l->get_property ("cause"))->name ();
208 programming_error (_f ("No spacing entry from %s to `%s'",
209 orig_string.c_str (),
210 sym_string.c_str ()));
214 SCM type = ly_symbol2scm ("extra-space");
218 entry = scm_cdr (entry);
220 distance = scm_to_double (scm_cdr (entry));
221 type = scm_car (entry);
226 if (type == ly_symbol2scm ("extra-space"))
227 offsets[next_idx] = extents[idx][RIGHT] + distance
228 - extents[next_idx][LEFT];
229 /* should probably junk minimum-space */
230 else if (type == ly_symbol2scm ("minimum-space"))
231 offsets[next_idx] = max (extents[idx][RIGHT], distance);
235 extra_right_space = distance;
236 if (idx < offsets.size() - 1)
237 offsets[idx+1] = extents[idx][RIGHT] + distance;
244 Interval total_extent;
246 Real alignment_off = 0.0;
247 for (vsize i = 0; i < offsets.size (); i++)
251 alignment_off = -here;
252 total_extent.unite (extents[i] + here);
255 if (total_extent.is_empty ())
258 if (me->break_status_dir () == LEFT)
259 alignment_off = -total_extent[RIGHT] - extra_right_space;
260 else if (edge_idx == VPOS)
261 alignment_off = -total_extent[LEFT];
263 here = alignment_off;
264 for (vsize i = 0; i < offsets.size (); i++)
267 elems[i]->translate_axis (here, X_AXIS);
273 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
274 "Items that are aligned in prefatory matter.\n"
276 "The spacing of these items is controlled by the @code{space-alist}\n"
277 "property. It contains a list @code{break-align-symbol}s with a specification\n"
278 "of the associated space. The space specification can be "
280 "@item (minimum-space . @var{spc}))\n"
281 " Pad space until the distance is @var{spc}\n"
282 "@item (fixed-space . @var{spc})\n"
283 " Set a fixed space\n"
284 "@item (semi-fixed-space . @var{spc})\n"
285 " Set a space. Half of it is fixed and half is stretchable. \n"
286 "(does not work at start of line. fixme)\n"
287 "@item (extra-space . @var{spc})\n"
288 " Add @var{spc} amount of space.\n"
291 "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
292 "the first note on a line, and the next note halfway a line.\n"
294 "Rules for this spacing are much more complicated than this. \n"
295 "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
298 "break-align-symbol "
302 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
303 "The object that performs break aligment. See @ref{break-aligned-interface}.",
307 "break-align-orders");
310 MAKE_SCHEME_CALLBACK(Break_alignment_align_interface, self_align_callback, 1)
312 Break_alignment_align_interface::self_align_callback (SCM grob)
314 Grob *me = unsmob_grob (grob);
315 Item *alignment = dynamic_cast<Item*> (me->get_parent (X_AXIS));
316 if (!Break_align_interface::has_interface (alignment))
317 return scm_from_int (0);
319 SCM my_align = me->get_property ("break-align-symbol");
320 SCM order = Break_align_interface::break_align_order (alignment);
322 vector<Grob*> elements = Break_align_interface::ordered_elements (alignment);
323 if (elements.size () == 0)
324 return scm_from_int (0);
326 int last_idx_found = -1;
328 for (SCM s = order; scm_is_pair (s); s = scm_cdr (s))
330 if (i < elements.size ()
331 && elements[i]->get_property ("break-align-symbol") == scm_car (s))
337 if (scm_car (s) == my_align)
341 Direction which_edge = LEFT;
342 if (vsize (last_idx_found + 1) < elements.size())
347 Grob *common = me->common_refpoint (elements[last_idx_found], X_AXIS);
349 return scm_from_double (robust_relative_extent (elements[last_idx_found], common, X_AXIS)[which_edge]
350 - me->relative_coordinate (common, X_AXIS));
353 ADD_INTERFACE (Break_alignment_align_interface, "break-alignment-align-interface",
354 "Object that is aligned on a break aligment. ",
357 "break-align-symbol "