]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-score.cc
release: 1.2.15
[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   paper_l_ =0;
28   outputter_l_ =0;
29   Line_of_score * line_p = new Line_of_score;
30   line_p->pscore_l_ = this;
31   element_smob_list_ = scm_protect_object (gh_cons (line_p->self_scm_, SCM_EOL));
32   line_l_ = line_p;
33 }
34
35 Paper_score::Paper_score (Paper_score const &s)
36   : Music_output (s)
37 {
38   assert (false);
39 }
40
41 Paper_score::~Paper_score ()
42 {
43   scm_unprotect_object (element_smob_list_);
44 }
45
46 void
47 Paper_score::typeset_element (Score_element * elem_p)
48 {
49   elem_p->pscore_l_ = this;
50
51   SCM_CDR(element_smob_list_) = gh_cons (elem_p->self_scm_,
52                                          SCM_CDR (element_smob_list_));
53   scm_unprotect_object (elem_p->self_scm_);
54 }
55
56 void
57 Paper_score::add_column (Paper_column *p)
58 {
59   p->set_rank (col_l_arr_.size ());
60   col_l_arr_.push (p);
61   typeset_element(p);
62 }
63
64 void
65 Paper_score::print () const
66 {
67 #ifndef NPRINT
68   if (!flower_dstream)
69     return ;
70
71   DEBUG_OUT << "Paper_score { ";
72   DEBUG_OUT << "\n elements: ";
73
74   for (SCM p = SCM_CDR (element_smob_list_);
75        p != SCM_EOL;
76        p = SCM_CDR(p))
77     gh_display (SCM_CAR(p));
78   DEBUG_OUT << "}\n";
79 #endif
80 }
81
82 int
83 Paper_score::find_col_idx (Paper_column const *c) const
84 {
85   Paper_column const *what = c;
86
87   return col_l_arr_.find_i ((Paper_column*)what);
88 }
89
90 Array<Column_x_positions>
91 Paper_score::calc_breaking ()
92 {
93   Break_algorithm *algorithm_p=0;
94   Array<Column_x_positions> sol;
95
96   algorithm_p = new Gourlay_breaking ;
97   algorithm_p->set_pscore (this);
98   sol = algorithm_p->solve ();
99   delete algorithm_p;
100
101   return sol;
102 }
103
104
105
106 void
107 Paper_score::process ()
108 {
109   Dictionary<int> type_stats;
110   type_stats["Item"] =0;
111   type_stats["Spanner"] =0;
112   type_stats["Total"]=0;
113
114   print ();
115   *mlog << _ ("Preprocessing elements...") << " " << flush;
116   line_l_->breakable_col_processing ();
117   line_l_->pre_processing ();
118   
119   *mlog << '\n' << _ ("Calculating column positions...") << " " << flush;
120   line_l_->space_processing ();
121
122   Array<Column_x_positions> breaking = calc_breaking ();
123
124
125   Paper_stream* paper_stream_p = paper_l_->paper_stream_p ();
126   outputter_l_ = paper_l_->paper_outputter_p (paper_stream_p, header_l_, origin_str_);
127
128   Link_array<Line_of_score> lines;
129   for (int i=0; i < breaking.size (); i++)
130     {
131       Line_of_score *line_l = line_l_->set_breaking (breaking, i);
132       lines.push (line_l);
133       if (line_l != line_l_)
134         typeset_element (line_l);
135     }
136
137
138   *mlog << "\n";
139   *mlog << _ ("Line ... ");
140   line_l_->break_processing ();
141   for (int i=0; i < lines.size (); i++)
142     {
143       *mlog << '[' << flush;
144       
145       Line_of_score *line_l = lines[i];
146
147       line_l->post_processing ();
148       *mlog << i << flush;
149       line_l->output_all (i + 1 == lines.size());
150       *mlog << ']' << flush;
151      }
152   
153   // huh?
154   delete outputter_l_;
155   delete paper_stream_p;
156   outputter_l_ = 0;
157
158
159   /*
160     todo: sort output
161    */
162   if (experimental_features_global_b)
163     {
164       for (Dictionary_iter<int> i(type_stats); i.ok(); i++)
165         {
166           *mlog << i.key () << ": " << i.val () << " objects\n";
167         }
168     }
169   *mlog << '\n' << flush;
170       
171 }
172
173 Link_array<Item>
174 Paper_score::broken_col_range (Item const*l, Item const*r) const
175 {
176   Link_array<Item> ret;
177
178   l = l->column_l ();
179   r = r->column_l ();
180   
181   int  start = l
182     ? find_col_idx (dynamic_cast<Paper_column*> ((Item*)l))+1
183     : 0;
184
185   int stop = r
186     ? find_col_idx (dynamic_cast<Paper_column*>((Item*)r))
187     : col_l_arr_.size ();
188
189   while (start < stop)
190     {
191       Paper_column *c = col_l_arr_[start];
192       if (c->breakable_b () && !c->line_l_)
193         ret.push (c);
194       start++;
195     }
196
197   return ret;
198 }