]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
Imported sources
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "all-font-metrics.hh"
10 #include "afm.hh"
11 #include "warn.hh"
12 #include "main.hh"
13 #include "system.hh"
14 #include "item.hh"
15 #include "score-engraver.hh"
16 #include "paper-score.hh"
17 #include "paper-column.hh"
18
19 #include "paper-def.hh"
20 #include "axis-group-interface.hh"
21 #include "context-def.hh"
22
23 #include "staff-spacing.hh"
24 #include "note-spacing.hh"
25
26 /*
27   TODO: the column creation logic is rather hairy. Revise it.
28  */
29 Score_engraver::Score_engraver ()
30 {
31   system_ =0;
32   command_column_ =0;
33   musical_column_ =0;
34   breaks_ =0;
35   pscore_ = 0;
36 }
37
38 void
39 Score_engraver::make_columns ()
40 {
41   /*
42     ugh.
43    */
44   if (!command_column_)
45     {
46       SCM nmp
47         = updated_grob_properties (this,
48                                    ly_symbol2scm ("NonMusicalPaperColumn"));
49       SCM pc = updated_grob_properties (this,
50                                         ly_symbol2scm ("PaperColumn"));
51       
52       set_columns (new Paper_column (nmp), new Paper_column (pc));
53   
54       command_column_->set_grob_property ("breakable", SCM_BOOL_T);
55
56
57       Grob_info i1;
58       i1.grob_ = command_column_;
59       i1.origin_trans_ = this;
60   
61       announce_grob (i1);
62
63       Grob_info i2;
64       i2.grob_ = musical_column_;
65       i2.origin_trans_ = this;
66
67       announce_grob (i2);
68     }
69 }
70
71 void
72 Score_engraver::prepare (Moment w)
73 {
74   Global_translator::prepare (w);
75
76   /*
77     TODO: don't make columns when skipTypesetting is true.
78    */
79   make_columns ();
80   
81   command_column_->set_grob_property ("when", now_mom_.smobbed_copy ());
82   musical_column_->set_grob_property ("when", now_mom_.smobbed_copy ());
83
84   
85   Translator_group::start_translation_timestep();
86 }
87
88 void
89 Score_engraver::finish ()
90 {
91   if ((breaks_%8))
92     progress_indication ("[" + to_string (breaks_) + "]");
93    
94   check_removal ();
95   finalize ();
96 }
97
98 /*
99   use start/finish?
100  */
101 void
102 Score_engraver::initialize ()
103 {
104   Font_metric *fm = all_fonts_global->find_afm("feta20");
105   if (!fm)
106     error (_f ("can't find `%s'", "feta20.afm")
107            + "\n" +_ ("Fonts have not been installed properly.  Aborting"));
108    
109   unsmob_context_def (definition_)->apply_default_property_operations (this);
110
111   assert (!daddy_trans_);
112   pscore_ = new Paper_score;
113   pscore_->paper_ = dynamic_cast<Paper_def*> (get_output_def ());
114
115   SCM props = updated_grob_properties (this, ly_symbol2scm ("System"));
116
117   pscore_->typeset_line (new System (props));
118   
119   make_columns ();
120   system_ = pscore_->system_;
121   system_->set_bound (LEFT, command_column_);
122   command_column_->set_grob_property ("breakable", SCM_BOOL_T);
123
124   Engraver_group_engraver::initialize ();
125 }
126
127
128 void
129 Score_engraver::finalize ()
130 {
131   Engraver_group_engraver::finalize ();
132
133   Grob * cc
134     = unsmob_grob (get_property ("currentCommandColumn"));
135   system_->set_bound (RIGHT, cc);
136   cc->set_grob_property ("breakable", SCM_BOOL_T);
137   
138   typeset_all ();
139 }
140
141 void
142 Score_engraver::one_time_step ()
143 {
144   if (!to_boolean (get_property ("skipTypesetting")))
145     {
146       process_music ();
147       do_announces ();
148     }
149   
150   stop_translation_timestep ();
151   
152   apply_finalizations ();
153   check_removal ();
154
155
156   for (int i = announce_infos_.size(); i--;)
157     {
158       Grob *g = announce_infos_[i].grob_;
159       if (!dynamic_cast<Paper_column*> (g)) // ugh.
160         {
161           String msg= "Grob "
162             + g->name()
163             + " was created too late!";
164           g->programming_error (msg);
165         }
166     }
167   announce_infos_.clear ();
168 }
169
170 void
171 Score_engraver::announce_grob (Grob_info info)
172 {
173   announce_infos_.push (info);
174   pscore_->system_->typeset_grob (info.grob_);
175 }
176
177 void
178 Score_engraver::typeset_grob (Grob *elem)
179 {
180   if (!elem)
181     programming_error ("Score_engraver: empty elt\n");
182   else
183     elems_.push (elem);
184 }
185
186 void
187 Score_engraver::typeset_all ()
188 {
189   for (int i =0; i < elems_.size (); i++) 
190     {
191       Grob * elem = elems_[i];
192       
193       if (Spanner *s = dynamic_cast <Spanner *> (elem))
194         {
195           /*
196             do something sensible if spanner not 
197             spanned on 2 items.
198           */
199           Direction d = LEFT;
200           do {
201             if (!s->get_bound (d))
202               {
203                 s->set_bound (d, command_column_);
204                 /* don't warn for empty/suicided spanners,
205                    it makes real warningsinvisible.
206                    maybe should be junked earlier? */
207                 if (!elem->live())
208                   ; // gdb hook
209                 else
210                   elem->warning (_f ("unbound spanner `%s'", s->name ().to_str0 ()));
211               }
212           }
213           while (flip (&d) != LEFT);
214
215           if (dynamic_cast<Item*> (s->get_parent (Y_AXIS)))
216             programming_error ("Spanner Y-parent is an item.");
217         }
218       else 
219         {
220           if (!elem->get_parent (X_AXIS))
221             {
222               bool br = to_boolean (elem->get_grob_property ("breakable"));
223               Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
224
225             }
226         }
227       if (!elem->get_parent (Y_AXIS))
228         Axis_group_interface::add_element (system_, elem);
229     }
230   elems_.clear ();
231 }
232
233 void
234 Score_engraver::stop_translation_timestep ()
235 {
236   // this generates all items.
237   Engraver_group_engraver::stop_translation_timestep ();
238   
239   typeset_all ();
240   if (to_boolean (command_column_->get_grob_property ("breakable")))
241     {
242       breaks_ ++;
243       if (! (breaks_%8))
244         progress_indication ("[" + to_string (breaks_) + "]");
245     }
246
247
248   system_->add_column (command_column_);
249   system_->add_column (musical_column_);
250   
251   command_column_ = 0;
252   musical_column_ = 0;
253 }
254
255 void
256 Score_engraver::set_columns (Paper_column *new_command, 
257                              Paper_column *new_musical)
258 {
259   assert (!command_column_ && !musical_column_);
260
261   command_column_ = new_command;
262   musical_column_ = new_musical;
263   if (new_command)
264     {
265       set_property ("currentCommandColumn", new_command->self_scm ());  
266     }
267   
268   if (new_musical)
269     {
270       set_property ("currentMusicalColumn", new_musical->self_scm ());
271     }
272 }
273
274 Music_output*
275 Score_engraver::get_output ()
276 {
277   Music_output * o = pscore_;
278   pscore_=0;
279   return o;
280 }
281
282
283 bool
284 Score_engraver::try_music (Music*r)
285 {
286   bool gotcha = Engraver_group_engraver::try_music (r);  
287
288   if (!gotcha && r->is_mus_type ("break-event"))
289     {
290       gotcha = true;
291
292       SCM pen = command_column_->get_grob_property ("penalty");
293       Real total_penalty = gh_number_p (pen)
294         ? gh_scm2double (pen)
295         : 0.0;
296
297       SCM rpen = r->get_mus_property ("penalty");
298       if (gh_number_p (rpen))
299         total_penalty +=  gh_scm2double (rpen);
300           
301       if (total_penalty > 10000.0) //  ugh. arbitrary.
302         forbid_breaks ();
303
304       command_column_->set_grob_property ("penalty",
305                                           gh_double2scm (total_penalty));
306     }
307   return gotcha;
308 }
309
310 /*
311   TODO:  use property Score.breakForbidden = #t
312  */
313 void
314 Score_engraver::forbid_breaks ()
315 {
316   /*
317     result is junked.
318    */
319   if (command_column_)
320     command_column_->set_grob_property ("breakable", SCM_EOL);
321 }
322   
323 void
324 Score_engraver::acknowledge_grob (Grob_info gi)
325 {
326   if (Staff_spacing::has_interface (gi.grob_))
327     {
328       Pointer_group_interface::add_grob (command_column_,
329                                          ly_symbol2scm ("spacing-wishes"),
330                                          gi.grob_);
331     }
332   if (Note_spacing::has_interface (gi.grob_))
333     {
334       Pointer_group_interface::add_grob (musical_column_,
335                                          ly_symbol2scm ("spacing-wishes"),
336                                          gi.grob_);
337     }
338 }
339
340
341
342 ENTER_DESCRIPTION(Score_engraver,
343 /* descr */       "Top level engraver. Takes care of generating columns and the complete  system (ie. System) "
344 "\n\n "
345 "This engraver decides whether a column is breakable. The default is "
346 "that a column is always breakable. However, when every Bar_engraver "
347 "that does not have a barline at a certain point will call "
348 "Score_engraver::forbid_breaks to stop linebreaks.  In practice, this "
349 "means that you can make a breakpoint by creating a barline (assuming "
350 "that there are no beams or notes that prevent a breakpoint.) "
351 ,
352 /* creats*/       "System PaperColumn NonMusicalPaperColumn", 
353 /* accepts */     "break-event",
354 /* acks  */       "note-spacing-interface staff-spacing-interface",
355 /* reads */       "currentMusicalColumn currentCommandColumn",
356 /* write */       "");