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