2 p-score.cc -- implement Paper_score
4 source file of the GNU LilyPond music typesetter
6 (c) 1996, 1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #include "paper-def.hh"
14 #include "line-of-score.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"
32 // sucking Cygnus egcs - w32
36 Paper_score::Paper_score ()
39 Line_of_score * line_p = new Line_of_score;
40 typeset_unbroken_spanner (line_p);
45 Paper_score::~Paper_score ()
48 for (int i=0; i< line_l_arr_.size (); i++)
49 line_l_arr_[i]->unlink_all ();
51 for (PCursor<Score_element*> i(elem_p_list_.top()); i.ok(); i++)
56 assert (! i->linked_b ());
62 Paper_score::typeset_element (Score_element * elem_p)
64 elem_p_list_.bottom ().add (elem_p);
65 elem_p->pscore_l_ = this;
66 elem_p->add_processing ();
70 Paper_score::typeset_broken_spanner (Spanner*span_p)
72 typeset_element (span_p);
77 Paper_score::typeset_unbroken_spanner (Spanner*span_p)
79 span_p_list_.bottom ().add (span_p);
80 span_p->pscore_l_=this;
82 // do not init start/stop fields. These are for broken spans only.
83 span_p->add_processing ();
88 Paper_score::clean_cols ()
91 for (iter_top (col_p_list_,c); c.ok ();)
93 c->set_rank (rank_i++);
99 Paper_score::add_column (Paper_column *p)
101 col_p_list_.bottom ().add (p);
108 Paper_score::print () const
113 DOUT << "Paper_score { ";
114 DOUT << "\n elements: ";
115 for (iter_top (elem_p_list_,cc); cc.ok (); cc++)
117 DOUT << "\n unbroken spanners: ";
118 for (iter (span_p_list_.top (), i); i.ok (); i++)
125 PCursor<Paper_column *>
126 Paper_score::find_col (Paper_column const *c) const
128 Paper_column const *what = c;
130 return col_p_list_.find ((Paper_column*)what);
136 Paper_score::set_breaking (Array<Column_x_positions> const &breaking)
138 for (iter (span_p_list_.top (),i); i.ok ();)
140 Spanner *span_p = i.remove_p ();
141 if (span_p->broken_b ()
142 || !((Score_element*)span_p)->line_l ())
149 typeset_broken_spanner (span_p);
152 for (iter (elem_p_list_.top (),i); i.ok () ;)
154 Item *i_l =i->access_Item ();
155 if (i_l && !i_l->line_l ())
158 Score_element * item_p= i.remove_p ();
168 Array<Column_x_positions>
169 Paper_score::calc_breaking ()
171 Break_algorithm *algorithm_p=0;
172 Array<Column_x_positions> sol;
173 bool try_wrap = ! paper_l_->get_var ("castingalgorithm");
177 algorithm_p = new Gourlay_breaking ;
178 algorithm_p->set_pscore (this);
179 sol = algorithm_p->solve ();
183 warning (_ ("Can't solve this casting problem exactly; revert to Word_wrap"));
189 algorithm_p = new Word_wrap;
190 algorithm_p->set_pscore (this);
191 sol = algorithm_p->solve ();
198 Paper_score::process ()
202 *mlog << _ ("Preprocessing elements...") << " " << flush;
203 line_l_->breakable_col_processing ();
204 line_l_->pre_processing ();
206 *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
207 line_l_->space_processing ();
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);
213 Link_array<Line_of_score> lines;
214 for (int i=0; i < breaking.size (); i++)
216 Line_of_score *line_l = line_l_->set_breaking (breaking, i);
218 if (line_l != line_l_)
219 typeset_broken_spanner (line_l);
223 *mlog << "\nLine ... ";
224 for (int i=0; i < lines.size (); i++)
226 *mlog << '[' << flush;
228 Line_of_score *line_l = lines[i];
229 line_l->break_processing ();
230 line_l->post_processing ();
232 line_l->output_all ();
233 *mlog << ']' << flush;
234 remove_line (line_l);
238 if (ps_output_global_b)
239 *paper_stream_p << "\nshowpage\n";
241 *paper_stream_p << "\n\\EndLilyPondOutput";
244 delete paper_stream_p;
250 Paper_score::remove_line (Line_of_score *l)
252 Link_array<Score_element> to_remove;
253 for (PCursor<Score_element*> i(elem_p_list_.top ()); i.ok (); )
255 if (i->line_l () == l)
256 to_remove.push (i.remove_p ());
261 for (PCursor<Spanner*> i (span_p_list_.top ()); i.ok (); )
263 Score_element *e = i.ptr ();
264 if (e->line_l () == l)
265 to_remove.push (i.remove_p ());
271 for (int i=0; i < to_remove.size (); i++)
273 to_remove[i]->unlink ();
274 assert (!to_remove[i]->linked_b ());
275 delete to_remove [i];
280 Paper_score::open_output_stream ()
283 String base_outname=paper_l_->get_default_output ();
285 if (base_outname.empty_b ())
287 base_outname = default_outname_base_global;
288 int def = paper_l_->get_next_default_count ();
291 base_outname += "-" + to_str (def);
295 String outname = base_outname;
298 if (ps_output_global_b)
302 *mlog << _f ("PostScript output to %s...",
303 outname == "-" ? String ("<stdout>") : outname ) << endl;
304 p = new Ps_stream (outname);
310 *mlog << _f ("TeX output to %s...",
311 outname == "-" ? String ("<stdout>") : outname ) << endl;
312 p = new Tex_stream (outname);
314 target_str_global_array.push (outname);
320 Paper_score::open_paper_outputter (Paper_stream* paper_stream_p)
322 if (ps_output_global_b)
323 return open_ps_outputter ((Ps_stream*)paper_stream_p);
325 return open_tex_outputter ((Tex_stream*)paper_stream_p);
330 Paper_score::open_ps_outputter (Ps_stream *ps_out_p)
332 Ps_outputter *interfees_p = new Ps_outputter (ps_out_p);
335 *ps_out_p << header_global_p->ps_string ();
337 *ps_out_p << _ ("% outputting Score, defined at: ") << origin_str_ << '\n';
341 *ps_out_p << header_l_->ps_string ();
343 *ps_out_p << paper_l_->ps_output_settings_str ();
345 if (experimental_features_global_b)
346 *ps_out_p << "turnOnExperimentalFeatures\n";
352 Paper_score::open_tex_outputter (Tex_stream *tex_out_p)
354 Tex_outputter *interfees_p= new Tex_outputter (tex_out_p);
357 *tex_out_p << header_global_p->tex_string ();
360 *tex_out_p << _ ("% outputting Score, defined at: ") << origin_str_ << '\n';
363 *tex_out_p << header_l_->tex_string();
364 *tex_out_p << paper_l_->tex_output_settings_str ();
367 if (experimental_features_global_b)
368 *tex_out_p << "\\turnOnExperimentalFeatures%\n";
370 *tex_out_p << "\\turnOnPostScript%\n";
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
378 Link_array<Paper_column> ret;
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 ());
384 ugh! windows-suck-suck-suck.
386 while (PCursor<Paper_column*>::compare (start,stop) < 0)
388 if (start->breakable_b_)
397 Link_array<Paper_column>
398 Paper_score::col_range (Paper_column*l, Paper_column*r) const
400 Link_array<Paper_column> ret;
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 ());
407 ugh! windows-suck-suck-suck.
409 while (PCursor<Paper_column*>::compare (start,stop) < 0)
416 Paper_score::broken_col_range (Item const*l_item_l, Item const*r_item_l) const
418 Link_array<Item> ret;
419 Item const*l=l_item_l;
420 Item const*r=r_item_l;
422 while (! l->is_type_b(Paper_column::static_name ()))
423 l = l->axis_group_l_a_[X_AXIS]->access_Score_element ()->access_Item ();
425 while (! r->is_type_b(Paper_column::static_name ()))
426 r = r->axis_group_l_a_[X_AXIS]->access_Score_element ()->access_Item ();
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 ());
432 ugh! windows-suck-suck-suck.
434 while (PCursor<Paper_column*>::compare (start,stop) < 0)
436 if (start->breakable_b_ && !start->line_l_)