]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-interface.cc
* lily/main.cc (setup_guile_env): new function. Set GC min_yields
[lilypond.git] / lily / break-align-interface.cc
1 /*
2   break-align-interface.cc -- implement Break_align_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <math.h>
10
11 #include "break-align-interface.hh"
12
13 #include "libc-extension.hh"    // isinf
14 #include "pointer-group-interface.hh"
15 #include "self-alignment-interface.hh"
16 #include "side-position-interface.hh"
17 #include "axis-group-interface.hh"
18 #include "warn.hh"
19 #include "dimensions.hh"
20 #include "output-def.hh"
21 #include "paper-column.hh"
22 #include "align-interface.hh"
23
24 MAKE_SCHEME_CALLBACK (Break_align_interface, alignment_callback, 2);
25 SCM
26 Break_align_interface::alignment_callback (SCM element_smob, SCM axis)
27 {
28   Grob *me = unsmob_grob (element_smob);
29   Axis a = (Axis) scm_to_int (axis);
30
31   assert (a == X_AXIS);
32   Grob *par = me->get_parent (a);
33   if (par && !to_boolean (par->get_property ("positioning-done")))
34     {
35       par->set_property ("positioning-done", SCM_BOOL_T);
36       Break_align_interface::do_alignment (par);
37     }
38
39   return scm_from_double (0);
40 }
41
42 MAKE_SCHEME_CALLBACK (Break_align_interface, self_align_callback, 2);
43 SCM
44 Break_align_interface::self_align_callback (SCM element_smob, SCM axis)
45 {
46   Grob *me = unsmob_grob (element_smob);
47   (void)  axis;
48   assert (scm_to_int (axis) == X_AXIS);
49
50   Item *item = dynamic_cast<Item *> (me);
51   Direction bsd = item->break_status_dir ();
52   if (bsd == LEFT)
53     {
54       me->set_property ("self-alignment-X", scm_from_int (RIGHT));
55     }
56
57   /*
58     Force break alignment itself to be done first, in the case
59   */
60   return Self_alignment_interface::aligned_on_self (element_smob, axis);
61 }
62
63 /*
64   This is tricky: we cannot modify 'elements, since callers are
65   iterating the same list. Reordering the list in-place, or resetting
66   'elements will skip elements in the loops of callers.
67
68   So we return the correct order as an array.
69 */
70 Link_array<Grob>
71 Break_align_interface::ordered_elements (Grob *grob)
72 {
73   Item *me = dynamic_cast<Item *> (grob);
74   extract_grob_set (me, "elements", elts);
75   
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 elts;
80
81   Link_array<Grob> writable_elts (elts);
82   SCM order = scm_vector_ref (order_vec,
83                               scm_from_int (me->break_status_dir () + 1));
84
85   /*
86     Copy in order specified in BREAK-ALIGN-ORDER.
87   */
88   Link_array<Grob> new_elts;
89   for (; scm_is_pair (order); order = scm_cdr (order))
90     {
91       SCM sym = scm_car (order);
92
93       for (int i = writable_elts.size(); i --; )
94         {
95           Grob *g = writable_elts[i];
96           if (g && sym == g->get_property ("break-align-symbol"))
97             {
98               new_elts.push (g);
99               writable_elts.del (i);
100             }
101         }
102     }
103   
104   return new_elts;
105 }
106
107 void
108 Break_align_interface::add_element (Grob *me, Grob *toadd)
109 {
110   Axis_group_interface::add_element (me, toadd);
111 }
112
113 void
114 Break_align_interface::do_alignment (Grob *grob)
115 {
116   Item *me = dynamic_cast<Item *> (grob);
117
118   Link_array<Grob> elems = ordered_elements (me);
119   Array<Interval> extents;
120
121   int last_nonempty = -1;
122   for (int i = 0; i < elems.size (); i++)
123     {
124       Interval y = elems[i]->extent (elems[i], X_AXIS);
125       extents.push (y);
126       if (!y.is_empty ())
127         last_nonempty = i;
128     }
129
130   int idx = 0;
131   while (idx < extents.size () && extents[idx].is_empty ())
132     idx++;
133
134   Array<Real> offsets;
135   offsets.set_size (elems.size ());
136   for (int i = 0; i < offsets.size ();i++)
137     offsets[i] = 0.0;
138
139   Real extra_right_space = 0.0;
140   int edge_idx = -1;
141   while (idx < elems.size ())
142     {
143       int next_idx = idx + 1;
144       while (next_idx < elems.size ()
145              && extents[next_idx].is_empty ())
146         next_idx++;
147
148       Grob *l = elems[idx];
149       Grob *r = 0;
150
151       if (next_idx < elems.size ())
152         r = elems[next_idx];
153
154       SCM alist = SCM_EOL;
155
156       /*
157         Find the first grob with a space-alist entry.
158       */
159       extract_grob_set (l, "elements", elts);
160       
161       for (int i = elts.size(); i--; )
162         {
163           Grob *elt = elts[i];
164
165           if (edge_idx < 0
166               && elt->get_property ("break-align-symbol")
167               == ly_symbol2scm ("left-edge"))
168             edge_idx = idx;
169
170           SCM l = elt->get_property ("space-alist");
171           if (scm_is_pair (l))
172             {
173               alist = l;
174               break;
175             }
176         }
177
178       SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
179
180       /*
181         We used to use #'cause to find out the symbol and the spacing
182         table, but that gets icky when that grob is suicided for some
183         reason.
184       */
185       if (r)
186         {
187           extract_grob_set (r, "elements", elts);
188           for (int i = elts.size();
189                !scm_is_symbol (rsym) && i--;)
190             {
191               Grob *elt = elts[i];
192               rsym = elt->get_property ("break-align-symbol");
193             }
194         }
195
196       if (rsym == ly_symbol2scm ("left-edge"))
197         edge_idx = next_idx;
198
199       SCM entry = SCM_EOL;
200       if (scm_is_symbol (rsym))
201         entry = scm_assq (rsym, alist);
202
203       bool entry_found = scm_is_pair (entry);
204       if (!entry_found)
205         {
206           String sym_string;
207           if (scm_is_symbol (rsym))
208             sym_string = ly_symbol2string (rsym);
209
210           String orig_string;
211           if (unsmob_grob (l->get_property ("cause")))
212             orig_string = unsmob_grob (l->get_property ("cause"))->name ();
213
214           programming_error (_f ("No spacing entry from %s to `%s'",
215                                  orig_string.to_str0 (),
216                                  sym_string.to_str0 ()));
217         }
218
219       Real distance = 1.0;
220       SCM type = ly_symbol2scm ("extra-space");
221
222       if (entry_found)
223         {
224           entry = scm_cdr (entry);
225
226           distance = scm_to_double (scm_cdr (entry));
227           type = scm_car (entry);
228         }
229
230       if (r)
231         {
232           if (type == ly_symbol2scm ("extra-space"))
233             offsets[next_idx] = extents[idx][RIGHT] + distance
234               - extents[next_idx][LEFT];
235           /* should probably junk minimum-space */
236           else if (type == ly_symbol2scm ("minimum-space"))
237             offsets[next_idx] = max (extents[idx][RIGHT], distance);
238         }
239       else
240         {
241           extra_right_space = distance;
242         }
243
244       idx = next_idx;
245     }
246
247   Real here = 0.0;
248   Interval total_extent;
249
250   Real alignment_off = 0.0;
251   for (int i = 0; i < offsets.size (); i++)
252     {
253       here += offsets[i];
254       if (i == edge_idx)
255         alignment_off = -here;
256       total_extent.unite (extents[i] + here);
257     }
258
259   if (total_extent.is_empty ())
260     return ;
261   
262   if (me->break_status_dir () == LEFT)
263     {
264       alignment_off = -total_extent[RIGHT] - extra_right_space;
265     }
266   else if (edge_idx < 0)
267     alignment_off = -total_extent[LEFT];
268
269   here = alignment_off;
270   for (int i = 0; i < offsets.size (); i++)
271     {
272       here += offsets[i];
273       elems[i]->translate_axis (here, X_AXIS);
274     }
275 }
276
277 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
278                "Items that are aligned in prefatory matter.\n"
279                "\n"
280                "The spacing of these items is controlled by the @code{space-alist}\n"
281                "property. It contains a list @code{break-align-symbol}s with a specification\n"
282                "of the associated space. The space specification can be "
283                "@table @code\n"
284                "@item (minimum-space . @var{spc}))\n"
285                "  Pad space until the distance is @var{spc}\n"
286                "@item (fixed-space . @var{spc})\n"
287                "  Set a fixed space\n"
288                "@item (semi-fixed-space . @var{spc})\n"
289                "  Set a space. Half of it is fixed and half is stretchable. \n"
290                "(does not work at start of line. fixme)\n"
291                "@item (extra-space . @var{spc})\n"
292                "  Add @var{spc} amount of space.\n"
293                "@end table\n"
294                "\n"
295                "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
296                "the first note on a line, and the next note halfway a line.\n"
297                "\n"
298                "Rules for this spacing are much more complicated than this. \n"
299                "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
300                "break-align-symbol space-alist");
301
302 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
303                "The object that performs break aligment. See @ref{break-aligned-interface}.",
304                "positioning-done break-align-orders");
305