]> git.donarmstrong.com Git - lilypond.git/blob - lily/percent-repeat-engraver.cc
Issue 4997/6: Use Preinit for font metrics
[lilypond.git] / lily / percent-repeat-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>, Erik Sandberg <mandolaerik@gmail.com>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "engraver.hh"
21 #include "global-context.hh"
22 #include "international.hh"
23 #include "item.hh"
24 #include "side-position-interface.hh"
25 #include "spanner.hh"
26 #include "stream-event.hh"
27 #include "warn.hh"
28
29 #include "translator.icc"
30
31 class Percent_repeat_engraver : public Engraver
32 {
33   void typeset_perc ();
34
35 public:
36   TRANSLATOR_DECLARATIONS (Percent_repeat_engraver);
37
38 protected:
39   Stream_event *percent_event_;
40
41   // moment (global time) where percent started
42   Moment start_mom_;
43   // moment (global time) where percent should end
44   Moment stop_mom_;
45
46   Spanner *percent_;
47   Spanner *percent_counter_;
48
49   Grob *first_command_column_;
50   Moment command_moment_;
51
52   virtual void finalize ();
53   void listen_percent (Stream_event *);
54
55   void start_translation_timestep ();
56   void stop_translation_timestep ();
57   void process_music ();
58 };
59
60 Percent_repeat_engraver::Percent_repeat_engraver ()
61 {
62   percent_ = 0;
63   percent_counter_ = 0;
64   percent_event_ = 0;
65
66   first_command_column_ = 0;
67   command_moment_ = Moment (-1);
68 }
69
70 void
71 Percent_repeat_engraver::start_translation_timestep ()
72 {
73   if (now_mom ().main_part_ != command_moment_.main_part_)
74     {
75       first_command_column_
76         = unsmob<Grob> (get_property ("currentCommandColumn"));
77       command_moment_ = now_mom ();
78     }
79
80   if (stop_mom_.main_part_ == now_mom ().main_part_)
81     {
82       if (percent_)
83         typeset_perc ();
84       percent_event_ = 0;
85     }
86 }
87
88 void
89 Percent_repeat_engraver::listen_percent (Stream_event *ev)
90 {
91   if (!percent_event_)
92     {
93       Moment body_length = get_event_length (ev);
94       start_mom_ = now_mom ();
95       stop_mom_ = now_mom () + body_length;
96       get_global_context ()->add_moment_to_process (stop_mom_);
97       percent_event_ = ev;
98     }
99   else
100     {
101       /*
102         print a warning: no assignment happens because
103         percent_event_ != 0
104       */
105       ASSIGN_EVENT_ONCE (percent_event_, ev);
106     }
107 }
108
109 void
110 Percent_repeat_engraver::process_music ()
111 {
112   if (percent_event_
113       && now_mom ().main_part_ == start_mom_.main_part_)
114     {
115       if (percent_)
116         typeset_perc ();
117
118       percent_ = make_spanner ("PercentRepeat", percent_event_->self_scm ());
119
120       Grob *col = first_command_column_;
121       percent_->set_bound (LEFT, col);
122
123       SCM count = percent_event_->get_property ("repeat-count");
124       if (!scm_is_null (count) && to_boolean (get_property ("countPercentRepeats"))
125           && check_repeat_count_visibility (context (), count))
126         {
127           percent_counter_ = make_spanner ("PercentRepeatCounter",
128                                            percent_event_->self_scm ());
129
130           SCM text = scm_number_to_string (count, scm_from_int (10));
131           percent_counter_->set_property ("text", text);
132           percent_counter_->set_bound (LEFT, col);
133           Side_position_interface::add_support (percent_counter_, percent_);
134           percent_counter_->set_parent (percent_, Y_AXIS);
135           percent_counter_->set_parent (percent_, X_AXIS);
136         }
137       else
138         percent_counter_ = 0;
139     }
140 }
141
142 void
143 Percent_repeat_engraver::finalize ()
144 {
145   if (percent_)
146     {
147       percent_event_->origin ()->warning (_ ("unterminated percent repeat"));
148       percent_->suicide ();
149       percent_counter_->suicide ();
150     }
151 }
152
153 void
154 Percent_repeat_engraver::typeset_perc ()
155 {
156   Grob *col = first_command_column_;
157
158   percent_->set_bound (RIGHT, col);
159   percent_ = 0;
160
161   if (percent_counter_)
162     percent_counter_->set_bound (RIGHT, col);
163   percent_counter_ = 0;
164 }
165
166 void
167 Percent_repeat_engraver::stop_translation_timestep ()
168 {
169 }
170
171 void
172 Percent_repeat_engraver::boot ()
173 {
174   ADD_LISTENER (Percent_repeat_engraver, percent);
175 }
176
177 ADD_TRANSLATOR (Percent_repeat_engraver,
178                 /* doc */
179                 "Make whole measure repeats.",
180
181                 /* create */
182                 "PercentRepeat "
183                 "PercentRepeatCounter ",
184
185                 /* read */
186                 "countPercentRepeats "
187                 "currentCommandColumn "
188                 "repeatCountVisibility ",
189
190                 /* write */
191                 ""
192                );