]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-score.cc
release: 1.3.6
[lilypond.git] / lily / paper-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--1999 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 "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"
25
26 Paper_score::Paper_score ()
27 {
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;
34 }
35
36 Paper_score::Paper_score (Paper_score const &s)
37   : Music_output (s)
38 {
39   assert (false);
40 }
41
42 Paper_score::~Paper_score ()
43 {
44   scm_unprotect_object (element_smob_list_);
45 }
46
47 void
48 Paper_score::typeset_element (Score_element * elem_p)
49 {
50   elem_p->pscore_l_ = this;
51
52   SCM_CDR(element_smob_list_) = gh_cons (elem_p->self_scm_,
53                                          SCM_CDR (element_smob_list_));
54   elem_p->set_elt_property ("full-name",
55                             gh_str02scm((char*)elem_p->name()));
56   
57   scm_unprotect_object (elem_p->self_scm_);
58 }
59
60 void
61 Paper_score::add_column (Paper_column *p)
62 {
63   p->set_rank (col_l_arr_.size ());
64   col_l_arr_.push (p);
65   typeset_element(p);
66 }
67
68 void
69 Paper_score::print () const
70 {
71 #ifndef NPRINT
72   if (!flower_dstream)
73     return ;
74
75   DEBUG_OUT << "Paper_score { ";
76   DEBUG_OUT << "\n elements: ";
77
78   for (SCM p = SCM_CDR (element_smob_list_);
79        p != SCM_EOL;
80        p = SCM_CDR(p))
81     gh_display (SCM_CAR(p));
82   DEBUG_OUT << "}\n";
83 #endif
84 }
85
86 int
87 Paper_score::find_col_idx (Paper_column const *c) const
88 {
89   Paper_column const *what = c;
90
91   return col_l_arr_.find_i ((Paper_column*)what);
92 }
93
94 Array<Column_x_positions>
95 Paper_score::calc_breaking ()
96 {
97   Break_algorithm *algorithm_p=0;
98   Array<Column_x_positions> sol;
99
100   algorithm_p = new Gourlay_breaking ;
101   algorithm_p->set_pscore (this);
102   sol = algorithm_p->solve ();
103   delete algorithm_p;
104
105   return sol;
106 }
107
108
109
110 void
111 Paper_score::process ()
112 {
113
114   print ();
115   *mlog << _ ("Preprocessing elements...") << " " << flush;
116   line_l_->breakable_col_processing ();
117   line_l_->pre_processing ();
118   
119   *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
120   line_l_->space_processing ();
121
122   Array<Column_x_positions> breaking = calc_breaking ();
123
124
125   outputter_l_ = new Paper_outputter ;
126   outputter_l_->output_header ();
127
128   outputter_l_->output_version();
129   
130   if (header_global_p)
131     outputter_l_->output_scope (header_global_p, "mudela");
132   if (header_l_)
133     outputter_l_->output_scope (header_l_, "mudela");
134
135   outputter_l_->output_comment (_ ("Outputting Score, defined at: "));
136   outputter_l_->output_comment (origin_str_);
137
138   if (paper_l_->scope_p_)
139     outputter_l_->output_scope (paper_l_->scope_p_, "mudelapaper");
140   
141   SCM scm = gh_list (ly_symbol2scm ("experimental-on"), SCM_UNDEFINED);
142   outputter_l_->output_scheme (scm);
143   scm = gh_list (ly_symbol2scm ("header-end"), SCM_UNDEFINED);
144   outputter_l_->output_scheme (scm);
145
146
147   /*
148     This is tricky: we have to put the font definitions before the
149     actual output, but we don't know all fonts in advanced: generating
150     the output might trigger loading of a new font.  So we store the
151     place to insert the font definitions, generate the output and then
152     insert the definitions
153     
154    */
155   SCM before_output = outputter_l_->last_cons_;
156   
157   Link_array<Line_of_score> lines;
158   for (int i=0; i < breaking.size (); i++)
159     {
160       Line_of_score *line_l = line_l_->set_breaking (breaking, i);
161       lines.push (line_l);
162       if (line_l != line_l_)
163         typeset_element (line_l);
164     }
165
166
167   *mlog << "\n";
168   *mlog << _ ("Line ... ");
169   line_l_->break_processing ();
170   for (int i=0; i < lines.size (); i++)
171     {
172       *mlog << '[' << flush;
173       
174       Line_of_score *line_l = lines[i];
175
176       line_l->post_processing ();
177       *mlog << i << flush;
178       line_l->output_all (i + 1 == lines.size ());
179       *mlog << ']' << flush;
180     }
181
182   SCM font_names = ly_quote_scm (all_fonts_global_p->font_descriptions ());
183   gh_set_cdr_x (before_output,
184                 gh_cons  (gh_list (ly_symbol2scm ("define-fonts"),
185                                    font_names,
186                                    SCM_UNDEFINED),
187                           gh_cdr (before_output)));
188   
189   Paper_stream* psp = paper_l_->paper_stream_p ();
190   outputter_l_->dump_onto (psp);
191   // huh?
192   delete outputter_l_;
193   
194   outputter_l_ = 0;
195   delete psp;
196   
197 }
198
199 Link_array<Item>
200 Paper_score::broken_col_range (Item const*l, Item const*r) const
201 {
202   Link_array<Item> ret;
203
204   l = l->column_l ();
205   r = r->column_l ();
206   
207   int  start = l
208     ? find_col_idx (dynamic_cast<Paper_column*> ((Item*)l))+1
209     : 0;
210
211   int stop = r
212     ? find_col_idx (dynamic_cast<Paper_column*>((Item*)r))
213     : col_l_arr_.size ();
214
215   while (start < stop)
216     {
217       Paper_column *c = col_l_arr_[start];
218       if (c->breakable_b () && !c->line_l_)
219         ret.push (c);
220       start++;
221     }
222
223   return ret;
224 }