]> 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 #include "open-type-font.hh"
24
25 /*
26   TODO: the column creation logic is rather hairy. Revise it.
27  */
28 Score_engraver::Score_engraver ()
29 {
30   system_ = 0;
31   command_column_ = 0;
32   musical_column_ = 0;
33   breaks_ = 0;
34   pscore_ = 0;
35 }
36
37 void
38 Score_engraver::make_columns ()
39 {
40   /*
41     ugh.
42    */
43   if (!command_column_)
44     {
45       SCM nmp
46         = updated_grob_properties (context (),
47                                    ly_symbol2scm ("NonMusicalPaperColumn"));
48
49       Object_key const *key1 = context()->get_grob_key ("NonMusicalPaperColumn");
50       
51       SCM pc = updated_grob_properties (context (),
52                                         ly_symbol2scm ("PaperColumn"));
53       Object_key const *key2 = context()->get_grob_key ("PaperColumn");      
54       set_columns (new Paper_column (nmp, key1), new Paper_column (pc, key2));
55
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 #define MUSIC_FONT "bigcheese20"
96
97 /*
98   use start/finish?
99  */
100 void
101 Score_engraver::initialize ()
102 {
103   Font_metric *fm = all_fonts_global->find_otf (MUSIC_FONT);
104   if (!fm)
105     error (_f ("cannot find `%s'", MUSIC_FONT ".otf")
106            + "\n"
107            + _ ("Music font has not been installed properly.")
108            + _ ("Aborting"));
109
110   if (!scm_is_string (ly_kpathsea_find_file (scm_makfrom0str ("ecrm10.pfa")))
111       && (!scm_is_string (ly_kpathsea_find_file (scm_makfrom0str ("lmr10.pfb")))))
112       error (_f ("cannot find `%s'", "ecrm10.pfa")
113              + "\n"
114              + _f ("cannot find `%s'", "lmr10.pfb")
115              + "\n"
116              + _f ("Install the ec-fonts-mftraced package from: %s.",
117                    "http://lilypond.org/download/fonts/")
118              + "\n"
119              + _ ("Aborting."));
120       
121   pscore_ = new Paper_score;
122   pscore_->layout_ = dynamic_cast<Output_def*> (get_output_def ());
123
124   SCM props = updated_grob_properties (context (), ly_symbol2scm ("System"));
125
126   Object_key const *sys_key = context()->get_grob_key ("System");
127   pscore_->typeset_line (new System (props, sys_key));
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 (
184               (!elem->get_parent (X_AXIS)
185                || !unsmob_grob  (elem->get_property ("axis-group-parent-X")))
186               && elem != command_column_
187               && elem != musical_column_
188               )
189             {
190               bool br = to_boolean (elem->get_property ("breakable"));
191               Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
192
193             }
194         }
195       if (!elem->get_parent (Y_AXIS))
196         Axis_group_interface::add_element (system_, elem);
197     }
198   elems_.clear ();
199 }
200
201 void
202 Score_engraver::stop_translation_timestep ()
203 {
204   // this generates all items.
205   Engraver_group_engraver::stop_translation_timestep ();
206   
207   typeset_all ();
208   if (to_boolean (command_column_->get_property ("breakable")))
209     {
210       breaks_ ++;
211       if (! (breaks_%8))
212         progress_indication ("[" + to_string (breaks_) + "]");
213     }
214
215
216   system_->add_column (command_column_);
217   system_->add_column (musical_column_);
218   
219   command_column_ = 0;
220   musical_column_ = 0;
221 }
222
223 void
224 Score_engraver::set_columns (Paper_column *new_command, 
225                              Paper_column *new_musical)
226 {
227   assert (!command_column_ && !musical_column_);
228
229   command_column_ = new_command;
230   musical_column_ = new_musical;
231   if (new_command)
232     {
233       context ()->set_property ("currentCommandColumn", new_command->self_scm ());  
234     }
235   
236   if (new_musical)
237     {
238       context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
239     }
240 }
241
242 Music_output*
243 Score_engraver::get_output ()
244 {
245   Music_output *o = pscore_;
246   ///FIXME WTF?  pscore_ = 0;
247   return o;
248 }
249
250 bool
251 Score_engraver::try_music (Music *m)
252 {
253   if (Engraver_group_engraver::try_music (m))
254     return true;
255
256   if (m->is_mus_type ("break-event"))
257     {
258       SCM pen = command_column_->get_property ("penalty");
259       Real total_penalty = scm_is_number (pen) ? scm_to_double (pen) : 0.0;
260
261       SCM mpen = m->get_property ("penalty");
262       if (scm_is_number (mpen))
263         total_penalty += scm_to_double (mpen);
264
265       command_column_->set_property ("penalty", scm_make_real (total_penalty));
266
267       /* ugh.  arbitrary, hardcoded */
268       if (total_penalty > 10000.0)
269         forbid_breaks ();
270
271       SCM page_pen = command_column_->get_property ("page-penalty");
272       Real total_pp = scm_is_number (page_pen) ? scm_to_double (page_pen) : 0.0;
273       SCM mpage_pen = m->get_property ("page-penalty");
274       if (scm_is_number (mpage_pen))
275         total_pp += scm_to_double (mpage_pen);
276       
277       command_column_->set_property ("page-penalty", scm_make_real (total_pp));
278       return true;
279     }
280   return false;
281 }
282
283 void
284 Score_engraver::forbid_breaks ()
285 {
286   if (command_column_)
287     command_column_->set_property ("breakable", SCM_EOL);
288 }
289   
290 void
291 Score_engraver::acknowledge_grob (Grob_info gi)
292 {
293   if (Staff_spacing::has_interface (gi.grob_))
294     {
295       Pointer_group_interface::add_grob (command_column_,
296                                          ly_symbol2scm ("spacing-wishes"),
297                                          gi.grob_);
298     }
299   if (Note_spacing::has_interface (gi.grob_))
300     {
301       Pointer_group_interface::add_grob (musical_column_,
302                                          ly_symbol2scm ("spacing-wishes"),
303                                          gi.grob_);
304     }
305
306   if (Axis_group_interface::has_interface (gi.grob_)
307       && gi.grob_->internal_has_interface (ly_symbol2scm ("vertically-spaceable-interface")))
308     {
309       SCM spaceable = get_property ("verticallySpacedContexts");
310       Context *orig = gi.origin_contexts (this)[0];
311       
312       if (scm_memq (ly_symbol2scm (orig->context_name ().to_str0()),
313                     spaceable) != SCM_BOOL_F)
314         {
315           Pointer_group_interface::add_grob (system_,
316                                              ly_symbol2scm ("spaceable-staves"),
317                                              gi.grob_);
318         }
319     }
320   
321 }
322
323
324
325 ADD_TRANSLATOR (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 axis-group-interface",
338 /* reads */       "currentMusicalColumn currentCommandColumn verticallySpacedContexts",
339 /* write */       "");