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