2 break-align-interface.cc -- implement Break_align_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include "break-align-interface.hh"
13 #include <libc-extension.hh> // isinf
15 #include "self-alignment-interface.hh"
16 #include "side-position-interface.hh"
17 #include "axis-group-interface.hh"
19 #include "dimensions.hh"
20 #include "output-def.hh"
21 #include "paper-column.hh"
22 #include "align-interface.hh"
24 MAKE_SCHEME_CALLBACK (Break_align_interface,alignment_callback,2);
27 Break_align_interface::alignment_callback (SCM element_smob, SCM axis)
29 Grob *me = unsmob_grob (element_smob);
30 Axis a = (Axis) scm_to_int (axis);
33 Grob *par = me->get_parent (a);
34 if (par && !to_boolean (par->get_property ("positioning-done")))
36 par->set_property ("positioning-done", SCM_BOOL_T);
37 Break_align_interface::do_alignment (par);
40 return scm_make_real (0);
43 MAKE_SCHEME_CALLBACK (Break_align_interface,self_align_callback,2);
45 Break_align_interface::self_align_callback (SCM element_smob, SCM axis)
47 Grob *me = unsmob_grob (element_smob);
48 Axis a = (Axis) scm_to_int (axis);
51 Item* item = dynamic_cast<Item*> (me);
52 Direction bsd = item->break_status_dir ();
55 me->set_property ("self-alignment-X", scm_int2num (RIGHT));
59 Force break alignment itself to be done first, in the case
61 return Self_alignment_interface::aligned_on_self (element_smob, axis);
66 This is tricky: we cannot modify 'elements, since callers are
67 iterating the same list. Reordering the list in-place, or resetting
68 'elements will skip elements in the loops of callers.
70 So we return the correct order as an array.
73 Break_align_interface::ordered_elements (Grob *grob)
75 Item *me = dynamic_cast<Item*> (grob);
76 SCM elts = me->get_property ("elements");
77 SCM order_vec = me->get_property ("break-align-orders");
78 if (!ly_c_vector_p (order_vec)
79 || ly_vector_length (order_vec) < 3)
80 return Pointer_group_interface__extract_grobs (me, (Grob*)0,
82 SCM order = scm_vector_ref (order_vec,
83 scm_int2num (me->break_status_dir() + 1));
87 Copy in order specified in BREAK-ALIGN-ORDER.
89 Link_array<Grob> new_elts;
90 for (; scm_is_pair (order); order = scm_cdr (order))
92 SCM sym = scm_car (order);
94 for (SCM s = elts; scm_is_pair (s); s = scm_cdr (s))
96 Grob *g = unsmob_grob (scm_car (s));
97 if (g && sym == g->get_property ("break-align-symbol"))
100 elts = scm_delq (g->self_scm (), elts);
108 Break_align_interface::add_element (Grob*me, Grob *toadd)
110 Axis_group_interface::add_element (me, toadd);
114 Break_align_interface::do_alignment (Grob *grob)
116 Item * me = dynamic_cast<Item*> (grob);
119 Link_array<Grob> elems = ordered_elements (me);
120 Array<Interval> extents;
122 int last_nonempty = -1;
123 for (int i = 0; i < elems.size (); i++)
125 Interval y = elems[i]->extent (elems[i], X_AXIS);
132 while (idx < extents.size () && extents[idx].is_empty ())
136 offsets.set_size (elems.size ());
137 for (int i = 0; i < offsets.size ();i ++)
141 Real extra_right_space = 0.0;
143 while (idx < elems.size ())
145 int next_idx = idx+1;
146 while (next_idx < elems.size () &&
147 extents[next_idx].is_empty () )
150 Grob *l = elems[idx];
153 if (next_idx < elems.size ())
160 Find the first grob with a space-alist entry.
162 for (SCM s = l->get_property ("elements");
163 scm_is_pair (s) ; s = scm_cdr (s))
165 Grob *elt = unsmob_grob (scm_car (s));
168 && elt->get_property ("break-align-symbol")
169 == ly_symbol2scm ( "left-edge"))
172 SCM l = elt->get_property ("space-alist");
180 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
183 We used to use #'cause to find out the symbol and the spacing
184 table, but that gets icky when that grob is suicided for some
187 for (SCM s = r ? r->get_property ("elements") : SCM_EOL;
188 !scm_is_symbol (rsym) && scm_is_pair (s); s = scm_cdr (s))
190 Grob * elt = unsmob_grob (scm_car (s));
192 rsym = elt->get_property ("break-align-symbol");
195 if (rsym == ly_symbol2scm ("left-edge"))
199 if (scm_is_symbol (rsym))
200 entry = scm_assq (rsym, alist);
202 bool entry_found = scm_is_pair (entry);
206 if (scm_is_symbol (rsym))
207 sym_string = ly_symbol2string (rsym);
210 if (unsmob_grob (l->get_property ("cause")))
211 orig_string = unsmob_grob (l->get_property ("cause"))->name ();
213 programming_error (_f ("No spacing entry from %s to `%s'",
214 orig_string.to_str0 (),
215 sym_string.to_str0 ()));
219 SCM type = ly_symbol2scm ("extra-space");
223 entry = scm_cdr (entry);
225 distance = scm_to_double (scm_cdr (entry));
226 type = scm_car (entry) ;
231 if (type == ly_symbol2scm ("extra-space"))
232 offsets[next_idx] = extents[idx][RIGHT] + distance
233 - extents[next_idx][LEFT];
234 /* should probably junk minimum-space */
235 else if (type == ly_symbol2scm ("minimum-space"))
236 offsets[next_idx] = extents[idx][RIGHT] >? distance;
240 extra_right_space = distance;
247 Interval total_extent;
249 Real alignment_off = 0.0;
250 for (int i = 0 ; i < offsets.size (); i++)
254 alignment_off = -here;
255 total_extent.unite (extents[i] + here);
259 if (me->break_status_dir () == LEFT)
261 alignment_off = - total_extent[RIGHT] - extra_right_space;
263 else if (edge_idx < 0)
264 alignment_off = -total_extent[LEFT];
266 here = alignment_off;
267 for (int i = 0 ; i < offsets.size (); i++)
270 elems[i]->translate_axis (here, X_AXIS);
275 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
276 "Items that are aligned in prefatory matter.\n"
278 "The spacing of these items is controlled by the @code{space-alist}\n"
279 "property. It contains a list @code{break-align-symbol}s with a specification\n"
280 "of the associated space. The space specification can be "
282 "@item (minimum-space . @var{spc}))\n"
283 " Pad space until the distance is @var{spc}\n"
284 "@item (fixed-space . @var{spc})\n"
285 " Set a fixed space\n"
286 "@item (semi-fixed-space . @var{spc})\n"
287 " Set a space. Half of it is fixed and half is stretchable. \n"
288 "(does not work at start of line. fixme)\n"
289 "@item (extra-space . @var{spc})\n"
290 " Add @var{spc} amount of space.\n"
293 "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
294 "the first note on a line, and the next note halfway a line.\n"
296 "Rules for this spacing are much more complicated than this. \n"
297 "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
298 "break-align-symbol space-alist");
300 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
301 "The object that performs break aligment. See @ref{break-aligned-interface}.",
302 "positioning-done break-align-orders");