]> git.donarmstrong.com Git - lilypond.git/blob - lily/volta-engraver.cc
* lily/engraver-group-engraver.cc (do_announces): rename
[lilypond.git] / lily / volta-engraver.cc
1 /*   
2   volta-engraver.cc --  implement Volta_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2000--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "engraver.hh"
11 #include "translator-group.hh"
12 #include "volta-bracket.hh"
13 #include "item.hh"
14 #include "note-column.hh"
15 #include "bar-line.hh"
16 #include "side-position-interface.hh"
17 #include "warn.hh"
18 #include "staff-symbol.hh"
19
20 /*
21   Create Volta spanners, by reading repeatCommands  property, usually
22   set by Unfolded_repeat_iterator.
23  */
24 class Volta_engraver : public Engraver
25 {
26 public:
27   TRANSLATOR_DECLARATIONS(Volta_engraver);
28 protected:
29
30   virtual void acknowledge_grob (Grob_info);
31   virtual void finalize ();
32   virtual void stop_translation_timestep ();
33   virtual void process_music ();
34   virtual void process_acknowledged_grobs ();
35   
36   Moment started_mom_;
37   Spanner *volta_span_p_;
38   Spanner *end_volta_span_p_;
39   SCM staff_;
40   SCM start_str_;
41 };
42
43 Volta_engraver::Volta_engraver ()
44 {
45   staff_ = SCM_EOL;
46   volta_span_p_ = 0;
47   end_volta_span_p_ = 0;
48 }
49
50
51 void
52 Volta_engraver::process_music ()
53 {
54   if (unsmob_grob (staff_))
55     {
56       /*
57         TODO: this does weird things when you open a piece with a
58         volta spanner.
59         
60        */
61       SCM staffs = get_property ("stavesFound");
62
63       /*
64         only put a volta on the top staff.
65         
66         May be this is a bit convoluted, and we should have a single
67         volta engraver in score context or somesuch.
68         
69       */
70       if (ly_car (scm_last_pair (staffs)) != staff_)
71         return ;
72     }
73         
74   
75   SCM cs = get_property ("repeatCommands");
76
77   bool  end = false;
78   start_str_ = SCM_EOL;
79   while (gh_pair_p (cs))
80     {
81       SCM c = ly_car (cs);
82
83       if (gh_pair_p (c) && ly_car (c) == ly_symbol2scm ("volta")
84           && gh_pair_p (ly_cdr (c)))
85         {
86           if (ly_cadr (c) ==  SCM_BOOL_F)
87             end = true;
88           else
89             start_str_ = ly_cadr (c);
90         }
91       
92       cs = ly_cdr (cs);
93     }
94
95   if (volta_span_p_)
96     {
97       SCM l (get_property ("voltaSpannerDuration"));
98       Moment now = now_mom ();
99   
100       bool early_stop = unsmob_moment (l)
101         && *unsmob_moment (l) <= now - started_mom_;
102       
103       end = end || early_stop;
104     }
105
106   
107   if (end && !volta_span_p_)
108     {
109       warning (_ ("No volta spanner to end")); // fixme: be more verbose.
110     }
111   else if (end)
112     {
113       end_volta_span_p_ = volta_span_p_;
114       volta_span_p_ =0;
115     }
116
117   if (gh_string_p (start_str_) && volta_span_p_)
118     {
119       warning (_ ("Already have a volta spanner.  Stopping that one prematurely."));
120       
121       if (end_volta_span_p_)
122         {
123           warning (_ ("Also have a stopped spanner.  Giving up."));
124           return ;
125         }
126
127       end_volta_span_p_ = volta_span_p_;
128       volta_span_p_ = 0;
129     }
130 }
131
132 /*
133   this could just as well be done in process_music (), but what the hack.
134  */
135 void
136 Volta_engraver::process_acknowledged_grobs ()
137 {
138   if (!volta_span_p_ && gh_string_p (start_str_))
139     {
140       started_mom_ = now_mom () ;
141
142       volta_span_p_ = new Spanner (get_property ("VoltaBracket"));
143
144       announce_grob (volta_span_p_, SCM_EOL);
145       volta_span_p_->set_grob_property ("text", start_str_);
146     }
147 }
148
149 void
150 Volta_engraver::acknowledge_grob (Grob_info i)
151 {
152   if (Item* item = dynamic_cast<Item*> (i.grob_l_))
153     {
154       if (Note_column::has_interface (item))
155         {
156           if (volta_span_p_)
157             Volta_bracket_interface::add_column (volta_span_p_,item);
158         }
159       if (Bar_line::has_interface (item))
160         {
161           if (volta_span_p_)
162             Volta_bracket_interface::add_bar (volta_span_p_, item);
163           if (end_volta_span_p_)
164             Volta_bracket_interface::add_bar (end_volta_span_p_ , item);
165         }
166     }
167   else if (Staff_symbol::has_interface (i.grob_l_))
168     {
169       /*
170         We only want to know about a single staff: then we add to the
171         support.  */
172       if (staff_ != SCM_EOL)
173         staff_ = SCM_UNDEFINED;
174
175       if (staff_ != SCM_UNDEFINED)
176         staff_ = i.grob_l_->self_scm();
177     }
178 }
179
180 void
181 Volta_engraver::finalize ()
182 {
183   if (volta_span_p_)
184     {
185       typeset_grob (volta_span_p_);
186     }
187   if (end_volta_span_p_)
188     {
189       typeset_grob (end_volta_span_p_);
190     }
191 }
192
193
194
195 void 
196 Volta_engraver::stop_translation_timestep ()
197 {
198   if (end_volta_span_p_)
199     {
200       typeset_grob (end_volta_span_p_);
201       end_volta_span_p_ =0;
202     }
203 }
204
205 /*
206   TODO: should attach volta to paper-column if no bar is found.
207  */
208
209 ENTER_DESCRIPTION(Volta_engraver,
210 /* descr */       "Make volta brackets",
211 /* creats*/       "VoltaBracket",
212 /* acks  */       "bar-line-interface staff-symbol-interface note-column-interface",
213 /* reads */       "repeatCommands voltaSpannerDuration stavesFound",
214 /* write */       "");