]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
* lily/context.cc (Context): take key argument in ctor.
[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 "output-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
53       Object_key const *key1 = get_grob_key ("NonMusicalPaperColumn");
54       
55       SCM pc = updated_grob_properties (context (),
56                                         ly_symbol2scm ("PaperColumn"));
57       Object_key const *key2 = get_grob_key ("PaperColumn");      
58       set_columns (new Paper_column (nmp, key1), new Paper_column (pc, key2));
59
60
61       scm_gc_unprotect_object (key1->self_scm());
62       scm_gc_unprotect_object (key2->self_scm());      
63
64       Grob_info i1;
65       i1.grob_ = command_column_;
66       i1.origin_trans_ = this;
67   
68       announce_grob (i1);
69
70       Grob_info i2;
71       i2.grob_ = musical_column_;
72       i2.origin_trans_ = this;
73
74       announce_grob (i2);
75     }
76 }
77
78 void
79 Score_engraver::prepare (Moment m)
80 {
81   /*
82     TODO: don't make columns when skipTypesetting is true.
83    */
84   make_columns ();
85
86   SCM w = m.smobbed_copy ();
87   command_column_->set_property ("when", w);
88   musical_column_->set_property ("when", w);
89
90   recurse_over_translators (context (), &Translator::start_translation_timestep, DOWN);
91 }
92
93 void
94 Score_engraver::finish ()
95 {
96   if ((breaks_%8))
97     progress_indication ("[" + to_string (breaks_) + "]");
98
99   recurse_over_translators (context (), &Translator::finalize, UP);
100 }
101
102 /*
103   use start/finish?
104  */
105 void
106 Score_engraver::initialize ()
107 {
108   Font_metric *fm = all_fonts_global->find_afm ("feta20");
109   if (!fm)
110     error (_f ("can't find `%s'", "feta20.afm")
111            + "\n" +_ ("Music font has not been installed properly.  Aborting"));
112
113   SCM pfa_path = ly_kpathsea_expand_path (scm_makfrom0str ("ecrm10.pfa"));
114   if (!scm_is_string (pfa_path))
115     error (_f ("can't find `%s'", "ecrm10.pfa")
116            + "\n" +_f ("Install the ec-mftraced package from %s. Aborting",
117                        "http://lilypond.org/download/fonts/"));
118    
119
120   pscore_ = new Paper_score;
121   pscore_->layout_ = dynamic_cast<Output_def*> (get_output_def ());
122
123   SCM props = updated_grob_properties (context (), ly_symbol2scm ("System"));
124
125   Object_key const *sys_key = get_grob_key ("System");
126   pscore_->typeset_line (new System (props, sys_key));
127   scm_gc_unprotect_object (sys_key->self_scm ());
128   
129   make_columns ();
130   system_ = pscore_->system_;
131   system_->set_bound (LEFT, command_column_);
132   command_column_->set_property ("breakable", SCM_BOOL_T);
133
134   Engraver_group_engraver::initialize ();
135 }
136
137
138 void
139 Score_engraver::finalize ()
140 {
141   Score_translator::finalize ();
142
143   Grob * cc
144     = unsmob_grob (get_property ("currentCommandColumn"));
145   system_->set_bound (RIGHT, cc);
146   cc->set_property ("breakable", SCM_BOOL_T);
147   
148   typeset_all ();
149 }
150
151
152 void
153 Score_engraver::one_time_step ()
154 {
155   if (!to_boolean (get_property ("skipTypesetting")))
156     {
157       recurse_over_translators (context (), &Engraver::process_music, UP);
158       Engraver_group_engraver::do_announces();
159     }
160   
161   recurse_over_translators (context (), &Translator::stop_translation_timestep, UP);
162 }
163
164 void
165 Score_engraver::announce_grob (Grob_info info)
166 {
167   announce_infos_.push (info);
168   pscore_->system_->typeset_grob (info.grob_);
169   elems_.push (info.grob_);
170 }
171
172
173 void
174 Score_engraver::typeset_all ()
175 {
176   for (int i =0; i < elems_.size (); i++) 
177     {
178       Grob * elem = elems_[i];
179
180       
181       if (dynamic_cast<Item*> (elem)) 
182         {
183           if (!elem->get_parent (X_AXIS)
184               && elem != command_column_
185               && elem != musical_column_
186               )
187             {
188               bool br = to_boolean (elem->get_property ("breakable"));
189               Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
190
191             }
192         }
193       if (!elem->get_parent (Y_AXIS))
194         Axis_group_interface::add_element (system_, elem);
195     }
196   elems_.clear ();
197 }
198
199 void
200 Score_engraver::stop_translation_timestep ()
201 {
202   // this generates all items.
203   Engraver_group_engraver::stop_translation_timestep ();
204   
205   typeset_all ();
206   if (to_boolean (command_column_->get_property ("breakable")))
207     {
208       breaks_ ++;
209       if (! (breaks_%8))
210         progress_indication ("[" + to_string (breaks_) + "]");
211     }
212
213
214   system_->add_column (command_column_);
215   system_->add_column (musical_column_);
216   
217   command_column_ = 0;
218   musical_column_ = 0;
219 }
220
221 void
222 Score_engraver::set_columns (Paper_column *new_command, 
223                              Paper_column *new_musical)
224 {
225   assert (!command_column_ && !musical_column_);
226
227   command_column_ = new_command;
228   musical_column_ = new_musical;
229   if (new_command)
230     {
231       context ()->set_property ("currentCommandColumn", new_command->self_scm ());  
232     }
233   
234   if (new_musical)
235     {
236       context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
237     }
238 }
239
240 Music_output*
241 Score_engraver::get_output ()
242 {
243   Music_output *o = pscore_;
244   ///FIXME WTF?  pscore_ = 0;
245   return o;
246 }
247
248 bool
249 Score_engraver::try_music (Music *m)
250 {
251   if (Engraver_group_engraver::try_music (m))
252     return true;
253
254   if (m->is_mus_type ("break-event"))
255     {
256       SCM pen = command_column_->get_property ("penalty");
257       Real total_penalty = scm_is_number (pen) ? scm_to_double (pen) : 0.0;
258
259       SCM mpen = m->get_property ("penalty");
260       if (scm_is_number (mpen))
261         total_penalty += scm_to_double (mpen);
262
263       command_column_->set_property ("penalty", scm_make_real (total_penalty));
264
265       /* ugh.  arbitrary, hardcoded */
266       if (total_penalty > 10000.0)
267         forbid_breaks ();
268
269       SCM page_pen = command_column_->get_property ("page-penalty");
270       Real total_pp = scm_is_number (page_pen) ? scm_to_double (page_pen) : 0.0;
271       SCM mpage_pen = m->get_property ("page-penalty");
272       if (scm_is_number (mpage_pen))
273         total_pp += scm_to_double (mpage_pen);
274       
275       command_column_->set_property ("page-penalty", scm_make_real (total_pp));
276       return true;
277     }
278   return false;
279 }
280
281 void
282 Score_engraver::forbid_breaks ()
283 {
284   if (command_column_)
285     command_column_->set_property ("breakable", SCM_EOL);
286 }
287   
288 void
289 Score_engraver::acknowledge_grob (Grob_info gi)
290 {
291   if (Staff_spacing::has_interface (gi.grob_))
292     {
293       Pointer_group_interface::add_grob (command_column_,
294                                          ly_symbol2scm ("spacing-wishes"),
295                                          gi.grob_);
296     }
297   if (Note_spacing::has_interface (gi.grob_))
298     {
299       Pointer_group_interface::add_grob (musical_column_,
300                                          ly_symbol2scm ("spacing-wishes"),
301                                          gi.grob_);
302     }
303
304   if (Axis_group_interface::has_interface (gi.grob_)
305       && gi.grob_->internal_has_interface (ly_symbol2scm ("vertically-spaceable-interface")))
306     {
307       SCM spaceable = get_property ("verticallySpacedContexts");
308       Context *orig = gi.origin_contexts (this)[0];
309       
310       if (scm_memq (ly_symbol2scm (orig->context_name ().to_str0()),
311                     spaceable) != SCM_BOOL_F)
312         {
313           Pointer_group_interface::add_grob (system_,
314                                              ly_symbol2scm ("spaceable-staves"),
315                                              gi.grob_);
316         }
317     }
318   
319 }
320
321
322
323 ENTER_DESCRIPTION (Score_engraver,
324 /* descr */       "Top level engraver. Takes care of generating columns and the complete  system (ie. System) "
325 "\n\n "
326 "This engraver decides whether a column is breakable. The default is "
327 "that a column is always breakable. However, when every Bar_engraver "
328 "that does not have a barline at a certain point will call "
329 "Score_engraver::forbid_breaks to stop linebreaks.  In practice, this "
330 "means that you can make a breakpoint by creating a barline (assuming "
331 "that there are no beams or notes that prevent a breakpoint.) "
332 ,
333 /* creats*/       "System PaperColumn NonMusicalPaperColumn", 
334 /* accepts */     "break-event",
335 /* acks  */       "note-spacing-interface staff-spacing-interface axis-group-interface",
336 /* reads */       "currentMusicalColumn currentCommandColumn verticallySpacedContexts",
337 /* write */       "");