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