]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
release: 1.3.107
[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   set_columns (new Paper_column (get_property (ly_symbol2scm ("NonMusicalPaperColumn"))),
36                new Paper_column (get_property (ly_symbol2scm ("PaperColumn"))));
37   
38   command_column_l_->set_elt_property ("when", w.smobbed_copy());
39   musical_column_l_->set_elt_property ("when", w.smobbed_copy());
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       
136       if (Spanner *s = dynamic_cast <Spanner *> (elem_p))
137         {
138             /*
139             do something sensible if spanner not 
140             spanned on 2 items.
141            */
142           Direction d = LEFT;
143           do {
144             if (!s->get_bound (d))
145               {
146                 s->set_bound(d, command_column_l_);
147                 ::warning (_f ("unbound spanner `%s'", s->name().ch_C()));
148               }
149           } while (flip(&d) != LEFT);
150         }
151       else 
152         {
153           if (!elem_p->parent_l (X_AXIS))
154             {
155               bool br = to_boolean (elem_p->get_elt_property ("breakable"));
156               Axis_group_interface::add_element (br ? command_column_l_ : musical_column_l_, elem_p);
157
158             }
159         }
160       if (!elem_p->parent_l(Y_AXIS))
161         Axis_group_interface::add_element (scoreline_l_, elem_p);
162     }
163   elem_p_arr_.clear();
164 }
165
166 void
167 Score_engraver::do_pre_move_processing()
168 {
169   // this generates all items.
170   Engraver_group_engraver::do_pre_move_processing();
171   
172   typeset_all();
173   if (to_boolean (command_column_l_->get_elt_property ("breakable")))
174     {
175       breaks_i_ ++;
176       if (! (breaks_i_%8))
177         progress_indication ("[" + to_str ( breaks_i_) + "]");
178     }
179 }
180
181 void
182 Score_engraver::set_columns (Paper_column *new_command_l, 
183                              Paper_column *new_musical_l)
184 {
185   Paper_column * news[] = {new_command_l, new_musical_l};
186   Paper_column **current[] = {&command_column_l_, &musical_column_l_};
187
188   for (int i=00; i< 2; i++) 
189     {
190       if (*current[i])
191         {
192           scoreline_l_->add_column ((*current[i]));
193 #if 0
194           /*
195             TODO: delay this decision.
196            */
197           if (!Paper_column::used_b (*current[i]))
198             {
199               (*current[i])->suicide ();
200               *current[i]  =0;
201             }
202 #endif
203         }
204       if (news[i])
205         *current[i] = news[i];
206     }
207
208   if (new_musical_l)
209     set_property ("currentMusicalColumn", new_musical_l->self_scm ());
210   if (new_command_l)
211     set_property ("currentCommandColumn", new_command_l->self_scm ());  
212 }
213
214 Music_output*
215 Score_engraver::get_output_p ()
216 {
217   Music_output * o = pscore_p_;
218   pscore_p_=0;
219   return o;
220 }
221
222 bool
223 Score_engraver::do_try_music (Music*r)
224 {
225   bool gotcha = Engraver_group_engraver::do_try_music (r);  
226
227   if (!gotcha)
228     {
229       if (Break_req* b = dynamic_cast<Break_req *> (r))
230         {
231           gotcha = true;
232
233
234           SCM pen = command_column_l_->get_elt_property  ("penalty");
235           Real total_penalty = gh_number_p (pen)
236             ? gh_scm2double(pen)
237             : 0.0;
238
239           SCM rpen = b->get_mus_property ("penalty");
240           if (gh_number_p (rpen))
241             total_penalty +=  gh_scm2double (rpen);
242           
243           if (total_penalty > 10000.0) //  ugh. arbitrary.
244             forbid_breaks ();
245
246           command_column_l_->set_elt_property ("penalty",
247                                                gh_double2scm (total_penalty));
248         }
249     }
250    return gotcha;
251 }
252
253 void
254 Score_engraver::forbid_breaks ()
255 {
256   /*
257     result is junked.
258    */
259   command_column_l_->remove_elt_property ("breakable");
260 }
261
262 ADD_THIS_TRANSLATOR(Score_engraver);
263
264 void
265 Score_engraver::do_add_processing ()
266 {
267   Translator_group::do_add_processing ();
268   assert (dynamic_cast<Paper_def *> (output_def_l_));
269   assert (!daddy_trans_l_);
270   pscore_p_ = new Paper_score;
271   pscore_p_->paper_l_ = dynamic_cast<Paper_def*>(output_def_l_);
272
273   SCM props = get_property (ly_symbol2scm ("LineOfScore"));
274
275   pscore_p_->typeset_line (new Line_of_score (props));
276 }
277