]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
*** empty log message ***
[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 #define MUSIC_FONT "bigcheese20"
95
96 /*
97   use start/finish?
98  */
99 void
100 Score_engraver::initialize ()
101 {
102   Font_metric *fm = all_fonts_global->find_otf (MUSIC_FONT);
103   if (!fm)
104     error (_f ("cannot find `%s'", MUSIC_FONT ".otf")
105            + "\n"
106            + _ ("Music font has not been installed properly.")
107            + _ ("Aborting"));
108
109   if (!scm_is_string (ly_kpathsea_find_file (scm_makfrom0str ("ecrm10.pfa")))
110       && (!scm_is_string (ly_kpathsea_find_file (scm_makfrom0str ("lmr10.pfb")))))
111       error (_f ("cannot find `%s'", "ecrm10.pfa")
112              + "\n"
113              + _f ("cannot find `%s'", "lmr10.pfb")
114              + "\n"
115              + _f ("Install the ec-fonts-mftraced package from: %s.",
116                    "http://lilypond.org/download/fonts/")
117              + "\n"
118              + _ ("Aborting."));
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 = context()->get_grob_key ("System");
126   pscore_->typeset_line (new System (props, sys_key));
127   
128   make_columns ();
129   system_ = pscore_->system_;
130   system_->set_bound (LEFT, command_column_);
131   command_column_->set_property ("breakable", SCM_BOOL_T);
132
133   Engraver_group_engraver::initialize ();
134 }
135
136
137 void
138 Score_engraver::finalize ()
139 {
140   Score_translator::finalize ();
141
142   Grob * cc
143     = unsmob_grob (get_property ("currentCommandColumn"));
144   system_->set_bound (RIGHT, cc);
145   cc->set_property ("breakable", SCM_BOOL_T);
146   
147   typeset_all ();
148 }
149
150
151 void
152 Score_engraver::one_time_step ()
153 {
154   if (!to_boolean (get_property ("skipTypesetting")))
155     {
156       recurse_over_translators (context (), &Engraver::process_music, UP);
157       Engraver_group_engraver::do_announces();
158     }
159   
160   recurse_over_translators (context (), &Translator::stop_translation_timestep, UP);
161 }
162
163 void
164 Score_engraver::announce_grob (Grob_info info)
165 {
166   announce_infos_.push (info);
167   pscore_->system_->typeset_grob (info.grob_);
168   elems_.push (info.grob_);
169 }
170
171
172 void
173 Score_engraver::typeset_all ()
174 {
175   for (int i = 0; i < elems_.size (); i++) 
176     {
177       Grob * elem = elems_[i];
178
179       
180       if (dynamic_cast<Item*> (elem)) 
181         {
182           if (
183               (!elem->get_parent (X_AXIS)
184                || !unsmob_grob  (elem->get_property ("axis-group-parent-X")))
185               && elem != command_column_
186               && elem != musical_column_
187               )
188             {
189               bool br = to_boolean (elem->get_property ("breakable"));
190               Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
191
192             }
193         }
194       if (!elem->get_parent (Y_AXIS))
195         Axis_group_interface::add_element (system_, elem);
196     }
197   elems_.clear ();
198 }
199
200 void
201 Score_engraver::stop_translation_timestep ()
202 {
203   // this generates all items.
204   Engraver_group_engraver::stop_translation_timestep ();
205   
206   typeset_all ();
207   if (to_boolean (command_column_->get_property ("breakable")))
208     {
209       breaks_ ++;
210       if (! (breaks_%8))
211         progress_indication ("[" + to_string (breaks_) + "]");
212     }
213
214
215   system_->add_column (command_column_);
216   system_->add_column (musical_column_);
217   
218   command_column_ = 0;
219   musical_column_ = 0;
220 }
221
222 void
223 Score_engraver::set_columns (Paper_column *new_command, 
224                              Paper_column *new_musical)
225 {
226   assert (!command_column_ && !musical_column_);
227
228   command_column_ = new_command;
229   musical_column_ = new_musical;
230   if (new_command)
231     {
232       context ()->set_property ("currentCommandColumn", new_command->self_scm ());  
233     }
234   
235   if (new_musical)
236     {
237       context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
238     }
239 }
240
241 Music_output*
242 Score_engraver::get_output ()
243 {
244   Music_output *o = pscore_;
245   ///FIXME WTF?  pscore_ = 0;
246   return o;
247 }
248
249 bool
250 Score_engraver::try_music (Music *m)
251 {
252   if (Engraver_group_engraver::try_music (m))
253     return true;
254
255   if (m->is_mus_type ("break-event"))
256     {
257       SCM pen = command_column_->get_property ("penalty");
258       Real total_penalty = scm_is_number (pen) ? scm_to_double (pen) : 0.0;
259
260       SCM mpen = m->get_property ("penalty");
261       if (scm_is_number (mpen))
262         total_penalty += scm_to_double (mpen);
263
264       command_column_->set_property ("penalty", scm_make_real (total_penalty));
265
266       /* ugh.  arbitrary, hardcoded */
267       if (total_penalty > 10000.0)
268         forbid_breaks ();
269
270       SCM page_pen = command_column_->get_property ("page-penalty");
271       Real total_pp = scm_is_number (page_pen) ? scm_to_double (page_pen) : 0.0;
272       SCM mpage_pen = m->get_property ("page-penalty");
273       if (scm_is_number (mpage_pen))
274         total_pp += scm_to_double (mpage_pen);
275       
276       command_column_->set_property ("page-penalty", scm_make_real (total_pp));
277       return true;
278     }
279   return false;
280 }
281
282 void
283 Score_engraver::forbid_breaks ()
284 {
285   if (command_column_)
286     command_column_->set_property ("breakable", SCM_EOL);
287 }
288   
289 void
290 Score_engraver::acknowledge_grob (Grob_info gi)
291 {
292   if (Staff_spacing::has_interface (gi.grob_))
293     {
294       Pointer_group_interface::add_grob (command_column_,
295                                          ly_symbol2scm ("spacing-wishes"),
296                                          gi.grob_);
297     }
298   if (Note_spacing::has_interface (gi.grob_))
299     {
300       Pointer_group_interface::add_grob (musical_column_,
301                                          ly_symbol2scm ("spacing-wishes"),
302                                          gi.grob_);
303     }
304
305   if (Axis_group_interface::has_interface (gi.grob_)
306       && gi.grob_->internal_has_interface (ly_symbol2scm ("vertically-spaceable-interface")))
307     {
308       SCM spaceable = get_property ("verticallySpacedContexts");
309       Context *orig = gi.origin_contexts (this)[0];
310       
311       if (scm_memq (ly_symbol2scm (orig->context_name ().to_str0()),
312                     spaceable) != SCM_BOOL_F)
313         {
314           Pointer_group_interface::add_grob (system_,
315                                              ly_symbol2scm ("spaceable-staves"),
316                                              gi.grob_);
317         }
318     }
319   
320 }
321
322
323
324 ADD_TRANSLATOR (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 axis-group-interface",
337 /* reads */       "currentMusicalColumn currentCommandColumn verticallySpacedContexts",
338 /* write */       "");