]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-align-engraver.cc
Run grand replace for 2015.
[lilypond.git] / lily / dynamic-align-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2008--2015 Han-Wen Nienhuys <hanwen@lilypond.org>
5
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <set>
22
23 #include "engraver.hh"
24
25 #include "axis-group-interface.hh"
26 #include "directional-element-interface.hh"
27 #include "item.hh"
28 #include "side-position-interface.hh"
29 #include "spanner.hh"
30 #include "stream-event.hh"
31
32 #include "translator.icc"
33
34 class Dynamic_align_engraver : public Engraver
35 {
36   TRANSLATOR_DECLARATIONS (Dynamic_align_engraver);
37   DECLARE_ACKNOWLEDGER (rhythmic_head);
38   DECLARE_ACKNOWLEDGER (stem);
39   DECLARE_ACKNOWLEDGER (dynamic);
40   DECLARE_ACKNOWLEDGER (footnote_spanner);
41   DECLARE_END_ACKNOWLEDGER (dynamic);
42
43 protected:
44   virtual void stop_translation_timestep ();
45
46 private:
47   void create_line_spanner (Grob *cause);
48   void set_spanner_bounds (Spanner *line, bool end);
49   Spanner *line_;
50   Spanner *ended_line_; // Spanner manually broken, don't use it for new grobs
51   Spanner *current_dynamic_spanner_;
52   vector<Spanner *> ended_;
53   vector<Spanner *> started_;
54   vector<Grob *> scripts_;
55   vector<Grob *> support_;
56
57   set<Spanner *> running_;
58 };
59
60 Dynamic_align_engraver::Dynamic_align_engraver ()
61 {
62   line_ = 0;
63   ended_line_ = 0;
64   current_dynamic_spanner_ = 0;
65 }
66
67 ADD_ACKNOWLEDGER (Dynamic_align_engraver, dynamic);
68 ADD_ACKNOWLEDGER (Dynamic_align_engraver, rhythmic_head);
69 ADD_ACKNOWLEDGER (Dynamic_align_engraver, stem);
70 ADD_ACKNOWLEDGER (Dynamic_align_engraver, footnote_spanner);
71 ADD_END_ACKNOWLEDGER (Dynamic_align_engraver, dynamic);
72
73 void
74 Dynamic_align_engraver::create_line_spanner (Grob *cause)
75 {
76   if (!line_)
77     line_ = make_spanner ("DynamicLineSpanner", cause->self_scm ());
78 }
79
80 void
81 Dynamic_align_engraver::acknowledge_end_dynamic (Grob_info info)
82 {
83   if (Spanner::has_interface (info.grob ()))
84     ended_.push_back (info.spanner ());
85
86   /* If the break flag is set, store the current spanner and let new dynamics
87    * create a new spanner
88    */
89   bool spanner_broken = current_dynamic_spanner_ == info.spanner ()
90                         && to_boolean (current_dynamic_spanner_->get_property ("spanner-broken"));
91   if (spanner_broken && line_)
92     {
93       if (ended_line_)
94         programming_error ("already have a force-ended DynamicLineSpanner.");
95       ended_line_ = line_;
96       line_ = 0;
97       current_dynamic_spanner_ = 0;
98     }
99 }
100
101 void
102 Dynamic_align_engraver::acknowledge_footnote_spanner (Grob_info info)
103 {
104   Grob *parent = info.grob ()->get_parent (Y_AXIS);
105   if (line_ && parent
106       && parent->internal_has_interface (ly_symbol2scm ("dynamic-interface")))
107     Axis_group_interface::add_element (line_, info.grob ());
108 }
109
110 void
111 Dynamic_align_engraver::acknowledge_rhythmic_head (Grob_info info)
112 {
113   support_.push_back (info.grob ());
114 }
115
116 void
117 Dynamic_align_engraver::acknowledge_stem (Grob_info info)
118 {
119   support_.push_back (info.grob ());
120 }
121
122 void
123 Dynamic_align_engraver::acknowledge_dynamic (Grob_info info)
124 {
125   Stream_event *cause = info.event_cause ();
126   // Check whether an existing line spanner has the same direction
127   if (line_ && cause)
128     {
129       Direction line_dir = get_grob_direction (line_);
130       Direction grob_dir = to_dir (cause->get_property ("direction"));
131
132       // If we have an explicit direction for the new dynamic grob
133       // that differs from the current line spanner, break the spanner
134       if (grob_dir && (line_dir != grob_dir))
135         {
136           if (!ended_line_)
137             ended_line_ = line_;
138           line_ = 0;
139           current_dynamic_spanner_ = 0;
140         }
141     }
142
143   create_line_spanner (info.grob ());
144   if (Spanner::has_interface (info.grob ()))
145     {
146       started_.push_back (info.spanner ());
147       current_dynamic_spanner_ = info.spanner ();
148     }
149   else if (info.item ())
150     scripts_.push_back (info.item ());
151   else
152     info.grob ()->programming_error ("unknown dynamic grob");
153
154   Axis_group_interface::add_element (line_, info.grob ());
155
156   if (cause)
157     {
158       if (Direction d = to_dir (cause->get_property ("direction")))
159         set_grob_direction (line_, d);
160     }
161 }
162
163 void
164 Dynamic_align_engraver::set_spanner_bounds (Spanner *line, bool end)
165 {
166   if (!line)
167     return;
168
169   for (LEFT_and_RIGHT (d))
170     {
171       if ((d == LEFT && !line->get_bound (LEFT))
172           || (end && d == RIGHT && !line->get_bound (RIGHT)))
173         {
174           vector<Spanner *> const &spanners
175             = (d == LEFT) ? started_ : ended_;
176
177           Grob *bound = 0;
178           if (scripts_.size ())
179             bound = scripts_[0];
180           else if (spanners.size ())
181             bound = spanners[0]->get_bound (d);
182           else
183             {
184               bound = Grob::unsmob (get_property ("currentMusicalColumn"));
185               programming_error ("started DynamicLineSpanner but have no left bound");
186             }
187
188           line->set_bound (d, bound);
189         }
190     }
191 }
192
193 void
194 Dynamic_align_engraver::stop_translation_timestep ()
195 {
196   for (vsize i = 0; i < started_.size (); i++)
197     running_.insert (started_[i]);
198   for (vsize i = 0; i < ended_.size (); i++)
199     {
200       Spanner *sp = ended_[i];
201
202       set<Spanner *>::iterator it = running_.find (sp);
203       if (it != running_.end ())
204         running_.erase (it);
205       else
206         started_[i]->programming_error ("lost track of this dynamic spanner");
207     }
208
209   bool end = line_ && running_.empty ();
210
211   // Set the proper bounds for the current spanner and for a spanner that
212   // is ended now
213   set_spanner_bounds (ended_line_, true);
214   set_spanner_bounds (line_, end);
215   // If the flag is set to break the spanner after the current child, don't
216   // add any more support points (needed e.g. for style=none, where the
217   // invisible spanner should NOT be shifted since we don't have a line).
218   bool spanner_broken = current_dynamic_spanner_
219                         && to_boolean (current_dynamic_spanner_->get_property ("spanner-broken"));
220   for (vsize i = 0; line_ && !spanner_broken && i < support_.size (); i++)
221     Side_position_interface::add_support (line_, support_[i]);
222
223   if (end)
224     {
225       line_ = 0;
226     }
227
228   ended_line_ = 0;
229   ended_.clear ();
230   started_.clear ();
231   scripts_.clear ();
232   support_.clear ();
233 }
234
235 ADD_TRANSLATOR (Dynamic_align_engraver,
236                 /* doc */
237                 "Align hairpins and dynamic texts on a horizontal line.",
238
239                 /* create */
240                 "DynamicLineSpanner ",
241
242                 /* read */
243                 "currentMusicalColumn ",
244
245                 /* write */
246                 ""
247                );