]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
* input/test/markup-score.ly: Remove \notes.
[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   (void) elem;
161   //  elems_.push (elem);
162 }
163
164 void
165 Score_engraver::typeset_all ()
166 {
167   for (int i =0; i < elems_.size (); i++) 
168     {
169       Grob * elem = elems_[i];
170
171 #if 0
172       if (Spanner *s = dynamic_cast <Spanner *> (elem))
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                 Grob * cc
183                   = unsmob_grob (get_property ("currentCommandColumn"));
184                 s->set_bound (d, cc);
185                 /* don't warn for empty/suicided spanners,
186                    it makes real warningsinvisible.
187                    maybe should be junked earlier? */
188                 if (elem->live ())
189                   elem->warning (_f ("unbound spanner `%s'", s->name ().to_str0 ()));
190               }
191           }
192           while (flip (&d) != LEFT);
193
194           if (dynamic_cast<Item*> (s->get_parent (Y_AXIS)))
195             programming_error ("Spanner Y-parent is an item.");
196         }
197 #endif
198       
199         if (dynamic_cast<Item*> (elem)) 
200         {
201           if (!elem->get_parent (X_AXIS)
202               && elem != command_column_
203               && elem != musical_column_
204               )
205             {
206               bool br = to_boolean (elem->get_property ("breakable"));
207               Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
208
209             }
210         }
211       if (!elem->get_parent (Y_AXIS))
212         Axis_group_interface::add_element (system_, elem);
213     }
214   elems_.clear ();
215 }
216
217 void
218 Score_engraver::stop_translation_timestep ()
219 {
220   // this generates all items.
221   Engraver_group_engraver::stop_translation_timestep ();
222   
223   typeset_all ();
224   if (to_boolean (command_column_->get_property ("breakable")))
225     {
226       breaks_ ++;
227       if (! (breaks_%8))
228         progress_indication ("[" + to_string (breaks_) + "]");
229     }
230
231
232   system_->add_column (command_column_);
233   system_->add_column (musical_column_);
234   
235   command_column_ = 0;
236   musical_column_ = 0;
237 }
238
239 void
240 Score_engraver::set_columns (Paper_column *new_command, 
241                              Paper_column *new_musical)
242 {
243   assert (!command_column_ && !musical_column_);
244
245   command_column_ = new_command;
246   musical_column_ = new_musical;
247   if (new_command)
248     {
249       context ()->set_property ("currentCommandColumn", new_command->self_scm ());  
250     }
251   
252   if (new_musical)
253     {
254       context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
255     }
256 }
257
258 Music_output*
259 Score_engraver::get_output ()
260 {
261   Music_output *o = pscore_;
262   ///FIXME WTF?  pscore_ = 0;
263   return o;
264 }
265
266 bool
267 Score_engraver::try_music (Music *m)
268 {
269   if (Engraver_group_engraver::try_music (m))
270     return true;
271
272   if (m->is_mus_type ("break-event"))
273     {
274       SCM pen = command_column_->get_property ("penalty");
275       Real total_penalty = ly_c_number_p (pen) ? ly_scm2double (pen) : 0.0;
276
277       SCM mpen = m->get_property ("penalty");
278       if (ly_c_number_p (mpen))
279         total_penalty += ly_scm2double (mpen);
280
281       command_column_->set_property ("penalty", scm_make_real (total_penalty));
282
283       /* ugh.  arbitrary, hardcoded */
284       if (total_penalty > 10000.0)
285         forbid_breaks ();
286
287       SCM page_pen = command_column_->get_property ("page-penalty");
288       Real total_pp = ly_c_number_p (page_pen) ? ly_scm2double (page_pen) : 0.0;
289       SCM mpage_pen = m->get_property ("page-penalty");
290       if (ly_c_number_p (mpage_pen))
291         total_pp += ly_scm2double (mpage_pen);
292       
293       command_column_->set_property ("page-penalty", scm_make_real (total_pp));
294       return true;
295     }
296   return false;
297 }
298
299 void
300 Score_engraver::forbid_breaks ()
301 {
302   if (command_column_)
303     command_column_->set_property ("breakable", SCM_EOL);
304 }
305   
306 void
307 Score_engraver::acknowledge_grob (Grob_info gi)
308 {
309   if (Staff_spacing::has_interface (gi.grob_))
310     {
311       Pointer_group_interface::add_grob (command_column_,
312                                          ly_symbol2scm ("spacing-wishes"),
313                                          gi.grob_);
314     }
315   if (Note_spacing::has_interface (gi.grob_))
316     {
317       Pointer_group_interface::add_grob (musical_column_,
318                                          ly_symbol2scm ("spacing-wishes"),
319                                          gi.grob_);
320     }
321 }
322
323
324
325 ENTER_DESCRIPTION (Score_engraver,
326 /* descr */       "Top level engraver. Takes care of generating columns and the complete  system (ie. System) "
327 "\n\n "
328 "This engraver decides whether a column is breakable. The default is "
329 "that a column is always breakable. However, when every Bar_engraver "
330 "that does not have a barline at a certain point will call "
331 "Score_engraver::forbid_breaks to stop linebreaks.  In practice, this "
332 "means that you can make a breakpoint by creating a barline (assuming "
333 "that there are no beams or notes that prevent a breakpoint.) "
334 ,
335 /* creats*/       "System PaperColumn NonMusicalPaperColumn", 
336 /* accepts */     "break-event",
337 /* acks  */       "note-spacing-interface staff-spacing-interface",
338 /* reads */       "currentMusicalColumn currentCommandColumn",
339 /* write */       "");