]> git.donarmstrong.com Git - lilypond.git/blob - lily/volta-engraver.cc
release: 1.5.3
[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--2001 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
18 /*
19   Create Volta spanners, by reading repeatCommands  property, usually
20   set by Unfolded_repeat_iterator.
21  */
22 class Volta_engraver : public Engraver
23 {
24 public:
25   Volta_engraver ();
26   VIRTUAL_COPY_CONS (Translator);
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
39   SCM start_str_;
40 };
41
42 ADD_THIS_TRANSLATOR (Volta_engraver);
43
44 Volta_engraver::Volta_engraver ()
45 {
46   volta_span_p_ = 0;
47   end_volta_span_p_ = 0;
48 }
49
50
51 void
52 Volta_engraver::process_music ()
53 {
54   SCM cs = get_property ("repeatCommands");
55
56   bool  end = false;
57   start_str_ = SCM_EOL;
58   while (gh_pair_p (cs))
59     {
60       SCM c = gh_car (cs);
61
62       if (gh_pair_p (c) && gh_car (c) == ly_symbol2scm ("volta")
63           && gh_pair_p (gh_cdr (c)))
64         {
65           if (gh_cadr (c) ==  SCM_BOOL_F)
66             end = true;
67           else
68             start_str_ = gh_cadr (c);
69         }
70       
71       cs = gh_cdr (cs);
72     }
73
74   if (volta_span_p_)
75     {
76       SCM l (get_property ("voltaSpannerDuration"));
77       Moment now = now_mom ();
78   
79       bool early_stop = unsmob_moment (l)
80         && *unsmob_moment (l) <= now - started_mom_;
81       
82       end = end || early_stop;
83     }
84
85   
86   if (end && !volta_span_p_)
87     {
88       warning (_ ("No volta spanner to end")); // fixme: be more verbose.
89     }
90   else if (end)
91     {
92       end_volta_span_p_ = volta_span_p_;
93       volta_span_p_ =0;
94
95       /*
96         maybe do typeset_grob () directly?
97       */
98
99       if (!gh_string_p (start_str_))
100         end_volta_span_p_->set_grob_property ("last-volta", SCM_BOOL_T);
101     }
102
103   if (gh_string_p (start_str_) && volta_span_p_)
104     {
105       warning (_ ("Already have a volta spanner.  Stopping that one prematurely."));
106       
107       if (end_volta_span_p_)
108         {
109           warning (_ ("Also have a stopped spanner.  Giving up."));
110           return ;
111         }
112
113       end_volta_span_p_ = volta_span_p_;
114       volta_span_p_ = 0;
115     }
116 }
117
118 /*
119   this could just as well be done in process_music (), but what the hack.
120  */
121 void
122 Volta_engraver::create_grobs ()
123 {
124   if (!volta_span_p_ && gh_string_p (start_str_))
125     {
126       started_mom_ = now_mom () ;
127
128       volta_span_p_ = new Spanner (get_property ("VoltaBracket"));
129       Volta_spanner::set_interface (volta_span_p_);
130       announce_grob (volta_span_p_,0);
131       volta_span_p_->set_grob_property ("text", start_str_);
132     }
133 }
134
135 void
136 Volta_engraver::acknowledge_grob (Grob_info i)
137 {
138   if (Item* item = dynamic_cast<Item*> (i.elem_l_))
139     {
140       if (Note_column::has_interface (item))
141         {
142           if (volta_span_p_)
143             Volta_spanner::add_column (volta_span_p_,item);
144           if (end_volta_span_p_)
145             Volta_spanner::add_column (end_volta_span_p_,item);      
146         }
147       if (Bar::has_interface (item))
148         {
149           if (volta_span_p_)
150             Volta_spanner::add_bar (volta_span_p_, item);
151           if (end_volta_span_p_)
152             Volta_spanner::add_bar (end_volta_span_p_ , item);
153         }
154     }
155 }
156
157 void
158 Volta_engraver::finalize ()
159 {
160   if (volta_span_p_)
161     {
162       typeset_grob (volta_span_p_);
163     }
164   if (end_volta_span_p_)
165     {
166       typeset_grob (end_volta_span_p_);
167     }
168 }
169
170
171
172 void 
173 Volta_engraver::stop_translation_timestep ()
174 {
175   if (end_volta_span_p_)
176     {
177       Side_position_interface::add_staff_support (end_volta_span_p_);
178       
179       typeset_grob (end_volta_span_p_);
180       end_volta_span_p_ =0;
181     }
182 }
183
184 /*
185   TODO: should attach volta to paper-column if no bar is found.
186  */