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