]> git.donarmstrong.com Git - lilypond.git/blob - lily/percent-repeat-engraver.cc
Update.
[lilypond.git] / lily / percent-repeat-engraver.cc
1 /*
2   new-chord-tremolo-engraver.cc -- implement Chord_tremolo_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "repeated-music.hh"
10 #include "global-context.hh"
11 #include "warn.hh"
12 #include "misc.hh"
13 #include "spanner.hh"
14 #include "item.hh"
15 #include "percent-repeat-iterator.hh"
16 #include "bar-line.hh"
17 #include "score-engraver.hh"
18
19 /**
20    This acknowledges repeated music with "percent" style.  It typesets
21    a % sign.
22
23    TODO:
24
25    - BEAT case: Create items for single beat repeats, i.e. c4 / / /
26
27    - DOUBLE_MEASURE case: attach a % to an appropriate barline.
28 */
29 class Percent_repeat_engraver : public Engraver
30 {
31   void typeset_perc ();
32 public:
33   TRANSLATOR_DECLARATIONS (Percent_repeat_engraver);
34 protected:
35   Music *repeat_;
36
37   /// moment (global time) where beam started.
38   Moment start_mom_;
39   Moment stop_mom_;
40
41   /// location  within measure where beam started.
42   Moment beam_start_location_;
43   Moment next_moment_;
44   Moment body_length_;
45
46   enum
47     {
48       UNKNOWN,
49       MEASURE,
50       DOUBLE_MEASURE,
51     }
52     repeat_sign_type_;
53
54   Item *double_percent_;
55   Spanner *perc_;
56   Spanner *finished_perc_;
57 protected:
58   virtual void finalize ();
59   virtual bool try_music (Music *);
60   virtual void stop_translation_timestep ();
61   virtual void start_translation_timestep ();
62   virtual void process_music ();
63 };
64
65 Percent_repeat_engraver::Percent_repeat_engraver ()
66 {
67   perc_ = 0;
68   finished_perc_ = 0;
69   repeat_ = 0;
70
71   double_percent_ = 0;
72 }
73
74 bool
75 Percent_repeat_engraver::try_music (Music *m)
76 {
77   if (m->is_mus_type ("repeated-music")
78       && m->get_property ("iterator-ctor")
79       == Percent_repeat_iterator::constructor_proc
80       && !repeat_)
81     {
82       body_length_ = Repeated_music::body_get_length (m);
83       int count = Repeated_music::repeat_count (m);
84
85       Moment now = now_mom ();
86       start_mom_ = now;
87       stop_mom_ = start_mom_ + Moment (count) * body_length_;
88       next_moment_ = start_mom_ + body_length_;
89
90       Moment meas_len (robust_scm2moment (get_property ("measureLength"), Moment (1)));
91       if (meas_len == body_length_)
92         repeat_sign_type_ = MEASURE;
93       else if (Moment (2) * meas_len == body_length_)
94         {
95           repeat_sign_type_ = DOUBLE_MEASURE;
96           next_moment_ += meas_len;
97         }
98       else
99         {
100           warning (_ ("Don't know how to handle a percent repeat of this length."));
101           return false;
102         }
103
104       repeat_ = m;
105
106       Global_context *global = get_global_context ();
107       for (int i = 0; i < count; i++)
108         {
109           global->add_moment_to_process (next_moment_ + Moment (i) * body_length_);
110
111           /*
112             bars between % too.
113           */
114           if (repeat_sign_type_ == DOUBLE_MEASURE)
115             global->add_moment_to_process (next_moment_ + meas_len + Moment (i) * body_length_);
116         }
117
118       return true;
119     }
120
121   return false;
122 }
123
124 void
125 Percent_repeat_engraver::process_music ()
126 {
127   if (repeat_ && now_mom () == next_moment_)
128     {
129       if (repeat_sign_type_ == MEASURE)
130         {
131           finished_perc_ = perc_;
132           typeset_perc ();
133           perc_ = make_spanner ("PercentRepeat", repeat_->self_scm ());
134           SCM col = get_property ("currentCommandColumn");
135           perc_->set_bound (LEFT, unsmob_grob (col));
136         }
137       else if (repeat_sign_type_ == DOUBLE_MEASURE)
138         {
139           double_percent_ = make_item ("DoublePercentRepeat", repeat_->self_scm ());
140           /*
141             forbid breaks on a % line. Should forbid all breaks, really.
142           */
143
144           get_score_engraver ()->forbid_breaks ();      // guh. Use properties!      
145         }
146       next_moment_ = next_moment_ + body_length_;
147
148       get_global_context ()->add_moment_to_process (next_moment_);
149     }
150 }
151
152 void
153 Percent_repeat_engraver::finalize ()
154 {
155   typeset_perc ();
156   if (perc_)
157     {
158       repeat_->origin ()->warning (_ ("unterminated percent repeat"));
159       perc_->suicide ();
160     }
161 }
162
163 void
164 Percent_repeat_engraver::typeset_perc ()
165 {
166   if (finished_perc_)
167     {
168       SCM col = get_property ("currentCommandColumn");
169       finished_perc_->set_bound (RIGHT, unsmob_grob (col));
170       finished_perc_ = 0;
171     }
172
173   double_percent_ = 0;
174 }
175
176 void
177 Percent_repeat_engraver::start_translation_timestep ()
178 {
179   if (stop_mom_ == now_mom ())
180     {
181       if (perc_)
182         {
183           finished_perc_ = perc_;
184           typeset_perc ();
185         }
186       repeat_ = 0;
187       perc_ = 0;
188       repeat_sign_type_ = UNKNOWN;
189     }
190 }
191
192 void
193 Percent_repeat_engraver::stop_translation_timestep ()
194 {
195   typeset_perc ();
196 }
197
198 ADD_TRANSLATOR (Percent_repeat_engraver,
199                 /* descr */ "Make whole bar and double bar repeats.",
200                 /* creats*/ "PercentRepeat DoublePercentRepeat",
201                 /* accepts */ "repeated-music",
202                 /* acks  */ "",
203                 /* reads */ "measureLength currentCommandColumn",
204                 /* write */ "");