]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-align-engraver.cc
Fix 1259/1433: linebreaks with \breakDynamicSpan or spanners with style=#'none
[lilypond.git] / lily / dynamic-align-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2008--2011 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 (note_column);
38   DECLARE_ACKNOWLEDGER (dynamic);
39   DECLARE_ACKNOWLEDGER (footnote_spanner);
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   void set_spanner_bounds (Spanner *line, bool end);
48   Spanner *line_;
49   Spanner *ended_line_; // Spanner manually broken, don't use it for new grobs
50   Spanner *current_dynamic_spanner_;
51   vector<Spanner *> ended_;
52   vector<Spanner *> started_;
53   vector<Grob *> scripts_;
54   vector<Grob *> support_;
55
56   set<Spanner *> running_;
57 };
58
59 Dynamic_align_engraver::Dynamic_align_engraver ()
60 {
61   line_ = 0;
62   ended_line_ = 0;
63   current_dynamic_spanner_ = 0;
64 }
65
66 ADD_ACKNOWLEDGER (Dynamic_align_engraver, dynamic);
67 ADD_ACKNOWLEDGER (Dynamic_align_engraver, note_column);
68 ADD_ACKNOWLEDGER (Dynamic_align_engraver, footnote_spanner);
69 ADD_END_ACKNOWLEDGER (Dynamic_align_engraver, dynamic);
70
71 void
72 Dynamic_align_engraver::create_line_spanner (Stream_event *event)
73 {
74   if (!line_)
75     line_ = make_spanner ("DynamicLineSpanner",
76                           event ? event->self_scm () : SCM_EOL);
77 }
78
79 void
80 Dynamic_align_engraver::acknowledge_end_dynamic (Grob_info info)
81 {
82   if (Spanner::has_interface (info.grob ()))
83     ended_.push_back (info.spanner ());
84
85   /* If the break flag is set, store the current spanner and let new dynamics
86    * create a new spanner 
87    */
88   bool spanner_broken = current_dynamic_spanner_ == info.spanner () &&
89                         to_boolean (current_dynamic_spanner_->get_property ("spanner-broken"));
90   if (spanner_broken && line_)
91     {
92       if (ended_line_)
93         programming_error ("already have a force-ended DynamicLineSpanner.");
94       ended_line_ = line_;
95       line_ = 0;
96       current_dynamic_spanner_ = 0;
97     }
98 }
99
100 void
101 Dynamic_align_engraver::acknowledge_footnote_spanner (Grob_info info)
102 {
103   Grob *parent = info.grob ()->get_parent (Y_AXIS);
104   if (line_ && parent
105       && parent->internal_has_interface (ly_symbol2scm ("dynamic-interface")))
106     Axis_group_interface::add_element (line_, info.grob ());
107 }
108
109 void
110 Dynamic_align_engraver::acknowledge_note_column (Grob_info info)
111 {
112   support_.push_back (info.grob ());
113 }
114
115 void
116 Dynamic_align_engraver::acknowledge_dynamic (Grob_info info)
117 {
118   Stream_event *cause = info.event_cause ();
119   create_line_spanner (cause);
120   if (Spanner::has_interface (info.grob ()))
121     {
122       started_.push_back (info.spanner ());
123       current_dynamic_spanner_ = info.spanner ();
124     }
125   else if (info.item ())
126     scripts_.push_back (info.item ());
127   else
128     info.grob ()->programming_error ("unknown dynamic grob");
129
130   Axis_group_interface::add_element (line_, info.grob ());
131
132   if (cause)
133     {
134       // TODO: Compare the direction of the existing spanner with
135       //       the new one and if they differ, create a new line
136       //       spanner.
137       if (Direction d = to_dir (cause->get_property ("direction")))
138         set_grob_direction (line_, d);
139     }
140 }
141
142 void
143 Dynamic_align_engraver::set_spanner_bounds (Spanner *line, bool end)
144 {
145   if (!line)
146     return;
147   Direction d = LEFT;
148   do
149     {
150       if ((d == LEFT && !line->get_bound (LEFT)) ||
151           (end && d == RIGHT && !line->get_bound (RIGHT)))
152         {
153           vector<Spanner *> const &spanners
154             = (d == LEFT) ? started_ : ended_;
155
156           Grob *bound = 0;
157           if (scripts_.size ())
158             bound = scripts_[0];
159           else if (spanners.size ())
160             bound = spanners[0]->get_bound (d);
161           else
162             {
163               bound = unsmob_grob (get_property ("currentMusicalColumn"));
164               programming_error ("started DynamicLineSpanner but have no left bound");
165             }
166
167           line->set_bound (d, bound);
168         }
169     }
170   while (flip (&d) != LEFT);
171 }
172
173 void
174 Dynamic_align_engraver::stop_translation_timestep ()
175 {
176   for (vsize i = 0; i < started_.size (); i++)
177     running_.insert (started_[i]);
178   for (vsize i = 0; i < ended_.size (); i++)
179     {
180       Spanner *sp = ended_[i];
181
182       set<Spanner *>::iterator it = running_.find (sp);
183       if (it != running_.end ())
184         running_.erase (it);
185       else
186         started_[i]->programming_error ("lost track of this dynamic spanner");
187     }
188
189   bool end = line_ && running_.empty ();
190
191   // Set the proper bounds for the current spanner and for a spanner that
192   // is ended now
193   set_spanner_bounds (ended_line_, true);
194   set_spanner_bounds (line_, end);
195   // If the flag is set to break the spanner after the current child, don't 
196   // add any more support points (needed e.g. for style=none, where the
197   // invisible spanner should NOT be shifted since we don't have a line).
198   bool spanner_broken = current_dynamic_spanner_ &&
199                         to_boolean (current_dynamic_spanner_->get_property ("spanner-broken"));
200   for (vsize i = 0; line_ && !spanner_broken && i < support_.size (); i++)
201     Side_position_interface::add_support (line_, support_[i]);
202
203   if (end)
204     {
205       line_ = 0;
206     }
207
208   ended_line_ = 0;
209   ended_.clear ();
210   started_.clear ();
211   scripts_.clear ();
212   support_.clear ();
213 }
214
215 ADD_TRANSLATOR (Dynamic_align_engraver,
216                 /* doc */
217                 "Align hairpins and dynamic texts on a horizontal line.",
218
219                 /* create */
220                 "DynamicLineSpanner ",
221
222                 /* read */
223                 "currentMusicalColumn ",
224
225                 /* write */
226                 ""
227                 );