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