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