]> git.donarmstrong.com Git - lilypond.git/blob - lily/volta-engraver.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / volta-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--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 "engraver.hh"
21
22 #include "axis-group-interface.hh"
23 #include "context.hh"
24 #include "grob-array.hh"
25 #include "international.hh"
26 #include "note-column.hh"
27 #include "item.hh"
28 #include "side-position-interface.hh"
29 #include "staff-symbol.hh"
30 #include "text-interface.hh"
31 #include "volta-bracket.hh"
32 #include "warn.hh"
33
34 #include "translator.icc"
35
36 /*
37   Create Volta spanners, by reading repeatCommands  property, usually
38   set by Volta_repeat_iterator.
39 */
40 class Volta_engraver : public Engraver
41 {
42 public:
43   TRANSLATOR_DECLARATIONS (Volta_engraver);
44 protected:
45
46   DECLARE_ACKNOWLEDGER (bar_line);
47
48   virtual void derived_mark () const;
49   void stop_translation_timestep ();
50   void process_music ();
51
52   Moment started_mom_;
53   Spanner *volta_bracket_;
54   Spanner *end_volta_bracket_;
55   Spanner *volta_spanner_;
56   SCM start_string_;
57 };
58
59 void
60 Volta_engraver::derived_mark () const
61 {
62   scm_gc_mark (start_string_);
63 }
64
65 Volta_engraver::Volta_engraver ()
66 {
67   start_string_ = SCM_EOL;
68   volta_bracket_ = 0;
69   end_volta_bracket_ = 0;
70   volta_spanner_ = 0;
71 }
72
73 void
74 Volta_engraver::process_music ()
75 {
76   SCM cs = get_property ("repeatCommands");
77
78   bool end = false;
79   start_string_ = SCM_EOL;
80   while (scm_is_pair (cs))
81     {
82       SCM c = scm_car (cs);
83
84       if (scm_is_pair (c)
85           && scm_car (c) == ly_symbol2scm ("volta")
86           && scm_is_pair (scm_cdr (c)))
87         {
88           if (scm_cadr (c) == SCM_BOOL_F)
89             end = true;
90           else
91             start_string_ = scm_cadr (c);
92         }
93
94       cs = scm_cdr (cs);
95     }
96
97   if (volta_bracket_)
98     {
99       SCM l (get_property ("voltaSpannerDuration"));
100       Moment now = now_mom ();
101
102       bool early_stop = unsmob_moment (l)
103                         && *unsmob_moment (l) <= now - started_mom_;
104
105       end = end || early_stop;
106     }
107
108   if (end && !volta_bracket_)
109     /* fixme: be more verbose.  */
110     warning (_ ("cannot end volta spanner"));
111   else if (end)
112     {
113       end_volta_bracket_ = volta_bracket_;
114       volta_bracket_ = 0;
115     }
116
117   if (volta_bracket_
118       && (scm_is_string (start_string_) || scm_is_pair (start_string_)))
119     {
120       warning (_ ("already have a volta spanner, ending that one prematurely"));
121
122       if (end_volta_bracket_)
123         {
124           warning (_ ("also already have an ended spanner"));
125           warning (_ ("giving up"));
126           return;
127         }
128
129       end_volta_bracket_ = volta_bracket_;
130       volta_bracket_ = 0;
131     }
132
133   if (!volta_bracket_
134       && Text_interface::is_markup (start_string_))
135     {
136       started_mom_ = now_mom ();
137
138       volta_bracket_ = make_spanner ("VoltaBracket", SCM_EOL);
139
140       volta_bracket_->set_property ("text", start_string_);
141
142       if (!volta_spanner_)
143         volta_spanner_ = make_spanner ("VoltaBracketSpanner", SCM_EOL);
144
145       Axis_group_interface::add_element (volta_spanner_, volta_bracket_);
146     }
147 }
148
149 void
150 Volta_engraver::acknowledge_bar_line (Grob_info i)
151 {
152   if (volta_bracket_)
153     Volta_bracket_interface::add_bar (volta_bracket_, i.item ());
154   if (end_volta_bracket_)
155     Volta_bracket_interface::add_bar (end_volta_bracket_, i.item ());
156
157   if (volta_spanner_)
158     Side_position_interface::add_support (volta_spanner_, i.grob ());
159 }
160
161 void
162 Volta_engraver::stop_translation_timestep ()
163 {
164   Grob *cc = unsmob_grob (get_property ("currentCommandColumn"));
165   Item *ci = dynamic_cast<Item *> (cc);
166
167   if (end_volta_bracket_ && !end_volta_bracket_->get_bound (RIGHT))
168     end_volta_bracket_->set_bound (RIGHT, ci);
169
170   if (volta_spanner_ && end_volta_bracket_)
171     volta_spanner_->set_bound (RIGHT, end_volta_bracket_->get_bound (RIGHT));
172
173   if (end_volta_bracket_ && !volta_bracket_)
174     {
175       for (SCM s = get_property ("stavesFound"); scm_is_pair (s); s = scm_cdr (s))
176         Side_position_interface::add_support (volta_spanner_, unsmob_grob (scm_car (s)));
177       volta_spanner_ = 0;
178     }
179
180   end_volta_bracket_ = 0;
181
182   if (volta_bracket_ && !volta_bracket_->get_bound (LEFT))
183     volta_bracket_->set_bound (LEFT, ci);
184
185   if (volta_spanner_ && volta_bracket_ && !volta_spanner_->get_bound (LEFT))
186     volta_spanner_->set_bound (LEFT, volta_bracket_->get_bound (LEFT));
187 }
188
189 /*
190   TODO: should attach volta to paper-column if no bar is found.
191 */
192 ADD_ACKNOWLEDGER (Volta_engraver, bar_line);
193 ADD_TRANSLATOR (Volta_engraver,
194                 /* doc */
195                 "Make volta brackets.",
196
197                 /* create */
198                 "VoltaBracket "
199                 "VoltaBracketSpanner ",
200
201                 /* read */
202                 "repeatCommands "
203                 "voltaSpannerDuration "
204                 "stavesFound ",
205
206                 /* write */
207                 ""
208                );