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