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