]> git.donarmstrong.com Git - lilypond.git/blob - lily/percent-repeat-engraver.cc
* flower
[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
119       return true;
120     }
121
122   return false;
123 }
124
125 void
126 Percent_repeat_engraver::process_music ()
127 {
128   if (repeat_ && now_mom () == next_moment_)
129     {
130       if (repeat_sign_type_ == MEASURE)
131         {
132           finished_perc_ = perc_;
133           typeset_perc ();
134           perc_ = make_spanner ("PercentRepeat", repeat_->self_scm ());
135           SCM col = get_property ("currentCommandColumn");
136           perc_->set_bound (LEFT, unsmob_grob (col));
137         }
138       else if (repeat_sign_type_ == DOUBLE_MEASURE)
139         {
140           double_percent_ = make_item ("DoublePercentRepeat", repeat_->self_scm ());
141           /*
142             forbid breaks on a % line. Should forbid all breaks, really.
143           */
144
145           get_score_engraver ()->forbid_breaks ();      // guh. Use properties!      
146         }
147       next_moment_ = next_moment_ + body_length_;
148
149       get_global_context ()->add_moment_to_process (next_moment_);
150     }
151 }
152
153 void
154 Percent_repeat_engraver::finalize ()
155 {
156   typeset_perc ();
157   if (perc_)
158     {
159       repeat_->origin ()->warning (_ ("unterminated percent repeat"));
160       perc_->suicide ();
161     }
162 }
163
164 void
165 Percent_repeat_engraver::typeset_perc ()
166 {
167   if (finished_perc_)
168     {
169       SCM col = get_property ("currentCommandColumn");
170       finished_perc_->set_bound (RIGHT, unsmob_grob (col));
171       finished_perc_ = 0;
172     }
173
174   double_percent_ = 0;
175 }
176
177
178 void
179 Percent_repeat_engraver::start_translation_timestep ()
180 {
181   if (stop_mom_ == now_mom ())
182     {
183       if (perc_)
184         {
185           finished_perc_ = perc_;
186           typeset_perc ();
187         }
188       repeat_ = 0;
189       perc_ = 0;
190       repeat_sign_type_ = UNKNOWN;
191     }
192 }
193
194 void
195 Percent_repeat_engraver::stop_translation_timestep ()
196 {
197   typeset_perc ();
198 }
199
200
201 ADD_TRANSLATOR (Percent_repeat_engraver,
202                 /* descr */ "Make whole bar and double bar repeats.",
203                 /* creats*/ "PercentRepeat DoublePercentRepeat",
204                 /* accepts */ "repeated-music",
205                 /* acks  */ "",
206                 /* reads */ "measureLength currentCommandColumn",
207                 /* write */ "");