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