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"
12 #include "libc-extension.hh" // isinf
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_make_real (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 ();
53 me->set_property ("self-alignment-X", scm_int2num (RIGHT));
57 Force break alignment itself to be done first, in the case
59 return Self_alignment_interface::aligned_on_self (element_smob, axis);
63 This is tricky: we cannot modify 'elements, since callers are
64 iterating the same list. Reordering the list in-place, or resetting
65 'elements will skip elements in the loops of callers.
67 So we return the correct order as an array.
70 Break_align_interface::ordered_elements (Grob *grob)
72 Item *me = dynamic_cast<Item *> (grob);
73 SCM elts = me->get_property ("elements");
74 SCM order_vec = me->get_property ("break-align-orders");
75 if (!scm_is_vector (order_vec)
76 || scm_c_vector_length (order_vec) < 3)
77 return extract_grob_array (me, ly_symbol2scm ("elements"));
78 SCM order = scm_vector_ref (order_vec,
79 scm_int2num (me->break_status_dir () + 1));
82 Copy in order specified in BREAK-ALIGN-ORDER.
84 Link_array<Grob> new_elts;
85 for (; scm_is_pair (order); order = scm_cdr (order))
87 SCM sym = scm_car (order);
89 for (SCM s = elts; scm_is_pair (s); s = scm_cdr (s))
91 Grob *g = unsmob_grob (scm_car (s));
92 if (g && sym == g->get_property ("break-align-symbol"))
95 elts = scm_delq (g->self_scm (), elts);
103 Break_align_interface::add_element (Grob *me, Grob *toadd)
105 Axis_group_interface::add_element (me, toadd);
109 Break_align_interface::do_alignment (Grob *grob)
111 Item *me = dynamic_cast<Item *> (grob);
113 Link_array<Grob> elems = ordered_elements (me);
114 Array<Interval> extents;
116 int last_nonempty = -1;
117 for (int i = 0; i < elems.size (); i++)
119 Interval y = elems[i]->extent (elems[i], X_AXIS);
126 while (idx < extents.size () && extents[idx].is_empty ())
130 offsets.set_size (elems.size ());
131 for (int i = 0; i < offsets.size ();i++)
134 Real extra_right_space = 0.0;
136 while (idx < elems.size ())
138 int next_idx = idx + 1;
139 while (next_idx < elems.size ()
140 && extents[next_idx].is_empty ())
143 Grob *l = elems[idx];
146 if (next_idx < elems.size ())
152 Find the first grob with a space-alist entry.
154 for (SCM s = l->get_property ("elements");
155 scm_is_pair (s); s = scm_cdr (s))
157 Grob *elt = unsmob_grob (scm_car (s));
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
179 for (SCM s = r ? r->get_property ("elements") : SCM_EOL;
180 !scm_is_symbol (rsym) && scm_is_pair (s); s = scm_cdr (s))
182 Grob *elt = unsmob_grob (scm_car (s));
184 rsym = elt->get_property ("break-align-symbol");
187 if (rsym == ly_symbol2scm ("left-edge"))
191 if (scm_is_symbol (rsym))
192 entry = scm_assq (rsym, alist);
194 bool entry_found = scm_is_pair (entry);
198 if (scm_is_symbol (rsym))
199 sym_string = ly_symbol2string (rsym);
202 if (unsmob_grob (l->get_property ("cause")))
203 orig_string = unsmob_grob (l->get_property ("cause"))->name ();
205 programming_error (_f ("No spacing entry from %s to `%s'",
206 orig_string.to_str0 (),
207 sym_string.to_str0 ()));
211 SCM type = ly_symbol2scm ("extra-space");
215 entry = scm_cdr (entry);
217 distance = scm_to_double (scm_cdr (entry));
218 type = scm_car (entry);
223 if (type == ly_symbol2scm ("extra-space"))
224 offsets[next_idx] = extents[idx][RIGHT] + distance
225 - extents[next_idx][LEFT];
226 /* should probably junk minimum-space */
227 else if (type == ly_symbol2scm ("minimum-space"))
228 offsets[next_idx] = max (extents[idx][RIGHT], distance);
232 extra_right_space = distance;
239 Interval total_extent;
241 Real alignment_off = 0.0;
242 for (int i = 0; i < offsets.size (); i++)
246 alignment_off = -here;
247 total_extent.unite (extents[i] + here);
250 if (total_extent.is_empty ())
253 if (me->break_status_dir () == LEFT)
255 alignment_off = -total_extent[RIGHT] - extra_right_space;
257 else if (edge_idx < 0)
258 alignment_off = -total_extent[LEFT];
260 here = alignment_off;
261 for (int i = 0; i < offsets.size (); i++)
264 elems[i]->translate_axis (here, X_AXIS);
268 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
269 "Items that are aligned in prefatory matter.\n"
271 "The spacing of these items is controlled by the @code{space-alist}\n"
272 "property. It contains a list @code{break-align-symbol}s with a specification\n"
273 "of the associated space. The space specification can be "
275 "@item (minimum-space . @var{spc}))\n"
276 " Pad space until the distance is @var{spc}\n"
277 "@item (fixed-space . @var{spc})\n"
278 " Set a fixed space\n"
279 "@item (semi-fixed-space . @var{spc})\n"
280 " Set a space. Half of it is fixed and half is stretchable. \n"
281 "(does not work at start of line. fixme)\n"
282 "@item (extra-space . @var{spc})\n"
283 " Add @var{spc} amount of space.\n"
286 "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
287 "the first note on a line, and the next note halfway a line.\n"
289 "Rules for this spacing are much more complicated than this. \n"
290 "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
291 "break-align-symbol space-alist");
293 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
294 "The object that performs break aligment. See @ref{break-aligned-interface}.",
295 "positioning-done break-align-orders");