]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-alignment-interface.cc
79f215623f8617dd1a98dd59804033e0f03af29b
[lilypond.git] / lily / break-alignment-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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 "side-position-interface.hh"
30 #include "warn.hh"
31
32 /*
33   This is tricky: we cannot modify 'elements, since callers are
34   iterating the same list. Reordering the list in-place, or resetting
35   'elements will skip elements in the loops of callers.
36
37   So we return the correct order as an array.
38 */
39 SCM
40 Break_alignment_interface::break_align_order (Item *me)
41 {
42   SCM order_vec = me->get_property ("break-align-orders");
43   if (!scm_is_vector (order_vec)
44       || scm_c_vector_length (order_vec) < 3)
45     return SCM_BOOL_F;
46
47   SCM order = scm_vector_ref (order_vec,
48                               scm_from_int (me->break_status_dir () + 1));
49
50   return order;
51 }
52
53 vector<Grob *>
54 Break_alignment_interface::ordered_elements (Grob *grob)
55 {
56   Item *me = dynamic_cast<Item *> (grob);
57   extract_grob_set (me, "elements", elts);
58
59   SCM order = break_align_order (me);
60
61   if (order == SCM_BOOL_F)
62     return elts;
63
64   vector<Grob *> writable_elts (elts);
65   /*
66    Copy in order specified in BREAK-ALIGN-ORDER.
67   */
68   vector<Grob *> new_elts;
69   for (; scm_is_pair (order); order = scm_cdr (order))
70     {
71       SCM sym = scm_car (order);
72
73       for (vsize i = writable_elts.size (); i--;)
74         {
75           Grob *g = writable_elts[i];
76           if (g && sym == g->get_property ("break-align-symbol"))
77             {
78               new_elts.push_back (g);
79               writable_elts.erase (writable_elts.begin () + i);
80             }
81         }
82     }
83
84   return new_elts;
85 }
86
87 void
88 Break_alignment_interface::add_element (Grob *me, Grob *toadd)
89 {
90   Align_interface::add_element (me, toadd);
91 }
92
93 MAKE_SCHEME_CALLBACK (Break_alignment_interface, calc_positioning_done, 1)
94 SCM
95 Break_alignment_interface::calc_positioning_done (SCM smob)
96 {
97   Grob *grob = unsmob_grob (smob);
98   Item *me = dynamic_cast<Item *> (grob);
99
100   me->set_property ("positioning-done", SCM_BOOL_T);
101
102   vector<Grob *> elems = ordered_elements (me);
103   vector<Interval> extents;
104
105   for (vsize i = 0; i < elems.size (); i++)
106     {
107       Interval y = elems[i]->extent (elems[i], X_AXIS);
108       extents.push_back (y);
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 (to_string ("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 MAKE_SCHEME_CALLBACK (Break_alignable_interface, self_align_callback, 1)
261 SCM
262 Break_alignable_interface::self_align_callback (SCM grob)
263 {
264   Grob *me = unsmob_grob (grob);
265   Item *alignment = dynamic_cast<Item *> (me->get_parent (X_AXIS));
266   if (!Break_alignment_interface::has_interface (alignment))
267     return scm_from_int (0);
268
269   SCM symbol_list = me->get_property ("break-align-symbols");
270   vector<Grob *> elements = Break_alignment_interface::ordered_elements (alignment);
271   if (elements.size () == 0)
272     return scm_from_int (0);
273
274   int break_aligned_grob = -1;
275   for (; scm_is_pair (symbol_list); symbol_list = scm_cdr (symbol_list))
276     {
277       SCM sym = scm_car (symbol_list);
278       for (vsize i = 0; i < elements.size (); i++)
279         {
280           if (elements[i]->get_property ("break-align-symbol") == sym)
281             {
282               if (Item::break_visible (elements[i])
283                   && !elements[i]->extent (elements[i], X_AXIS).is_empty ())
284                 {
285                   break_aligned_grob = i;
286                   goto found_break_aligned_grob; /* ugh. need to break out of 2 loops */
287                 }
288               else if (break_aligned_grob == -1)
289                 break_aligned_grob = i;
290             }
291         }
292     }
293
294 found_break_aligned_grob:
295   if (break_aligned_grob == -1)
296     return scm_from_int (0);
297
298   Grob *alignment_parent = elements[break_aligned_grob];
299   Grob *common = me->common_refpoint (alignment_parent, X_AXIS);
300   Real anchor = robust_scm2double (alignment_parent->get_property ("break-align-anchor"), 0);
301
302   return scm_from_double (alignment_parent->relative_coordinate (common, X_AXIS)
303                           - me->relative_coordinate (common, X_AXIS)
304                           + anchor);
305 }
306
307 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_average_anchor, 1)
308 SCM
309 Break_aligned_interface::calc_average_anchor (SCM grob)
310 {
311   Grob *me = unsmob_grob (grob);
312   Real avg = 0.0;
313   int count = 0;
314
315   /* average the anchors of those children that have it set */
316   extract_grob_set (me, "elements", elts);
317   for (vsize i = 0; i < elts.size (); i++)
318     {
319       SCM anchor = elts[i]->get_property ("break-align-anchor");
320       if (scm_is_number (anchor))
321         {
322           count++;
323           avg += scm_to_double (anchor);
324         }
325     }
326
327   return scm_from_double (count > 0 ? avg / count : 0);
328 }
329
330 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_extent_aligned_anchor, 1)
331 SCM
332 Break_aligned_interface::calc_extent_aligned_anchor (SCM smob)
333 {
334   Grob *me = unsmob_grob (smob);
335   Real alignment = robust_scm2double (me->get_property ("break-align-anchor-alignment"), 0.0);
336   Interval iv = me->extent (me, X_AXIS);
337
338   if (isinf (iv[LEFT]) && isinf (iv[RIGHT])) /* avoid NaN */
339     return scm_from_double (0.0);
340
341   return scm_from_double (iv.linear_combination (alignment));
342 }
343
344 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_break_visibility, 1)
345 SCM
346 Break_aligned_interface::calc_break_visibility (SCM smob)
347 {
348   /* a BreakAlignGroup is break-visible if it has one element that is break-visible */
349   Grob *me = unsmob_grob (smob);
350   SCM ret = scm_c_make_vector (3, SCM_EOL);
351   extract_grob_set (me, "elements", elts);
352   for (int dir = 0; dir <= 2; dir++)
353     {
354       bool visible = false;
355       for (vsize i = 0; i < elts.size (); i++)
356         {
357           SCM vis = elts[i]->get_property ("break-visibility");
358           if (scm_is_vector (vis) && to_boolean (scm_c_vector_ref (vis, dir)))
359             visible = true;
360         }
361       scm_c_vector_set_x (ret, dir, scm_from_bool (visible));
362     }
363   return ret;
364 }
365
366 ADD_INTERFACE (Break_alignable_interface,
367                "Object that is aligned on a break alignment.",
368
369                /* properties */
370                "break-align-symbols "
371                "non-break-align-symbols "
372               );
373
374 ADD_INTERFACE (Break_aligned_interface,
375                "Items that are aligned in prefatory matter.\n"
376                "\n"
377                "The spacing of these items is controlled by the"
378                " @code{space-alist} property.  It contains a list"
379                " @code{break-align-symbol}s with a specification of the"
380                " associated space.  The space specification can be\n"
381                "\n"
382                "@table @code\n"
383                "@item (minimum-space . @var{spc}))\n"
384                "Pad space until the distance is @var{spc}.\n"
385                "@item (fixed-space . @var{spc})\n"
386                "Set a fixed space.\n"
387                "@item (semi-fixed-space . @var{spc})\n"
388                "Set a space.  Half of it is fixed and half is stretchable."
389                " (does not work at start of line. fixme)\n"
390                "@item (extra-space . @var{spc})\n"
391                "Add @var{spc} amount of space.\n"
392                "@end table\n"
393                "\n"
394                "Special keys for the alist are @code{first-note} and"
395                " @code{next-note}, signifying the first note on a line, and"
396                " the next note halfway a line.\n"
397                "\n"
398                "Rules for this spacing are much more complicated than this."
399                "  See [Wanske] page 126--134, [Ross] page 143--147.",
400
401                /* properties */
402                "break-align-anchor "
403                "break-align-anchor-alignment "
404                "break-align-symbol "
405                "space-alist "
406               );
407
408 ADD_INTERFACE (Break_alignment_interface,
409                "The object that performs break alignment.  See"
410                " @ref{break-aligned-interface}.",
411
412                /* properties */
413                "positioning-done "
414                "break-align-orders "
415               );