]> git.donarmstrong.com Git - lilypond.git/blob - lily/p-score.cc
patch::: 1.0.12.hwn1
[lilypond.git] / lily / p-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--1998 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 "pcursor.hh"
16 #include "plist.hh"
17 #include "p-col.hh"
18 #include "p-score.hh"
19 #include "tex-stream.hh"
20 #include "p-col.hh"
21 #include "header.hh"
22 #include "word-wrap.hh"
23 #include "gourlay-breaking.hh"
24 #include "tex-outputter.hh"
25 #include "file-results.hh"
26 #include "misc.hh"
27
28 // sucking Cygnus egcs - w32
29 #include "list.tcc"
30 #include "cursor.tcc"
31
32 Paper_score::Paper_score ()
33 {
34   outputter_l_ =0;
35   Line_of_score * line_p = new Line_of_score;
36   typeset_unbroken_spanner (line_p);
37
38   line_l_ = line_p;
39 }
40
41 Paper_score::~Paper_score ()
42 {
43 #if 0
44   for (int i=0; i< line_l_arr_.size (); i++)
45     line_l_arr_[i]->unlink_all ();
46
47   for (PCursor<Score_element*> i(elem_p_list_.top()); i.ok(); i++)
48     {
49
50       if (i->linked_b())
51         i->unlink ();
52       assert (! i->linked_b ());
53     }
54 #endif
55 }
56
57 void
58 Paper_score::typeset_element (Score_element * elem_p)
59 {
60   elem_p_list_.bottom ().add (elem_p);
61   elem_p->pscore_l_ = this;
62   elem_p->add_processing ();
63 }
64
65 void
66 Paper_score::typeset_broken_spanner (Spanner*span_p)
67 {
68   typeset_element (span_p);
69 }
70
71
72 void
73 Paper_score::typeset_unbroken_spanner (Spanner*span_p)
74 {
75   span_p_list_.bottom ().add (span_p);
76   span_p->pscore_l_=this;
77
78   // do not init start/stop fields. These are for broken spans only.
79   span_p->add_processing ();
80 }
81
82
83 void
84 Paper_score::clean_cols ()
85 {
86   int rank_i = 0;
87   for (iter_top (col_p_list_,c); c.ok ();)
88     {
89       c->set_rank (rank_i++);
90       c++;
91     }
92 }
93
94 void
95 Paper_score::add_column (Paper_column *p)
96 {
97   col_p_list_.bottom ().add (p);
98   typeset_element(p);
99 }
100
101
102
103 void
104 Paper_score::print () const
105 {
106 #ifndef NPRINT
107   if (!check_debug)
108     return ;
109   DOUT << "Paper_score { ";
110   DOUT << "\n elements: ";
111   for (iter_top (elem_p_list_,cc); cc.ok (); cc++)
112     cc->print ();
113   DOUT << "\n unbroken spanners: ";
114   for (iter (span_p_list_.top (), i); i.ok  (); i++)
115     i->print ();
116
117   DOUT << "}\n";
118 #endif
119 }
120
121 PCursor<Paper_column *>
122 Paper_score::find_col (Paper_column const *c) const
123 {
124   Paper_column const *what = c;
125
126   return col_p_list_.find ((Paper_column*)what);
127 }
128
129
130 #if 0
131 void
132 Paper_score::set_breaking (Array<Column_x_positions> const &breaking)
133 {
134   for (iter (span_p_list_.top (),i); i.ok  ();)
135     {
136       Spanner *span_p = i.remove_p ();
137       if (span_p->broken_b ()
138           || !((Score_element*)span_p)->line_l ())
139         {
140           span_p->unlink ();
141           delete span_p;
142         }
143       else 
144         {
145           typeset_broken_spanner (span_p);
146         }
147     }
148   for (iter (elem_p_list_.top (),i); i.ok  () ;)
149     {
150       Item *i_l =i->access_Item ();
151       if (i_l && !i_l->line_l ())
152         {
153           i_l->unlink ();
154           Score_element * item_p= i.remove_p ();
155           delete item_p;
156         }
157       else
158         i++;
159     }
160 }
161 #endif
162
163
164 Array<Column_x_positions>
165 Paper_score::calc_breaking ()
166 {
167   Break_algorithm *algorithm_p=0;
168   Array<Column_x_positions> sol;
169   bool try_wrap = ! paper_l_->get_var ("castingalgorithm");
170
171   if (!try_wrap)
172     {
173       algorithm_p = new Gourlay_breaking ;
174       algorithm_p->set_pscore (this);
175       sol = algorithm_p->solve ();
176       delete algorithm_p;
177       if (! sol.size ())
178         {
179           warning (_ ("Can't solve this casting problem exactly; revert to Word_wrap"));
180           try_wrap = true;
181         }
182     }
183   if  (try_wrap)
184     {
185       algorithm_p = new Word_wrap;
186       algorithm_p->set_pscore (this);
187       sol = algorithm_p->solve ();
188       delete algorithm_p;
189     }
190   return sol;
191 }
192
193 void
194 Paper_score::process ()
195 {
196   clean_cols ();
197   print ();
198   *mlog << _ ("Preprocessing elements...") << " " << flush;
199       line_l_->breakable_col_processing ();
200       line_l_->pre_processing ();
201   
202       *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
203       line_l_->space_processing ();
204
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);
208
209   Link_array<Line_of_score> lines;
210   for (int i=0; i < breaking.size (); i++)
211     {
212       Line_of_score *line_l = line_l_->set_breaking (breaking, i);
213       lines.push (line_l);
214       if (line_l != line_l_)
215         typeset_broken_spanner (line_l);
216       
217     }
218
219   *mlog << "\nLine ... ";
220   for (int i=0; i < lines.size (); i++)
221     {
222       *mlog << '[' << flush;
223       
224       Line_of_score *line_l = lines[i];
225       line_l->break_processing ();
226       line_l->post_processing ();
227         *mlog << i << flush;
228       line_l->output_all ();
229         *mlog << ']' << flush;
230       remove_line (line_l);
231         
232     }
233   *tex_stream_p << "\n\\EndLilyPondOutput";
234   delete outputter_l_;
235   delete tex_stream_p;
236   outputter_l_ = 0;
237 }
238
239
240 void
241 Paper_score::remove_line (Line_of_score *l)
242 {
243   Link_array<Score_element> to_remove;
244   for (PCursor<Score_element*> i(elem_p_list_.top ()); i.ok (); )
245     {
246       if (i->line_l () == l)
247         to_remove.push (i.remove_p ());
248       else
249         i++;
250     }
251
252   for (PCursor<Spanner*> i (span_p_list_.top ()); i.ok (); )
253     {
254       Score_element *e = i.ptr ();
255       if (e->line_l () == l)
256         to_remove.push (i.remove_p ());
257       else
258         i++;
259     }
260
261   //  l->unlink_all ();
262   for (int i=0; i < to_remove.size (); i++)
263     {
264       to_remove[i]->unlink ();
265       assert (!to_remove[i]->linked_b ());
266       delete to_remove [i];
267     }
268 }
269
270 Tex_stream *
271 Paper_score::open_output_stream ()
272 {
273   // output
274   String base_outname=paper_l_->get_default_output ();
275
276   if (base_outname.empty_b ())
277     {
278       base_outname = default_outname_base_global;
279       int def = paper_l_->get_next_default_count ();
280       if (def)
281         {
282           base_outname += "-" + to_str (def);
283         }
284     }
285
286   String outname = base_outname;
287   if (outname != "-")
288      outname += ".tex";
289   target_str_global_array.push (outname);
290
291   *mlog << _f ("TeX output to %s...", 
292     outname == "-" ? String ("<stdout>") : outname ) << endl;
293
294   return  new Tex_stream (outname);
295 }
296
297
298
299 Tex_outputter *
300 Paper_score::open_tex_outputter (Tex_stream *tex_out_p)
301 {
302   Tex_outputter *interfees_p= new Tex_outputter (tex_out_p);
303
304   if (header_global_p)
305     {
306       *tex_out_p << header_global_p->TeX_string ();
307     }
308     
309   
310   *tex_out_p << _ ("% outputting Score, defined at: ") << origin_str_ << '\n';
311
312   if (header_l_)
313     {
314       *tex_out_p << header_l_->TeX_string();
315     }
316   *tex_out_p << paper_l_->TeX_output_settings_str ();
317   
318
319   if (experimental_features_global_b)
320     *tex_out_p << "\\turnOnExperimentalFeatures%\n";
321
322   *tex_out_p << "\\turnOnPostScript%\n";
323   return interfees_p;
324 }
325
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
329 {
330   Link_array<Paper_column> ret;
331
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 ());
334
335   /*
336     ugh! windows-suck-suck-suck.
337     */
338   while (PCursor<Paper_column*>::compare (start,stop) < 0)
339     {
340       if (start->breakable_b_)
341         ret.push (start);
342       start++;
343     }
344
345   return ret;
346 }
347
348
349 Link_array<Paper_column>
350 Paper_score::col_range (Paper_column*l, Paper_column*r) const
351 {
352   Link_array<Paper_column> ret;
353
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 ());
356   ret.push (l);
357
358   /*
359     ugh! windows-suck-suck-suck.
360     */
361   while (PCursor<Paper_column*>::compare (start,stop) < 0)
362     ret.push (start++);
363   ret.push (r);
364   return ret;
365 }
366
367 Link_array<Item>
368 Paper_score::broken_col_range (Item const*l_item_l, Item const*r_item_l) const
369 {
370   Link_array<Item> ret;
371   Item const*l=l_item_l;
372   Item const*r=r_item_l;
373
374   while (! l->is_type_b(Paper_column::static_name ()))
375     l = l->axis_group_l_a_[X_AXIS]->access_Score_element ()->access_Item ();
376
377   while (! r->is_type_b(Paper_column::static_name ()))
378     r = r->axis_group_l_a_[X_AXIS]->access_Score_element ()->access_Item ();
379
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 ());
382
383   /*
384     ugh! windows-suck-suck-suck.
385     */
386   while (PCursor<Paper_column*>::compare (start,stop) < 0)
387     {
388       if (start->breakable_b_ && !start->line_l_)
389         ret.push (start);
390       start++;
391     }
392
393   return ret;
394 }