]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-interface.cc
* lily/include/group-interface.hh (extract_grob_array): rename
[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 <math.h>
11
12 #include "break-align-interface.hh"
13 #include "libc-extension.hh"    // isinf
14
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_make_real (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   Axis a = (Axis) scm_to_int (axis);
48   assert (a == 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_int2num (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 /*
65   This is tricky: we cannot modify 'elements, since callers are
66   iterating the same list. Reordering the list in-place, or resetting
67   'elements will skip elements in the loops of callers.
68
69   So we return the correct order as an array.
70  */
71 Link_array<Grob>
72 Break_align_interface::ordered_elements (Grob *grob)
73 {
74   Item *me = dynamic_cast<Item*> (grob);
75   SCM elts = me->get_property ("elements");
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  extract_grob_array (me, ly_symbol2scm ("elements"));
80   SCM order = scm_vector_ref (order_vec,
81                               scm_int2num (me->break_status_dir () + 1));
82
83
84   /*
85     Copy in order specified in BREAK-ALIGN-ORDER.
86   */
87   Link_array<Grob> new_elts;
88   for (; scm_is_pair (order); order = scm_cdr (order))
89     {
90       SCM sym = scm_car (order);
91
92       for (SCM s = elts; scm_is_pair (s); s = scm_cdr (s))
93         {
94           Grob *g = unsmob_grob (scm_car (s));
95           if (g && sym == g->get_property ("break-align-symbol"))
96             {
97               new_elts.push (g);
98               elts = scm_delq (g->self_scm (), elts);
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
117   Link_array<Grob> elems = ordered_elements (me);
118   Array<Interval> extents;
119
120   int last_nonempty = -1;
121   for (int i = 0; i < elems.size (); i++)
122     {
123       Interval y = elems[i]->extent (elems[i], X_AXIS);
124       extents.push (y);
125       if (!y.is_empty ())
126         last_nonempty = i;
127     }
128
129   int idx  = 0;
130   while (idx < extents.size  () && extents[idx].is_empty ())
131     idx++;
132
133   Array<Real> offsets;
134   offsets.set_size (elems.size ());
135   for (int i = 0; i < offsets.size ();i ++)
136     offsets[i] = 0.0;
137
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       /*
158         Find the first grob with a space-alist entry.
159        */
160       for (SCM s = l->get_property ("elements");
161            scm_is_pair (s) ; s = scm_cdr (s))
162           {
163             Grob *elt = unsmob_grob (scm_car (s));
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       for (SCM s = r ? r->get_property ("elements") : SCM_EOL;
186            !scm_is_symbol (rsym) && scm_is_pair (s); s = scm_cdr (s))
187         {
188           Grob * elt = unsmob_grob (scm_car (s));
189
190           rsym = elt->get_property ("break-align-symbol");
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] = extents[idx][RIGHT] >? distance;
235         }
236       else
237         {
238           extra_right_space = distance;
239         }
240
241       idx = next_idx;
242     }
243
244   Real here = 0.0;
245   Interval total_extent;
246
247   Real alignment_off = 0.0;
248   for (int i = 0 ; i < offsets.size (); i++)
249     {
250       here += offsets[i];
251       if (i == edge_idx)
252         alignment_off = -here;
253       total_extent.unite (extents[i] + here);
254     }
255
256
257   if (me->break_status_dir () == LEFT)
258     {
259       alignment_off = - total_extent[RIGHT] - extra_right_space;
260     }
261   else if (edge_idx < 0)
262     alignment_off = -total_extent[LEFT];
263
264   here = alignment_off;
265   for (int i = 0 ; i < offsets.size (); i++)
266     {
267       here += offsets[i];
268       elems[i]->translate_axis (here, X_AXIS);
269     }
270 }
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                "break-align-symbol space-alist");
297
298 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
299                "The object that performs break aligment. See @ref{break-aligned-interface}.",
300                "positioning-done break-align-orders");
301
302
303