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