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);
26 Break_align_interface::alignment_callback (SCM element_smob, SCM axis)
28 Grob *me = unsmob_grob (element_smob);
29 Axis a = (Axis) scm_to_int (axis);
32 Grob *par = me->get_parent (a);
33 if (par && !to_boolean (par->get_property ("positioning-done")))
35 par->set_property ("positioning-done", SCM_BOOL_T);
36 Break_align_interface::do_alignment (par);
39 return scm_make_real (0);
42 MAKE_SCHEME_CALLBACK (Break_align_interface, self_align_callback, 2);
44 Break_align_interface::self_align_callback (SCM element_smob, SCM axis)
46 Grob *me = unsmob_grob (element_smob);
47 Axis a = (Axis) scm_to_int (axis);
50 Item* item = dynamic_cast<Item*> (me);
51 Direction bsd = item->break_status_dir ();
54 me->set_property ("self-alignment-X", scm_int2num (RIGHT));
58 Force break alignment itself to be done first, in the case
60 return Self_alignment_interface::aligned_on_self (element_smob, axis);
65 This is tricky: we cannot modify 'elements, since callers are
66 iterating the same list. Reordering the list in-place, or resetting
67 'elements will skip elements in the loops of callers.
69 So we return the correct order as an array.
72 Break_align_interface::ordered_elements (Grob *grob)
74 Item *me = dynamic_cast<Item*> (grob);
75 SCM elts = me->get_property ("elements");
76 SCM order_vec = me->get_property ("break-align-orders");
77 if (!scm_is_vector (order_vec)
78 || scm_c_vector_length (order_vec) < 3)
79 return Pointer_group_interface__extract_grobs (me, (Grob*)0,
81 SCM order = scm_vector_ref (order_vec,
82 scm_int2num (me->break_status_dir () + 1));
86 Copy in order specified in BREAK-ALIGN-ORDER.
88 Link_array<Grob> new_elts;
89 for (; scm_is_pair (order); order = scm_cdr (order))
91 SCM sym = scm_car (order);
93 for (SCM s = elts; scm_is_pair (s); s = scm_cdr (s))
95 Grob *g = unsmob_grob (scm_car (s));
96 if (g && sym == g->get_property ("break-align-symbol"))
99 elts = scm_delq (g->self_scm (), elts);
107 Break_align_interface::add_element (Grob *me, Grob *toadd)
109 Axis_group_interface::add_element (me, toadd);
113 Break_align_interface::do_alignment (Grob *grob)
115 Item * me = dynamic_cast<Item*> (grob);
118 Link_array<Grob> elems = ordered_elements (me);
119 Array<Interval> extents;
121 int last_nonempty = -1;
122 for (int i = 0; i < elems.size (); i++)
124 Interval y = elems[i]->extent (elems[i], X_AXIS);
131 while (idx < extents.size () && extents[idx].is_empty ())
135 offsets.set_size (elems.size ());
136 for (int i = 0; i < offsets.size ();i ++)
140 Real extra_right_space = 0.0;
142 while (idx < elems.size ())
144 int next_idx = idx+1;
145 while (next_idx < elems.size () &&
146 extents[next_idx].is_empty () )
149 Grob *l = elems[idx];
152 if (next_idx < elems.size ())
159 Find the first grob with a space-alist entry.
161 for (SCM s = l->get_property ("elements");
162 scm_is_pair (s) ; s = scm_cdr (s))
164 Grob *elt = unsmob_grob (scm_car (s));
167 && elt->get_property ("break-align-symbol")
168 == ly_symbol2scm ( "left-edge"))
171 SCM l = elt->get_property ("space-alist");
179 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
182 We used to use #'cause to find out the symbol and the spacing
183 table, but that gets icky when that grob is suicided for some
186 for (SCM s = r ? r->get_property ("elements") : SCM_EOL;
187 !scm_is_symbol (rsym) && scm_is_pair (s); s = scm_cdr (s))
189 Grob * elt = unsmob_grob (scm_car (s));
191 rsym = elt->get_property ("break-align-symbol");
194 if (rsym == ly_symbol2scm ("left-edge"))
198 if (scm_is_symbol (rsym))
199 entry = scm_assq (rsym, alist);
201 bool entry_found = scm_is_pair (entry);
205 if (scm_is_symbol (rsym))
206 sym_string = ly_symbol2string (rsym);
209 if (unsmob_grob (l->get_property ("cause")))
210 orig_string = unsmob_grob (l->get_property ("cause"))->name ();
212 programming_error (_f ("No spacing entry from %s to `%s'",
213 orig_string.to_str0 (),
214 sym_string.to_str0 ()));
218 SCM type = ly_symbol2scm ("extra-space");
222 entry = scm_cdr (entry);
224 distance = scm_to_double (scm_cdr (entry));
225 type = scm_car (entry) ;
230 if (type == ly_symbol2scm ("extra-space"))
231 offsets[next_idx] = extents[idx][RIGHT] + distance
232 - extents[next_idx][LEFT];
233 /* should probably junk minimum-space */
234 else if (type == ly_symbol2scm ("minimum-space"))
235 offsets[next_idx] = extents[idx][RIGHT] >? distance;
239 extra_right_space = distance;
246 Interval total_extent;
248 Real alignment_off = 0.0;
249 for (int i = 0 ; i < offsets.size (); i++)
253 alignment_off = -here;
254 total_extent.unite (extents[i] + here);
258 if (me->break_status_dir () == LEFT)
260 alignment_off = - total_extent[RIGHT] - extra_right_space;
262 else if (edge_idx < 0)
263 alignment_off = -total_extent[LEFT];
265 here = alignment_off;
266 for (int i = 0 ; i < offsets.size (); i++)
269 elems[i]->translate_axis (here, X_AXIS);
274 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
275 "Items that are aligned in prefatory matter.\n"
277 "The spacing of these items is controlled by the @code{space-alist}\n"
278 "property. It contains a list @code{break-align-symbol}s with a specification\n"
279 "of the associated space. The space specification can be "
281 "@item (minimum-space . @var{spc}))\n"
282 " Pad space until the distance is @var{spc}\n"
283 "@item (fixed-space . @var{spc})\n"
284 " Set a fixed space\n"
285 "@item (semi-fixed-space . @var{spc})\n"
286 " Set a space. Half of it is fixed and half is stretchable. \n"
287 "(does not work at start of line. fixme)\n"
288 "@item (extra-space . @var{spc})\n"
289 " Add @var{spc} amount of space.\n"
292 "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
293 "the first note on a line, and the next note halfway a line.\n"
295 "Rules for this spacing are much more complicated than this. \n"
296 "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
297 "break-align-symbol space-alist");
299 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
300 "The object that performs break aligment. See @ref{break-aligned-interface}.",
301 "positioning-done break-align-orders");