]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-alignment-interface.cc
Clean up g++ 4.6.1 compiler warnings (-Wunused-but-set-variable).
[lilypond.git] / lily / break-alignment-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "break-align-interface.hh"
21
22 #include "align-interface.hh"
23 #include "axis-group-interface.hh"
24 #include "dimensions.hh"
25 #include "international.hh"
26 #include "output-def.hh"
27 #include "paper-column.hh"
28 #include "pointer-group-interface.hh"
29 #include "self-alignment-interface.hh"
30 #include "side-position-interface.hh"
31 #include "warn.hh"
32
33 /*
34   This is tricky: we cannot modify 'elements, since callers are
35   iterating the same list. Reordering the list in-place, or resetting
36   'elements will skip elements in the loops of callers.
37
38   So we return the correct order as an array.
39 */
40 SCM
41 Break_alignment_interface::break_align_order (Item *me)
42 {
43   SCM order_vec = me->get_property ("break-align-orders");
44   if (!scm_is_vector (order_vec)
45       || scm_c_vector_length (order_vec) < 3)
46     return SCM_BOOL_F;
47
48   SCM order = scm_vector_ref (order_vec,
49                               scm_from_int (me->break_status_dir () + 1));
50
51   return order;
52 }
53
54 vector<Grob *>
55 Break_alignment_interface::ordered_elements (Grob *grob)
56 {
57   Item *me = dynamic_cast<Item *> (grob);
58   extract_grob_set (me, "elements", elts);
59
60   SCM order = break_align_order (me);
61
62   if (order == SCM_BOOL_F)
63     return elts;
64
65   vector<Grob *> writable_elts (elts);
66   /*
67    Copy in order specified in BREAK-ALIGN-ORDER.
68   */
69   vector<Grob *> new_elts;
70   for (; scm_is_pair (order); order = scm_cdr (order))
71     {
72       SCM sym = scm_car (order);
73
74       for (vsize i = writable_elts.size (); i--;)
75         {
76           Grob *g = writable_elts[i];
77           if (g && sym == g->get_property ("break-align-symbol"))
78             {
79               new_elts.push_back (g);
80               writable_elts.erase (writable_elts.begin () + i);
81             }
82         }
83     }
84
85   return new_elts;
86 }
87
88 void
89 Break_alignment_interface::add_element (Grob *me, Grob *toadd)
90 {
91   Align_interface::add_element (me, toadd);
92 }
93
94 MAKE_SCHEME_CALLBACK (Break_alignment_interface, calc_positioning_done, 1)
95 SCM
96 Break_alignment_interface::calc_positioning_done (SCM smob)
97 {
98   Grob *grob = unsmob_grob (smob);
99   Item *me = dynamic_cast<Item *> (grob);
100
101   me->set_property ("positioning-done", SCM_BOOL_T);
102
103   vector<Grob *> elems = ordered_elements (me);
104   vector<Interval> extents;
105
106   for (vsize i = 0; i < elems.size (); i++)
107     {
108       Interval y = elems[i]->extent (elems[i], X_AXIS);
109       extents.push_back (y);
110     }
111
112   vsize idx = 0;
113   while (idx < extents.size () && extents[idx].is_empty ())
114     idx++;
115
116   vector<Real> offsets;
117   offsets.resize (elems.size ());
118   for (vsize i = 0; i < offsets.size (); i++)
119     offsets[i] = 0.0;
120
121   Real extra_right_space = 0.0;
122   vsize edge_idx = VPOS;
123   while (idx < elems.size ())
124     {
125       vsize next_idx = idx + 1;
126       while (next_idx < elems.size ()
127              && extents[next_idx].is_empty ())
128         next_idx++;
129
130       Grob *l = elems[idx];
131       Grob *r = 0;
132
133       if (next_idx < elems.size ())
134         r = elems[next_idx];
135
136       SCM alist = SCM_EOL;
137
138       /*
139         Find the first grob with a space-alist entry.
140       */
141       extract_grob_set (l, "elements", elts);
142
143       for (vsize i = elts.size (); i--;)
144         {
145           Grob *elt = elts[i];
146
147           if (edge_idx == VPOS
148               && (elt->get_property ("break-align-symbol")
149                   == ly_symbol2scm ("left-edge")))
150             edge_idx = idx;
151
152           SCM l = elt->get_property ("space-alist");
153           if (scm_is_pair (l))
154             {
155               alist = l;
156               break;
157             }
158         }
159
160       SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
161
162       /*
163         We used to use #'cause to find out the symbol and the spacing
164         table, but that gets icky when that grob is suicided for some
165         reason.
166       */
167       if (r)
168         {
169           extract_grob_set (r, "elements", elts);
170           for (vsize i = elts.size ();
171                !scm_is_symbol (rsym) && i--;)
172             {
173               Grob *elt = elts[i];
174               rsym = elt->get_property ("break-align-symbol");
175             }
176         }
177
178       if (rsym == ly_symbol2scm ("left-edge"))
179         edge_idx = next_idx;
180
181       SCM entry = SCM_EOL;
182       if (scm_is_symbol (rsym))
183         entry = scm_assq (rsym, alist);
184
185       bool entry_found = scm_is_pair (entry);
186       if (!entry_found)
187         {
188           string sym_string;
189           if (scm_is_symbol (rsym))
190             sym_string = ly_symbol2string (rsym);
191
192           string orig_string;
193           if (unsmob_grob (l->get_property ("cause")))
194             orig_string = unsmob_grob (l->get_property ("cause"))->name ();
195
196           programming_error (_f ("No spacing entry from %s to `%s'",
197                                  orig_string.c_str (),
198                                  sym_string.c_str ()));
199         }
200
201       Real distance = 1.0;
202       SCM type = ly_symbol2scm ("extra-space");
203
204       if (entry_found)
205         {
206           entry = scm_cdr (entry);
207
208           distance = scm_to_double (scm_cdr (entry));
209           type = scm_car (entry);
210         }
211
212       if (r)
213         {
214           if (type == ly_symbol2scm ("extra-space"))
215             offsets[next_idx] = extents[idx][RIGHT] + distance
216                                 - extents[next_idx][LEFT];
217           /* should probably junk minimum-space */
218           else if (type == ly_symbol2scm ("minimum-space"))
219             offsets[next_idx] = max (extents[idx][RIGHT], distance);
220         }
221       else
222         {
223           extra_right_space = distance;
224           if (idx + 1 < offsets.size ())
225             offsets[idx + 1] = extents[idx][RIGHT] + distance;
226         }
227
228       idx = next_idx;
229     }
230
231   Real here = 0.0;
232   Interval total_extent;
233
234   Real alignment_off = 0.0;
235   for (vsize i = 0; i < offsets.size (); i++)
236     {
237       here += offsets[i];
238       if (i == edge_idx)
239         alignment_off = -here;
240       total_extent.unite (extents[i] + here);
241     }
242
243   if (total_extent.is_empty ())
244     return SCM_BOOL_T;
245
246   if (me->break_status_dir () == LEFT)
247     alignment_off = -total_extent[RIGHT] - extra_right_space;
248   else if (edge_idx == VPOS)
249     alignment_off = -total_extent[LEFT];
250
251   here = alignment_off;
252   for (vsize i = 0; i < offsets.size (); i++)
253     {
254       here += offsets[i];
255       elems[i]->translate_axis (here, X_AXIS);
256     }
257
258   return SCM_BOOL_T;
259 }
260
261 MAKE_SCHEME_CALLBACK (Break_alignable_interface, self_align_callback, 1)
262 SCM
263 Break_alignable_interface::self_align_callback (SCM grob)
264 {
265   Grob *me = unsmob_grob (grob);
266   Item *alignment = dynamic_cast<Item *> (me->get_parent (X_AXIS));
267   if (!Break_alignment_interface::has_interface (alignment))
268     return scm_from_int (0);
269
270   SCM symbol_list = me->get_property ("break-align-symbols");
271   vector<Grob *> elements = Break_alignment_interface::ordered_elements (alignment);
272   if (elements.size () == 0)
273     return scm_from_int (0);
274
275   int break_aligned_grob = -1;
276   for (; scm_is_pair (symbol_list); symbol_list = scm_cdr (symbol_list))
277     {
278       SCM sym = scm_car (symbol_list);
279       for (vsize i = 0; i < elements.size (); i++)
280         {
281           if (elements[i]->get_property ("break-align-symbol") == sym)
282             {
283               if (Item::break_visible (elements[i])
284                   && !elements[i]->extent (elements[i], X_AXIS).is_empty ())
285                 {
286                   break_aligned_grob = i;
287                   goto found_break_aligned_grob; /* ugh. need to break out of 2 loops */
288                 }
289               else if (break_aligned_grob == -1)
290                 break_aligned_grob = i;
291             }
292         }
293     }
294
295 found_break_aligned_grob:
296   if (break_aligned_grob == -1)
297     return scm_from_int (0);
298
299   Grob *alignment_parent = elements[break_aligned_grob];
300   Grob *common = me->common_refpoint (alignment_parent, X_AXIS);
301   Real anchor = robust_scm2double (alignment_parent->get_property ("break-align-anchor"), 0);
302
303   return scm_from_double (alignment_parent->relative_coordinate (common, X_AXIS)
304                           - me->relative_coordinate (common, X_AXIS)
305                           + anchor);
306 }
307
308 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_average_anchor, 1)
309 SCM
310 Break_aligned_interface::calc_average_anchor (SCM grob)
311 {
312   Grob *me = unsmob_grob (grob);
313   Real avg = 0.0;
314   int count = 0;
315
316   /* average the anchors of those children that have it set */
317   extract_grob_set (me, "elements", elts);
318   for (vsize i = 0; i < elts.size (); i++)
319     {
320       SCM anchor = elts[i]->get_property ("break-align-anchor");
321       if (scm_is_number (anchor))
322         {
323           count++;
324           avg += scm_to_double (anchor);
325         }
326     }
327
328   return scm_from_double (count > 0 ? avg / count : 0);
329 }
330
331 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_extent_aligned_anchor, 1)
332 SCM
333 Break_aligned_interface::calc_extent_aligned_anchor (SCM smob)
334 {
335   Grob *me = unsmob_grob (smob);
336   Real alignment = robust_scm2double (me->get_property ("break-align-anchor-alignment"), 0.0);
337   Interval iv = me->extent (me, X_AXIS);
338
339   if (isinf (iv[LEFT]) && isinf (iv[RIGHT])) /* avoid NaN */
340     return scm_from_double (0.0);
341
342   return scm_from_double (iv.linear_combination (alignment));
343 }
344
345 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_break_visibility, 1)
346 SCM
347 Break_aligned_interface::calc_break_visibility (SCM smob)
348 {
349   /* a BreakAlignGroup is break-visible if it has one element that is break-visible */
350   Grob *me = unsmob_grob (smob);
351   SCM ret = scm_c_make_vector (3, SCM_EOL);
352   extract_grob_set (me, "elements", elts);
353   for (int dir = 0; dir <= 2; dir++)
354     {
355       bool visible = false;
356       for (vsize i = 0; i < elts.size (); i++)
357         {
358           SCM vis = elts[i]->get_property ("break-visibility");
359           if (scm_is_vector (vis) && to_boolean (scm_c_vector_ref (vis, dir)))
360             visible = true;
361         }
362       scm_c_vector_set_x (ret, dir, scm_from_bool (visible));
363     }
364   return ret;
365 }
366
367 ADD_INTERFACE (Break_alignable_interface,
368                "Object that is aligned on a break alignment.",
369
370                /* properties */
371                "break-align-symbols "
372                "non-break-align-symbols "
373               );
374
375 ADD_INTERFACE (Break_aligned_interface,
376                "Items that are aligned in prefatory matter.\n"
377                "\n"
378                "The spacing of these items is controlled by the"
379                " @code{space-alist} property.  It contains a list"
380                " @code{break-align-symbol}s with a specification of the"
381                " associated space.  The space specification can be\n"
382                "\n"
383                "@table @code\n"
384                "@item (minimum-space . @var{spc}))\n"
385                "Pad space until the distance is @var{spc}.\n"
386                "@item (fixed-space . @var{spc})\n"
387                "Set a fixed space.\n"
388                "@item (semi-fixed-space . @var{spc})\n"
389                "Set a space.  Half of it is fixed and half is stretchable."
390                " (does not work at start of line. fixme)\n"
391                "@item (extra-space . @var{spc})\n"
392                "Add @var{spc} amount of space.\n"
393                "@end table\n"
394                "\n"
395                "Special keys for the alist are @code{first-note} and"
396                " @code{next-note}, signifying the first note on a line, and"
397                " the next note halfway a line.\n"
398                "\n"
399                "Rules for this spacing are much more complicated than this."
400                "  See [Wanske] page 126--134, [Ross] page 143--147.",
401
402                /* properties */
403                "break-align-anchor "
404                "break-align-anchor-alignment "
405                "break-align-symbol "
406                "space-alist "
407               );
408
409 ADD_INTERFACE (Break_alignment_interface,
410                "The object that performs break alignment.  See"
411                " @ref{break-aligned-interface}.",
412
413                /* properties */
414                "positioning-done "
415                "break-align-orders "
416               );