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