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