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