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