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