]> git.donarmstrong.com Git - lilypond.git/blob - lily/p-score.cc
release: 1.1.18
[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 }
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 #if 0
124 void
125 Paper_score::set_breaking (Array<Column_x_positions> const &breaking)
126 {
127   for (iter (span_p_list_.top (),i); i.ok  ();)
128     {
129       Spanner *span_p = i.remove_p ();
130       if (span_p->broken_b ()
131           || !((Score_element*)span_p)->line_l ())
132         {
133           span_p->unlink ();
134           delete span_p;
135         }
136       else 
137         {
138           typeset_broken_spanner (span_p);
139         }
140     }
141   for (iter (elem_p_list_.top (),i); i.ok  () ;)
142     {
143       Item *i_l =dynamic_cast <Item *> (i);
144       if (i_l && !i_l->line_l ())
145         {
146           i_l->unlink ();
147           Score_element * item_p= i.remove_p ();
148           delete item_p;
149         }
150       else
151         i++;
152     }
153 }
154 #endif
155
156
157 Array<Column_x_positions>
158 Paper_score::calc_breaking ()
159 {
160   Break_algorithm *algorithm_p=0;
161   Array<Column_x_positions> sol;
162   bool try_wrap = ! paper_l_->get_var ("castingalgorithm");
163
164   if (!try_wrap)
165     {
166       algorithm_p = new Gourlay_breaking ;
167       algorithm_p->set_pscore (this);
168       sol = algorithm_p->solve ();
169       delete algorithm_p;
170       if (! sol.size ())
171         {
172           warning (_ ("Can't solve this casting problem exactly; revert to Word_wrap"));
173           try_wrap = true;
174         }
175     }
176   if  (try_wrap)
177     {
178       algorithm_p = new Word_wrap;
179       algorithm_p->set_pscore (this);
180       sol = algorithm_p->solve ();
181       delete algorithm_p;
182     }
183   return sol;
184 }
185
186
187
188 void
189 Paper_score::process ()
190 {
191   clean_cols ();
192   print ();
193   *mlog << _ ("Preprocessing elements...") << " " << flush;
194       line_l_->breakable_col_processing ();
195       line_l_->pre_processing ();
196   
197       *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
198       line_l_->space_processing ();
199
200   Array<Column_x_positions> breaking = calc_breaking ();
201
202   Paper_stream* paper_stream_p = paper_l_->paper_stream_p ();
203   outputter_l_ = paper_l_->paper_outputter_p (paper_stream_p, header_l_, origin_str_);
204
205   Link_array<Line_of_score> lines;
206   for (int i=0; i < breaking.size (); i++)
207     {
208       Line_of_score *line_l = line_l_->set_breaking (breaking, i);
209       lines.push (line_l);
210       if (line_l != line_l_)
211         typeset_broken_spanner (line_l);
212       
213     }
214
215   *mlog << "\nLine ... ";
216   for (int i=0; i < lines.size (); i++)
217     {
218       *mlog << '[' << flush;
219       
220       Line_of_score *line_l = lines[i];
221       line_l->break_processing ();
222       line_l->post_processing ();
223         *mlog << i << flush;
224       line_l->output_all ();
225         *mlog << ']' << flush;
226       remove_line (line_l);
227     }
228   
229   // huh?
230   delete outputter_l_;
231   delete paper_stream_p;
232   outputter_l_ = 0;
233
234   *mlog << '\n' << flush;
235 }
236
237 void
238 Paper_score::remove_line (Line_of_score *l)
239 {
240   Link_array<Score_element> to_remove;
241   for (PCursor<Score_element*> i(elem_p_list_.top ()); i.ok (); )
242     {
243       if (i->line_l () == l)
244         to_remove.push (i.remove_p ());
245       else
246         i++;
247     }
248
249   for (PCursor<Spanner*> i (span_p_list_.top ()); i.ok (); )
250     {
251       Score_element *e = i.ptr ();
252       if (e->line_l () == l)
253         to_remove.push (i.remove_p ());
254       else
255         i++;
256     }
257
258   //  l->unlink_all ();
259   for (int i=0; i < to_remove.size (); i++)
260     {
261       to_remove[i]->unlink ();
262       assert (!to_remove[i]->linked_b ());
263       delete to_remove [i];
264     }
265 }
266
267 /** Get all breakable columns between l and r, (not counting l and r).  */
268 Link_array<Paper_column>
269 Paper_score::breakable_col_range (Paper_column*l, Paper_column*r) const
270 {
271   Link_array<Paper_column> ret;
272
273   PCursor<Paper_column*> start (l ? find_col (l)+1 : col_p_list_.top ());
274   PCursor<Paper_column*> stop (r ? find_col (r) : col_p_list_.bottom ());
275
276   /*
277     ugh! windows-suck-suck-suck.
278     */
279   while (PCursor<Paper_column*>::compare (start,stop) < 0)
280     {
281       if (start->breakable_b_)
282         ret.push (start);
283       start++;
284     }
285
286   return ret;
287 }
288
289
290 Link_array<Paper_column>
291 Paper_score::col_range (Paper_column*l, Paper_column*r) const
292 {
293   Link_array<Paper_column> ret;
294
295   PCursor<Paper_column*> start (l ? find_col (l)+1 : col_p_list_.top ());
296   PCursor<Paper_column*> stop (r ? find_col (r) : col_p_list_.bottom ());
297   ret.push (l);
298
299   /*
300     ugh! windows-suck-suck-suck.
301     */
302   while (PCursor<Paper_column*>::compare (start,stop) < 0)
303     ret.push (start++);
304   ret.push (r);
305   return ret;
306 }
307
308 Link_array<Item>
309 Paper_score::broken_col_range (Item const*l_item_l, Item const*r_item_l) const
310 {
311   Link_array<Item> ret;
312   Item const*l=l_item_l;
313   Item const*r=r_item_l;
314
315   // huh? see Item::left_right_compare ()
316   /*
317   while (! (dynamic_cast<Paper_column const *> (l)))
318     l = dynamic_cast<Item*> (l->axis_group_l_a_[X_AXIS]);
319
320   while (! (dynamic_cast<Paper_column const *> (r)))
321     r = dynamic_cast<Item*>(r->axis_group_l_a_[X_AXIS]);
322   */
323   l = l->column_l ();
324   r = r->column_l ();
325   
326   PCursor<Paper_column*> start (l ? find_col ((Paper_column*)l)+1 : col_p_list_.top ());
327   PCursor<Paper_column*> stop (r ? find_col ((Paper_column*)r) : col_p_list_.bottom ());
328
329   /*
330     ugh! windows-suck-suck-suck.
331     */
332   while (PCursor<Paper_column*>::compare (start,stop) < 0)
333     {
334       if (start->breakable_b_ && !start->line_l_)
335         ret.push (start);
336       start++;
337     }
338
339   return ret;
340 }