]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-align-engraver.cc
Fix #305: Allow alignment spanner to be broken for dynamics.
[lilypond.git] / lily / dynamic-align-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2008--2010 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_TRANSLATOR_LISTENER (break_span);
38   DECLARE_ACKNOWLEDGER (note_column);
39   DECLARE_ACKNOWLEDGER (dynamic);
40   DECLARE_END_ACKNOWLEDGER (dynamic);
41
42 protected:
43   virtual void stop_translation_timestep ();
44
45 private:
46   void create_line_spanner (Stream_event *cause);
47   Spanner *line_;
48   vector<Spanner *> ended_;
49   vector<Spanner *> started_;
50   vector<Grob *> scripts_;
51   vector<Grob *> support_;
52
53   set<Spanner *> running_;
54
55   bool early_end_;
56 };
57
58 Dynamic_align_engraver::Dynamic_align_engraver ()
59 {
60   line_ = 0;
61   early_end_ = false;
62 }
63
64 ADD_ACKNOWLEDGER (Dynamic_align_engraver, dynamic);
65 ADD_ACKNOWLEDGER (Dynamic_align_engraver, note_column);
66 ADD_END_ACKNOWLEDGER (Dynamic_align_engraver, dynamic);
67
68 void
69 Dynamic_align_engraver::create_line_spanner (Stream_event *event)
70 {
71   if (!line_)
72     line_ = make_spanner ("DynamicLineSpanner",
73                           event ? event->self_scm () : SCM_EOL);
74 }
75
76 void
77 Dynamic_align_engraver::acknowledge_end_dynamic (Grob_info info)
78 {
79   if (Spanner::has_interface (info.grob ()))
80     ended_.push_back (info.spanner ());
81 }
82
83 void
84 Dynamic_align_engraver::acknowledge_note_column (Grob_info info)
85 {
86   support_.push_back (info.grob ());
87 }
88
89 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_align_engraver, break_span);
90 void
91 Dynamic_align_engraver::listen_break_span (Stream_event *event)
92 {
93   if (event->in_event_class ("break-dynamic-span-event"))
94     early_end_ = true;
95 }
96
97 void
98 Dynamic_align_engraver::acknowledge_dynamic (Grob_info info)
99 {
100   Stream_event *cause = info.event_cause ();
101   create_line_spanner (cause);
102   if (Spanner::has_interface (info.grob ()))
103     started_.push_back (info.spanner ());
104   else if (info.item ())
105     scripts_.push_back (info.item ());
106   else
107     info.grob ()->programming_error ("unknown dynamic grob");
108
109   Axis_group_interface::add_element (line_, info.grob ());
110
111   if (cause)
112     {
113       if (Direction d = to_dir (cause->get_property ("direction")))
114         set_grob_direction (line_, d);
115     }
116 }
117
118 void
119 Dynamic_align_engraver::stop_translation_timestep ()
120 {
121   for (vsize i = 0; i < started_.size (); i++)
122     running_.insert (started_[i]);
123   for (vsize i = 0; i < ended_.size (); i++)
124     {
125       Spanner *sp = ended_[i];
126
127       set<Spanner *>::iterator it = running_.find (sp);
128       if (it != running_.end ())
129         running_.erase (it);
130       else
131         started_[i]->programming_error ("lost track of this dynamic spanner");
132     }
133
134   bool end = line_ && (running_.empty ()
135                        || early_end_);
136   Direction d = LEFT;
137   do
138     {
139       if (line_
140           && ((d == LEFT && !line_->get_bound (LEFT))
141               || (end && d == RIGHT && !line_->get_bound (RIGHT))))
142         {
143           vector<Spanner *> const &spanners
144             = (d == LEFT) ? started_ : ended_;
145
146           Grob *bound = 0;
147           if (scripts_.size ())
148             bound = scripts_[0];
149           else if (spanners.size ())
150             bound = spanners[0]->get_bound (d);
151           else
152             {
153               bound = unsmob_grob (get_property ("currentMusicalColumn"));
154               if (!early_end_)
155                 programming_error ("started DynamicLineSpanner but have no left bound");
156             }
157
158           line_->set_bound (d, bound);
159         }
160     }
161   while (flip (&d) != LEFT);
162
163   for (vsize i = 0; line_ && i < support_.size (); i++)
164     Side_position_interface::add_support (line_, support_[i]);
165
166   if (end)
167     {
168       line_ = 0;
169       early_end_ = false;
170     }
171
172   ended_.clear ();
173   started_.clear ();
174   scripts_.clear ();
175   support_.clear ();
176 }
177
178 ADD_TRANSLATOR (Dynamic_align_engraver,
179                 /* doc */
180                 "Align hairpins and dynamic texts on a horizontal line.",
181
182                 /* create */
183                 "DynamicLineSpanner ",
184
185                 /* read */
186                 "currentMusicalColumn ",
187
188                 /* write */
189                 ""
190                 );