summaryrefslogtreecommitdiff
path: root/lily/ledger-line-spanner.cc
blob: d36f908b2c81d78aae69d3b1b8136500350b961d (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
/*
  This file is part of LilyPond, the GNU music typesetter.

  Copyright (C) 2004--2012 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 <map>
using namespace std;

#include "note-head.hh"
#include "staff-symbol-referencer.hh"
#include "staff-symbol.hh"
#include "lookup.hh"
#include "spanner.hh"
#include "pointer-group-interface.hh"
#include "paper-column.hh"

struct Ledger_line_spanner
{
  DECLARE_SCHEME_CALLBACK (print, (SCM));
  DECLARE_SCHEME_CALLBACK (set_spacing_rods, (SCM));
  DECLARE_GROB_INTERFACE ();
};

static void
set_rods (Drul_array<Interval> const &current_extents,
          Drul_array<Interval> const &previous_extents,
          Item *current_column,
          Item *previous_column,
          Real min_length)
{
  for (UP_and_DOWN (d))
    {
      if (!current_extents[d].is_empty ()
          && !previous_extents[d].is_empty ())
        {
          Rod rod;
          rod.distance_ = 2 * min_length
                          /*
                            we go from right to left.
                          */
                          - previous_extents[d][LEFT]
                          + current_extents[d][RIGHT];

          rod.item_drul_[LEFT] = current_column;
          rod.item_drul_[RIGHT] = previous_column;
          rod.add_to_cols ();
        }
    }
}

MAKE_SCHEME_CALLBACK (Ledger_line_spanner, set_spacing_rods, 1);
SCM
Ledger_line_spanner::set_spacing_rods (SCM smob)
{
  Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));

  // find size of note heads.
  Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
  if (!staff)
    {
      me->suicide ();
      return SCM_EOL;
    }

  Real min_length_fraction
    = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15);

  Drul_array<Interval> current_extents;
  Drul_array<Interval> previous_extents;
  Real current_head_width = 0.0;
  Item *previous_column = 0;
  Item *current_column = 0;

  Real halfspace = Staff_symbol::staff_space (staff) / 2;

  Interval staff_extent = staff->extent (staff, Y_AXIS);
  staff_extent *= 1 / halfspace;

  /*
    Run through heads using a loop. Since Ledger_line_spanner can
    contain a lot of noteheads, superlinear performance is too slow.
  */
  extract_item_set (me, "note-heads", heads);
  for (vsize i = heads.size (); i--;)
    {
      Item *h = heads[i];

      int pos = Staff_symbol_referencer::get_rounded_position (h);
      if (staff_extent.contains (pos))
        continue;

      /* Ambitus heads can appear out-of-order in heads[],
       * but as part of prefatory matter, they need no rods */
      if (h->internal_has_interface (ly_symbol2scm ("ambitus-interface")))
        continue;

      Item *column = h->get_column ();
      if (current_column != column)
        {
          set_rods (current_extents, previous_extents,
                    current_column, previous_column,
                    current_head_width * min_length_fraction);

          previous_column = current_column;
          current_column = column;
          previous_extents = current_extents;

          current_extents[DOWN].set_empty ();
          current_extents[UP].set_empty ();
          current_head_width = 0.0;
        }

      Interval head_extent = h->extent (column, X_AXIS);
      Direction vdir = Direction (sign (pos));
      if (!vdir)
        continue;

      current_extents[vdir].unite (head_extent);
      current_head_width = max (current_head_width, head_extent.length ());
    }

  if (previous_column && current_column)
    set_rods (current_extents, previous_extents,
              current_column, previous_column,
              current_head_width * min_length_fraction);

  return SCM_UNSPECIFIED;
}

struct Ledger_request
{
  Interval ledger_extent_;
  Interval head_extent_;
  int position_;
  Ledger_request ()
  {
    ledger_extent_.set_empty ();
    head_extent_.set_empty ();
    position_ = 0;
  }
};

typedef map < int, Drul_array<Ledger_request> > Ledger_requests;

