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