2 break-align-interface.cc -- implement Break_alignment_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 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"
26 This is tricky: we cannot modify 'elements, since callers are
27 iterating the same list. Reordering the list in-place, or resetting
28 'elements will skip elements in the loops of callers.
30 So we return the correct order as an array.
33 Break_alignment_interface::break_align_order (Item *me)
35 SCM order_vec = me->get_property ("break-align-orders");
36 if (!scm_is_vector (order_vec)
37 || scm_c_vector_length (order_vec) < 3)
40 SCM order = scm_vector_ref (order_vec,
41 scm_from_int (me->break_status_dir () + 1));
49 Break_alignment_interface::ordered_elements (Grob *grob)
51 Item *me = dynamic_cast<Item *> (grob);
52 extract_grob_set (me, "elements", elts);
55 SCM order = break_align_order (me);
57 if (order == SCM_BOOL_F)
60 vector<Grob*> writable_elts (elts);
62 Copy in order specified in BREAK-ALIGN-ORDER.
64 vector<Grob*> new_elts;
65 for (; scm_is_pair (order); order = scm_cdr (order))
67 SCM sym = scm_car (order);
69 for (vsize i = writable_elts.size (); i--;)
71 Grob *g = writable_elts[i];
72 if (g && sym == g->get_property ("break-align-symbol"))
74 new_elts.push_back (g);
75 writable_elts.erase (writable_elts.begin () + i);
84 Break_alignment_interface::add_element (Grob *me, Grob *toadd)
86 Align_interface::add_element (me, toadd);
89 MAKE_SCHEME_CALLBACK(Break_alignment_interface, calc_positioning_done, 1)
91 Break_alignment_interface::calc_positioning_done (SCM smob)
93 Grob *grob = unsmob_grob (smob);
94 Item *me = dynamic_cast<Item *> (grob);
96 vector<Grob*> elems = ordered_elements (me);
97 vector<Interval> extents;
99 int last_nonempty = -1;
100 for (vsize i = 0; i < elems.size (); i++)
102 Interval y = elems[i]->extent (elems[i], X_AXIS);
103 extents.push_back (y);
109 while (idx < extents.size () && extents[idx].is_empty ())
112 vector<Real> offsets;
113 offsets.resize (elems.size ());
114 for (vsize i = 0; i < offsets.size ();i++)
117 Real extra_right_space = 0.0;
118 vsize edge_idx = VPOS;
119 while (idx < elems.size ())
121 vsize next_idx = idx + 1;
122 while (next_idx < elems.size ()
123 && extents[next_idx].is_empty ())
126 Grob *l = elems[idx];
129 if (next_idx < elems.size ())
135 Find the first grob with a space-alist entry.
137 extract_grob_set (l, "elements", elts);
139 for (vsize i = elts.size (); i--;)
144 && (elt->get_property ("break-align-symbol")
145 == ly_symbol2scm ("left-edge")))
148 SCM l = elt->get_property ("space-alist");
156 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
159 We used to use #'cause to find out the symbol and the spacing
160 table, but that gets icky when that grob is suicided for some
165 extract_grob_set (r, "elements", elts);
166 for (vsize i = elts.size ();
167 !scm_is_symbol (rsym) && i--;)
170 rsym = elt->get_property ("break-align-symbol");
174 if (rsym == ly_symbol2scm ("left-edge"))
178 if (scm_is_symbol (rsym))
179 entry = scm_assq (rsym, alist);
181 bool entry_found = scm_is_pair (entry);
185 if (scm_is_symbol (rsym))
186 sym_string = ly_symbol2string (rsym);
189 if (unsmob_grob (l->get_property ("cause")))
190 orig_string = unsmob_grob (l->get_property ("cause"))->name ();
192 programming_error (_f ("No spacing entry from %s to `%s'",
193 orig_string.c_str (),
194 sym_string.c_str ()));
198 SCM type = ly_symbol2scm ("extra-space");
202 entry = scm_cdr (entry);
204 distance = scm_to_double (scm_cdr (entry));
205 type = scm_car (entry);
210 if (type == ly_symbol2scm ("extra-space"))
211 offsets[next_idx] = extents[idx][RIGHT] + distance
212 - extents[next_idx][LEFT];
213 /* should probably junk minimum-space */
214 else if (type == ly_symbol2scm ("minimum-space"))
215 offsets[next_idx] = max (extents[idx][RIGHT], distance);
219 extra_right_space = distance;
220 if (idx < offsets.size() - 1)
221 offsets[idx+1] = extents[idx][RIGHT] + distance;
228 Interval total_extent;
230 Real alignment_off = 0.0;
231 for (vsize i = 0; i < offsets.size (); i++)
235 alignment_off = -here;
236 total_extent.unite (extents[i] + here);
239 if (total_extent.is_empty ())
242 if (me->break_status_dir () == LEFT)
243 alignment_off = -total_extent[RIGHT] - extra_right_space;
244 else if (edge_idx == VPOS)
245 alignment_off = -total_extent[LEFT];
247 here = alignment_off;
248 for (vsize i = 0; i < offsets.size (); i++)
251 elems[i]->translate_axis (here, X_AXIS);
259 MAKE_SCHEME_CALLBACK(Break_alignable_interface, self_align_callback, 1)
261 Break_alignable_interface::self_align_callback (SCM grob)
263 Grob *me = unsmob_grob (grob);
264 Item *alignment = dynamic_cast<Item*> (me->get_parent (X_AXIS));
265 if (!Break_alignment_interface::has_interface (alignment))
266 return scm_from_int (0);
268 SCM my_align = me->get_property ("break-align-symbol");
269 SCM order = Break_alignment_interface::break_align_order (alignment);
271 vector<Grob*> elements = Break_alignment_interface::ordered_elements (alignment);
272 if (elements.size () == 0)
273 return scm_from_int (0);
275 int last_idx_found = -1;
277 for (SCM s = order; scm_is_pair (s); s = scm_cdr (s))
279 if (i < elements.size ()
280 && elements[i]->get_property ("break-align-symbol") == scm_car (s))
286 if (scm_car (s) == my_align)
290 Direction which_edge = LEFT;
291 if (vsize (last_idx_found + 1) < elements.size())
296 Grob *common = me->common_refpoint (elements[last_idx_found], X_AXIS);
298 return scm_from_double (robust_relative_extent (elements[last_idx_found], common, X_AXIS)[which_edge]
299 - me->relative_coordinate (common, X_AXIS));
302 ADD_INTERFACE (Break_alignable_interface,
303 "Object that is aligned on a break aligment. ",
306 "break-align-symbol "
311 ADD_INTERFACE (Break_aligned_interface,
312 "Items that are aligned in prefatory matter.\n"
314 "The spacing of these items is controlled by the @code{space-alist}\n"
315 "property. It contains a list @code{break-align-symbol}s with a specification\n"
316 "of the associated space. The space specification can be "
318 "@item (minimum-space . @var{spc}))\n"
319 " Pad space until the distance is @var{spc}\n"
320 "@item (fixed-space . @var{spc})\n"
321 " Set a fixed space\n"
322 "@item (semi-fixed-space . @var{spc})\n"
323 " Set a space. Half of it is fixed and half is stretchable. \n"
324 "(does not work at start of line. fixme)\n"
325 "@item (extra-space . @var{spc})\n"
326 " Add @var{spc} amount of space.\n"
329 "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
330 "the first note on a line, and the next note halfway a line.\n"
332 "Rules for this spacing are much more complicated than this. \n"
333 "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
336 "break-align-symbol "
340 ADD_INTERFACE (Break_alignment_interface,
341 "The object that performs break aligment. See @ref{break-aligned-interface}.",
345 "break-align-orders");