]> git.donarmstrong.com Git - lilypond.git/blob - lily/p-score.cc
be107cd929467a8b64e2c3cb35222caa53fde825
[lilypond.git] / lily / p-score.cc
1 /*
2   p-score.cc -- implement Paper_score
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996,  1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "main.hh"
10 #include "debug.hh"
11 #include "lookup.hh"
12 #include "spanner.hh"
13 #include "paper-def.hh"
14 #include "line-of-score.hh"
15 #include "pcursor.hh"
16 #include "plist.hh"
17 #include "p-col.hh"
18 #include "p-score.hh"
19 #include "p-col.hh"
20 #include "scope.hh"
21 #include "word-wrap.hh"
22 #include "gourlay-breaking.hh"
23 #include "paper-stream.hh"
24 #include "paper-outputter.hh"
25 #include "file-results.hh"
26 #include "misc.hh"
27
28 // sucking Cygnus egcs - w32
29 #include "list.tcc"
30 #include "cursor.tcc"
31
32 Paper_score::Paper_score ()
33 {
34   outputter_l_ =0;
35   Line_of_score * line_p = new Line_of_score;
36   typeset_unbroken_spanner (line_p);
37
38   line_l_ = line_p;
39 }
40
41 Paper_score::~Paper_score ()
42 {
43 #if 0
44   for (int i=0; i< line_l_arr_.size (); i++)
45     line_l_arr_[i]->unlink_all ();
46
47   for (PCursor<Score_element*> i(elem_p_list_.top()); i.ok(); i++)
48     {
49
50       if (i->linked_b())
51         i->unlink ();
52       assert (! i->linked_b ());
53     }
54 #endif
55 }
56
57 void
58 Paper_score::typeset_element (Score_element * elem_p)
59 {
60   elem_p_list_.bottom ().add (elem_p);
61   elem_p->pscore_l_ = this;
62   elem_p->add_processing ();
63 }
64
65 void
66 Paper_score::typeset_broken_spanner (Spanner*span_p)
67 {
68   typeset_element (span_p);
69 }
70
71
72 void
73 Paper_score::typeset_unbroken_spanner (Spanner*span_p)
74 {
75   span_p_list_.bottom ().add (span_p);
76   span_p->pscore_l_=this;
77
78   // do not init start/stop fields. These are for broken spans only.
79   span_p->add_processing ();
80 }
81
82
83 void
84 Paper_score::clean_cols ()
85 {
86   int rank_i = 0;
87   for (iter_top (col_p_list_,c); c.ok ();)
88     {
89       c->set_rank (rank_i++);
90       c++;
91     }
92 }
93
94 void
95 Paper_score::add_column (Paper_column *p)
96 {
97   col_p_list_.bottom ().add (p);
98   typeset_element(p);
99 }
100
101
102
103 void
104 Paper_score::print () const
105 {
106 #ifndef NPRINT
107   if (!check_debug)
108     return ;
109   DOUT << "Paper_score { ";
110   DOUT << "\n elements: ";
111   for (iter_top (elem_p_list_,cc); cc.ok (); cc++)
112     cc->print ();
113   DOUT << "\n unbroken spanners: ";
114   for (iter (span_p_list_.top (), i); i.ok  (); i++)
115     i->print ();
116
117   DOUT << "}\n";
118 #endif
119 }
120
121 PCursor<Paper_column *>
122 Paper_score::find_col (Paper_column const *c) const
123 {
124   Paper_column const *what = c;
125
126   return col_p_list_.find ((Paper_column*)what);
127 }
128
129
130 #if 0
131 void
132 Paper_score::set_breaking (Array<Column_x_positions> const &breaking)
133 {
134   for (iter (span_p_list_.top (),i); i.ok  ();)
135     {
136       Spanner *span_p = i.remove_p ();
137       if (span_p->broken_b ()
138           || !((Score_element*)span_p)->line_l ())
139         {
140           span_p->unlink ();
141           delete span_p;
142         }
143       else 
144         {
145           typeset_broken_spanner (span_p);
146         }
147     }
148   for (iter (elem_p_list_.top (),i); i.ok  () ;)
149     {
150       Item *i_l =dynamic_cast <Item *> (i);
151       if (i_l && !i_l->line_l ())
152         {
153           i_l->unlink ();
154           Score_element * item_p= i.remove_p ();
155           delete item_p;
156         }
157       else
158         i++;
159     }
160 }
161 #endif
162
163
164 Array<Column_x_positions>
165 Paper_score::calc_breaking ()
166 {
167   Break_algorithm *algorithm_p=0;
168   Array<Column_x_positions> sol;
169   bool try_wrap = ! paper_l_->get_var ("castingalgorithm");
170
171   if (!try_wrap)
172     {
173       algorithm_p = new Gourlay_breaking ;
174       algorithm_p->set_pscore (this);
175       sol = algorithm_p->solve ();
176       delete algorithm_p;
177       if (! sol.size ())
178         {
179           warning (_ ("Can't solve this casting problem exactly; revert to Word_wrap"));
180           try_wrap = true;
181         }
182     }
183   if  (try_wrap)
184     {
185       algorithm_p = new Word_wrap;
186       algorithm_p->set_pscore (this);
187       sol = algorithm_p->solve ();
188       delete algorithm_p;
189     }
190   return sol;
191 }
192
193
194
195 void
196 Paper_score::process ()
197 {
198   clean_cols ();
199   print ();
200   *mlog << _ ("Preprocessing elements...") << " " << flush;
201       line_l_->breakable_col_processing ();
202       line_l_->pre_processing ();
203   
204       *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
205       line_l_->space_processing ();
206
207   Array<Column_x_positions> breaking = calc_breaking ();
208
209   Paper_stream* paper_stream_p = paper_l_->paper_stream_p ();
210   outputter_l_ = paper_l_->paper_outputter_p (paper_stream_p, header_l_, origin_str_);
211
212   Link_array<Line_of_score> lines;
213   for (int i=0; i < breaking.size (); i++)
214     {
215       Line_of_score *line_l = line_l_->set_breaking (breaking, i);
216       lines.push (line_l);
217       if (line_l != line_l_)
218         typeset_broken_spanner (line_l);
219       
220     }
221
222   *mlog << "\nLine ... ";
223   for (int i=0; i < lines.size (); i++)
224     {
225       *mlog << '[' << flush;
226       
227       Line_of_score *line_l = lines[i];
228       line_l->break_processing ();
229       line_l->post_processing ();
230         *mlog << i << flush;
231       line_l->output_all ();
232         *mlog << ']' << flush;
233       remove_line (line_l);
234     }
235   
236   // huh?
237   delete outputter_l_;
238   delete paper_stream_p;
239   outputter_l_ = 0;
240
241   *mlog << '\n' << flush;
242 }
243
244 void
245 Paper_score::remove_line (Line_of_score *l)
246 {
247   Link_array<Score_element> to_remove;
248   for (PCursor<Score_element*> i(elem_p_list_.top ()); i.ok (); )
249     {
250       if (i->line_l () == l)
251         to_remove.push (i.remove_p ());
252       else
253         i++;
254     }
255
256   for (PCursor<Spanner*> i (span_p_list_.top ()); i.ok (); )
257     {
258       Score_element *e = i.ptr ();
259       if (e->line_l () == l)
260         to_remove.push (i.remove_p ());
261       else
262         i++;
263     }
264
265   //  l->unlink_all ();
266   for (int i=0; i < to_remove.size (); i++)
267     {
268       to_remove[i]->unlink ();
269       assert (!to_remove[i]->linked_b ());
270       delete to_remove [i];
271     }
272 }
273
274 /** Get all breakable columns between l and r, (not counting l and r).  */
275 Link_array<Paper_column>
276 Paper_score::breakable_col_range (Paper_column*l, Paper_column*r) const
277 {
278   Link_array<Paper_column> ret;
279
280   PCursor<Paper_column*> start (l ? find_col (l)+1 : col_p_list_.top ());
281   PCursor<Paper_column*> stop (r ? find_col (r) : col_p_list_.bottom ());
282
283   /*
284     ugh! windows-suck-suck-suck.
285     */
286   while (PCursor<Paper_column*>::compare (start,stop) < 0)
287     {
288       if (start->breakable_b_)
289         ret.push (start);
290       start++;
291     }
292
293   return ret;
294 }
295
296
297 Link_array<Paper_column>
298 Paper_score::col_range (Paper_column*l, Paper_column*r) const
299 {
300   Link_array<Paper_column> ret;
301
302   PCursor<Paper_column*> start (l ? find_col (l)+1 : col_p_list_.top ());
303   PCursor<Paper_column*> stop (r ? find_col (r) : col_p_list_.bottom ());
304   ret.push (l);
305
306   /*
307     ugh! windows-suck-suck-suck.
308     */
309   while (PCursor<Paper_column*>::compare (start,stop) < 0)
310     ret.push (start++);
311   ret.push (r);
312   return ret;
313 }
314
315 Link_array<Item>
316 Paper_score::broken_col_range (Item const*l_item_l, Item const*r_item_l) const
317 {
318   Link_array<Item> ret;
319   Item const*l=l_item_l;
320   Item const*r=r_item_l;
321
322   while (! l->is_type_b(Paper_column::static_name ()))
323     l = dynamic_cast<Item*> (l->axis_group_l_a_[X_AXIS]);
324
325   while (! r->is_type_b(Paper_column::static_name ()))
326     r = dynamic_cast<Item*>(r->axis_group_l_a_[X_AXIS]);
327
328   PCursor<Paper_column*> start (l ? find_col ((Paper_column*)l)+1 : col_p_list_.top ());
329   PCursor<Paper_column*> stop (r ? find_col ((Paper_column*)r) : col_p_list_.bottom ());
330
331   /*
332     ugh! windows-suck-suck-suck.
333     */
334   while (PCursor<Paper_column*>::compare (start,stop) < 0)
335     {
336       if (start->breakable_b_ && !start->line_l_)
337         ret.push (start);
338       start++;
339     }
340
341   return ret;
342 }