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"
19 #include "tex-stream.hh"
22 #include "word-wrap.hh"
23 #include "gourlay-breaking.hh"
24 #include "tex-outputter.hh"
25 #include "file-results.hh"
28 // sucking Cygnus egcs - w32
32 Paper_score::Paper_score ()
35 Line_of_score * line_p = new Line_of_score;
36 typeset_unbroken_spanner (line_p);
41 Paper_score::~Paper_score ()
44 for (int i=0; i< line_l_arr_.size (); i++)
45 line_l_arr_[i]->unlink_all ();
47 for (PCursor<Score_element*> i(elem_p_list_.top()); i.ok(); i++)
52 assert (! i->linked_b ());
58 Paper_score::typeset_element (Score_element * elem_p)
60 elem_p_list_.bottom ().add (elem_p);
61 elem_p->pscore_l_ = this;
62 elem_p->add_processing ();
66 Paper_score::typeset_broken_spanner (Spanner*span_p)
68 typeset_element (span_p);
73 Paper_score::typeset_unbroken_spanner (Spanner*span_p)
75 span_p_list_.bottom ().add (span_p);
76 span_p->pscore_l_=this;
78 // do not init start/stop fields. These are for broken spans only.
79 span_p->add_processing ();
84 Paper_score::clean_cols ()
87 for (iter_top (col_p_list_,c); c.ok ();)
89 c->set_rank (rank_i++);
95 Paper_score::add_column (Paper_column *p)
97 col_p_list_.bottom ().add (p);
104 Paper_score::print () const
109 DOUT << "Paper_score { ";
110 DOUT << "\n elements: ";
111 for (iter_top (elem_p_list_,cc); cc.ok (); cc++)
113 DOUT << "\n unbroken spanners: ";
114 for (iter (span_p_list_.top (), i); i.ok (); i++)
121 PCursor<Paper_column *>
122 Paper_score::find_col (Paper_column const *c) const
124 Paper_column const *what = c;
126 return col_p_list_.find ((Paper_column*)what);
132 Paper_score::set_breaking (Array<Column_x_positions> const &breaking)
134 for (iter (span_p_list_.top (),i); i.ok ();)
136 Spanner *span_p = i.remove_p ();
137 if (span_p->broken_b ()
138 || !((Score_element*)span_p)->line_l ())
145 typeset_broken_spanner (span_p);
148 for (iter (elem_p_list_.top (),i); i.ok () ;)
150 Item *i_l =i->access_Item ();
151 if (i_l && !i_l->line_l ())
154 Score_element * item_p= i.remove_p ();
164 Array<Column_x_positions>
165 Paper_score::calc_breaking ()
167 Break_algorithm *algorithm_p=0;
168 Array<Column_x_positions> sol;
169 bool try_wrap = ! paper_l_->get_var ("castingalgorithm");
173 algorithm_p = new Gourlay_breaking ;
174 algorithm_p->set_pscore (this);
175 sol = algorithm_p->solve ();
179 warning (_ ("Can't solve this casting problem exactly; revert to Word_wrap"));
185 algorithm_p = new Word_wrap;
186 algorithm_p->set_pscore (this);
187 sol = algorithm_p->solve ();
194 Paper_score::process ()
198 *mlog << _ ("Preprocessing elements...") << " " << flush;
199 line_l_->breakable_col_processing ();
200 line_l_->pre_processing ();
202 *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
203 line_l_->space_processing ();
205 Array<Column_x_positions> breaking = calc_breaking ();
206 Tex_stream *tex_stream_p = open_output_stream ();
207 outputter_l_=open_tex_outputter (tex_stream_p);
209 Link_array<Line_of_score> lines;
210 for (int i=0; i < breaking.size (); i++)
212 Line_of_score *line_l = line_l_->set_breaking (breaking, i);
214 if (line_l != line_l_)
215 typeset_broken_spanner (line_l);
219 *mlog << "\nLine ... ";
220 for (int i=0; i < lines.size (); i++)
222 *mlog << '[' << flush;
224 Line_of_score *line_l = lines[i];
225 line_l->break_processing ();
226 line_l->post_processing ();
228 line_l->output_all ();
229 *mlog << ']' << flush;
230 remove_line (line_l);
233 *tex_stream_p << "\n\\EndLilyPondOutput";
241 Paper_score::remove_line (Line_of_score *l)
243 Link_array<Score_element> to_remove;
244 for (PCursor<Score_element*> i(elem_p_list_.top ()); i.ok (); )
246 if (i->line_l () == l)
247 to_remove.push (i.remove_p ());
252 for (PCursor<Spanner*> i (span_p_list_.top ()); i.ok (); )
254 Score_element *e = i.ptr ();
255 if (e->line_l () == l)
256 to_remove.push (i.remove_p ());
262 for (int i=0; i < to_remove.size (); i++)
264 to_remove[i]->unlink ();
265 assert (!to_remove[i]->linked_b ());
266 delete to_remove [i];
271 Paper_score::open_output_stream ()
274 String base_outname=paper_l_->get_default_output ();
276 if (base_outname.empty_b ())
278 base_outname = default_outname_base_global;
279 int def = paper_l_->get_next_default_count ();
282 base_outname += "-" + to_str (def);
286 String outname = base_outname;
289 target_str_global_array.push (outname);
291 *mlog << _f ("TeX output to %s...",
292 outname == "-" ? String ("<stdout>") : outname ) << endl;
294 return new Tex_stream (outname);
300 Paper_score::open_tex_outputter (Tex_stream *tex_out_p)
302 Tex_outputter *interfees_p= new Tex_outputter (tex_out_p);
306 *tex_out_p << header_global_p->TeX_string ();
310 *tex_out_p << _ ("% outputting Score, defined at: ") << origin_str_ << '\n';
314 *tex_out_p << header_l_->TeX_string();
316 *tex_out_p << paper_l_->TeX_output_settings_str ();
319 if (experimental_features_global_b)
320 *tex_out_p << "\\turnOnExperimentalFeatures%\n";
322 *tex_out_p << "\\turnOnPostScript%\n";
326 /** Get all breakable columns between l and r, (not counting l and r). */
327 Link_array<Paper_column>
328 Paper_score::breakable_col_range (Paper_column*l, Paper_column*r) const
330 Link_array<Paper_column> ret;
332 PCursor<Paper_column*> start (l ? find_col (l)+1 : col_p_list_.top ());
333 PCursor<Paper_column*> stop (r ? find_col (r) : col_p_list_.bottom ());
336 ugh! windows-suck-suck-suck.
338 while (PCursor<Paper_column*>::compare (start,stop) < 0)
340 if (start->breakable_b_)
349 Link_array<Paper_column>
350 Paper_score::col_range (Paper_column*l, Paper_column*r) const
352 Link_array<Paper_column> ret;
354 PCursor<Paper_column*> start (l ? find_col (l)+1 : col_p_list_.top ());
355 PCursor<Paper_column*> stop (r ? find_col (r) : col_p_list_.bottom ());
359 ugh! windows-suck-suck-suck.
361 while (PCursor<Paper_column*>::compare (start,stop) < 0)
368 Paper_score::broken_col_range (Item const*l_item_l, Item const*r_item_l) const
370 Link_array<Item> ret;
371 Item const*l=l_item_l;
372 Item const*r=r_item_l;
374 while (! l->is_type_b(Paper_column::static_name ()))
375 l = l->axis_group_l_a_[X_AXIS]->access_Score_element ()->access_Item ();
377 while (! r->is_type_b(Paper_column::static_name ()))
378 r = r->axis_group_l_a_[X_AXIS]->access_Score_element ()->access_Item ();
380 PCursor<Paper_column*> start (l ? find_col ((Paper_column*)l)+1 : col_p_list_.top ());
381 PCursor<Paper_column*> stop (r ? find_col ((Paper_column*)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_ && !start->line_l_)