]> git.donarmstrong.com Git - lilypond.git/blob - lily/p-score.cc
0ab995f15c11434d86afdf1a549f0cc6206ac240
[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 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 void
198 Paper_score::process ()
199 {
200   clean_cols ();
201   print ();
202   *mlog << _ ("Preprocessing elements...") << " " << flush;
203       line_l_->breakable_col_processing ();
204       line_l_->pre_processing ();
205   
206       *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
207       line_l_->space_processing ();
208
209   Array<Column_x_positions> breaking = calc_breaking ();
210   Paper_stream *paper_stream_p = open_output_stream ();
211   outputter_l_ = open_paper_outputter (paper_stream_p);
212
213   Link_array<Line_of_score> lines;
214   for (int i=0; i < breaking.size (); i++)
215     {
216       Line_of_score *line_l = line_l_->set_breaking (breaking, i);
217       lines.push (line_l);
218       if (line_l != line_l_)
219         typeset_broken_spanner (line_l);
220       
221     }
222
223   *mlog << "\nLine ... ";
224   for (int i=0; i < lines.size (); i++)
225     {
226       *mlog << '[' << flush;
227       
228       Line_of_score *line_l = lines[i];
229       line_l->break_processing ();
230       line_l->post_processing ();
231         *mlog << i << flush;
232       line_l->output_all ();
233         *mlog << ']' << flush;
234       remove_line (line_l);
235         
236     }
237
238   if (ps_output_global_b)
239     *paper_stream_p << "\nshowpage\n";
240   else
241     *paper_stream_p << "\n\\EndLilyPondOutput";
242
243   delete outputter_l_;
244   delete paper_stream_p;
245   outputter_l_ = 0;
246 }
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 Paper_stream *
280 Paper_score::open_output_stream ()
281 {
282   // output
283   String base_outname=paper_l_->get_default_output ();
284
285   if (base_outname.empty_b ())
286     {
287       base_outname = default_outname_base_global;
288       int def = paper_l_->get_next_default_count ();
289       if (def)
290         {
291           base_outname += "-" + to_str (def);
292         }
293     }
294
295   String outname = base_outname;
296
297   Paper_stream* p;
298   if (ps_output_global_b)
299     {
300       if (outname != "-")
301          outname += ".ps";
302       *mlog << _f ("PostScript output to %s...", 
303                    outname == "-" ? String ("<stdout>") : outname ) << endl;
304       p = new Ps_stream (outname);
305     }
306   else
307     {
308       if (outname != "-")
309          outname += ".tex";
310       *mlog << _f ("TeX output to %s...", 
311                    outname == "-" ? String ("<stdout>") : outname ) << endl;
312       p = new Tex_stream (outname);
313     }
314   target_str_global_array.push (outname);
315   return p;
316 }
317
318 // urg
319 Paper_outputter*
320 Paper_score::open_paper_outputter (Paper_stream* paper_stream_p)
321 {
322   if (ps_output_global_b)
323     return open_ps_outputter ((Ps_stream*)paper_stream_p);
324   else
325     return open_tex_outputter ((Tex_stream*)paper_stream_p);
326 }
327
328 // urg urg urg
329 Paper_outputter*
330 Paper_score::open_ps_outputter (Ps_stream *ps_out_p)
331 {
332   Ps_outputter *interfees_p = new Ps_outputter (ps_out_p);
333
334   if (header_global_p)
335     *ps_out_p << header_global_p->ps_string ();
336   
337   *ps_out_p << _ ("% outputting Score, defined at: ") << origin_str_ << '\n';
338
339   // urg
340   if (header_l_)
341     *ps_out_p << header_l_->ps_string ();
342   //aaarg
343   *ps_out_p << paper_l_->ps_output_settings_str ();
344
345   if (experimental_features_global_b)
346     *ps_out_p << "turnOnExperimentalFeatures\n";
347
348   return interfees_p;
349 }
350
351 Paper_outputter*
352 Paper_score::open_tex_outputter (Tex_stream *tex_out_p)
353 {
354   Tex_outputter *interfees_p= new Tex_outputter (tex_out_p);
355
356   if (header_global_p)
357     *tex_out_p << header_global_p->tex_string ();
358     
359   
360   *tex_out_p << _ ("% outputting Score, defined at: ") << origin_str_ << '\n';
361
362   if (header_l_)
363     *tex_out_p << header_l_->tex_string();
364   *tex_out_p << paper_l_->tex_output_settings_str ();
365   
366
367   if (experimental_features_global_b)
368     *tex_out_p << "\\turnOnExperimentalFeatures%\n";
369
370   *tex_out_p << "\\turnOnPostScript%\n";
371   return interfees_p;
372 }
373
374 /** Get all breakable columns between l and r, (not counting l and r).  */
375 Link_array<Paper_column>
376 Paper_score::breakable_col_range (Paper_column*l, Paper_column*r) const
377 {
378   Link_array<Paper_column> ret;
379
380   PCursor<Paper_column*> start (l ? find_col (l)+1 : col_p_list_.top ());
381   PCursor<Paper_column*> stop (r ? find_col (r) : col_p_list_.bottom ());
382
383   /*
384     ugh! windows-suck-suck-suck.
385     */
386   while (PCursor<Paper_column*>::compare (start,stop) < 0)
387     {
388       if (start->breakable_b_)
389         ret.push (start);
390       start++;
391     }
392
393   return ret;
394 }
395
396
397 Link_array<Paper_column>
398 Paper_score::col_range (Paper_column*l, Paper_column*r) const
399 {
400   Link_array<Paper_column> ret;
401
402   PCursor<Paper_column*> start (l ? find_col (l)+1 : col_p_list_.top ());
403   PCursor<Paper_column*> stop (r ? find_col (r) : col_p_list_.bottom ());
404   ret.push (l);
405
406   /*
407     ugh! windows-suck-suck-suck.
408     */
409   while (PCursor<Paper_column*>::compare (start,stop) < 0)
410     ret.push (start++);
411   ret.push (r);
412   return ret;
413 }
414
415 Link_array<Item>
416 Paper_score::broken_col_range (Item const*l_item_l, Item const*r_item_l) const
417 {
418   Link_array<Item> ret;
419   Item const*l=l_item_l;
420   Item const*r=r_item_l;
421
422   while (! l->is_type_b(Paper_column::static_name ()))
423     l = l->axis_group_l_a_[X_AXIS]->access_Score_element ()->access_Item ();
424
425   while (! r->is_type_b(Paper_column::static_name ()))
426     r = r->axis_group_l_a_[X_AXIS]->access_Score_element ()->access_Item ();
427
428   PCursor<Paper_column*> start (l ? find_col ((Paper_column*)l)+1 : col_p_list_.top ());
429   PCursor<Paper_column*> stop (r ? find_col ((Paper_column*)r) : col_p_list_.bottom ());
430
431   /*
432     ugh! windows-suck-suck-suck.
433     */
434   while (PCursor<Paper_column*>::compare (start,stop) < 0)
435     {
436       if (start->breakable_b_ && !start->line_l_)
437         ret.push (start);
438       start++;
439     }
440
441   return ret;
442 }