]> git.donarmstrong.com Git - lilypond.git/blob - lily/p-score.cc
release: 1.1.37
[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--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 "p-col.hh"
16 #include "p-score.hh"
17 #include "p-col.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   paper_l_ =0;
29   outputter_l_ =0;
30   Line_of_score * line_p = new Line_of_score;
31   typeset_unbroken_spanner (line_p);
32
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   /*  for (int i=0; i < span_p_arr_.size (); i++)
45     delete span_p_arr_[i];
46   for (int i=0; i < elem_p_arr_.size (); i++)
47   delete elem_p_arr_[i];*/
48 }
49
50 void
51 Paper_score::typeset_element (Score_element * elem_p)
52 {
53   elem_p_arr_.push (elem_p);
54   elem_p->pscore_l_ = this;
55   elem_p->add_processing ();
56
57   SCM p =  elem_p->remove_elt_property (break_helper_only_scm_sym);
58   if (p != SCM_BOOL_F)
59     break_helpers_arr_.push (elem_p);
60 }
61
62
63 void
64 Paper_score::typeset_unbroken_spanner (Spanner*span_p)
65 {
66   span_p_arr_.push (span_p);
67   span_p->pscore_l_=this;
68   span_p->add_processing ();
69
70   SCM p =  span_p->remove_elt_property (break_helper_only_scm_sym);
71   if (p != SCM_BOOL_F)
72     break_helpers_arr_.push (span_p);
73 }
74
75 void
76 Paper_score::add_column (Paper_column *p)
77 {
78   p->set_rank (col_l_arr_.size ());
79   col_l_arr_.push (p);
80   typeset_element(p);
81 }
82
83
84
85 void
86 Paper_score::print () const
87 {
88 #ifndef NPRINT
89   if (!check_debug)
90     return ;
91   DOUT << "Paper_score { ";
92   DOUT << "\n elements: ";
93   for (int i=0; i < span_p_arr_.size (); i++)
94     span_p_arr_[i]->print ();
95   for (int i=0; i < elem_p_arr_.size (); i++)
96     elem_p_arr_[i]->print();
97   
98   DOUT << "}\n";
99 #endif
100 }
101
102 int
103 Paper_score::find_col_idx (Paper_column const *c) const
104 {
105   Paper_column const *what = c;
106
107   return col_l_arr_.find_i ((Paper_column*)what);
108 }
109
110 Array<Column_x_positions>
111 Paper_score::calc_breaking ()
112 {
113   Break_algorithm *algorithm_p=0;
114   Array<Column_x_positions> sol;
115   bool try_wrap = !paper_l_->get_var ("castingalgorithm");
116
117   if (!try_wrap)
118     {
119       algorithm_p = new Gourlay_breaking ;
120       algorithm_p->set_pscore (this);
121       sol = algorithm_p->solve ();
122       delete algorithm_p;
123       if (! sol.size ())
124         {
125           warning (_ ("Can't solve this casting problem exactly; revert to Word_wrap"));
126           try_wrap = true;
127         }
128     }
129   if  (try_wrap)
130     {
131       algorithm_p = new Word_wrap;
132       algorithm_p->set_pscore (this);
133       sol = algorithm_p->solve ();
134       delete algorithm_p;
135     }
136   return sol;
137 }
138
139
140
141 /*
142   not clean.  Should update elem_p_arr_ and span_p_arr_.  That would
143   also repair the stats.
144
145   This may be done efficiently by first sorting the arrays.  */
146 void
147 delete_array_contents (Link_array<Score_element> &to_remove, Dictionary<int> &type_stats)
148 {
149   for (int i=0; i < to_remove.size (); i++)
150     {
151       Score_element * e = to_remove[i];
152       String nm = e->name();
153       if (type_stats.elem_b (nm))
154         type_stats[nm] ++;
155       else
156         type_stats[nm] = 1;
157
158       if (dynamic_cast<Item*> (e))
159         type_stats["Item"] ++;
160       else if (dynamic_cast<Spanner*>(e))
161         type_stats["Spanner"] ++;
162       type_stats["Total"] ++;
163  //      delete e; //TODO!
164     }
165
166   to_remove.clear ();
167   to_remove.tighten_maxsize ();
168 }
169
170 void
171 Paper_score::schedule_for_delete (Score_element*e)
172 {
173   to_delete_arr_.push (e);
174 }
175
176 void
177 Paper_score::process ()
178 {
179   Dictionary<int> type_stats;
180   type_stats["Item"] =0;
181   type_stats["Spanner"] =0;
182   type_stats["Total"]=0;
183
184   print ();
185   *mlog << _ ("Preprocessing elements...") << " " << flush;
186   line_l_->breakable_col_processing ();
187   line_l_->pre_processing ();
188   
189   *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
190   line_l_->space_processing ();
191
192   Array<Column_x_positions> breaking = calc_breaking ();
193
194
195   delete_array_contents (break_helpers_arr_, type_stats);
196   
197   Paper_stream* paper_stream_p = paper_l_->paper_stream_p ();
198   outputter_l_ = paper_l_->paper_outputter_p (paper_stream_p, header_l_, origin_str_);
199
200   Link_array<Line_of_score> lines;
201   for (int i=0; i < breaking.size (); i++)
202     {
203       Line_of_score *line_l = line_l_->set_breaking (breaking, i);
204       lines.push (line_l);
205       if (line_l != line_l_)
206         typeset_element (line_l);
207     }
208
209   if (experimental_features_global_b)
210     *mlog << elem_p_arr_.size ()  + span_p_arr_.size () << " elements. ";
211
212   *mlog << "\nLine ... ";
213   for (int i=0; i < lines.size (); i++)
214     {
215       *mlog << '[' << flush;
216       
217       Line_of_score *line_l = lines[i];
218       line_l->break_processing ();
219       line_l->post_processing ();
220         *mlog << i << flush;
221       line_l->output_all ();
222
223       if (experimental_features_global_b)
224         *mlog << '(' << elem_p_arr_.size () + span_p_arr_.size () << ')';
225       
226       *mlog << ']' << flush;
227       
228       delete_array_contents (to_delete_arr_, type_stats);
229      }
230   
231   // huh?
232   delete outputter_l_;
233   delete paper_stream_p;
234   outputter_l_ = 0;
235
236
237   /*
238     todo: sort output
239    */
240   if (experimental_features_global_b)
241     {
242       for (Dictionary_iter<int> i(type_stats); i.ok(); i++)
243         {
244           *mlog << i.key () << ": " << i.val () << " objects\n";
245         }
246     }
247   *mlog << '\n' << flush;
248       
249 }
250
251 Link_array<Item>
252 Paper_score::broken_col_range (Item const*l, Item const*r) const
253 {
254   Link_array<Item> ret;
255
256   l = l->column_l ();
257   r = r->column_l ();
258   
259   int  start = l
260     ? find_col_idx (dynamic_cast<Paper_column*> ((Item*)l))+1
261     : 0;
262
263   int stop = r
264     ? find_col_idx (dynamic_cast<Paper_column*>((Item*)r))
265     : col_l_arr_.size ();
266
267   while (start < stop)
268     {
269       Paper_column *c = col_l_arr_[start];
270       if (c->breakable_b () && !c->line_l_)
271         ret.push (c);
272       start++;
273     }
274
275   return ret;
276 }