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