release commit
[lilypond.git] / lily / score-engraver.cc
blobd19bf5dd91861990541035de24c80603a8787981
1 /*
2 score-engraver.cc -- implement Score_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
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"
19 #include "paper-def.hh"
20 #include "axis-group-interface.hh"
21 #include "translator-def.hh"
23 #include "staff-spacing.hh"
24 #include "note-spacing.hh"
27 TODO: the column creation logic is rather hairy. Revise it.
29 Score_engraver::Score_engraver ()
31 system_ =0;
32 command_column_ =0;
33 musical_column_ =0;
34 breaks_ =0;
35 pscore_ = 0;
38 void
39 Score_engraver::make_columns ()
42 ugh.
44 if (!command_column_)
46 set_columns (new Paper_column (get_property ("NonMusicalPaperColumn")),
47 new Paper_column (get_property ("PaperColumn")));
49 command_column_->set_grob_property ("breakable", SCM_BOOL_T);
52 Grob_info i1 (command_column_);
53 i1.origin_trans_ = this;
55 Grob_info i2 (musical_column_);
56 i2.origin_trans_ = this;
58 announce_grob (i1);
59 announce_grob (i2);
63 void
64 Score_engraver::prepare (Moment w)
66 Global_translator::prepare (w);
69 TODO: don't make columns when skipTypesetting is true.
71 make_columns ();
73 command_column_->set_grob_property ("when", now_mom_.smobbed_copy ());
74 musical_column_->set_grob_property ("when", now_mom_.smobbed_copy ());
77 Translator_group::start_translation_timestep();
80 void
81 Score_engraver::finish ()
83 if ((breaks_%8))
84 progress_indication ("[" + to_string (breaks_) + "]");
86 check_removal ();
87 removal_processing ();
92 use start/finish?
94 void
95 Score_engraver::initialize ()
97 Font_metric *fm = all_fonts_global->find_afm("feta20");
98 if (!fm)
99 error (_f ("can't find `%s'", "feta20.afm")
100 + "\n" +_ ("Fonts have not been installed properly. Aborting"));
102 unsmob_translator_def (definition_)->apply_property_operations (this);
104 assert (dynamic_cast<Paper_def *> (output_def_));
105 assert (!daddy_trans_);
106 pscore_ = new Paper_score;
107 pscore_->paper_ = dynamic_cast<Paper_def*> (output_def_);
109 SCM props = get_property ("System");
111 pscore_->typeset_line (new System (props));
113 make_columns ();
114 system_ = pscore_->system_;
115 system_->set_bound (LEFT, command_column_);
116 command_column_->set_grob_property ("breakable", SCM_BOOL_T);
118 Engraver_group_engraver::initialize ();
122 void
123 Score_engraver::finalize ()
125 Engraver_group_engraver::finalize ();
127 Grob * cc
128 = unsmob_grob (get_property ("currentCommandColumn"));
129 system_->set_bound (RIGHT, cc);
130 cc->set_grob_property ("breakable", SCM_BOOL_T);
132 typeset_all ();
135 void
136 Score_engraver::one_time_step ()
138 if (!to_boolean (get_property ("skipTypesetting")))
140 process_music ();
141 do_announces ();
144 stop_translation_timestep ();
146 apply_finalizations ();
147 check_removal ();
150 for (int i = announce_infos_.size(); i--;)
152 Grob *g = announce_infos_[i].grob_;
153 if (!dynamic_cast<Paper_column*> (g)) // ugh.
156 String msg= "Grob "
157 + g->name()
158 + " was created too late!";
159 g->programming_error (msg);
162 announce_infos_.clear ();
165 void
166 Score_engraver::announce_grob (Grob_info info)
168 announce_infos_.push (info);
169 pscore_->system_->typeset_grob (info.grob_);
172 void
173 Score_engraver::typeset_grob (Grob *elem)
175 if (!elem)
176 programming_error ("Score_engraver: empty elt\n");
177 else
178 elems_.push (elem);
181 void
182 Score_engraver::typeset_all ()
184 for (int i =0; i < elems_.size (); i++)
186 Grob * elem = elems_[i];
188 if (Spanner *s = dynamic_cast <Spanner *> (elem))
191 do something sensible if spanner not
192 spanned on 2 items.
194 Direction d = LEFT;
195 do {
196 if (!s->get_bound (d))
198 s->set_bound (d, command_column_);
199 /* don't warn for empty/suicided spanners,
200 it makes real warningsinvisible.
201 maybe should be junked earlier? */
202 if (!elem->live())
203 ; // gdb hook
204 else
205 elem->warning (_f ("unbound spanner `%s'", s->name ().to_str0 ()));
208 while (flip (&d) != LEFT);
210 if (dynamic_cast<Item*> (s->get_parent (Y_AXIS)))
211 programming_error ("Spanner Y-parent is an item.");
213 else
215 if (!elem->get_parent (X_AXIS))
217 bool br = to_boolean (elem->get_grob_property ("breakable"));
218 Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
222 if (!elem->get_parent (Y_AXIS))
223 Axis_group_interface::add_element (system_, elem);
225 elems_.clear ();
228 void
229 Score_engraver::stop_translation_timestep ()
231 // this generates all items.
232 Engraver_group_engraver::stop_translation_timestep ();
234 typeset_all ();
235 if (to_boolean (command_column_->get_grob_property ("breakable")))
237 breaks_ ++;
238 if (! (breaks_%8))
239 progress_indication ("[" + to_string (breaks_) + "]");
243 system_->add_column (command_column_);
244 system_->add_column (musical_column_);
246 command_column_ = 0;
247 musical_column_ = 0;
250 void
251 Score_engraver::set_columns (Paper_column *new_command,
252 Paper_column *new_musical)
254 assert (!command_column_ && !musical_column_);
256 command_column_ = new_command;
257 musical_column_ = new_musical;
258 if (new_command)
260 set_property ("currentCommandColumn", new_command->self_scm ());
263 if (new_musical)
265 set_property ("currentMusicalColumn", new_musical->self_scm ());
269 Music_output*
270 Score_engraver::get_output ()
272 Music_output * o = pscore_;
273 pscore_=0;
274 return o;
277 bool
278 Score_engraver::try_music (Music*r)
280 bool gotcha = Engraver_group_engraver::try_music (r);
282 if (!gotcha && r->is_mus_type ("break-event"))
284 gotcha = true;
286 SCM pen = command_column_->get_grob_property ("penalty");
287 Real total_penalty = gh_number_p (pen)
288 ? gh_scm2double (pen)
289 : 0.0;
291 SCM rpen = r->get_mus_property ("penalty");
292 if (gh_number_p (rpen))
293 total_penalty += gh_scm2double (rpen);
295 if (total_penalty > 10000.0) // ugh. arbitrary.
296 forbid_breaks ();
298 command_column_->set_grob_property ("penalty",
299 gh_double2scm (total_penalty));
301 return gotcha;
305 TODO: use property Score.breakForbidden = #t
307 void
308 Score_engraver::forbid_breaks ()
311 result is junked.
313 if (command_column_)
314 command_column_->set_grob_property ("breakable", SCM_EOL);
317 void
318 Score_engraver::acknowledge_grob (Grob_info gi)
320 if (Staff_spacing::has_interface (gi.grob_))
322 Pointer_group_interface::add_grob (command_column_,
323 ly_symbol2scm ("spacing-wishes"),
324 gi.grob_);
326 if (Note_spacing::has_interface (gi.grob_))
328 Pointer_group_interface::add_grob (musical_column_,
329 ly_symbol2scm ("spacing-wishes"),
330 gi.grob_);
336 ENTER_DESCRIPTION(Score_engraver,
337 /* descr */ "Top level engraver. Takes care of generating columns and the complete system (ie. System) "
338 "\n\n "
339 "This engraver decides whether a column is breakable. The default is "
340 "that a column is always breakable. However, when every Bar_engraver "
341 "that does not have a barline at a certain point will call "
342 "Score_engraver::forbid_breaks to stop linebreaks. In practice, this "
343 "means that you can make a breakpoint by creating a barline (assuming "
344 "that there are no beams or notes that prevent a breakpoint.) "
346 /* creats*/ "System PaperColumn NonMusicalPaperColumn",
347 /* accepts */ "break-event",
348 /* acks */ "note-spacing-interface staff-spacing-interface",
349 /* reads */ "currentMusicalColumn currentCommandColumn",
350 /* write */ "");