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