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