]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
release commit
[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 void
144 Score_engraver::typeset_grob (Grob *elem_p)
145 {
146   if (!elem_p)
147     programming_error ("Score_engraver: empty elt\n");
148   else
149
150     elem_p_arr_.push (elem_p);
151 }
152
153 void
154 Score_engraver::typeset_all ()
155 {
156   for (int i =0; i < elem_p_arr_.size (); i++) 
157     {
158       Grob * elem_p = elem_p_arr_[i];
159       
160       if (Spanner *s = dynamic_cast <Spanner *> (elem_p))
161         {
162           /*
163             do something sensible if spanner not 
164             spanned on 2 items.
165           */
166           Direction d = LEFT;
167           do {
168             if (!s->get_bound (d))
169               {
170                 s->set_bound (d, command_column_l_);
171                 /* don't warn for empty/suicided spanners,
172                    it makes real warningsinvisible.
173                    maybe should be junked earlier? */
174                 if (!elem_p->live())
175                   ; // gdb hook
176                 else
177                   elem_p->warning (_f ("unbound spanner `%s'", s->name ().ch_C ()));
178               }
179           }
180           while (flip (&d) != LEFT);
181
182           if (dynamic_cast<Item*> (s->get_parent (Y_AXIS)))
183             programming_error ("Spanner Y-parent is an item.");
184         }
185       else 
186         {
187           if (!elem_p->get_parent (X_AXIS))
188             {
189               bool br = to_boolean (elem_p->get_grob_property ("breakable"));
190               Axis_group_interface::add_element (br ? command_column_l_ : musical_column_l_, elem_p);
191
192             }
193         }
194       if (!elem_p->get_parent (Y_AXIS))
195         Axis_group_interface::add_element (scoreline_l_, elem_p);
196     }
197   elem_p_arr_.clear ();
198 }
199
200 void
201 Score_engraver::stop_translation_timestep ()
202 {
203   // this generates all items.
204   Engraver_group_engraver::stop_translation_timestep ();
205   
206   typeset_all ();
207   if (to_boolean (command_column_l_->get_grob_property ("breakable")))
208     {
209       breaks_i_ ++;
210       if (! (breaks_i_%8))
211         progress_indication ("[" + to_str (breaks_i_) + "]");
212     }
213
214
215   scoreline_l_->add_column (command_column_l_);
216   scoreline_l_->add_column (musical_column_l_);
217   
218   command_column_l_ = 0;
219   musical_column_l_ = 0;
220 }
221
222 void
223 Score_engraver::set_columns (Paper_column *new_command_l, 
224                              Paper_column *new_musical_l)
225 {
226   assert (!command_column_l_ && !musical_column_l_);
227
228   command_column_l_ = new_command_l;
229   musical_column_l_ = new_musical_l;
230   if (new_command_l)
231     {
232       set_property ("currentCommandColumn", new_command_l->self_scm ());  
233     }
234   
235   if (new_musical_l)
236     {
237       set_property ("currentMusicalColumn", new_musical_l->self_scm ());
238     }
239 }
240
241 Music_output*
242 Score_engraver::get_output_p ()
243 {
244   Music_output * o = pscore_p_;
245   pscore_p_=0;
246   return o;
247 }
248
249 bool
250 Score_engraver::try_music (Music*r)
251 {
252   bool gotcha = Engraver_group_engraver::try_music (r);  
253
254   if (!gotcha)
255     {
256       if (Break_req* b = dynamic_cast<Break_req *> (r))
257         {
258           gotcha = true;
259
260
261           SCM pen = command_column_l_->get_grob_property ("penalty");
262           Real total_penalty = gh_number_p (pen)
263             ? gh_scm2double (pen)
264             : 0.0;
265
266           SCM rpen = b->get_mus_property ("penalty");
267           if (gh_number_p (rpen))
268             total_penalty +=  gh_scm2double (rpen);
269           
270           if (total_penalty > 10000.0) //  ugh. arbitrary.
271             forbid_breaks ();
272
273           command_column_l_->set_grob_property ("penalty",
274                                                gh_double2scm (total_penalty));
275         }
276     }
277    return gotcha;
278 }
279
280 /*
281   TODO:  use property Score.breakForbidden = #t
282  */
283 void
284 Score_engraver::forbid_breaks ()
285 {
286   /*
287     result is junked.
288    */
289   command_column_l_->remove_grob_property ("breakable");
290 }
291   
292 void
293 Score_engraver::acknowledge_grob (Grob_info gi)
294 {
295   if (Staff_spacing::has_interface (gi.grob_l_))
296     {
297       Pointer_group_interface::add_grob (command_column_l_,
298                                          ly_symbol2scm ("spacing-wishes"),
299                                          gi.grob_l_);
300     }
301   if (Note_spacing::has_interface (gi.grob_l_))
302     {
303       Pointer_group_interface::add_grob (musical_column_l_,
304                                          ly_symbol2scm ("spacing-wishes"),
305                                          gi.grob_l_);
306     }
307 }
308
309
310
311 ENTER_DESCRIPTION(Score_engraver,
312 /* descr */       "Top level engraver. Takes care of generating columns and the complete  system (ie. System)
313
314
315 This engraver decides whether a column is breakable. The default is
316 that a column is always breakable. However, when every Bar_engraver
317 that does not have a barline at a certain point will call
318 Score_engraver::forbid_breaks to stop linebreaks.  In practice, this
319 means that you can make a breakpoint by creating a barline (assuming
320 that there are no beams or notes that prevent a breakpoint.)
321
322
323 ",
324 /* creats*/       "System PaperColumn NonMusicalPaperColumn",
325 /* acks  */       "note-spacing-interface staff-spacing-interface",
326 /* reads */       "currentMusicalColumn currentCommandColumn",
327 /* write */       "");