lilypond-1.3.16
[lilypond.git] / lily / paper-score.cc
blobb23d002e55c943a7769189d5764e562cce9bc559
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 "paper-column.hh"
16 #include "paper-score.hh"
17 #include "paper-column.hh"
18 #include "scope.hh"
19 #include "gourlay-breaking.hh"
20 #include "paper-stream.hh"
21 #include "paper-outputter.hh"
22 #include "file-results.hh"
23 #include "misc.hh"
24 #include "all-font-metrics.hh"
26 Paper_score::Paper_score ()
28 paper_l_ =0;
29 outputter_l_ =0;
30 Line_of_score * line_p = new Line_of_score;
31 line_p->pscore_l_ = this;
32 element_smob_list_ = scm_protect_object (gh_cons (line_p->self_scm_, SCM_EOL));
33 line_l_ = line_p;
36 Paper_score::Paper_score (Paper_score const &s)
37 : Music_output (s)
39 assert (false);
42 Paper_score::~Paper_score ()
44 scm_unprotect_object (element_smob_list_);
47 void
48 Paper_score::typeset_element (Score_element * elem_p)
50 elem_p->pscore_l_ = this;
52 gh_set_cdr_x(element_smob_list_,
53 gh_cons (elem_p->self_scm_, gh_cdr (element_smob_list_)));
54 elem_p->set_elt_property ("full-name",
55 gh_str02scm((char*)elem_p->name()));
57 scm_unprotect_object (elem_p->self_scm_);
60 void
61 Paper_score::add_column (Paper_column *p)
63 p->set_rank (col_l_arr_.size ());
64 col_l_arr_.push (p);
65 typeset_element(p);
68 void
69 Paper_score::print () const
71 #ifndef NPRINT
72 if (!flower_dstream)
73 return ;
75 DEBUG_OUT << "Paper_score { ";
76 DEBUG_OUT << "\n elements: ";
78 for (SCM p = gh_cdr (element_smob_list_);
79 p != SCM_EOL;
80 p = gh_cdr(p))
81 gh_display (gh_car (p));
82 DEBUG_OUT << "}\n";
83 #endif
86 int
87 Paper_score::find_col_idx (Paper_column const *c) const
89 Paper_column const *what = c;
91 return col_l_arr_.find_i ((Paper_column*)what);
94 Array<Column_x_positions>
95 Paper_score::calc_breaking ()
97 Break_algorithm *algorithm_p=0;
98 Array<Column_x_positions> sol;
100 algorithm_p = new Gourlay_breaking ;
101 algorithm_p->set_pscore (this);
102 sol = algorithm_p->solve ();
103 delete algorithm_p;
105 return sol;
110 void
111 Paper_score::process ()
114 print ();
115 *mlog << _ ("Preprocessing elements...") << " " << flush;
116 line_l_->breakable_col_processing ();
117 fixup_refpoints ();
118 line_l_->pre_processing ();
120 *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
121 line_l_->space_processing ();
123 Array<Column_x_positions> breaking = calc_breaking ();
124 line_l_->break_into_pieces (breaking);
126 for (SCM s = element_smob_list_; gh_pair_p (s); s = gh_cdr (s))
128 Score_element *sc = unsmob_element (gh_car (s));
129 sc->do_break_processing ();
131 for (SCM s = element_smob_list_; gh_pair_p (s); s = gh_cdr (s))
133 Score_element *sc = unsmob_element (gh_car (s));
134 sc->handle_broken_dependencies ();
137 outputter_l_ = new Paper_outputter ;
138 outputter_l_->output_header ();
140 outputter_l_->output_version();
142 if (header_global_p)
143 outputter_l_->output_scope (header_global_p, "mudela");
144 if (header_l_)
145 outputter_l_->output_scope (header_l_, "mudela");
147 outputter_l_->output_comment (_ ("Outputting Score, defined at: "));
148 outputter_l_->output_comment (origin_str_);
150 if (paper_l_->scope_p_)
151 outputter_l_->output_scope (paper_l_->scope_p_, "mudelapaper");
153 SCM scm = gh_list (ly_symbol2scm ("experimental-on"), SCM_UNDEFINED);
154 outputter_l_->output_scheme (scm);
155 scm = gh_list (ly_symbol2scm ("header-end"), SCM_UNDEFINED);
156 outputter_l_->output_scheme (scm);
160 This is tricky: we have to put the font definitions before the
161 actual output, but we don't know all fonts in advanced: generating
162 the output might trigger loading of a new font. So we store the
163 place to insert the font definitions, generate the output and then
164 insert the definitions
167 SCM before_output = outputter_l_->last_cons_;
170 fixup_refpoints ();
171 line_l_->output_lines ();
174 SCM font_names = ly_quote_scm (all_fonts_global_p->font_descriptions ());
175 gh_set_cdr_x (before_output,
176 gh_cons (gh_list (ly_symbol2scm ("define-fonts"),
177 font_names,
178 SCM_UNDEFINED),
179 gh_cdr (before_output)));
181 Paper_stream* psp = paper_l_->paper_stream_p ();
182 outputter_l_->dump_onto (psp);
183 // huh?
184 delete outputter_l_;
186 outputter_l_ = 0;
187 delete psp;
191 Link_array<Item>
192 Paper_score::broken_col_range (Item const*l, Item const*r) const
194 Link_array<Item> ret;
196 l = l->column_l ();
197 r = r->column_l ();
199 int start = l
200 ? find_col_idx (dynamic_cast<Paper_column*> ((Item*)l))+1
201 : 0;
203 int stop = r
204 ? find_col_idx (dynamic_cast<Paper_column*>((Item*)r))
205 : col_l_arr_.size ();
207 while (start < stop)
209 Paper_column *c = col_l_arr_[start];
210 if (c->breakable_b () && !c->line_l_)
211 ret.push (c);
212 start++;
215 return ret;
219 void
220 Paper_score::fixup_refpoints ()
222 for (SCM s = element_smob_list_; gh_pair_p (s); s = gh_cdr (s))
224 SCM e = gh_car (s);
225 if (SMOB_IS_TYPE_B(Score_element, e))
227 Score_element * se = unsmob_element (e);
228 se->fixup_refpoint ();