]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-score.cc
patch::: 1.2.8.jcn1
[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 "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"
25
26 Paper_score::Paper_score ()
27 {
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);
33
34   line_l_ = line_p;
35 }
36
37 Paper_score::Paper_score (Paper_score const &s)
38   : Music_output (s)
39 {
40   assert (false);
41 }
42
43 Paper_score::~Paper_score ()
44 {
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];
49
50   scm_unprotect_object (protected_scms_);
51 }
52
53 void
54 Paper_score::typeset_element (Score_element * elem_p)
55 {
56   elem_p_arr_.push (elem_p);
57   elem_p->pscore_l_ = this;
58
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_);
63   
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);
67 }
68
69
70 void
71 Paper_score::typeset_unbroken_spanner (Spanner*span_p)
72 {
73   span_p_arr_.push (span_p);
74   span_p->pscore_l_=this;
75
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);
79 }
80
81 void
82 Paper_score::add_column (Paper_column *p)
83 {
84   p->set_rank (col_l_arr_.size ());
85   col_l_arr_.push (p);
86   typeset_element(p);
87 }
88
89
90
91 void
92 Paper_score::print () const
93 {
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();
103   
104   DOUT << "}\n";
105 #endif
106 }
107
108 int
109 Paper_score::find_col_idx (Paper_column const *c) const
110 {
111   Paper_column const *what = c;
112
113   return col_l_arr_.find_i ((Paper_column*)what);
114 }
115
116 Array<Column_x_positions>
117 Paper_score::calc_breaking ()
118 {
119   Break_algorithm *algorithm_p=0;
120   Array<Column_x_positions> sol;
121   bool try_wrap = !paper_l_->get_var ("castingalgorithm");
122
123   if (!try_wrap)
124     {
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 ())
130         {
131           warning (_ ("Can't solve this casting problem exactly; reverting to Word_wrap"));
132           try_wrap = true;
133         }
134     }
135   if  (try_wrap)
136     {
137       algorithm_p = new Word_wrap;
138       algorithm_p->set_pscore (this);
139       sol = algorithm_p->solve ();
140       delete algorithm_p;
141     }
142   return sol;
143 }
144
145
146
147 void
148 Paper_score::process ()
149 {
150   Dictionary<int> type_stats;
151   type_stats["Item"] =0;
152   type_stats["Spanner"] =0;
153   type_stats["Total"]=0;
154
155   print ();
156   *mlog << _ ("Preprocessing elements...") << " " << flush;
157   line_l_->breakable_col_processing ();
158   line_l_->pre_processing ();
159   
160   *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
161   line_l_->space_processing ();
162
163   Array<Column_x_positions> breaking = calc_breaking ();
164
165
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_);
168
169   Link_array<Line_of_score> lines;
170   for (int i=0; i < breaking.size (); i++)
171     {
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);
176     }
177
178   if (experimental_features_global_b)
179     *mlog << _f ("%s elements", elem_p_arr_.size () + span_p_arr_.size ());
180
181   *mlog << "\n";
182   *mlog << _ ("Line ... ");
183   line_l_->break_processing ();
184   for (int i=0; i < lines.size (); i++)
185     {
186       *mlog << '[' << flush;
187       
188       Line_of_score *line_l = lines[i];
189
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 () << ')';
195       
196       *mlog << ']' << flush;
197      }
198   
199   // huh?
200   delete outputter_l_;
201   delete paper_stream_p;
202   outputter_l_ = 0;
203
204
205   /*
206     todo: sort output
207    */
208   if (experimental_features_global_b)
209     {
210       for (Dictionary_iter<int> i(type_stats); i.ok(); i++)
211         {
212           *mlog << i.key () << ": " << i.val () << " objects\n";
213         }
214     }
215   *mlog << '\n' << flush;
216       
217 }
218
219 Link_array<Item>
220 Paper_score::broken_col_range (Item const*l, Item const*r) const
221 {
222   Link_array<Item> ret;
223
224   l = l->column_l ();
225   r = r->column_l ();
226   
227   int  start = l
228     ? find_col_idx (dynamic_cast<Paper_column*> ((Item*)l))+1
229     : 0;
230
231   int stop = r
232     ? find_col_idx (dynamic_cast<Paper_column*>((Item*)r))
233     : col_l_arr_.size ();
234
235   while (start < stop)
236     {
237       Paper_column *c = col_l_arr_[start];
238       if (c->breakable_b () && !c->line_l_)
239         ret.push (c);
240       start++;
241     }
242
243   return ret;
244 }