]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-interface.cc
(SCM_VECTOR_P): Compile fix.
[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--2004 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  Pointer_group_interface__extract_grobs (me, (Grob*)0,
80                                                     "elements");
81   SCM order = scm_vector_ref (order_vec,
82                               scm_int2num (me->break_status_dir () + 1));
83
84
85   /*
86     Copy in order specified in BREAK-ALIGN-ORDER.
87   */
88   Link_array<Grob> new_elts;
89   for (; scm_is_pair (order); order = scm_cdr (order))
90     {
91       SCM sym = scm_car (order);
92
93       for (SCM s = elts; scm_is_pair (s); s = scm_cdr (s))
94         {
95           Grob *g = unsmob_grob (scm_car (s));
96           if (g && sym == g->get_property ("break-align-symbol"))
97             {
98               new_elts.push (g);
99               elts = scm_delq (g->self_scm (), elts);
100             }
101         }
102     }
103   return new_elts;
104 }
105
106 void
107 Break_align_interface::add_element (Grob *me, Grob *toadd)
108 {
109   Axis_group_interface::add_element (me, toadd);
110 }
111
112 void
113 Break_align_interface::do_alignment (Grob *grob)
114 {
115   Item * me = dynamic_cast<Item*> (grob);
116
117
118   Link_array<Grob> elems = ordered_elements (me);
119   Array<Interval> extents;
120
121   int last_nonempty = -1;
122   for (int i = 0; i < elems.size (); i++)
123     {
124       Interval y = elems[i]->extent (elems[i], X_AXIS);
125       extents.push (y);
126       if (!y.is_empty ())
127         last_nonempty = i;
128     }
129
130   int idx  = 0;
131   while (idx < extents.size  () && extents[idx].is_empty ())
132     idx++;
133
134   Array<Real> offsets;
135   offsets.set_size (elems.size ());
136   for (int i = 0; i < offsets.size ();i ++)
137     offsets[i] = 0.0;
138
139
140   Real extra_right_space = 0.0;
141   int edge_idx = -1;
142   while (idx < elems.size ())
143     {
144       int next_idx = idx+1;
145       while (next_idx < elems.size () &&
146              extents[next_idx].is_empty () )
147         next_idx++;
148
149       Grob *l = elems[idx];
150       Grob *r = 0;
151
152       if (next_idx < elems.size ())
153         r = elems[next_idx];
154
155       SCM alist = SCM_EOL;
156
157
158       /*
159         Find the first grob with a space-alist entry.
160        */
161       for (SCM s = l->get_property ("elements");
162            scm_is_pair (s) ; s = scm_cdr (s))
163           {
164             Grob *elt = unsmob_grob (scm_car (s));
165
166             if (edge_idx < 0
167                 && elt->get_property ("break-align-symbol")
168                 == ly_symbol2scm ( "left-edge"))
169               edge_idx = idx;
170
171             SCM l = elt->get_property ("space-alist");
172             if (scm_is_pair (l))
173               {
174                 alist = l;
175                 break;
176               }
177           }
178
179       SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
180
181       /*
182         We used to use #'cause to find out the symbol and the spacing
183         table, but that gets icky when that grob is suicided for some
184         reason.
185       */
186       for (SCM s = r ? r->get_property ("elements") : SCM_EOL;
187            !scm_is_symbol (rsym) && scm_is_pair (s); s = scm_cdr (s))
188         {
189           Grob * elt = unsmob_grob (scm_car (s));
190
191           rsym = elt->get_property ("break-align-symbol");
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] = extents[idx][RIGHT] >? distance;
236         }
237       else
238         {
239           extra_right_space = distance;
240         }
241
242       idx = next_idx;
243     }
244
245   Real here = 0.0;
246   Interval total_extent;
247
248   Real alignment_off = 0.0;
249   for (int i = 0 ; i < offsets.size (); i++)
250     {
251       here += offsets[i];
252       if (i == edge_idx)
253         alignment_off = -here;
254       total_extent.unite (extents[i] + here);
255     }
256
257
258   if (me->break_status_dir () == LEFT)
259     {
260       alignment_off = - total_extent[RIGHT] - extra_right_space;
261     }
262   else if (edge_idx < 0)
263     alignment_off = -total_extent[LEFT];
264
265   here = alignment_off;
266   for (int i = 0 ; i < offsets.size (); i++)
267     {
268       here += offsets[i];
269       elems[i]->translate_axis (here, X_AXIS);
270     }
271 }
272
273
274 ADD_INTERFACE (Break_aligned_interface, "break-aligned-interface",
275                "Items that are aligned in prefatory matter.\n"
276                "\n"
277                "The spacing of these items is controlled by the @code{space-alist}\n"
278                "property. It contains a list @code{break-align-symbol}s with a specification\n"
279                "of the associated space. The space specification can be "
280                "@table @code\n"
281                "@item (minimum-space . @var{spc}))\n"
282                "  Pad space until the distance is @var{spc}\n"
283                "@item (fixed-space . @var{spc})\n"
284                "  Set a fixed space\n"
285                "@item (semi-fixed-space . @var{spc})\n"
286                "  Set a space. Half of it is fixed and half is stretchable. \n"
287                "(does not work at start of line. fixme)\n"
288                "@item (extra-space . @var{spc})\n"
289                "  Add @var{spc} amount of space.\n"
290                "@end table\n"
291                "\n"
292                "Special keys for the alist are @code{first-note} and @code{next-note}, signifying\n"
293                "the first note on a line, and the next note halfway a line.\n"
294                "\n"
295                "Rules for this spacing are much more complicated than this. \n"
296                "See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147\n",
297                "break-align-symbol space-alist");
298
299 ADD_INTERFACE (Break_align_interface, "break-alignment-interface",
300                "The object that performs break aligment. See @ref{break-aligned-interface}.",
301                "positioning-done break-align-orders");
302
303
304