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