]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-score.cc
release: 1.3.39
[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--2000 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   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()));
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 = 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
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   print ();
114   progress_indication (_ ("Preprocessing elements...") + " ");
115
116
117   /*
118     Be sure to set breakability on first & last column.
119    */
120   col_l_arr_[0]->set_elt_property ("breakable", SCM_BOOL_T);
121   col_l_arr_.top ()->set_elt_property ("breakable", SCM_BOOL_T);
122
123   for (SCM s = element_smob_list_; gh_pair_p (s); s = gh_cdr (s))
124     unsmob_element (gh_car (s))->do_breakable_col_processing ();
125
126   fixup_refpoints ();
127
128   for (SCM s = element_smob_list_; gh_pair_p (s); s = gh_cdr (s))
129     {
130       Score_element* sc = unsmob_element (gh_car (s));
131       sc->calculate_dependencies (PRECALCED, PRECALCING, &Score_element::before_line_breaking);
132     }
133
134   progress_indication ("\n" + _ ("Calculating column positions...") + " " );
135   for (SCM s = element_smob_list_; gh_pair_p (s); s = gh_cdr (s))
136     unsmob_element (gh_car (s))->do_space_processing ();
137
138   Array<Column_x_positions> breaking = calc_breaking ();
139   line_l_->break_into_pieces (breaking);
140
141   for (SCM s = element_smob_list_; gh_pair_p (s); s = gh_cdr (s))
142     {
143       unsmob_element (gh_car (s))->do_break_processing ();
144     }
145   for (SCM s = element_smob_list_; gh_pair_p (s); s = gh_cdr (s))
146     {
147       unsmob_element (gh_car (s))->handle_broken_dependencies ();
148     }
149   
150   outputter_l_ = new Paper_outputter ;
151   outputter_l_->output_header ();
152   outputter_l_->output_version();
153   
154   if (header_global_p)
155     outputter_l_->output_scope (header_global_p, "mudela");
156   if (header_l_)
157     outputter_l_->output_scope (header_l_, "mudela");
158
159   outputter_l_->output_comment (_ ("Outputting Score, defined at: "));
160   outputter_l_->output_comment (origin_str_);
161
162   if (paper_l_->scope_p_)
163     outputter_l_->output_scope (paper_l_->scope_p_, "mudelapaper");
164   
165   SCM scm = gh_list (ly_symbol2scm ("experimental-on"), SCM_UNDEFINED);
166   outputter_l_->output_scheme (scm);
167   scm = gh_list (ly_symbol2scm ("header-end"), SCM_UNDEFINED);
168   outputter_l_->output_scheme (scm);
169
170
171   /*
172     This is tricky: we have to put the font definitions before the
173     actual output, but we don't know all fonts in advanced: generating
174     the output might trigger loading of a new font.  So we store the
175     place to insert the font definitions, generate the output and then
176     insert the definitions
177     
178    */
179   SCM before_output = outputter_l_->last_cons_;
180   fixup_refpoints ();
181
182   /*
183     TODO: change this, so that each element ouputs its molecules into
184     its line, and then output all lines one by one; then we can do
185     
186     foreach element: output
187   */
188   line_l_->output_lines ();
189
190
191   SCM font_names = ly_quote_scm (all_fonts_global_p->font_descriptions ());
192   gh_set_cdr_x (before_output,
193                 gh_cons  (gh_list (ly_symbol2scm ("define-fonts"),
194                                    font_names,
195                                    SCM_UNDEFINED),
196                           gh_cdr (before_output)));
197   
198   Paper_stream* psp = paper_l_->paper_stream_p ();
199   outputter_l_->dump_onto (psp);
200
201   // huh?
202   delete outputter_l_;
203   outputter_l_ = 0;
204   delete psp;
205   
206 }
207
208 Link_array<Item>
209 Paper_score::broken_col_range (Item const*l, Item const*r) const
210 {
211   Link_array<Item> ret;
212
213   l = l->column_l ();
214   r = r->column_l ();
215   
216   int  start = l
217     ? find_col_idx (dynamic_cast<Paper_column*> ((Item*)l))+1
218     : 0;
219
220   int stop = r
221     ? find_col_idx (dynamic_cast<Paper_column*>((Item*)r))
222     : col_l_arr_.size ();
223
224   while (start < stop)
225     {
226       Paper_column *c = col_l_arr_[start];
227       if (c->breakable_b () && !c->line_l_)
228         ret.push (c);
229       start++;
230     }
231
232   return ret;
233 }
234
235
236 void
237 Paper_score::fixup_refpoints ()
238 {
239   for (SCM s = element_smob_list_; gh_pair_p (s); s = gh_cdr (s))
240     {
241       SCM e = gh_car (s);
242       if (SMOB_IS_TYPE_B(Score_element, e))
243         {
244           Score_element * se = unsmob_element (e);
245           se->fixup_refpoint ();
246
247           if (!dynamic_cast<Line_of_score*> (se) && !se->parent_l (Y_AXIS))
248             {
249               programming_error ("No parent!");
250             }
251         }
252     }
253 }