/*
  TODO: ledger share a lot of info. Lots of room to optimize away
  common use of objects/variables.
*/
MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1);
SCM
Ledger_line_spanner::print (SCM smob)
{
  Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));

  extract_grob_set (me, "note-heads", heads);

  if (heads.empty ())
    return SCM_EOL;

  // find size of note heads.
  Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
  if (!staff)
    return SCM_EOL;

  Real halfspace = Staff_symbol::staff_space (staff) / 2;

  Interval staff_extent = staff->extent (staff, Y_AXIS);
  staff_extent *= 1 / halfspace;

  Real length_fraction
    = robust_scm2double (me->get_property ("length-fraction"), 0.25);

  Stencil ledgers;

  Grob *common[NO_AXES];

  for (int i = X_AXIS; i < NO_AXES; i++)
    {
      Axis a = Axis (i);
      common[a] = common_refpoint_of_array (heads, me, a);
      for (vsize i = heads.size (); i--;)
        if (Grob *g = unsmob_grob (me->get_object ("accidental-grob")))
          common[a] = common[a]->common_refpoint (g, a);
    }

  Ledger_requests reqs;
  for (vsize i = heads.size (); i--;)
    {
      Item *h = dynamic_cast<Item *> (heads[i]);

      int pos = Staff_symbol_referencer::get_rounded_position (h);
      if (pos && !staff_extent.contains (pos))
        {
          Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
          Interval ledger_extent = head_extent;
          ledger_extent.widen (length_fraction * head_extent.length ());

          Direction vdir = Direction (sign (pos));
          int rank = h->get_column ()->get_rank ();

          reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
          reqs[rank][vdir].head_extent_.unite (head_extent);
          reqs[rank][vdir].position_
            = vdir * max (vdir * reqs[rank][vdir].position_, vdir * pos);
        }
    }

  // determine maximum size for non-colliding ledger.
  Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
  Ledger_requests::iterator last (reqs.end ());
  for (Ledger_requests::iterator i (reqs.begin ());
       i != reqs.end (); last = i++)
    {
      if (last == reqs.end ())
        continue;

      for (DOWN_and_UP (d))
        {
          if (!staff_extent.contains (last->second[d].position_)
              && !staff_extent.contains (i->second[d].position_))
            {
              Real center
                = (last->second[d].head_extent_[RIGHT]
                   + i->second[d].head_extent_[LEFT]) / 2;

              for (LEFT_and_RIGHT (which))
                {
                  Ledger_request &lr = ((which == LEFT) ? * last : *i).second[d];

                  // due tilt of quarter note-heads
                  /* FIXME */
                  bool both
                    = (!staff_extent.contains (last->second[d].position_
                                               - sign (last->second[d].position_))
                       && !staff_extent.contains (i->second[d].position_
                                                  - sign (i->second[d].position_)));
                  Real limit = (center + (both ? which * gap / 2 : 0));
                  lr.ledger_extent_.at (-which)
                    = which * max (which * lr.ledger_extent_[-which], which * limit);
                }
            }
        }
    }

  // create ledgers for note heads
  Real ledgerlinethickness
    = Staff_symbol::get_ledger_line_thickness (staff);
  for (vsize i = heads.size (); i--;)
    {
      Item *h = dynamic_cast<Item *> (heads[i]);

      int pos = Staff_symbol_referencer::get_rounded_position (h);
      vector<Real> ledger_positions = Staff_symbol::ledger_positions (staff, pos);
      if (!ledger_positions.empty ())
        {
          int ledger_count = ledger_positions.size ();
          Interval head_size = h->extent (common[X_AXIS], X_AXIS);
          Interval ledger_size = head_size;
          ledger_size.widen (ledger_size.length () * length_fraction);

          if (pos && !staff_extent.contains (pos))
            {
              Interval max_size = reqs[h->get_column ()->get_rank ()]
                                  [Direction (sign (pos))].ledger_extent_;

              if (!max_size.is_empty ())
                ledger_size.intersect (max_size);
            }

          for (int i = 0; i < ledger_count; i++)
            {
              Real lpos = ledger_positions[i];
              Interval x_extent = ledger_size;

              if (i == 0)
                if (Grob *g = unsmob_grob (h->get_object ("accidental-grob")))
                  {
                    Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
                    Real d
                      = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
                                                              head_size[LEFT]),
                                            0.0);

                    Real left_shorten = max (-ledger_size[LEFT] + d, 0.0);

                    x_extent[LEFT] += left_shorten;
                    /*
                      TODO: shorten 2 ledger lines for the case natural +
                      downstem.
                    */
                  }

              Real blotdiameter = ledgerlinethickness;
              Interval y_extent
                = Interval (-0.5 * (ledgerlinethickness),
                            +0.5 * (ledgerlinethickness));
              Stencil ledger_line
                = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);

              ledger_line.translate_axis ( lpos * halfspace, Y_AXIS);
              ledgers.add_stencil (ledger_line);
            }
        }
    }

  ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
                          X_AXIS);

  return ledgers.smobbed_copy ();
}

ADD_INTERFACE (Ledger_line_spanner,
               "This spanner draws the ledger lines of a staff.  This is a"
               " separate grob because it has to process all potential"
               " collisions between all note heads.",

               /* properties */
               "gap "
               "length-fraction "
               "minimum-length-fraction "
               "note-heads "
               "thickness "
              );

struct Ledgered_interface
{
  DECLARE_GROB_INTERFACE ();
};

ADD_INTERFACE (Ledgered_interface,
               "Objects that need ledger lines, typically note heads.  See"
               " also @ref{ledger-line-spanner-interface}.",

               /* properties */
               "no-ledgers "
              );