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