]> git.donarmstrong.com Git - lilypond.git/blob - lily/book.cc
indent fix.
[lilypond.git] / lily / book.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "book.hh"
21
22 #include <cstdio>
23 using namespace std;
24
25 #include "main.hh"
26 #include "music.hh"
27 #include "output-def.hh"
28 #include "paper-book.hh"
29 #include "score.hh"
30 #include "text-interface.hh"
31 #include "warn.hh"
32 #include "performance.hh"
33 #include "paper-score.hh"
34 #include "page-marker.hh"
35
36 #include "ly-smobs.icc"
37
38 Book::Book ()
39 {
40   paper_ = 0;
41   header_ = SCM_EOL;
42   scores_ = SCM_EOL;
43   bookparts_ = SCM_EOL;
44   input_location_ = SCM_EOL;
45   smobify_self ();
46
47   input_location_ = make_input (Input ());
48 }
49
50 Book::Book (Book const &s)
51 {
52   paper_ = 0;
53   header_ = SCM_EOL;
54   scores_ = SCM_EOL;
55   bookparts_ = SCM_EOL;
56   input_location_ = SCM_EOL;
57   smobify_self ();
58
59   if (s.paper_)
60     {
61       paper_ = s.paper_->clone ();
62       paper_->unprotect ();
63     }
64   
65   input_location_ = make_input (*s.origin ());
66
67   header_ = ly_make_anonymous_module (false);
68   if (ly_is_module (s.header_))
69     ly_module_copy (header_, s.header_);
70   
71   SCM *t = &scores_;
72   for (SCM p = s.scores_; scm_is_pair (p); p = scm_cdr (p))
73     {
74       Score *newscore = unsmob_score (scm_car (p))->clone ();
75
76       *t = scm_cons (newscore->self_scm (), SCM_EOL);
77       t = SCM_CDRLOC (*t);
78       newscore->unprotect ();
79     }
80
81   t = &bookparts_;
82   for (SCM p = s.bookparts_; scm_is_pair (p); p = scm_cdr (p))
83     {
84       Book *newpart = unsmob_book (scm_car (p))->clone ();
85
86       *t = scm_cons (newpart->self_scm (), SCM_EOL);
87       t = SCM_CDRLOC (*t);
88       newpart->unprotect ();
89     }
90 }
91
92 Input *
93 Book::origin () const
94 {
95   return unsmob_input (input_location_);
96 }
97
98 Book::~Book ()
99 {
100 }
101
102 IMPLEMENT_SMOBS (Book);
103 IMPLEMENT_DEFAULT_EQUAL_P (Book);
104
105 SCM
106 Book::mark_smob (SCM s)
107 {
108   Book *book = (Book *) SCM_CELL_WORD_1 (s);
109
110   if (book->paper_)
111     scm_gc_mark (book->paper_->self_scm ());
112   scm_gc_mark (book->scores_);
113   scm_gc_mark (book->bookparts_);
114   scm_gc_mark (book->input_location_);
115   
116   return book->header_;
117 }
118
119 int
120 Book::print_smob (SCM, SCM p, scm_print_state*)
121 {
122   scm_puts ("#<Book>", p);
123   return 1;
124 }
125
126 void
127 Book::add_score (SCM s)
128 {
129   scores_ = scm_cons (s, scores_);
130 }
131
132 void
133 Book::set_parent (Book *parent)
134 {
135   if (!paper_)
136     {
137       paper_ = new Output_def ();
138       paper_->unprotect ();
139     }
140   paper_->parent_ = parent->paper_;
141   /* Copy the header block of the parent */
142   if (ly_is_module (parent->header_))
143     {
144       SCM tmp_header = ly_make_anonymous_module (false);
145       ly_module_copy (tmp_header, parent->header_);
146       if (ly_is_module (header_))
147         ly_module_copy (tmp_header, header_);
148       header_ = tmp_header;
149     }
150 }
151
152 /* Before an explicit \bookpart is encountered, scores are added to the book.
153  * But once a bookpart is added, the previous scores shall be collected into
154  * a new bookpart.
155  */
156 void
157 Book::add_scores_to_bookpart ()
158 {
159   if (scm_is_pair (scores_))
160     {
161       /* If scores have been added to this book, add them to a child 
162        * book part */
163       Book *part = new Book;
164       part->set_parent (this);
165       part->scores_ = scores_;
166       bookparts_ = scm_cons (part->self_scm (), bookparts_);
167       part->unprotect ();
168       scores_ = SCM_EOL;
169     }
170 }
171
172 void
173 Book::add_bookpart (SCM b)
174 {
175   add_scores_to_bookpart ();
176   Book *part = unsmob_book (b);
177   part->set_parent (this);
178   bookparts_ = scm_cons (b, bookparts_);
179 }
180
181 bool
182 Book::error_found ()
183 {
184   for (SCM s = scores_; scm_is_pair (s); s = scm_cdr (s))
185     if (Score *score = unsmob_score (scm_car (s)))
186       if (score->error_found_)
187         return true;
188   
189   for (SCM part = bookparts_; scm_is_pair (part); part = scm_cdr (part))
190     if (Book *bookpart = unsmob_book (scm_car (part)))
191       if (bookpart->error_found ())
192         return true;
193
194   return false;
195 }
196
197 Paper_book *
198 Book::process (Output_def *default_paper,
199                Output_def *default_layout)
200 {
201   return process (default_paper, default_layout, 0);
202 }
203
204 void
205 Book::process_bookparts (Paper_book *output_paper_book, Output_def *paper, Output_def *layout)
206 {
207   add_scores_to_bookpart ();
208   for (SCM p = scm_reverse (bookparts_); scm_is_pair (p); p = scm_cdr (p))
209     {
210       if (Book *book = unsmob_book (scm_car (p)))
211         {
212           Paper_book *paper_book_part = book->process (paper, layout, output_paper_book);
213           if (paper_book_part)
214             {
215               output_paper_book->add_bookpart (paper_book_part->self_scm ());
216               paper_book_part->unprotect ();
217             }
218         }
219     }
220   /* In a Paper_book, bookparts are stored in straight order */
221   output_paper_book->bookparts_ = scm_reverse_x (output_paper_book->bookparts_, SCM_EOL);
222 }
223
224 void
225 Book::process_score (SCM s, Paper_book *output_paper_book, Output_def *layout)
226 {
227   if (Score *score = unsmob_score (scm_car (s)))
228     {
229       SCM outputs = score
230         ->book_rendering (output_paper_book->paper_, layout);
231               
232       while (scm_is_pair (outputs))
233         {
234           Music_output *output = unsmob_music_output (scm_car (outputs));
235                   
236           if (Performance *perf = dynamic_cast<Performance *> (output))
237             output_paper_book->add_performance (perf->self_scm ());
238           else if (Paper_score *pscore = dynamic_cast<Paper_score *> (output))
239             {
240               if (ly_is_module (score->get_header ()))
241                 output_paper_book->add_score (score->get_header ());
242               output_paper_book->add_score (pscore->self_scm ());
243             }
244                   
245           outputs = scm_cdr (outputs);
246         }
247     }
248   else if (Text_interface::is_markup_list (scm_car (s))
249            || unsmob_page_marker (scm_car (s)))
250     output_paper_book->add_score (scm_car (s));
251   else
252     assert (0);
253     
254 }
255
256 /* Concatenate all score or book part outputs into a Paper_book
257  */
258 Paper_book *
259 Book::process (Output_def *default_paper,
260                Output_def *default_layout,
261                Paper_book *parent_part)
262 {
263   Output_def *paper = paper_ ? paper_ : default_paper;
264
265   /* If top book, recursively check score errors */
266   if (!parent_part && error_found ())
267     return 0;
268
269   if (!paper)
270     return 0;
271
272   Paper_book *paper_book = new Paper_book ();
273   Real scale = scm_to_double (paper->c_variable ("output-scale"));
274   Output_def *scaled_bookdef = scale_output_def (paper, scale);
275   paper_book->paper_ = scaled_bookdef;
276   if (parent_part)
277     {
278       paper_book->parent_ = parent_part;
279       paper_book->paper_->parent_ = parent_part->paper_;
280     }
281   paper_book->header_ = header_;
282   scaled_bookdef->unprotect ();
283
284   if (scm_is_pair (bookparts_))
285     {
286       /* Process children book parts */
287       process_bookparts (paper_book, paper, default_layout);
288     }
289   else
290     {
291       paper_book->paper_->normalize ();
292       /* Process scores */
293       /* Render in order of parsing.  */
294       for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
295         {
296           process_score (s, paper_book, default_layout);
297         }
298     }
299
300   return paper_book;
301 }