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