]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
release: 1.5.24
[lilypond.git] / lily / score-engraver.cc
1 /*
2   score-engraver.cc -- implement Score_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "debug.hh"
10
11 #include "line-of-score.hh"
12 #include "item.hh"
13 #include "score-engraver.hh"
14 #include "paper-score.hh"
15 #include "paper-column.hh"
16 #include "command-request.hh"
17 #include "paper-def.hh"
18 #include "axis-group-interface.hh"
19 #include "translator-def.hh"
20
21
22 /*
23   TODO: the column creation logic is rather hairy. Revise it.
24  */
25 Score_engraver::Score_engraver ()
26 {
27   scoreline_l_ =0;
28   command_column_l_ =0;
29   musical_column_l_ =0;
30   breaks_i_ =0;
31   pscore_p_ = 0;
32 }
33
34 void
35 Score_engraver::make_columns ()
36 {
37   /*
38     ugh.
39    */
40   if (!command_column_l_)
41     //      || *unsmob_moment (command_column_l_->get_grob_property ("when")) != w)
42     {
43       set_columns (new Paper_column (get_property ("NonMusicalPaperColumn")),
44                    new Paper_column (get_property ("PaperColumn")));
45   
46       command_column_l_->set_grob_property ("breakable", SCM_BOOL_T);
47
48       Grob_info i1 (command_column_l_, SCM_EOL);
49       i1.origin_trans_l_ = this;
50
51       Grob_info i2 (musical_column_l_, SCM_EOL);
52       i2.origin_trans_l_ = this;
53
54       announce_grob (i1);
55       announce_grob (i2);
56     }
57 }
58
59 void
60 Score_engraver::prepare (Moment w)
61 {
62   Global_translator::prepare (w);
63
64   make_columns ();
65   start_translation_timestep ();
66 }
67
68 void
69 Score_engraver::finish ()
70 {
71   if ((breaks_i_%8))
72     progress_indication ("[" + to_str (breaks_i_) + "]");
73    
74   check_removal ();
75   removal_processing ();
76
77 }
78
79 /*
80   use start/finish?
81  */
82 void
83 Score_engraver::initialize ()
84 {
85   unsmob_translator_def (definition_)->apply_property_operations (this);
86
87   assert (dynamic_cast<Paper_def *> (output_def_l_));
88   assert (!daddy_trans_l_);
89   pscore_p_ = new Paper_score;
90   pscore_p_->paper_l_ = dynamic_cast<Paper_def*> (output_def_l_);
91
92   SCM props = get_property ("LineOfScore");
93
94   pscore_p_->typeset_line (new Line_of_score (props));
95   
96   make_columns ();
97   scoreline_l_ = pscore_p_->line_l_;
98   scoreline_l_->set_bound (LEFT, command_column_l_);
99   command_column_l_->set_grob_property ("breakable", SCM_BOOL_T);
100
101   Engraver_group_engraver::initialize ();
102 }
103
104
105 void
106 Score_engraver::finalize ()
107 {
108   Engraver_group_engraver::finalize ();
109
110   Grob * cc
111     = unsmob_grob (get_property ("currentCommandColumn"));
112   scoreline_l_->set_bound (RIGHT, cc);
113   cc->set_grob_property ("breakable", SCM_BOOL_T);
114   
115   typeset_all ();
116 }
117
118 void
119 Score_engraver::one_time_step ()
120 {
121   if (!to_boolean (get_property ("skipTypesetting")))
122     {
123       process_music ();
124       announces ();
125     }
126
127   
128   stop_translation_timestep ();
129   check_removal ();
130 }
131
132 void
133 Score_engraver::announce_grob (Grob_info info)
134 {
135   announce_info_arr_.push (info);
136   pscore_p_->line_l_->typeset_grob (info.grob_l_);
137 }
138
139 /* All elements are propagated to the top upon announcement. If
140    something was created during one run of
141    Engraver_group_engraver::do_announces, then
142    announce_info_arr_.size () will be nonzero again
143 */
144 /* junkme? Done by Engraver_group_engraver::do_announces ()?
145  */
146    
147 void
148 Score_engraver::do_announces ()
149 {
150   Engraver_group_engraver::do_announces ();
151 }
152
153
154 void
155 Score_engraver::typeset_grob (Grob *elem_p)
156 {
157   if (!elem_p)
158     programming_error ("Score_engraver: empty elt\n");
159   else
160
161     elem_p_arr_.push (elem_p);
162 }
163
164
165 void
166 Score_engraver::typeset_all ()
167 {
168   for (int i =0; i < elem_p_arr_.size (); i++) 
169     {
170       Grob * elem_p = elem_p_arr_[i];
171       
172       if (Spanner *s = dynamic_cast <Spanner *> (elem_p))
173         {
174             /*
175             do something sensible if spanner not 
176             spanned on 2 items.
177            */
178           Direction d = LEFT;
179           do {
180             if (!s->get_bound (d))
181               {
182                 s->set_bound (d, command_column_l_);
183                 /* don't warn for empty/suicided spanners,
184                    it makes real warningsinvisible.
185                    maybe should be junked earlier? */
186                 if (elem_p->immutable_property_alist_ == SCM_EOL)
187                   ; // gdb hook
188                 else
189                   ::warning (_f ("unbound spanner `%s'", s->name ().ch_C ()));
190               }
191           } while (flip (&d) != LEFT);
192         }
193       else 
194         {
195           if (!elem_p->get_parent (X_AXIS))
196             {
197               bool br = to_boolean (elem_p->get_grob_property ("breakable"));
198               Axis_group_interface::add_element (br ? command_column_l_ : musical_column_l_, elem_p);
199
200             }
201         }
202       if (!elem_p->get_parent (Y_AXIS))
203         Axis_group_interface::add_element (scoreline_l_, elem_p);
204     }
205   elem_p_arr_.clear ();
206 }
207
208 void
209 Score_engraver::stop_translation_timestep ()
210 {
211   // this generates all items.
212   Engraver_group_engraver::stop_translation_timestep ();
213   
214   typeset_all ();
215   if (to_boolean (command_column_l_->get_grob_property ("breakable")))
216     {
217       breaks_i_ ++;
218       if (! (breaks_i_%8))
219         progress_indication ("[" + to_str (breaks_i_) + "]");
220     }
221
222   command_column_l_->set_grob_property ("when", now_mom_.smobbed_copy ());
223   musical_column_l_->set_grob_property ("when", now_mom_.smobbed_copy ());
224
225   scoreline_l_->add_column (command_column_l_);
226   scoreline_l_->add_column (musical_column_l_);
227   
228   command_column_l_ = 0;
229   musical_column_l_ = 0;
230 }
231
232 void
233 Score_engraver::set_columns (Paper_column *new_command_l, 
234                              Paper_column *new_musical_l)
235 {
236   assert (!command_column_l_ && !musical_column_l_);
237   command_column_l_ = new_command_l;
238   musical_column_l_ = new_musical_l;
239   
240   if (new_musical_l)
241     set_property ("currentMusicalColumn", new_musical_l->self_scm ());
242   if (new_command_l)
243     set_property ("currentCommandColumn", new_command_l->self_scm ());  
244 }
245
246 Music_output*
247 Score_engraver::get_output_p ()
248 {
249   Music_output * o = pscore_p_;
250   pscore_p_=0;
251   return o;
252 }
253
254 bool
255 Score_engraver::try_music (Music*r)
256 {
257   bool gotcha = Engraver_group_engraver::try_music (r);  
258
259   if (!gotcha)
260     {
261       if (Break_req* b = dynamic_cast<Break_req *> (r))
262         {
263           gotcha = true;
264
265
266           SCM pen = command_column_l_->get_grob_property ("penalty");
267           Real total_penalty = gh_number_p (pen)
268             ? gh_scm2double (pen)
269             : 0.0;
270
271           SCM rpen = b->get_mus_property ("penalty");
272           if (gh_number_p (rpen))
273             total_penalty +=  gh_scm2double (rpen);
274           
275           if (total_penalty > 10000.0) //  ugh. arbitrary.
276             forbid_breaks ();
277
278           command_column_l_->set_grob_property ("penalty",
279                                                gh_double2scm (total_penalty));
280         }
281     }
282    return gotcha;
283 }
284
285 /*
286   TODO:  use property Score.breakForbidden = #t
287  */
288
289 void
290 Score_engraver::forbid_breaks ()
291 {
292   /*
293     result is junked.
294    */
295   command_column_l_->remove_grob_property ("breakable");
296 }
297
298
299
300
301 ENTER_DESCRIPTION(Score_engraver,
302 /* descr */       "Top level engraver. Takes care of generating columns and the complete  system (ie. LineOfScore)
303
304
305 This engraver decides whether a column is breakable. The default is
306 that a column is always breakable. However, when every Bar_engraver
307 that does not have a barline at a certain point will call
308 Score_engraver::forbid_breaks to stop linebreaks.  In practice, this
309 means that you can make a breakpoint by creating a barline (assuming
310 that there are no beams or notes that prevent a breakpoint.)
311
312
313 ",
314 /* creats*/       "LineOfScore PaperColumn NonMusicalPaperColumn",
315 /* acks  */       "grob-interface",
316 /* reads */       "currentMusicalColumn currentCommandColumn",
317 /* write */       "");