lilypond-1.4.4
[lilypond.git] / lily / p-score.cc
blob261d3795f2995d2e5bf1b16d5e57aa447ee480b8
1 /*
2 p-score.cc -- implement Paper_score
4 source file of the GNU LilyPond music typesetter
6 (c) 1996, 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
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 "p-col.hh"
16 #include "p-score.hh"
17 #include "p-col.hh"
18 #include "scope.hh"
19 #include "word-wrap.hh"
20 #include "gourlay-breaking.hh"
21 #include "paper-stream.hh"
22 #include "paper-outputter.hh"
23 #include "file-results.hh"
24 #include "misc.hh"
26 Paper_score::Paper_score ()
28 protected_scms_ = scm_protect_object (gh_cons (SCM_BOOL_T, SCM_EOL));
29 paper_l_ =0;
30 outputter_l_ =0;
31 Line_of_score * line_p = new Line_of_score;
32 typeset_unbroken_spanner (line_p);
34 line_l_ = line_p;
37 Paper_score::Paper_score (Paper_score const &s)
38 : Music_output (s)
40 assert (false);
43 Paper_score::~Paper_score ()
45 for (int i=span_p_arr_.size (); --i >=0 ; )
46 delete span_p_arr_[i];
47 for (int i=elem_p_arr_.size (); --i >=0 ; )
48 delete elem_p_arr_[i];
50 scm_unprotect_object (protected_scms_);
53 void
54 Paper_score::typeset_element (Score_element * elem_p)
56 elem_p_arr_.push (elem_p);
57 elem_p->pscore_l_ = this;
59 // take over protection.
60 SCM_CDR(protected_scms_) = gh_cons (elem_p->element_property_alist_,
61 SCM_CDR (protected_scms_));
62 scm_unprotect_object (elem_p->element_property_alist_);
64 SCM p = elem_p->remove_elt_property (break_helper_only_scm_sym);
65 if (p != SCM_BOOL_F)
66 break_helpers_arr_.push (elem_p);
70 void
71 Paper_score::typeset_unbroken_spanner (Spanner*span_p)
73 span_p_arr_.push (span_p);
74 span_p->pscore_l_=this;
76 SCM p = span_p->remove_elt_property (break_helper_only_scm_sym);
77 if (p != SCM_BOOL_F)
78 break_helpers_arr_.push (span_p);
81 void
82 Paper_score::add_column (Paper_column *p)
84 p->set_rank (col_l_arr_.size ());
85 col_l_arr_.push (p);
86 typeset_element(p);
91 void
92 Paper_score::print () const
94 #ifndef NPRINT
95 if (!check_debug)
96 return ;
97 DOUT << "Paper_score { ";
98 DOUT << "\n elements: ";
99 for (int i=0; i < span_p_arr_.size (); i++)
100 span_p_arr_[i]->print ();
101 for (int i=0; i < elem_p_arr_.size (); i++)
102 elem_p_arr_[i]->print();
104 DOUT << "}\n";
105 #endif
109 Paper_score::find_col_idx (Paper_column const *c) const
111 Paper_column const *what = c;
113 return col_l_arr_.find_i ((Paper_column*)what);
116 Array<Column_x_positions>
117 Paper_score::calc_breaking ()
119 Break_algorithm *algorithm_p=0;
120 Array<Column_x_positions> sol;
121 bool try_wrap = !paper_l_->get_var ("castingalgorithm");
123 if (!try_wrap)
125 algorithm_p = new Gourlay_breaking ;
126 algorithm_p->set_pscore (this);
127 sol = algorithm_p->solve ();
128 delete algorithm_p;
129 if (! sol.size ())
131 warning (_ ("Can't solve this casting problem exactly; revert to Word_wrap"));
132 try_wrap = true;
135 if (try_wrap)
137 algorithm_p = new Word_wrap;
138 algorithm_p->set_pscore (this);
139 sol = algorithm_p->solve ();
140 delete algorithm_p;
142 return sol;
147 void
148 Paper_score::process ()
150 Dictionary<int> type_stats;
151 type_stats["Item"] =0;
152 type_stats["Spanner"] =0;
153 type_stats["Total"]=0;
155 print ();
156 *mlog << _ ("Preprocessing elements...") << " " << flush;
157 line_l_->breakable_col_processing ();
158 line_l_->pre_processing ();
160 *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
161 line_l_->space_processing ();
163 Array<Column_x_positions> breaking = calc_breaking ();
166 Paper_stream* paper_stream_p = paper_l_->paper_stream_p ();
167 outputter_l_ = paper_l_->paper_outputter_p (paper_stream_p, header_l_, origin_str_);
169 Link_array<Line_of_score> lines;
170 for (int i=0; i < breaking.size (); i++)
172 Line_of_score *line_l = line_l_->set_breaking (breaking, i);
173 lines.push (line_l);
174 if (line_l != line_l_)
175 typeset_element (line_l);
178 if (experimental_features_global_b)
179 *mlog << elem_p_arr_.size () + span_p_arr_.size () << _ (" elements. ");
181 *mlog << "\n";
182 *mlog << _ ("Line ... ");
183 line_l_->break_processing ();
184 for (int i=0; i < lines.size (); i++)
186 *mlog << '[' << flush;
188 Line_of_score *line_l = lines[i];
190 line_l->post_processing ();
191 *mlog << i << flush;
192 line_l->output_all (i + 1 == lines.size());
193 if (experimental_features_global_b)
194 *mlog << '(' << elem_p_arr_.size () + span_p_arr_.size () << ')';
196 *mlog << ']' << flush;
199 // huh?
200 delete outputter_l_;
201 delete paper_stream_p;
202 outputter_l_ = 0;
206 todo: sort output
208 if (experimental_features_global_b)
210 for (Dictionary_iter<int> i(type_stats); i.ok(); i++)
212 *mlog << i.key () << ": " << i.val () << " objects\n";
215 *mlog << '\n' << flush;
219 Link_array<Item>
220 Paper_score::broken_col_range (Item const*l, Item const*r) const
222 Link_array<Item> ret;
224 l = l->column_l ();
225 r = r->column_l ();
227 int start = l
228 ? find_col_idx (dynamic_cast<Paper_column*> ((Item*)l))+1
229 : 0;
231 int stop = r
232 ? find_col_idx (dynamic_cast<Paper_column*>((Item*)r))
233 : col_l_arr_.size ();
235 while (start < stop)
237 Paper_column *c = col_l_arr_[start];
238 if (c->breakable_b () && !c->line_l_)
239 ret.push (c);
240 start++;
243 return ret;