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