]> git.donarmstrong.com Git - lilypond.git/blob - lily/p-score.cc
release: 0.0.46.jcn1
[lilypond.git] / lily / p-score.cc
1 #include "idealspacing.hh"
2 #include "debug.hh"
3 #include "lookup.hh"
4 #include "spanner.hh"
5 #include "paper-def.hh"
6 #include "molecule.hh"
7 #include "dimen.hh"
8 #include "scoreline.hh"
9 #include "p-score.hh"
10 #include "tex-stream.hh"
11 #include "item.hh"
12 #include "break.hh"
13
14 Idealspacing*
15 PScore::get_spacing(PCol*l, PCol*r)
16 {
17     assert(l!=r);
18
19     Idealspacing*i_p =new Idealspacing(l,r);
20     suz.bottom().add(i_p);
21
22     return i_p;
23 }
24
25
26 void
27 PScore::clean_cols()
28 {
29     int rank_i = 0;
30     for (iter_top(cols,c); c.ok(); )
31         if (!c->used_b()) {
32             delete c.remove_p();
33         } else {
34             c->set_rank(rank_i++);
35             c++;
36         }
37     
38 }
39
40
41 void
42 PScore::add(PStaff *s)
43 {
44     assert(s->pscore_l_ == this);
45     staffs.bottom().add(s);
46 }
47
48
49 void
50 PScore::do_connect(PCol *c1, PCol *c2, Real d, Real h)
51 {
52     if (!c1 || !c2 )
53         return;
54     Idealspacing*s_l=get_spacing(c1,c2);
55
56     
57     s_l->hooke = h;
58     s_l->space =d;
59 }
60
61 void
62 PScore::connect(PCol* c1, PCol *c2, Real d, Real h)
63 {
64     do_connect(c1,c2,d,h);
65     do_connect(c1->postbreak_p_, c2,d,h);
66     do_connect(c1, c2->prebreak_p_,d,h);
67     do_connect(c1->postbreak_p_, c2->prebreak_p_,d,h);
68 }
69
70 void
71 PScore::typeset_item(Item *i, PCol *c, PStaff *s, int breakstat)
72 {
73     assert(c && i && s);
74
75     if (breakstat == 0) {
76         typeset_item(i, c->prebreak_p_, s);
77         return;
78     }
79
80     if (breakstat == 2) {
81         typeset_item(i, c->postbreak_p_, s);
82         return;
83     }
84
85
86     its.bottom().add(i);
87     s->add(i);
88     c->add(i);
89
90     /* first do this, because i->width() may follow the 0-pointer */
91     i->add_processing();    
92 }
93
94 void
95 PScore::typeset_spanner(Spanner*span_p, PStaff*ps)
96 {
97     span_p->pstaff_l_ = ps;
98     spanners.bottom().add(span_p);
99     ps->spans.bottom().add(span_p);
100
101     // do not init start/stop fields. These are for broken spans only.
102     span_p->add_processing();
103 }
104
105
106 void
107 PScore::add(PCol *p)
108 {
109     p->pscore_l_ = this;
110     if (p->breakable_b()){
111         p->prebreak_p_->pscore_l_ = this;
112         p->postbreak_p_->pscore_l_ = this;
113     }
114     cols.bottom().add(p);
115 }
116
117 PScore::PScore( Paper_def*p)
118 {
119     paper_l_ = p;
120 }
121
122 void
123 PScore::output(Tex_stream &ts)
124 {
125     int l=1;
126
127     ts << "\n "<<  paper_l_->lookup_l()->texsetting << "%(Tex id)\n";
128     for (iter_top(lines,lic); lic.ok(); lic++) {
129         ts << "% line of score no. " << l++ <<"\n";
130         ts << lic->TeXstring();
131         if ((lic+1).ok())
132             ts << "\\interscoreline\n";
133     }   
134 }
135
136
137 Array<Item*>
138 PScore::select_items(PStaff*ps, PCol*pc)
139 {
140     Array<Item*> ret;
141     assert(ps && pc);
142     for (iter_top(pc->its,i); i.ok(); i++){
143         if (i->pstaff_l_ == ps)
144             ret.push((Item*)(Item const *)i);
145     }
146     return ret;
147 }
148
149 void
150 PScore::OK()const
151 {
152 #ifdef NDEBUG
153     for (iter_top(cols,cc); cc.ok(); cc++)
154         cc->OK();
155     for (iter_top(suz,i); i.ok(); i++)
156         i->OK();
157 #endif
158 }
159
160 void
161 PScore::print() const
162 {    
163 #ifndef NPRINT
164     mtor << "PScore { ";
165     paper_l_->print();
166     mtor << "\ncolumns: ";
167     for (iter_top(cols,cc); cc.ok(); cc++)
168         cc->print();
169     
170     mtor << "\nideals: ";
171     for (iter_top(suz,i); i.ok(); i++)
172         i->print();
173     mtor << "}\n";
174 #endif 
175 }
176
177 void
178 PScore::preprocess()
179 {
180     for (iter_top(spanners,i); i.ok(); i++) {
181         i->pre_processing();
182     }
183     for (iter_top(its,i); i.ok(); i++){
184         i->pre_processing();
185     }
186 }
187
188 void
189 PScore::postprocess()
190 {
191     for (iter_top(broken_spans,i); i.ok(); i++) { // could chase spans as well.
192         i->post_processing();
193     }
194     for (iter_top(its,i); i.ok(); i++){
195         i->post_processing();
196     }
197     
198     for (iter_top(broken_spans,i); i.ok(); i++) {
199         i->molecule_processing();
200     }
201     for (iter_top(its,i); i.ok(); i++){
202         i->molecule_processing();
203     }
204
205     for (iter_top(lines,i); i.ok(); i++)
206         i->process();
207
208 }
209
210 PCursor<PCol *>
211 PScore::find_col(PCol const *c)const
212 {
213     PCol const *what = c;
214     if (what->daddy_l_ )
215         what = what->daddy_l_;
216     
217     return cols.find((PCol*)what);
218 }
219
220 void
221 PScore::add_broken(Spanner*s)
222 {
223     assert(s->left_col_l_->line_l_ == s->right_col_l_->line_l_);
224     broken_spans.bottom().add(s);
225     s->left_col_l_->starters.bottom().add (s);
226     s->right_col_l_->stoppers.bottom().add (s);
227 }
228
229 void
230 PScore::set_breaking(Array<Col_hpositions> breaking)
231 {
232     for (int j=0; j < breaking.size(); j++) {
233         Array<PCol*> &curline(breaking[j].cols);
234         Array<Real> &config(breaking[j].config);
235         
236         Line_of_score *s_p = new Line_of_score(curline,this);
237         lines.bottom().add(s_p);        
238         for (int i=0; i < curline.size(); i++){
239             curline[i]->hpos = config[i];
240         }
241     }
242 }
243
244 void
245 PScore::calc_breaking()
246 {
247     Word_wrap w(*this);
248     set_breaking(w.solve());
249 }
250
251 void
252 PScore::process()
253 {
254     clean_cols();
255     
256     *mlog << "Preprocessing ... " <<flush;
257     preprocess();
258     *mlog << "\nCalculating column positions ... " <<flush;
259     calc_breaking();
260     *mlog << "\nPostprocessing ..." << endl;
261     postprocess();
262 }