]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
66b759e7d237bc7c9cd31c67ce45afd7fbdd1b35
[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--2000 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 "musical-request.hh"
16 #include "paper-column.hh"
17 #include "command-request.hh"
18 #include "paper-def.hh"
19 #include "axis-group-interface.hh"
20
21 Score_engraver::Score_engraver()
22 {
23   scoreline_l_ =0;
24   command_column_l_ =0;
25   musical_column_l_ =0;
26   breaks_i_ =0;
27   pscore_p_ = 0;
28 }
29  
30 void
31 Score_engraver::prepare (Moment w)
32 {
33   Global_translator::prepare (w);
34
35   SCM props = get_property (ly_symbol2scm ("basicPaperColumnProperties"));
36   set_columns (new Paper_column (props),  new Paper_column (props));
37   
38   command_column_l_->set_elt_property ("when", w.make_scm());
39   musical_column_l_->set_elt_property ("when", w.make_scm());
40   command_column_l_->set_elt_property ("breakable", SCM_BOOL_T);
41   
42   Score_element_info i1(command_column_l_, 0), i2 (musical_column_l_,0);
43
44   i1.origin_trans_l_ = this;
45   i2.origin_trans_l_ = this;
46   announce_element (i1);
47   announce_element (i2);
48   
49   post_move_processing();
50 }
51
52 void
53 Score_engraver::finish()
54 {
55   if ((breaks_i_%8))
56     progress_indication ("[" + to_str ( breaks_i_) + "]");
57    
58   check_removal();
59   removal_processing();
60 }
61
62 /*
63   use start/finish?
64  */
65 void
66 Score_engraver::do_creation_processing ()
67 {
68   scoreline_l_ = pscore_p_->line_l_;
69
70   scoreline_l_->set_bound(LEFT, command_column_l_);
71   
72   command_column_l_->set_elt_property ("breakable", SCM_BOOL_T);
73
74   Engraver_group_engraver::do_creation_processing();
75 }
76
77
78 void
79 Score_engraver::do_removal_processing()
80 {
81   Engraver_group_engraver::do_removal_processing();
82   scoreline_l_->set_bound(RIGHT,command_column_l_);
83   command_column_l_->set_elt_property ("breakable", SCM_BOOL_T);
84   
85   typeset_all ();
86
87   set_columns (0,0);
88 }
89
90 void
91 Score_engraver::process()
92 {
93   process_music();
94   do_announces();
95   pre_move_processing();
96   check_removal();
97 }
98
99 void
100 Score_engraver::announce_element (Score_element_info info)
101 {
102   announce_info_arr_.push (info);
103   pscore_p_->line_l_->typeset_element (info.elem_l_);
104 }
105
106 /* All elements are propagated to the top upon announcement. If
107    something was created during one run of
108    Engraver_group_engraver::do_announces, then
109    announce_info_arr_.size() will be nonzero again
110 */
111 /* junkme? Done by Engraver_group_engraver::do_announces ()?
112  */
113    
114 void
115 Score_engraver::do_announces()
116 {
117   while (announce_info_arr_.size()) 
118     Engraver_group_engraver::do_announces();
119 }
120
121
122 void
123 Score_engraver::typeset_element (Score_element *elem_p)
124 {
125   elem_p_arr_.push (elem_p);
126 }
127
128
129 void
130 Score_engraver::typeset_all()
131 {
132   for  (int i =0; i < elem_p_arr_.size(); i++) 
133     {
134       Score_element * elem_p = elem_p_arr_[i];
135       elem_p->add_processing ();
136
137       
138       if (Spanner *s = dynamic_cast <Spanner *> (elem_p))
139         {
140             /*
141             do something sensible if spanner not 
142             spanned on 2 items.
143            */
144           Direction d = LEFT;
145           do {
146             if (!s->get_bound (d))
147               {
148                 s->set_bound(d, command_column_l_);
149                 ::warning (_f ("unbound spanner `%s'", s->name().ch_C()));
150               }
151           } while (flip(&d) != LEFT);
152         }
153       else 
154         {
155           if (!elem_p->parent_l (X_AXIS))
156             {
157               bool br = to_boolean (elem_p->get_elt_property ("breakable"));
158               Axis_group_interface::add_element (br ? command_column_l_ : musical_column_l_, elem_p);
159
160             }
161         }
162       if (!elem_p->parent_l(Y_AXIS))
163         Axis_group_interface::add_element (scoreline_l_, elem_p);
164     }
165   elem_p_arr_.clear();
166 }
167
168 void
169 Score_engraver::do_pre_move_processing()
170 {
171   // this generates all items.
172   Engraver_group_engraver::do_pre_move_processing();
173   
174   typeset_all();
175   if (to_boolean (command_column_l_->get_elt_property ("breakable")))
176     {
177       breaks_i_ ++;
178       if (! (breaks_i_%8))
179         progress_indication ("[" + to_str ( breaks_i_) + "]");
180     }
181 }
182
183 void
184 Score_engraver::set_columns (Paper_column *new_command_l, 
185                              Paper_column *new_musical_l)
186 {
187   Paper_column * news[] = {new_command_l, new_musical_l};
188   Paper_column **current[] = {&command_column_l_, &musical_column_l_};
189
190   for (int i=00; i< 2; i++) 
191     {
192       if (*current[i])
193         {
194           scoreline_l_->add_column ((*current[i]));
195 #if 0
196           /*
197             TODO: delay this decision.
198            */
199           if (!Paper_column::used_b (*current[i]))
200             {
201               (*current[i])->suicide ();
202               *current[i]  =0;
203             }
204 #endif
205         }
206       if (news[i])
207         *current[i] = news[i];
208     }
209
210   if (new_musical_l)
211     set_property ("currentMusicalColumn", new_musical_l->self_scm ());
212   if (new_command_l)
213     set_property ("currentCommandColumn", new_command_l->self_scm ());  
214 }
215
216 Music_output*
217 Score_engraver::get_output_p ()
218 {
219   Music_output * o = pscore_p_;
220   pscore_p_=0;
221   return o;
222 }
223
224 bool
225 Score_engraver::do_try_music (Music*r)
226 {
227   bool gotcha = Engraver_group_engraver::do_try_music (r);  
228
229   if (!gotcha)
230     {
231       if (Break_req* b = dynamic_cast<Break_req *> (r))
232         {
233           gotcha = true;
234
235
236           SCM pen = command_column_l_->get_elt_property  ("penalty");
237           Real total_penalty = gh_number_p (pen)
238             ? gh_scm2double(pen)
239             : 0.0;
240
241           total_penalty += b->penalty_f_;
242           if (b->penalty_f_ > 10000.0) //  ugh. arbitrary.
243             forbid_breaks ();
244
245           command_column_l_->set_elt_property ("penalty",
246                                                gh_double2scm (total_penalty));
247         }
248     }
249    return gotcha;
250 }
251
252 void
253 Score_engraver::forbid_breaks ()
254 {
255   /*
256     result is junked.
257    */
258   command_column_l_->remove_elt_property ("breakable");
259 }
260
261 ADD_THIS_TRANSLATOR(Score_engraver);
262
263 void
264 Score_engraver::do_add_processing ()
265 {
266   Translator_group::do_add_processing ();
267   assert (dynamic_cast<Paper_def *> (output_def_l_));
268   assert (!daddy_trans_l_);
269   pscore_p_ = new Paper_score;
270   pscore_p_->paper_l_ = dynamic_cast<Paper_def*>(output_def_l_);
271
272   SCM props = get_property(ly_symbol2scm ("basicLineOfScoreProperties"));
273
274   pscore_p_->typeset_line (new Line_of_score (props));
275 }
276