]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
* lily/include/paper-score.hh (class Paper_score): remove unused prototypes.
[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--2005 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       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 "emmentaler-20"
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     {
105       error (_f ("cannot find `%s'", MUSIC_FONT ".otf")
106              + "\n"
107              + _ ("Music font has not been installed properly.")
108              + "\n"
109              + _f ("Search path `%s'", global_path.to_string ().to_str0 ())
110              + "\n"
111              + _ ("Aborting"));
112     }
113
114   pscore_ = new Paper_score (dynamic_cast<Output_def *> (get_output_def ()));
115
116   SCM props = updated_grob_properties (context (), ly_symbol2scm ("System"));
117
118   Object_key const *sys_key = context ()->get_grob_key ("System");
119   pscore_->typeset_system (new System (props, sys_key));
120
121   system_ = pscore_->root_system ();
122   make_columns ();
123   system_->set_bound (LEFT, command_column_);
124   command_column_->set_property ("breakable", SCM_BOOL_T);
125
126   Engraver_group_engraver::initialize ();
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 void
143 Score_engraver::one_time_step ()
144 {
145   if (!to_boolean (get_property ("skipTypesetting")))
146     {
147       recurse_over_translators (context (), &Engraver::process_music, UP);
148       Engraver_group_engraver::do_announces ();
149     }
150
151   recurse_over_translators (context (), &Translator::stop_translation_timestep, UP);
152 }
153
154 void
155 Score_engraver::announce_grob (Grob_info info)
156 {
157   announce_infos_.push (info);
158   pscore_->root_system ()->typeset_grob (info.grob_);
159   elems_.push (info.grob_);
160 }
161
162 void
163 Score_engraver::typeset_all ()
164 {
165   for (int i = 0; i < elems_.size (); i++)
166     {
167       Grob *elem = elems_[i];
168
169       if (dynamic_cast<Item *> (elem))
170         {
171           if ((!elem->get_parent (X_AXIS)
172                || !unsmob_grob (elem->get_property ("axis-group-parent-X")))
173               && elem != command_column_
174               && elem != musical_column_)
175             {
176               bool br = to_boolean (elem->get_property ("breakable"));
177               Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
178             }
179         }
180       if (!elem->get_parent (Y_AXIS))
181         Axis_group_interface::add_element (system_, elem);
182     }
183   elems_.clear ();
184 }
185
186 void
187 Score_engraver::stop_translation_timestep ()
188 {
189   // this generates all items.
190   Engraver_group_engraver::stop_translation_timestep ();
191
192   typeset_all ();
193   if (to_boolean (command_column_->get_property ("breakable")))
194     {
195       breaks_++;
196       if (! (breaks_%8))
197         progress_indication ("[" + to_string (breaks_) + "]");
198     }
199
200   command_column_ = 0;
201   musical_column_ = 0;
202 }
203
204 void
205 Score_engraver::set_columns (Paper_column *new_command,
206                              Paper_column *new_musical)
207 {
208   assert (!command_column_ && !musical_column_);
209
210   command_column_ = new_command;
211   musical_column_ = new_musical;
212   if (new_command)
213     {
214       context ()->set_property ("currentCommandColumn", new_command->self_scm ());
215     }
216
217   if (new_musical)
218     {
219       context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
220     }
221
222   system_->add_column (command_column_);
223   system_->add_column (musical_column_);
224 }
225
226 Music_output *
227 Score_engraver::get_output ()
228 {
229   Music_output *o = pscore_;
230   ///FIXME WTF ? pscore_ = 0;
231   return o;
232 }
233
234 bool
235 Score_engraver::try_music (Music *m)
236 {
237   if (Engraver_group_engraver::try_music (m))
238     return true;
239
240   if (m->is_mus_type ("break-event"))
241     {
242       SCM pen = command_column_->get_property ("penalty");
243       Real total_penalty = scm_is_number (pen) ? scm_to_double (pen) : 0.0;
244
245       SCM mpen = m->get_property ("penalty");
246       if (scm_is_number (mpen))
247         total_penalty += scm_to_double (mpen);
248
249       command_column_->set_property ("penalty", scm_make_real (total_penalty));
250
251       /* ugh.  arbitrary, hardcoded */
252       if (total_penalty > 10000.0)
253         forbid_breaks ();
254
255       SCM page_pen = command_column_->get_property ("page-penalty");
256       Real total_pp = scm_is_number (page_pen) ? scm_to_double (page_pen) : 0.0;
257       SCM mpage_pen = m->get_property ("page-penalty");
258       if (scm_is_number (mpage_pen))
259         total_pp += scm_to_double (mpage_pen);
260
261       command_column_->set_property ("page-penalty", scm_make_real (total_pp));
262       return true;
263     }
264   return false;
265 }
266
267 void
268 Score_engraver::forbid_breaks ()
269 {
270   if (command_column_)
271     command_column_->set_property ("breakable", SCM_EOL);
272 }
273
274 void
275 Score_engraver::acknowledge_grob (Grob_info gi)
276 {
277   if (Staff_spacing::has_interface (gi.grob_))
278     {
279       Pointer_group_interface::add_grob (command_column_,
280                                          ly_symbol2scm ("spacing-wishes"),
281                                          gi.grob_);
282     }
283   if (Note_spacing::has_interface (gi.grob_))
284     {
285       Pointer_group_interface::add_grob (musical_column_,
286                                          ly_symbol2scm ("spacing-wishes"),
287                                          gi.grob_);
288     }
289
290   if (Axis_group_interface::has_interface (gi.grob_)
291       && gi.grob_->internal_has_interface (ly_symbol2scm ("vertically-spaceable-interface")))
292     {
293       SCM spaceable = get_property ("verticallySpacedContexts");
294       Context *orig = gi.origin_contexts (this)[0];
295
296       if (scm_memq (ly_symbol2scm (orig->context_name ().to_str0 ()),
297                     spaceable) != SCM_BOOL_F)
298         {
299           Pointer_group_interface::add_grob (system_,
300                                              ly_symbol2scm ("spaceable-staves"),
301                                              gi.grob_);
302         }
303     }
304 }
305
306 ADD_TRANSLATOR (Score_engraver,
307                 /* descr */ "Top level engraver. Takes care of generating columns and the complete  system (ie. System) "
308                 "\n\n "
309                 "This engraver decides whether a column is breakable. The default is "
310                 "that a column is always breakable. However, when every Bar_engraver "
311                 "that does not have a barline at a certain point will call "
312                 "Score_engraver::forbid_breaks to stop linebreaks.  In practice, this "
313                 "means that you can make a breakpoint by creating a barline (assuming "
314                 "that there are no beams or notes that prevent a breakpoint.) ",
315                 /* creats*/ "System PaperColumn NonMusicalPaperColumn",
316                 /* accepts */ "break-event",
317                 /* acks  */ "note-spacing-interface staff-spacing-interface axis-group-interface",
318                 /* reads */ "currentMusicalColumn currentCommandColumn verticallySpacedContexts",
319                 /* write */ "");