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