]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-engraver.cc
release: 1.3.68
[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 #include "dimension-cache.hh"
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   SCM when = smobify (new Moment (w));
39   command_column_l_->set_elt_property ("when", when);
40   musical_column_l_->set_elt_property ("when", when);
41   command_column_l_->set_elt_property ("breakable", SCM_BOOL_T);
42   
43   Score_element_info i1(command_column_l_, 0), i2 (musical_column_l_,0);
44
45   i1.origin_trans_l_ = this;
46   i2.origin_trans_l_ = this;
47   announce_element (i1);
48   announce_element (i2);
49   
50   post_move_processing();
51 }
52
53 void
54 Score_engraver::finish()
55 {
56   if ((breaks_i_%8))
57     progress_indication ("[" + to_str ( breaks_i_) + "]");
58    
59   check_removal();
60   removal_processing();
61 }
62
63 /*
64   use start/finish?
65  */
66 void
67 Score_engraver::do_creation_processing ()
68 {
69   scoreline_l_ = pscore_p_->line_l_;
70
71   scoreline_l_->set_bound(LEFT, command_column_l_);
72   
73   command_column_l_->set_elt_property ("breakable", SCM_BOOL_T);
74
75   Engraver_group_engraver::do_creation_processing();
76 }
77
78
79 void
80 Score_engraver::do_removal_processing()
81 {
82   Engraver_group_engraver::do_removal_processing();
83   scoreline_l_->set_bound(RIGHT,command_column_l_);
84   command_column_l_->set_elt_property ("breakable", SCM_BOOL_T);
85
86   
87   typeset_all ();
88
89
90   set_columns (0,0);
91 }
92
93 void
94 Score_engraver::process()
95 {
96   process_music();
97   do_announces();
98   pre_move_processing();
99   check_removal();
100 }
101
102 void
103 Score_engraver::announce_element (Score_element_info info)
104 {
105   announce_info_arr_.push (info);
106   pscore_p_->line_l_->typeset_element (info.elem_l_);
107 }
108
109 /* All elements are propagated to the top upon announcement. If
110    something was created during one run of
111    Engraver_group_engraver::do_announces, then
112    announce_info_arr_.size() will be nonzero again
113 */
114 /* junkme? Done by Engraver_group_engraver::do_announces ()?
115  */
116    
117 void
118 Score_engraver::do_announces()
119 {
120   while (announce_info_arr_.size()) 
121     Engraver_group_engraver::do_announces();
122 }
123
124
125 void
126 Score_engraver::typeset_element (Score_element *elem_p)
127 {
128   elem_p_arr_.push (elem_p);
129 }
130
131
132 void
133 Score_engraver::typeset_all()
134 {
135   for  (int i =0; i < elem_p_arr_.size(); i++) 
136     {
137       Score_element * elem_p = elem_p_arr_[i];
138       elem_p->add_processing ();
139
140       
141       if (Spanner *s = dynamic_cast <Spanner *> (elem_p))
142         {
143             /*
144             do something sensible if spanner not 
145             spanned on 2 items.
146            */
147           Direction d = LEFT;
148           do {
149             if (!s->get_bound (d))
150               {
151                 s->set_bound(d, command_column_l_);
152                 ::warning (_f ("unbound spanner `%s'", classname(s)));
153               }
154           } while (flip(&d) != LEFT);
155         }
156       else 
157         {
158           if (!elem_p->parent_l (X_AXIS))
159             {
160               bool br = to_boolean (elem_p->get_elt_property ("breakable"));
161               Axis_group_interface gi ((br) ? command_column_l_ : musical_column_l_) ;
162               gi.add_element(elem_p);
163             }
164         }
165       if (!elem_p->parent_l(Y_AXIS))
166         Axis_group_interface (scoreline_l_).add_element (elem_p);
167     }
168   elem_p_arr_.clear();
169 }
170
171 void
172 Score_engraver::do_pre_move_processing()
173 {
174   if (to_boolean (command_column_l_->get_elt_property ("breakable")))
175     {
176       breaks_i_ ++;
177       if (! (breaks_i_%8))
178         progress_indication ("[" + to_str ( breaks_i_) + "]");
179     }
180   // this generates all items.
181   Engraver_group_engraver::do_pre_move_processing();
182   
183   typeset_all();
184 }
185
186 void
187 Score_engraver::set_columns (Paper_column *new_command_l, 
188                              Paper_column *new_musical_l)
189 {
190   Paper_column * news[] = {new_command_l, new_musical_l};
191   Paper_column **current[] = {&command_column_l_, &musical_column_l_};
192
193   for (int i=00; i< 2; i++) 
194     {
195       if (*current[i])
196         {
197           scoreline_l_->add_column ((*current[i]));
198           if (!(*current[i])->used_b())
199             {
200               (*current[i])->suicide ();
201               *current[i]  =0;
202             }
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           total_penalty += b->penalty_f_;
240           if (b->penalty_f_ > 10000.0) //  ugh. arbitrary.
241             forbid_breaks ();
242
243           command_column_l_->set_elt_property ("penalty",
244                                                gh_double2scm (total_penalty));
245         }
246     }
247    return gotcha;
248 }
249
250 void
251 Score_engraver::forbid_breaks ()
252 {
253   /*
254     result is junked.
255    */
256   command_column_l_->remove_elt_property ("breakable");
257 }
258
259 ADD_THIS_TRANSLATOR(Score_engraver);
260
261 void
262 Score_engraver::do_add_processing ()
263 {
264   Translator_group::do_add_processing ();
265   assert (dynamic_cast<Paper_def *> (output_def_l_));
266   assert (!daddy_trans_l_);
267   pscore_p_ = new Paper_score;
268   pscore_p_->paper_l_ = dynamic_cast<Paper_def*>(output_def_l_);
269 }
270