]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-score.cc
8769502ec9a581cc74526a68f9a875edad07cc3f
[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   assert (elem_p->self_scm_ != SCM_EOL);
61   SCM_CDR(protected_scms_) = gh_cons (elem_p->self_scm_,
62                                       SCM_CDR (protected_scms_));
63   scm_unprotect_object (elem_p->self_scm_);
64   
65   SCM p =  elem_p->remove_elt_property (break_helper_only_scm_sym);
66   if (p != SCM_BOOL_F)
67     break_helpers_arr_.push (elem_p);
68 }
69
70
71 void
72 Paper_score::typeset_unbroken_spanner (Spanner*span_p)
73 {
74   span_p_arr_.push (span_p);
75   span_p->pscore_l_=this;
76
77   SCM p =  span_p->remove_elt_property (break_helper_only_scm_sym);
78   if (p != SCM_BOOL_F)
79     break_helpers_arr_.push (span_p);
80 }
81
82 void
83 Paper_score::add_column (Paper_column *p)
84 {
85   p->set_rank (col_l_arr_.size ());
86   col_l_arr_.push (p);
87   typeset_element(p);
88 }
89
90
91
92 void
93 Paper_score::print () const
94 {
95 #ifndef NPRINT
96   if (!flower_dstream)
97     return ;
98   DEBUG_OUT << "Paper_score { ";
99   DEBUG_OUT << "\n elements: ";
100   for (int i=0; i < span_p_arr_.size (); i++)
101     span_p_arr_[i]->print ();
102   for (int i=0; i < elem_p_arr_.size (); i++)
103     elem_p_arr_[i]->print();
104   
105   DEBUG_OUT << "}\n";
106 #endif
107 }
108
109 int
110 Paper_score::find_col_idx (Paper_column const *c) const
111 {
112   Paper_column const *what = c;
113
114   return col_l_arr_.find_i ((Paper_column*)what);
115 }
116
117 Array<Column_x_positions>
118 Paper_score::calc_breaking ()
119 {
120   Break_algorithm *algorithm_p=0;
121   Array<Column_x_positions> sol;
122   bool try_wrap = !paper_l_->get_var ("castingalgorithm");
123
124   if (!try_wrap)
125     {
126       algorithm_p = new Gourlay_breaking ;
127       algorithm_p->set_pscore (this);
128       sol = algorithm_p->solve ();
129       delete algorithm_p;
130       if (! sol.size ())
131         {
132           warning (_ ("Can't solve this casting problem exactly; reverting to Word_wrap"));
133           try_wrap = true;
134         }
135     }
136   if  (try_wrap)
137     {
138       algorithm_p = new Word_wrap;
139       algorithm_p->set_pscore (this);
140       sol = algorithm_p->solve ();
141       delete algorithm_p;
142     }
143   return sol;
144 }
145
146
147
148 void
149 Paper_score::process ()
150 {
151   Dictionary<int> type_stats;
152   type_stats["Item"] =0;
153   type_stats["Spanner"] =0;
154   type_stats["Total"]=0;
155
156   print ();
157   *mlog << _ ("Preprocessing elements...") << " " << flush;
158   line_l_->breakable_col_processing ();
159   line_l_->pre_processing ();
160   
161   *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
162   line_l_->space_processing ();
163
164   Array<Column_x_positions> breaking = calc_breaking ();
165
166
167   Paper_stream* paper_stream_p = paper_l_->paper_stream_p ();
168   outputter_l_ = paper_l_->paper_outputter_p (paper_stream_p, header_l_, origin_str_);
169
170   Link_array<Line_of_score> lines;
171   for (int i=0; i < breaking.size (); i++)
172     {
173       Line_of_score *line_l = line_l_->set_breaking (breaking, i);
174       lines.push (line_l);
175       if (line_l != line_l_)
176         typeset_element (line_l);
177     }
178
179   if (experimental_features_global_b)
180     *mlog << _f ("%s elements", elem_p_arr_.size () + span_p_arr_.size ());
181
182   *mlog << "\n";
183   *mlog << _ ("Line ... ");
184   line_l_->break_processing ();
185   for (int i=0; i < lines.size (); i++)
186     {
187       *mlog << '[' << flush;
188       
189       Line_of_score *line_l = lines[i];
190
191       line_l->post_processing ();
192       *mlog << i << flush;
193       line_l->output_all (i + 1 == lines.size());
194       if (experimental_features_global_b)
195         *mlog << '(' << elem_p_arr_.size () + span_p_arr_.size () << ')';
196       
197       *mlog << ']' << flush;
198      }
199   
200   // huh?
201   delete outputter_l_;
202   delete paper_stream_p;
203   outputter_l_ = 0;
204
205
206   /*
207     todo: sort output
208    */
209   if (experimental_features_global_b)
210     {
211       for (Dictionary_iter<int> i(type_stats); i.ok(); i++)
212         {
213           *mlog << i.key () << ": " << i.val () << " objects\n";
214         }
215     }
216   *mlog << '\n' << flush;
217       
218 }
219
220 Link_array<Item>
221 Paper_score::broken_col_range (Item const*l, Item const*r) const
222 {
223   Link_array<Item> ret;
224
225   l = l->column_l ();
226   r = r->column_l ();
227   
228   int  start = l
229     ? find_col_idx (dynamic_cast<Paper_column*> ((Item*)l))+1
230     : 0;
231
232   int stop = r
233     ? find_col_idx (dynamic_cast<Paper_column*>((Item*)r))
234     : col_l_arr_.size ();
235
236   while (start < stop)
237     {
238       Paper_column *c = col_l_arr_[start];
239       if (c->breakable_b () && !c->line_l_)
240         ret.push (c);
241       start++;
242     }
243
244   return ret;
245 }