summaryrefslogtreecommitdiff
path: root/lily/span-bar-stub-engraver.cc
blob: a0c10f8594ae1165cbe74fcdd3b077ad94c0cd86 (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
/*
  This file is part of LilyPond, the GNU music typesetter.

  Copyright (C) 2011--2015 Mike Solomon <mike@mikesolomon.org>

  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 <algorithm>

#include "align-interface.hh"
#include "context.hh"
#include "grob.hh"
#include "grob-properties.hh"
#include "item.hh"
#include "pointer-group-interface.hh"
#include "engraver.hh"

#include "translator.icc"

/*
  The Span_bar_stub_engraver creates SpanBarStub grobs in the contexts
  that a grouping context contains.  For example, if a PianoStaff contains
  two Staffs, a Dynamics, and a Lyrics, SpanBarStubs will be created in
  all contexts that do not have bar lines (Dynamics and Lyrics).

  We only want to create these SpanBarStubs in contexts that the SpanBar
  traverses.  However, Contexts do not contain layout information and it
  is thus difficult to know if they will eventually be above or below other
  Contexts.  To determine this we use the VerticalAxisGroup created in the
  Context.  We relate VerticalAxisGroups to Contexts in the variable
  axis_groups_ and weed out unused contexts after each translation timestep.

  Note that SpanBarStubs exist for pure height calculations ONLY.
  They should never be visually present on the page and should never
  be engraved in contexts where BarLines are engraved.
*/

class Span_bar_stub_engraver : public Engraver
{
  vector<Grob *> spanbars_;
  SCM axis_groups_;

public:
  TRANSLATOR_DECLARATIONS (Span_bar_stub_engraver);
protected:
  void acknowledge_span_bar (Grob_info);
  void acknowledge_hara_kiri_group_spanner (Grob_info);
  void process_acknowledged ();
  void stop_translation_timestep ();
  virtual void derived_mark () const;
};

Span_bar_stub_engraver::Span_bar_stub_engraver ()
{
  axis_groups_ = SCM_EOL;
}

void
Span_bar_stub_engraver::derived_mark () const
{
  scm_gc_mark (axis_groups_);
}

void
Span_bar_stub_engraver::acknowledge_span_bar (Grob_info i)
{
  spanbars_.push_back (i.grob ());
}

void
Span_bar_stub_engraver::acknowledge_hara_kiri_group_spanner (Grob_info i)
{
  axis_groups_ = scm_cons (scm_cons (i.grob ()->self_scm (), i.context ()->self_scm ()), axis_groups_);
}

void
Span_bar_stub_engraver::process_acknowledged ()
{
  if (!spanbars_.size ())
    return;

  if (!scm_is_pair (axis_groups_))
    {
      programming_error ("At least one vertical axis group needs to be created in the first time step.");
      return;
    }
  Grob *vertical_alignment = Grob::get_root_vertical_alignment (unsmob<Grob> (scm_caar (axis_groups_)));
  if (!vertical_alignment) // we are at the beginning of a score, so no need for stubs
    return;

  for (vsize i = 0; i < spanbars_.size (); i++)
    {
      extract_grob_set (spanbars_[i], "elements", bars);
      vector<vsize> bar_axis_indices;
      for (vsize j = 0; j < bars.size (); j++)
        {
          int i = Grob::get_vertical_axis_group_index (bars[j]);
          if (i >= 0)
            bar_axis_indices.push_back ((vsize) i);
        }
      vector<Context *> affected_contexts;
      vector<Grob *> y_parents;
      vector<bool> keep_extent;
      for (SCM s = axis_groups_; scm_is_pair (s); s = scm_cdr (s))
        {
          Context *c = unsmob<Context> (scm_cdar (s));
          Grob *g = unsmob<Grob> (scm_caar (s));
          if (!c || !g)
            continue;
          if (c->is_removable ())
            continue;

          vsize j = Grob::get_vertical_axis_group_index (g);
          if (j > bar_axis_indices[0]
              && j < bar_axis_indices.back ()
              && find (bar_axis_indices.begin (), bar_axis_indices.end (), j) == bar_axis_indices.end ())
            {
              vsize k = 0;
              for (; k < bar_axis_indices.size (); k++)
                if (bar_axis_indices[k] > j)
                  break;

              k--;

              if (c && c->get_parent_context ())
                {
                  keep_extent.push_back (to_boolean (bars[k]->get_property ("allow-span-bar")));
                  y_parents.push_back (g);
                  affected_contexts.push_back (c);
                }
            }
        }

      for (vsize j = 0; j < affected_contexts.size (); j++)
        {
          Item *it = new Item (Grob_property_info (affected_contexts[j], ly_symbol2scm ("SpanBarStub")).updated ());
          it->set_parent (spanbars_[i], X_AXIS);
          Grob_info gi = make_grob_info (it, spanbars_[i]->self_scm ());
          announce_grob (gi, affected_contexts[j]);
          if (!keep_extent[j])
            it->suicide ();
        }
    }

  spanbars_.clear ();
}

// removes all unused contexts
void
Span_bar_stub_engraver::stop_translation_timestep ()
{
  SCM axis_groups = SCM_EOL;
  for (SCM s = axis_groups_; scm_is_pair (s); s = scm_cdr (s))
    {
      Context *c = unsmob<Context> (scm_cdar (s));
      Grob *g = unsmob<Grob> (scm_caar (s));
      if (!c || !g)
        continue;
      if (c->is_removable ())
        continue;
      axis_groups = scm_cons (scm_car (s), axis_groups);
    }
  axis_groups_ = axis_groups;
}

void
Span_bar_stub_engraver::boot ()
{
  ADD_ACKNOWLEDGER (Span_bar_stub_engraver, span_bar);
  ADD_ACKNOWLEDGER (Span_bar_stub_engraver, hara_kiri_group_spanner);
}

ADD_TRANSLATOR (Span_bar_stub_engraver,
                /* doc */
                "Make stubs for span bars in all contexts that the span bars cross.",

                /* create */
                "SpanBarStub ",

                /* read */
                "",

                /* write */
                ""
               );