]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
* lily/include/lily-guile.hh: is_x -> ly_c_X_p naming.
[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 "paper-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 (get_parent_context (),
51                                    ly_symbol2scm ("NonMusicalPaperColumn"));
52       SCM pc = updated_grob_properties (get_parent_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 (get_parent_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 (get_parent_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" +_ ("Fonts have not been installed properly.  Aborting"));
105    
106
107   pscore_ = new Paper_score;
108   pscore_->paper_ = dynamic_cast<Paper_def*> (get_output_def ());
109
110   SCM props = updated_grob_properties (get_parent_context (), ly_symbol2scm ("System"));
111
112   pscore_->typeset_line (new System (props));
113   
114   make_columns ();
115   system_ = pscore_->system_;
116   system_->set_bound (LEFT, command_column_);
117   command_column_->set_property ("breakable", SCM_BOOL_T);
118
119   Engraver_group_engraver::initialize ();
120 }
121
122
123 void
124 Score_engraver::finalize ()
125 {
126   Score_translator::finalize ();
127
128   Grob * cc
129     = unsmob_grob (get_property ("currentCommandColumn"));
130   system_->set_bound (RIGHT, cc);
131   cc->set_property ("breakable", SCM_BOOL_T);
132   
133   typeset_all ();
134 }
135
136
137 void
138 Score_engraver::one_time_step ()
139 {
140   if (!to_boolean (get_property ("skipTypesetting")))
141     {
142       recurse_over_translators (get_parent_context (), &Engraver::process_music, UP);
143       recurse_over_translators (get_parent_context (), &Engraver::do_announces, UP);
144     }
145   
146   recurse_over_translators (get_parent_context (), &Translator::stop_translation_timestep, UP);
147 }
148
149 void
150 Score_engraver::announce_grob (Grob_info info)
151 {
152   announce_infos_.push (info);
153   pscore_->system_->typeset_grob (info.grob_);
154 }
155
156 void
157 Score_engraver::typeset_grob (Grob *elem)
158 {
159   if (!elem)
160     programming_error ("Score_engraver: empty elt\n");
161   else
162     elems_.push (elem);
163 }
164
165 void
166 Score_engraver::typeset_all ()
167 {
168   for (int i =0; i < elems_.size (); i++) 
169     {
170       Grob * elem = elems_[i];
171       
172       if (Spanner *s = dynamic_cast <Spanner *> (elem))
173         {
174           /*
175             do something sensible if spanner not 
176             spanned on 2 items.
177           */
178           Direction d = LEFT;
179           do {
180             if (!s->get_bound (d))
181               {
182                 Grob * cc
183                   = unsmob_grob (get_property ("currentCommandColumn"));
184                 s->set_bound (d, cc);
185                 /* don't warn for empty/suicided spanners,
186                    it makes real warningsinvisible.
187                    maybe should be junked earlier? */
188                 if (elem->live ())
189                   elem->warning (_f ("unbound spanner `%s'", s->name ().to_str0 ()));
190               }
191           }
192           while (flip (&d) != LEFT);
193
194           if (dynamic_cast<Item*> (s->get_parent (Y_AXIS)))
195             programming_error ("Spanner Y-parent is an item.");
196         }
197       else 
198         {
199           if (!elem->get_parent (X_AXIS))
200             {
201               bool br = to_boolean (elem->get_property ("breakable"));
202               Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
203
204             }
205         }
206       if (!elem->get_parent (Y_AXIS))
207         Axis_group_interface::add_element (system_, elem);
208     }
209   elems_.clear ();
210 }
211
212 void
213 Score_engraver::stop_translation_timestep ()
214 {
215   // this generates all items.
216   Engraver_group_engraver::stop_translation_timestep ();
217   
218   typeset_all ();
219   if (to_boolean (command_column_->get_property ("breakable")))
220     {
221       breaks_ ++;
222       if (! (breaks_%8))
223         progress_indication ("[" + to_string (breaks_) + "]");
224     }
225
226
227   system_->add_column (command_column_);
228   system_->add_column (musical_column_);
229   
230   command_column_ = 0;
231   musical_column_ = 0;
232 }
233
234 void
235 Score_engraver::set_columns (Paper_column *new_command, 
236                              Paper_column *new_musical)
237 {
238   assert (!command_column_ && !musical_column_);
239
240   command_column_ = new_command;
241   musical_column_ = new_musical;
242   if (new_command)
243     {
244       get_parent_context ()->set_property ("currentCommandColumn", new_command->self_scm ());  
245     }
246   
247   if (new_musical)
248     {
249       get_parent_context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
250     }
251 }
252
253 Music_output*
254 Score_engraver::get_output ()
255 {
256   Music_output *o = pscore_;
257   ///FIXME WTF?  pscore_ = 0;
258   return o;
259 }
260
261 bool
262 Score_engraver::try_music (Music *m)
263 {
264   if (Engraver_group_engraver::try_music (m))
265     return true;
266
267   if (m->is_mus_type ("break-event"))
268     {
269       SCM pen = command_column_->get_property ("penalty");
270       Real total_penalty = ly_c_number_p (pen) ? ly_scm2double (pen) : 0.0;
271
272       SCM mpen = m->get_property ("penalty");
273       if (ly_c_number_p (mpen))
274         total_penalty += ly_scm2double (mpen);
275
276       command_column_->set_property ("penalty", scm_make_real (total_penalty));
277
278       /* ugh.  arbitrary, hardcoded */
279       if (total_penalty > 10000.0)
280         forbid_breaks ();
281
282       SCM page_pen = command_column_->get_property ("page-penalty");
283       Real total_pp = ly_c_number_p (page_pen) ? ly_scm2double (page_pen) : 0.0;
284       SCM mpage_pen = m->get_property ("page-penalty");
285       if (ly_c_number_p (mpage_pen))
286         total_pp += ly_scm2double (mpage_pen);
287       
288       command_column_->set_property ("page-penalty", scm_make_real (total_pp));
289       return true;
290     }
291   return false;
292 }
293
294 void
295 Score_engraver::forbid_breaks ()
296 {
297   if (command_column_)
298     command_column_->set_property ("breakable", SCM_EOL);
299 }
300   
301 void
302 Score_engraver::acknowledge_grob (Grob_info gi)
303 {
304   if (Staff_spacing::has_interface (gi.grob_))
305     {
306       Pointer_group_interface::add_grob (command_column_,
307                                          ly_symbol2scm ("spacing-wishes"),
308                                          gi.grob_);
309     }
310   if (Note_spacing::has_interface (gi.grob_))
311     {
312       Pointer_group_interface::add_grob (musical_column_,
313                                          ly_symbol2scm ("spacing-wishes"),
314                                          gi.grob_);
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",
333 /* reads */       "currentMusicalColumn currentCommandColumn",
334 /* write */       "");