summaryrefslogtreecommitdiff
path: root/lily/break-alignment-interface.cc
blob: e2a546f6a676ed1ecbbbc85ef56414aea512e0a2 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
  This file is part of LilyPond, the GNU music typesetter.

  Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>

  LilyPond is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  LilyPond is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "break-align-interface.hh"

#include "align-interface.hh"
#include "axis-group-interface.hh"
#include "dimensions.hh"
#include "international.hh"
#include "output-def.hh"
#include "paper-column.hh"
#include "pointer-group-interface.hh"
#include "side-position-interface.hh"
#include "warn.hh"

/*
  This is tricky: we cannot modify 'elements, since callers are
  iterating the same list. Reordering the list in-place, or resetting
  'elements will skip elements in the loops of callers.

  So we return the correct order as an array.
*/
SCM
Break_alignment_interface::break_align_order (Item *me)
{
  SCM order_vec = me->get_property ("break-align-orders");
  if (!scm_is_vector (order_vec)
      || scm_c_vector_length (order_vec) < 3)
    return SCM_BOOL_F;

  SCM order = scm_vector_ref (order_vec,
                              scm_from_int (me->break_status_dir () + 1));

  return order;
}

vector<Grob *>
Break_alignment_interface::ordered_elements (Grob *grob)
{
  Item *me = dynamic_cast<Item *> (grob);
  extract_grob_set (me, "elements", elts);

  SCM order = break_align_order (me);

  if (scm_is_false (order))
    return elts;

  vector<Grob *> writable_elts (elts);
  /*
   Copy in order specified in BREAK-ALIGN-ORDER.
  */
  vector<Grob *> new_elts;
  for (; scm_is_pair (order); order = scm_cdr (order))
    {
      SCM sym = scm_car (order);

      for (vsize i = writable_elts.size (); i--;)
        {
          Grob *g = writable_elts[i];
          if (g && sym == g->get_property ("break-align-symbol"))
            {
              new_elts.push_back (g);
              writable_elts.erase (writable_elts.begin () + i);
            }
        }
    }

  return new_elts;
}

void
Break_alignment_interface::add_element (Grob *me, Grob *toadd)
{
  Align_interface::add_element (me, toadd);
}

