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