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>
11 #include <libc-extension.hh> // isinf
13 #include "self-alignment-interface.hh"
14 #include "side-position-interface.hh"
15 #include "axis-group-interface.hh"
17 #include "lily-guile.hh"
18 #include "break-align-interface.hh"
19 #include "dimensions.hh"
20 #include "paper-def.hh"
21 #include "paper-column.hh"
22 #include "group-interface.hh"
23 #include "align-interface.hh"
25 MAKE_SCHEME_CALLBACK (Break_align_interface,alignment_callback,2);
28 Break_align_interface::alignment_callback (SCM element_smob, SCM axis)
30 Grob *me = unsmob_grob (element_smob);
31 Axis a = (Axis) gh_scm2int (axis);
34 Grob *par = me->get_parent (a);
35 if (par && !to_boolean (par->get_property ("positioning-done")))
37 par->set_property ("positioning-done", SCM_BOOL_T);
38 Break_align_interface::do_alignment (par);
41 return gh_double2scm (0);
44 MAKE_SCHEME_CALLBACK (Break_align_interface,self_align_callback,2);
46 Break_align_interface::self_align_callback (SCM element_smob, SCM axis)
48 Grob *me = unsmob_grob (element_smob);
49 Axis a = (Axis) gh_scm2int (axis);
52 Item* item = dynamic_cast<Item*> (me);
53 Direction bsd = item->break_status_dir ();
56 me->set_property ("self-alignment-X", scm_int2num (RIGHT));
60 Force break alignment itself to be done first, in the case
62 return Self_alignment_interface::aligned_on_self (element_smob, axis);
67 Break_align_interface::order_elements (Grob *grob)
69 Item *me = dynamic_cast<Item*> (grob);
70 SCM elts = me->get_property ("elements");
71 SCM order_vec = me->get_property ("break-align-orders");
72 if (!gh_vector_p (order_vec)
73 || gh_vector_length (order_vec) < 3)
76 SCM order = scm_vector_ref (order_vec,
77 gh_int2scm (me->break_status_dir() + 1));
80 Copy in order specified in BREAK-ALIGN-ORDER. We use
81 Pointer_group_interface__extract_grobs (which reverses the list)
82 down the line, so it's ok to prepend onto newlist.
84 SCM new_list = SCM_EOL;
85 for (; gh_pair_p (order); order = ly_cdr (order))
87 SCM sym = gh_car (order);
90 for (; gh_pair_p (*tail); tail = SCM_CDRLOC(*tail))
92 Grob * g = unsmob_grob (gh_car (*tail));
93 if (sym == g->get_property ("break-align-symbol"))
96 *tail = gh_cdr (*tail);
97 if (gh_pair_p (*tail))
98 tail = SCM_CDRLOC(*tail);
100 gh_set_cdr_x (new_pair, new_list);
107 new_list = scm_reverse_x (elts, new_list);
108 me->set_property ("elements", new_list);
112 Break_align_interface::add_element (Grob*me, Grob *toadd)
114 Axis_group_interface::add_element (me, toadd);
118 Break_align_interface::do_alignment (Grob *grob)
120 Item * me = dynamic_cast<Item*> (grob);
124 Link_array<Grob> elems
125 = Pointer_group_interface__extract_grobs (me, (Grob*)0,
127 Array<Interval> extents;
129 int last_nonempty = -1;
130 for (int i=0; i < elems.size (); i++)
132 Interval y = elems[i]->extent (elems[i], X_AXIS);
139 while (idx < extents.size () && extents[idx].is_empty ())
143 offsets.set_size (elems.size ());
144 for (int i= 0; i < offsets.size ();i ++)
148 Real extra_right_space = 0.0;
150 while (idx < elems.size ())
152 int next_idx = idx+1;
153 while (next_idx < elems.size () &&
154 extents[next_idx].is_empty () )
157 Grob *l = elems[idx];
160 if (next_idx < elems.size ())
167 Find the first grob with a space-alist entry.
169 for (SCM s= l->get_property ("elements");
170 gh_pair_p (s) ; s = gh_cdr (s))
172 Grob *elt = unsmob_grob (gh_car (s));
175 && elt->get_property ("break-align-symbol")
176 == ly_symbol2scm ( "left-edge"))
179 SCM l =elt->get_property ("space-alist");
187 SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
190 We used to use #'cause to find out the symbol and the spacing
191 table, but that gets icky when that grob is suicided for some
194 for (SCM s = r ? r->get_property ("elements") : SCM_EOL;
195 !gh_symbol_p (rsym) && gh_pair_p (s); s = gh_cdr (s))
197 Grob * elt =unsmob_grob (gh_car (s));
199 rsym = elt->get_property ("break-align-symbol");
202 if (rsym == ly_symbol2scm ("left-edge"))
206 if (gh_symbol_p (rsym))
207 entry = scm_assq (rsym, alist);
209 bool entry_found = gh_pair_p (entry);
213 if (gh_symbol_p (rsym))
214 sym_string = ly_symbol2string (rsym);
217 if (unsmob_grob (l->get_property ("cause")))
218 orig_string = unsmob_grob (l->get_property ("cause"))->name ();
220 programming_error (_f ("No spacing entry from %s to `%s'",
221 orig_string.to_str0 (),
222 sym_string.to_str0 ()));
226 SCM type = ly_symbol2scm ("extra-space");
230 entry = gh_cdr (entry);
232 distance = gh_scm2double (gh_cdr (entry));
233 type = gh_car (entry) ;
238 if (type == ly_symbol2scm ("extra-space"))
239 offsets[next_idx] = extents[idx][RIGHT] + distance
240 - extents[next_idx][LEFT];
241 /* should probably junk minimum-space */
242 else if (type == ly_symbol2scm ("minimum-space"))
243 offsets[next_idx] = extents[idx][RIGHT] >? distance;
247 extra_right_space = distance;
254 Interval total_extent;
256 Real alignment_off =0.0;
257 for (int i =0 ; i < offsets.size (); i++)
261 alignment_off = -here;
262 total_extent.unite (extents[i] + here);
266 if (me->break_status_dir () == LEFT)
268 alignment_off = - total_extent[RIGHT] - extra_right_space;
270 else if (edge_idx < 0)
271 alignment_off = -total_extent[LEFT];
273 here = alignment_off;
274 for (int i =0 ; i < offsets.size (); i++)
277 elems[i]->translate_axis (here, X_AXIS);
282 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
283 "Items that are aligned in prefatory matter.\n"
285 "The spacing of these items is controlled by the @code{space-alist}\n"
286 "property. It contains a list @code{break-align-symbol}s with a specification\n"
287 "of the associated space. The space specification can be "
289 "@item (minimum-space . @var{spc}))\n"
290 " Pad space until the distance is @var{spc}\n"
291 "@item (fixed-space . @var{spc})\n"
292 " Set a fixed space\n"
293 "@item (semi-fixed-space . @var{spc})\n"
294 " Set a space. Half of it is fixed and half is stretchable. \n"
295 "(does not work at start of line. fixme)\n"
296 "@item (extra-space . @var{spc})\n"
297 " Add @var{spc} amount of space.\n"
300 "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
301 "the first note on a line, and the next note halfway a line.\n"
303 "Rules for this spacing are much more complicated than this. \n"
304 "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
305 "break-align-symbol space-alist");
307 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
308 "The object that performs break aligment. See @ref{break-aligned-interface}.",
309 "positioning-done break-align-orders");