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