]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-alignment-interface.cc
Revert "Issue 4550 (2/2) Avoid "using namespace std;" in included files"
[lilypond.git] / lily / break-alignment-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 using std::string;
33 using std::vector;
34
35 /*
36   This is tricky: we cannot modify 'elements, since callers are
37   iterating the same list. Reordering the list in-place, or resetting
38   'elements will skip elements in the loops of callers.
39
40   So we return the correct order as an array.
41 */
42 SCM
43 Break_alignment_interface::break_align_order (Item *me)
44 {
45   SCM order_vec = me->get_property ("break-align-orders");
46   if (!scm_is_vector (order_vec)
47       || scm_c_vector_length (order_vec) < 3)
48     return SCM_BOOL_F;
49
50   SCM order = scm_vector_ref (order_vec,
51                               scm_from_int (me->break_status_dir () + 1));
52
53   return order;
54 }
55
56 vector<Grob *>
57 Break_alignment_interface::ordered_elements (Grob *grob)
58 {
59   Item *me = dynamic_cast<Item *> (grob);
60   extract_grob_set (me, "elements", elts);
61
62   SCM order = break_align_order (me);
63
64   if (scm_is_false (order))
65     return elts;
66
67   vector<Grob *> writable_elts (elts);
68   /*
69    Copy in order specified in BREAK-ALIGN-ORDER.
70   */
71   vector<Grob *> new_elts;
72   for (; scm_is_pair (order); order = scm_cdr (order))
73     {
74       SCM sym = scm_car (order);
75
76       for (vsize i = writable_elts.size (); i--;)
77         {
78           Grob *g = writable_elts[i];
79           if (g && sym == g->get_property ("break-align-symbol"))
80             {
81               new_elts.push_back (g);
82               writable_elts.erase (writable_elts.begin () + i);
83             }
84         }
85     }
86
87   return new_elts;
88 }
89
90 void
91 Break_alignment_interface::add_element (Grob *me, Grob *toadd)
92 {
93   Align_interface::add_element (me, toadd);
94 }
95
96 MAKE_SCHEME_CALLBACK (Break_alignment_interface, calc_positioning_done, 1)
97 SCM
98 Break_alignment_interface::calc_positioning_done (SCM smob)
99 {
100   Grob *grob = unsmob<Grob> (smob);
101   Item *me = dynamic_cast<Item *> (grob);
102
103   me->set_property ("positioning-done", SCM_BOOL_T);
104
105   vector<Grob *> elems = ordered_elements (me);
106   vector<Interval> extents;
107
108   for (vsize i = 0; i < elems.size (); i++)
109     {
110       Interval y = elems[i]->extent (elems[i], X_AXIS);
111       extents.push_back (y);
112     }
113
114   vsize idx = 0;
115   while (idx < extents.size () && extents[idx].is_empty ())
116     idx++;
117
118   vector<Real> offsets;
119   offsets.resize (elems.size ());
120   for (vsize i = 0; i < offsets.size (); i++)
121     offsets[i] = 0.0;
122
123   Real extra_right_space = 0.0;
124   vsize edge_idx = VPOS;
125   while (idx < elems.size ())
126     {
127       vsize next_idx = idx + 1;
128       while (next_idx < elems.size ()
129              && extents[next_idx].is_empty ())
130         next_idx++;
131
132       Grob *l = elems[idx];
133       Grob *r = 0;
134
135       if (next_idx < elems.size ())
136         r = elems[next_idx];
137
138       SCM alist = SCM_EOL;
139
140       /*
141         Find the first grob with a space-alist entry.
142       */
143       extract_grob_set (l, "elements", elts);
144
145       for (vsize i = elts.size (); i--;)
146         {
147           Grob *elt = elts[i];
148
149           if (edge_idx == VPOS
150               && scm_is_eq (elt->get_property ("break-align-symbol"),
151                             ly_symbol2scm ("left-edge")))
152             edge_idx = idx;
153
154           SCM l = elt->get_property ("space-alist");
155           if (scm_is_pair (l))
156             {
157               alist = l;
158               break;
159             }
160         }
161
162       SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");
163
164       /*
165         We used to use #'cause to find out the symbol and the spacing
166         table, but that gets icky when that grob is suicided for some
167         reason.
168       */
169       if (r)
170         {
171           extract_grob_set (r, "elements", elts);
172           for (vsize i = elts.size ();
173                !scm_is_symbol (rsym) && i--;)
174             {
175               Grob *elt = elts[i];
176               rsym = elt->get_property ("break-align-symbol");
177             }
178         }
179
180       if (scm_is_eq (rsym, ly_symbol2scm ("left-edge")))
181         edge_idx = next_idx;
182
183       SCM entry = SCM_EOL;
184       if (scm_is_symbol (rsym))
185         entry = scm_assq (rsym, alist);
186
187       bool entry_found = scm_is_pair (entry);
188       if (!entry_found)
189         {
190           string sym_string;
191           if (scm_is_symbol (rsym))
192             sym_string = ly_symbol2string (rsym);
193
194           string orig_string;
195           if (unsmob<Grob> (l->get_property ("cause")))
196             orig_string = unsmob<Grob> (l->get_property ("cause"))->name ();
197
198           programming_error (to_string ("No spacing entry from %s to `%s'",
199                                         orig_string.c_str (),
200                                         sym_string.c_str ()));
201         }
202
203       Real distance = 1.0;
204       SCM type = ly_symbol2scm ("extra-space");
205
206       if (entry_found)
207         {
208           entry = scm_cdr (entry);
209
210           distance = scm_to_double (scm_cdr (entry));
211           type = scm_car (entry);
212         }
213
214       if (r)
215         {
216           if (scm_is_eq (type, ly_symbol2scm ("extra-space")))
217             offsets[next_idx] = extents[idx][RIGHT] + distance
218                                 - extents[next_idx][LEFT];
219           /* should probably junk minimum-space */
220           else if (scm_is_eq (type, ly_symbol2scm ("minimum-space")))
221             offsets[next_idx] = max (extents[idx][RIGHT], distance);
222         }
223       else
224         {
225           extra_right_space = distance;
226           if (idx + 1 < offsets.size ())
227             offsets[idx + 1] = extents[idx][RIGHT] + distance;
228         }
229
230       idx = next_idx;
231     }
232
233   Real here = 0.0;
234   Interval total_extent;
235
236   Real alignment_off = 0.0;
237   for (vsize i = 0; i < offsets.size (); i++)
238     {
239       here += offsets[i];
240       if (i == edge_idx)
241         alignment_off = -here;
242       total_extent.unite (extents[i] + here);
243     }
244
245   if (total_extent.is_empty ())
246     return SCM_BOOL_T;
247
248   if (me->break_status_dir () == LEFT)
249     alignment_off = -total_extent[RIGHT] - extra_right_space;
250   else if (edge_idx == VPOS)
251     alignment_off = -total_extent[LEFT];
252
253   here = alignment_off;
254   for (vsize i = 0; i < offsets.size (); i++)
255     {
256       here += offsets[i];
257       elems[i]->translate_axis (here, X_AXIS);
258     }
259
260   return SCM_BOOL_T;
261 }
262
263 MAKE_SCHEME_CALLBACK (Break_alignable_interface, self_align_callback, 1)
264 SCM
265 Break_alignable_interface::self_align_callback (SCM grob)
266 {
267   Grob *me = unsmob<Grob> (grob);
268   Item *alignment = dynamic_cast<Item *> (me->get_parent (X_AXIS));
269   if (!has_interface<Break_alignment_interface> (alignment))
270     return scm_from_int (0);
271
272   SCM symbol_list = me->get_property ("break-align-symbols");
273   vector<Grob *> elements = Break_alignment_interface::ordered_elements (alignment);
274   if (elements.size () == 0)
275     return scm_from_int (0);
276
277   int break_aligned_grob = -1;
278   for (; scm_is_pair (symbol_list); symbol_list = scm_cdr (symbol_list))
279     {
280       SCM sym = scm_car (symbol_list);
281       for (vsize i = 0; i < elements.size (); i++)
282         {
283           if (elements[i]->get_property ("break-align-symbol") == sym)
284             {
285               if (Item::break_visible (elements[i])
286                   && !elements[i]->extent (elements[i], X_AXIS).is_empty ())
287                 {
288                   break_aligned_grob = i;
289                   goto found_break_aligned_grob; /* ugh. need to break out of 2 loops */
290                 }
291               else if (break_aligned_grob == -1)
292                 break_aligned_grob = i;
293             }
294         }
295     }
296
297 found_break_aligned_grob:
298   if (break_aligned_grob == -1)
299     return scm_from_int (0);
300
301   Grob *alignment_parent = elements[break_aligned_grob];
302   Grob *common = me->common_refpoint (alignment_parent, X_AXIS);
303   Real anchor = robust_scm2double (alignment_parent->get_property ("break-align-anchor"), 0);
304
305   return scm_from_double (alignment_parent->relative_coordinate (common, X_AXIS)
306                           - me->relative_coordinate (common, X_AXIS)
307                           + anchor);
308 }
309
310 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_average_anchor, 1)
311 SCM
312 Break_aligned_interface::calc_average_anchor (SCM grob)
313 {
314   Grob *me = unsmob<Grob> (grob);
315   Real avg = 0.0;
316   int count = 0;
317
318   /* average the anchors of those children that have it set */
319   extract_grob_set (me, "elements", elts);
320   for (vsize i = 0; i < elts.size (); i++)
321     {
322       SCM anchor = elts[i]->get_property ("break-align-anchor");
323       if (scm_is_number (anchor))
324         {
325           count++;
326           avg += scm_to_double (anchor);
327         }
328     }
329
330   return scm_from_double (count > 0 ? avg / count : 0);
331 }
332
333 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_extent_aligned_anchor, 1)
334 SCM
335 Break_aligned_interface::calc_extent_aligned_anchor (SCM smob)
336 {
337   Grob *me = unsmob<Grob> (smob);
338   Real alignment = robust_scm2double (me->get_property ("break-align-anchor-alignment"), 0.0);
339   Interval iv = me->extent (me, X_AXIS);
340
341   if (isinf (iv[LEFT]) && isinf (iv[RIGHT])) /* avoid NaN */
342     return scm_from_double (0.0);
343
344   return scm_from_double (iv.linear_combination (alignment));
345 }
346
347 MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_break_visibility, 1)
348 SCM
349 Break_aligned_interface::calc_break_visibility (SCM smob)
350 {
351   /* a BreakAlignGroup is break-visible if it has one element that is break-visible */
352   Grob *me = unsmob<Grob> (smob);
353   SCM ret = scm_c_make_vector (3, SCM_EOL);
354   extract_grob_set (me, "elements", elts);
355   for (int dir = 0; dir <= 2; dir++)
356     {
357       bool visible = false;
358       for (vsize i = 0; i < elts.size (); i++)
359         {
360           SCM vis = elts[i]->get_property ("break-visibility");
361           if (scm_is_vector (vis) && to_boolean (scm_c_vector_ref (vis, dir)))
362             visible = true;
363         }
364       scm_c_vector_set_x (ret, dir, scm_from_bool (visible));
365     }
366   return ret;
367 }
368
369 ADD_INTERFACE (Break_alignment_interface,
370                "The object that performs break alignment.\n"
371                "\n"
372                "Three interfaces deal specifically with break alignment:\n"
373                "@enumerate\n"
374                "@item break-alignment-interface (this one),\n"
375                "@item @ref{break-alignable-interface}, and\n"
376                "@item @ref{break-aligned-interface}.\n"
377                "@end enumerate\n"
378                "\n"
379                " Each of these interfaces supports grob properties that use"
380                " @w{@emph{break-align symbols}}, which are Scheme symbols that"
381                " are used to specify the alignment, ordering, and spacing of"
382                " certain notational elements (@q{breakable}@tie{}items)."
383                "\n"
384                "@subsubheading Available break-align symbols:\n"
385                "\n"
386                "@example\n"
387                "ambitus\n"
388                "breathing-sign\n"
389                "clef\n"
390                "cue-clef\n"
391                "cue-end-clef\n"
392                "custos\n"
393                "key-cancellation\n"
394                "key-signature\n"
395                "left-edge\n"
396                "staff-bar\n"
397                "time-signature\n"
398                "@end example",
399
400                /* properties */
401                "positioning-done "
402                "break-align-orders "
403               );
404
405 ADD_INTERFACE (Break_alignable_interface,
406                "Object that is aligned on a break alignment.",
407
408                /* properties */
409                "break-align-symbols "
410                "non-break-align-symbols "
411               );
412
413 ADD_INTERFACE (Break_aligned_interface,
414                "Breakable items.",
415
416                /* properties */
417                "break-align-anchor "
418                "break-align-anchor-alignment "
419                "break-align-symbol "
420                "space-alist "
421               );