MAKE_SCHEME_CALLBACK (Break_alignment_interface, calc_positioning_done, 1)
SCM
Break_alignment_interface::calc_positioning_done (SCM smob)
{
  Grob *grob = unsmob<Grob> (smob);
  Item *me = dynamic_cast<Item *> (grob);

  me->set_property ("positioning-done", SCM_BOOL_T);

  vector<Grob *> elems = ordered_elements (me);
  vector<Interval> extents;

  for (vsize i = 0; i < elems.size (); i++)
    {
      Interval y = elems[i]->extent (elems[i], X_AXIS);
      extents.push_back (y);
    }

  vsize idx = 0;
  while (idx < extents.size () && extents[idx].is_empty ())
    idx++;

  vector<Real> offsets;
  offsets.resize (elems.size ());
  for (vsize i = 0; i < offsets.size (); i++)
    offsets[i] = 0.0;

  Real extra_right_space = 0.0;
  vsize edge_idx = VPOS;
  while (idx < elems.size ())
    {
      vsize next_idx = idx + 1;
      while (next_idx < elems.size ()
             && extents[next_idx].is_empty ())
        next_idx++;

      Grob *l = elems[idx];
      Grob *r = 0;

      if (next_idx < elems.size ())
        r = elems[next_idx];

      SCM alist = SCM_EOL;

      /*
        Find the first grob with a space-alist entry.
      */
      extract_grob_set (l, "elements", elts);

      for (vsize i = elts.size (); i--;)
        {
          Grob *elt = elts[i];

          if (edge_idx == VPOS
              && scm_is_eq (elt->get_property ("break-align-symbol"),
                            ly_symbol2scm ("left-edge")))
            edge_idx = idx;

          SCM l = elt->get_property ("space-alist");
          if (scm_is_pair (l))
            {
              alist = l;
              break;
            }
        }

      SCM rsym = r ? SCM_EOL : ly_symbol2scm ("right-edge");

      /*
        We used to use #'cause to find out the symbol and the spacing
        table, but that gets icky when that grob is suicided for some
        reason.
      */
      if (r)
        {
          extract_grob_set (r, "elements", elts);
          for (vsize i = elts.size ();
               !scm_is_symbol (rsym) && i--;)
            {
              Grob *elt = elts[i];
              rsym = elt->get_property ("break-align-symbol");
            }
        }

      if (scm_is_eq (rsym, ly_symbol2scm ("left-edge")))
        edge_idx = next_idx;

      SCM entry = SCM_EOL;
      if (scm_is_symbol (rsym))
        entry = scm_assq (rsym, alist);

      bool entry_found = scm_is_pair (entry);
      if (!entry_found)
        {
          string sym_string;
          if (scm_is_symbol (rsym))
            sym_string = ly_symbol2string (rsym);

          string orig_string;
          if (unsmob<Grob> (l->get_property ("cause")))
            orig_string = unsmob<Grob> (l->get_property ("cause"))->name ();

          programming_error (to_string ("No spacing entry from %s to `%s'",
                                        orig_string.c_str (),
                                        sym_string.c_str ()));
        }

      Real distance = 1.0;
      SCM type = ly_symbol2scm ("extra-space");

      if (entry_found)
        {
          entry = scm_cdr (entry);

          distance = scm_to_double (scm_cdr (entry));
          type = scm_car (entry);
        }

      if (r)
        {
          if (scm_is_eq (type, ly_symbol2scm ("extra-space")))
            offsets[next_idx] = extents[idx][RIGHT] + distance
                                - extents[next_idx][LEFT];
          /* should probably junk minimum-space */
          else if (scm_is_eq (type, ly_symbol2scm ("minimum-space")))
            offsets[next_idx] = max (extents[idx][RIGHT], distance);
        }
      else
        {
          extra_right_space = distance;
          if (idx + 1 < offsets.size ())
            offsets[idx + 1] = extents[idx][RIGHT] + distance;
        }

      idx = next_idx;
    }

  Real here = 0.0;
  Interval total_extent;

  Real alignment_off = 0.0;
  for (vsize i = 0; i < offsets.size (); i++)
    {
      here += offsets[i];
      if (i == edge_idx)
        alignment_off = -here;
      total_extent.unite (extents[i] + here);
    }

  if (total_extent.is_empty ())
    return SCM_BOOL_T;

  if (me->break_status_dir () == LEFT)
    alignment_off = -total_extent[RIGHT] - extra_right_space;
  else if (edge_idx == VPOS)
    alignment_off = -total_extent[LEFT];

  here = alignment_off;
  for (vsize i = 0; i < offsets.size (); i++)
    {
      here += offsets[i];
      elems[i]->translate_axis (here, X_AXIS);
    }

  return SCM_BOOL_T;
}

MAKE_SCHEME_CALLBACK (Break_alignable_interface, self_align_callback, 1)
SCM
Break_alignable_interface::self_align_callback (SCM grob)
{
  Grob *me = unsmob<Grob> (grob);
  Item *alignment = dynamic_cast<Item *> (me->get_parent (X_AXIS));
  if (!has_interface<Break_alignment_interface> (alignment))
    return scm_from_int (0);

  SCM symbol_list = me->get_property ("break-align-symbols");
  vector<Grob *> elements = Break_alignment_interface::ordered_elements (alignment);
  if (elements.size () == 0)
    return scm_from_int (0);

  int break_aligned_grob = -1;
  for (; scm_is_pair (symbol_list); symbol_list = scm_cdr (symbol_list))
    {
      SCM sym = scm_car (symbol_list);
      for (vsize i = 0; i < elements.size (); i++)
        {
          if (elements[i]->get_property ("break-align-symbol") == sym)
            {
              if (Item::break_visible (elements[i])
                  && !elements[i]->extent (elements[i], X_AXIS).is_empty ())
                {
                  break_aligned_grob = i;
                  goto found_break_aligned_grob; /* ugh. need to break out of 2 loops */
                }
              else if (break_aligned_grob == -1)
                break_aligned_grob = i;
            }
        }
    }

found_break_aligned_grob:
  if (break_aligned_grob == -1)
    return scm_from_int (0);

  Grob *alignment_parent = elements[break_aligned_grob];
  Grob *common = me->common_refpoint (alignment_parent, X_AXIS);
  Real anchor = robust_scm2double (alignment_parent->get_property ("break-align-anchor"), 0);

  return scm_from_double (alignment_parent->relative_coordinate (common, X_AXIS)
                          - me->relative_coordinate (common, X_AXIS)
                          + anchor);
}

MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_average_anchor, 1)
SCM
Break_aligned_interface::calc_average_anchor (SCM grob)
{
  Grob *me = unsmob<Grob> (grob);
  Real avg = 0.0;
  int count = 0;

  /* average the anchors of those children that have it set */
  extract_grob_set (me, "elements", elts);
  for (vsize i = 0; i < elts.size (); i++)
    {
      SCM anchor = elts[i]->get_property ("break-align-anchor");
      if (scm_is_number (anchor))
        {
          count++;
          avg += scm_to_double (anchor);
        }
    }

  return scm_from_double (count > 0 ? avg / count : 0);
}

MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_extent_aligned_anchor, 1)
SCM
Break_aligned_interface::calc_extent_aligned_anchor (SCM smob)
{
  Grob *me = unsmob<Grob> (smob);
  Real alignment = robust_scm2double (me->get_property ("break-align-anchor-alignment"), 0.0);
  Interval iv = me->extent (me, X_AXIS);

  if (isinf (iv[LEFT]) && isinf (iv[RIGHT])) /* avoid NaN */
    return scm_from_double (0.0);

  return scm_from_double (iv.linear_combination (alignment));
}

MAKE_SCHEME_CALLBACK (Break_aligned_interface, calc_break_visibility, 1)
SCM
Break_aligned_interface::calc_break_visibility (SCM smob)
{
  /* a BreakAlignGroup is break-visible if it has one element that is break-visible */
  Grob *me = unsmob<Grob> (smob);
  SCM ret = scm_c_make_vector (3, SCM_EOL);
  extract_grob_set (me, "elements", elts);
  for (int dir = 0; dir <= 2; dir++)
    {
      bool visible = false;
      for (vsize i = 0; i < elts.size (); i++)
        {
          SCM vis = elts[i]->get_property ("break-visibility");
          if (scm_is_vector (vis) && to_boolean (scm_c_vector_ref (vis, dir)))
            visible = true;
        }
      scm_c_vector_set_x (ret, dir, scm_from_bool (visible));
    }
  return ret;
}

ADD_INTERFACE (Break_alignment_interface,
               "The object that performs break alignment.\n"
               "\n"
               "Three interfaces deal specifically with break alignment:\n"
               "@enumerate\n"
               "@item break-alignment-interface (this one),\n"
               "@item @ref{break-alignable-interface}, and\n"
               "@item @ref{break-aligned-interface}.\n"
               "@end enumerate\n"
               "\n"
               " Each of these interfaces supports grob properties that use"
               " @w{@emph{break-align symbols}}, which are Scheme symbols that"
               " are used to specify the alignment, ordering, and spacing of"
               " certain notational elements (@q{breakable}@tie{}items)."
               "\n"
               "@subsubheading Available break-align symbols:\n"
               "\n"
               "@example\n"
               "ambitus\n"
               "breathing-sign\n"
               "clef\n"
               "cue-clef\n"
               "cue-end-clef\n"
               "custos\n"
               "key-cancellation\n"
               "key-signature\n"
               "left-edge\n"
               "staff-bar\n"
               "time-signature\n"
               "@end example",

               /* properties */
               "positioning-done "
               "break-align-orders "
              );

ADD_INTERFACE (Break_alignable_interface,
               "Object that is aligned on a break alignment.",

               /* properties */
               "break-align-symbols "
               "non-break-align-symbols "
              );

ADD_INTERFACE (Break_aligned_interface,
               "Breakable items.",

               /* properties */
               "break-align-anchor "
               "break-align-anchor-alignment "
               "break-align-symbol "
               "space-alist "
              );