]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeat-engraver.cc
patch::: 1.1.43.crypton1
[lilypond.git] / lily / repeat-engraver.cc
1 /*
2   repeat-engraver.cc -- implement Repeat_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--1999 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "repeat-engraver.hh"
10 #include "bar.hh"
11 #include "bar-engraver.hh"
12 #include "musical-request.hh"
13 #include "multi-measure-rest.hh"
14 #include "command-request.hh"
15 #include "time-description.hh"
16 #include "engraver-group.hh"
17 #include "new-repeated-music.hh"
18 #include "time-description.hh"
19 #include "volta-spanner.hh"
20 #include "note-column.hh"
21 #include "paper-def.hh"
22 #include "music-list.hh"
23
24 ADD_THIS_TRANSLATOR (Repeat_engraver);
25
26 /*
27   Urg. Hairy.  Needs redesign?
28  */
29 bool
30 Repeat_engraver::do_try_music (Music* m)
31 {
32   if (New_repeated_music* r = dynamic_cast<New_repeated_music *> (m))
33     {
34       if (!r->semi_fold_b_)
35         return true;
36
37       Music_sequence* alt = r->alternatives_p_;
38       Moment repeat_length_mom = r->repeat_body_p_->length_mom ();
39       Moment stop_mom = now_mom () + repeat_length_mom;
40       Moment alt_mom = now_mom () + repeat_length_mom;
41
42       /*
43         if there's a repeat block, we'll need a stop-repeat :| 
44         */
45       if (repeat_length_mom)
46         {
47           /*
48             if there are alternatives, the :| comes before last alternative
49             (only for semi-unfolded, but otherwise we're not here anyway?)
50             */
51           if (alt)
52             {
53               for (Cons<Music> *i (alt->music_p_list_p_->head_); i && i->next_; i = i->next_)
54                 {
55                   stop_mom += i->car_->length_mom ();
56                   if (r->fold_b_)
57                     break;
58                 }
59              }
60           repeated_music_arr_.push (r);
61           stop_mom_arr_.push (stop_mom);
62         }
63
64       /* 
65          Counting nested repeats, it seems safest to forbid
66          two pieces of alternative music to start at the same time.
67       */
68       for (int i = 0; i < alternative_start_mom_arr_.size (); i++)
69         if (alternative_start_mom_arr_[i] == alt_mom)
70           return false;
71
72       /*
73         Coda kludge: see input/test/coda-kludge.ly
74        */
75       Moment span_mom;
76       Scalar prop = get_property ("voltaSpannerDuration", 0);
77       if (prop.length_i ())
78         span_mom = prop.to_rat ();
79
80       if (!alt)
81         return true;
82
83       int alt_i = r->repeats_i_ + 1 - cons_list_size_i (alt->music_p_list_p_->head_ ) >? 1;
84       for (Cons<Music> *i = alt->music_p_list_p_->head_; i ; i = i->next_)
85         {
86           alternative_music_arr_.push (i->car_);
87           alternative_start_mom_arr_.push (alt_mom);
88           if (span_mom)
89             alternative_stop_mom_arr_.push (alt_mom + span_mom);
90           else
91             alternative_stop_mom_arr_.push (alt_mom + i->car_->length_mom ());
92           String str;
93           if ((alt_i != 1) && (alt_i != r->repeats_i_) && (i == alt->music_p_list_p_->head_))
94             str = "1.-";
95           str += to_str (alt_i) + ".";
96           alt_i++;
97           alternative_str_arr_.push (str);
98           if (!dynamic_cast<Simultaneous_music *> (alt))
99             alt_mom += i->car_->length_mom ();
100         }
101       return true;
102     }
103   return false;
104 }
105
106 void
107 Repeat_engraver::acknowledge_element (Score_element_info i)
108 {
109   Moment now = now_mom ();
110   if (Note_column *c = dynamic_cast<Note_column *> (i.elem_l_))
111     {
112       for (int i = 0; i < volta_p_arr_.size (); i++)
113         if (volta_p_arr_[i] && (now >= alternative_start_mom_arr_[i]))
114           volta_p_arr_[i]->add_column (c);
115     }
116   if (Bar *c = dynamic_cast<Bar*> (i.elem_l_))
117     {
118       for (int i = 0; i < volta_p_arr_.size (); i++)
119         if (volta_p_arr_[i] && (now >= alternative_start_mom_arr_[i]))
120           volta_p_arr_[i]->add_column (c);
121     }
122 }
123
124 void
125 Repeat_engraver::do_removal_processing ()
126 {
127   for (int i = 0; i < volta_p_arr_.size (); i++)
128     if (volta_p_arr_[i])
129       typeset_element (volta_p_arr_[i]);
130 }
131
132 void
133 Repeat_engraver::do_process_requests ()
134 {  
135   Moment now = now_mom ();
136   Bar_engraver* bar_engraver_l = dynamic_cast <Bar_engraver*>
137     (daddy_grav_l ()->get_simple_translator ("Bar_engraver"));
138   for (int i = bar_b_arr_.size (); i < repeated_music_arr_.size (); i++)
139     {
140       if (bar_engraver_l)
141         bar_engraver_l->request_bar ("|:");
142       bar_b_arr_.push (true);
143     }
144   for (int i = 0; i < bar_b_arr_.size (); i++)
145     {
146       if (!bar_b_arr_[i] && (now >= stop_mom_arr_[i]))
147         {
148           if (bar_engraver_l)
149             bar_engraver_l->request_bar (":|");
150         }
151     }
152   for (int i = volta_p_arr_.size (); i < alternative_music_arr_.size (); i++)
153     {
154       Volta_spanner* v = new Volta_spanner;
155       Scalar prop = get_property ("voltaVisibility", 0);
156       if (!prop.to_bool ())
157         v->set_elt_property (transparent_scm_sym, SCM_BOOL_T);
158       prop = get_property ("voltaSpannerDuration", 0);
159       if ((i == alternative_music_arr_.size () - 1) || prop.length_i ())
160         v->last_b_ = true;
161
162       v->number_str_ = alternative_str_arr_[i];
163       volta_p_arr_.push (v);
164       announce_element (Score_element_info (v, alternative_music_arr_[i]));
165     }
166 }
167
168 void 
169 Repeat_engraver::do_pre_move_processing ()
170 {
171   Moment now = now_mom ();
172   for (int i = bar_b_arr_.size (); i--; )
173     {
174       if (bar_b_arr_[i])
175         bar_b_arr_[i] = false;
176       if (now >= stop_mom_arr_[i])
177         {
178           bar_b_arr_.del (i);
179           stop_mom_arr_.del (i);
180           repeated_music_arr_.del (i);
181         }
182     }
183   for (int i = volta_p_arr_.size (); i--; )
184     {
185       if (volta_p_arr_[i] && (now >= alternative_stop_mom_arr_[i])
186           && (volta_p_arr_[i]->column_arr_.size () >= 1))
187         {
188           typeset_element (volta_p_arr_[i]);
189           volta_p_arr_[i] = 0;
190           volta_p_arr_.del (i);
191           alternative_music_arr_[i] = 0;
192           alternative_music_arr_.del (i);
193           alternative_start_mom_arr_.del (i);
194           alternative_stop_mom_arr_.del (i);
195           alternative_str_arr_.del (i);
196         }
197     }
198 }