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