]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
* lily/include/debug.hh: deprecate.
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "warn.hh"
10 #include "main.hh"
11 #include "system.hh"
12 #include "item.hh"
13 #include "score-engraver.hh"
14 #include "paper-score.hh"
15 #include "paper-column.hh"
16 #include "command-request.hh"
17 #include "paper-def.hh"
18 #include "axis-group-interface.hh"
19 #include "translator-def.hh"
20
21 #include "staff-spacing.hh"
22 #include "note-spacing.hh"
23
24 /*
25   TODO: the column creation logic is rather hairy. Revise it.
26  */
27 Score_engraver::Score_engraver ()
28 {
29   scoreline_l_ =0;
30   command_column_l_ =0;
31   musical_column_l_ =0;
32   breaks_i_ =0;
33   pscore_p_ = 0;
34 }
35
36 void
37 Score_engraver::make_columns ()
38 {
39   /*
40     ugh.
41    */
42   if (!command_column_l_)
43     {
44       set_columns (new Paper_column (get_property ("NonMusicalPaperColumn")),
45                    new Paper_column (get_property ("PaperColumn")));
46   
47       command_column_l_->set_grob_property ("breakable", SCM_BOOL_T);
48
49
50       Grob_info i1 (command_column_l_);
51       i1.origin_trans_l_ = this;
52   
53       Grob_info i2 (musical_column_l_);
54       i2.origin_trans_l_ = this;
55
56   
57       announce_grob (i1);
58       announce_grob (i2);
59
60       
61     }
62 }
63
64 void
65 Score_engraver::prepare (Moment w)
66 {
67   Global_translator::prepare (w);
68
69   /*
70     TODO: don't make columns when skipTypesetting is true.
71    */
72   make_columns ();
73   
74   command_column_l_->set_grob_property ("when", now_mom_.smobbed_copy ());
75   musical_column_l_->set_grob_property ("when", now_mom_.smobbed_copy ());
76
77   
78   Translator_group::start_translation_timestep();
79 }
80
81 void
82 Score_engraver::finish ()
83 {
84   if ((breaks_i_%8))
85     progress_indication ("[" + to_str (breaks_i_) + "]");
86    
87   check_removal ();
88   removal_processing ();
89
90 }
91
92 /*
93   use start/finish?
94  */
95 void
96 Score_engraver::initialize ()
97 {
98   unsmob_translator_def (definition_)->apply_property_operations (this);
99
100   assert (dynamic_cast<Paper_def *> (output_def_l_));
101   assert (!daddy_trans_l_);
102   pscore_p_ = new Paper_score;
103   pscore_p_->paper_l_ = dynamic_cast<Paper_def*> (output_def_l_);
104
105   SCM props = get_property ("System");
106
107   pscore_p_->typeset_line (new System (props));
108   
109   make_columns ();
110   scoreline_l_ = pscore_p_->line_l_;
111   scoreline_l_->set_bound (LEFT, command_column_l_);
112   command_column_l_->set_grob_property ("breakable", SCM_BOOL_T);
113
114   Engraver_group_engraver::initialize ();
115 }
116
117
118 void
119 Score_engraver::finalize ()
120 {
121   Engraver_group_engraver::finalize ();
122
123   Grob * cc
124     = unsmob_grob (get_property ("currentCommandColumn"));
125   scoreline_l_->set_bound (RIGHT, cc);
126   cc->set_grob_property ("breakable", SCM_BOOL_T);
127   
128   typeset_all ();
129 }
130
131 void
132 Score_engraver::one_time_step ()
133 {
134   if (!to_boolean (get_property ("skipTypesetting")))
135     {
136       process_music ();
137       do_announces ();
138     }
139   
140   stop_translation_timestep ();
141   check_removal ();
142
143
144   for (int i = announce_info_arr_.size(); i--;)
145     {
146       Grob *g = announce_info_arr_[i].grob_l_;
147       if (!dynamic_cast<Paper_column*> (g)) // ugh.
148         {
149       
150           String msg= "Grob "
151             + g->name()
152             + " was created too late!";
153           g->programming_error (msg);
154         }
155     }
156   announce_info_arr_.clear ();
157 }
158
159 void
160 Score_engraver::announce_grob (Grob_info info)
161 {
162   announce_info_arr_.push (info);
163   pscore_p_->line_l_->typeset_grob (info.grob_l_);
164 }
165
166 void
167 Score_engraver::typeset_grob (Grob *elem_p)
168 {
169   if (!elem_p)
170     programming_error ("Score_engraver: empty elt\n");
171   else
172
173     elem_p_arr_.push (elem_p);
174 }
175
176 void
177 Score_engraver::typeset_all ()
178 {
179   for (int i =0; i < elem_p_arr_.size (); i++) 
180     {
181       Grob * elem_p = elem_p_arr_[i];
182       
183       if (Spanner *s = dynamic_cast <Spanner *> (elem_p))
184         {
185           /*
186             do something sensible if spanner not 
187             spanned on 2 items.
188           */
189           Direction d = LEFT;
190           do {
191             if (!s->get_bound (d))
192               {
193                 s->set_bound (d, command_column_l_);
194                 /* don't warn for empty/suicided spanners,
195                    it makes real warningsinvisible.
196                    maybe should be junked earlier? */
197                 if (!elem_p->live())
198                   ; // gdb hook
199                 else
200                   elem_p->warning (_f ("unbound spanner `%s'", s->name ().ch_C ()));
201               }
202           }
203           while (flip (&d) != LEFT);
204
205           if (dynamic_cast<Item*> (s->get_parent (Y_AXIS)))
206             programming_error ("Spanner Y-parent is an item.");
207         }
208       else 
209         {
210           if (!elem_p->get_parent (X_AXIS))
211             {
212               bool br = to_boolean (elem_p->get_grob_property ("breakable"));
213               Axis_group_interface::add_element (br ? command_column_l_ : musical_column_l_, elem_p);
214
215             }
216         }
217       if (!elem_p->get_parent (Y_AXIS))
218         Axis_group_interface::add_element (scoreline_l_, elem_p);
219     }
220   elem_p_arr_.clear ();
221 }
222
223 void
224 Score_engraver::stop_translation_timestep ()
225 {
226   // this generates all items.
227   Engraver_group_engraver::stop_translation_timestep ();
228   
229   typeset_all ();
230   if (to_boolean (command_column_l_->get_grob_property ("breakable")))
231     {
232       breaks_i_ ++;
233       if (! (breaks_i_%8))
234         progress_indication ("[" + to_str (breaks_i_) + "]");
235     }
236
237
238   scoreline_l_->add_column (command_column_l_);
239   scoreline_l_->add_column (musical_column_l_);
240   
241   command_column_l_ = 0;
242   musical_column_l_ = 0;
243 }
244
245 void
246 Score_engraver::set_columns (Paper_column *new_command_l, 
247                              Paper_column *new_musical_l)
248 {
249   assert (!command_column_l_ && !musical_column_l_);
250
251   command_column_l_ = new_command_l;
252   musical_column_l_ = new_musical_l;
253   if (new_command_l)
254     {
255       set_property ("currentCommandColumn", new_command_l->self_scm ());  
256     }
257   
258   if (new_musical_l)
259     {
260       set_property ("currentMusicalColumn", new_musical_l->self_scm ());
261     }
262 }
263
264 Music_output*
265 Score_engraver::get_output_p ()
266 {
267   Music_output * o = pscore_p_;
268   pscore_p_=0;
269   return o;
270 }
271
272 bool
273 Score_engraver::try_music (Music*r)
274 {
275   bool gotcha = Engraver_group_engraver::try_music (r);  
276
277   if (!gotcha)
278     {
279       if (Break_req* b = dynamic_cast<Break_req *> (r))
280         {
281           gotcha = true;
282
283
284           SCM pen = command_column_l_->get_grob_property ("penalty");
285           Real total_penalty = gh_number_p (pen)
286             ? gh_scm2double (pen)
287             : 0.0;
288
289           SCM rpen = b->get_mus_property ("penalty");
290           if (gh_number_p (rpen))
291             total_penalty +=  gh_scm2double (rpen);
292           
293           if (total_penalty > 10000.0) //  ugh. arbitrary.
294             forbid_breaks ();
295
296           command_column_l_->set_grob_property ("penalty",
297                                                gh_double2scm (total_penalty));
298         }
299     }
300    return gotcha;
301 }
302
303 /*
304   TODO:  use property Score.breakForbidden = #t
305  */
306 void
307 Score_engraver::forbid_breaks ()
308 {
309   /*
310     result is junked.
311    */
312   if (command_column_l_)
313     command_column_l_->remove_grob_property ("breakable");
314 }
315   
316 void
317 Score_engraver::acknowledge_grob (Grob_info gi)
318 {
319   if (Staff_spacing::has_interface (gi.grob_l_))
320     {
321       Pointer_group_interface::add_grob (command_column_l_,
322                                          ly_symbol2scm ("spacing-wishes"),
323                                          gi.grob_l_);
324     }
325   if (Note_spacing::has_interface (gi.grob_l_))
326     {
327       Pointer_group_interface::add_grob (musical_column_l_,
328                                          ly_symbol2scm ("spacing-wishes"),
329                                          gi.grob_l_);
330     }
331 }
332
333
334
335 ENTER_DESCRIPTION(Score_engraver,
336 /* descr */       "Top level engraver. Takes care of generating columns and the complete  system (ie. System)
337
338
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.)
345
346
347 ",
348 /* creats*/       "System PaperColumn NonMusicalPaperColumn",
349 /* acks  */       "note-spacing-interface staff-spacing-interface",
350 /* reads */       "currentMusicalColumn currentCommandColumn",
351 /* write */       "");