2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
27 #include "output-def.hh"
28 #include "paper-book.hh"
30 #include "text-interface.hh"
32 #include "performance.hh"
33 #include "paper-score.hh"
34 #include "page-marker.hh"
43 input_location_ = SCM_EOL;
46 input_location_ = Input ().smobbed_copy ();
49 Book::Book (Book const &s)
55 input_location_ = SCM_EOL;
60 paper_ = s.paper_->clone ();
64 input_location_ = s.origin ()->smobbed_copy ();
66 header_ = ly_make_module (false);
67 if (ly_is_module (s.header_))
68 ly_module_copy (header_, s.header_);
70 for (SCM p = s.scores_; scm_is_pair (p); p = scm_cdr (p))
72 SCM entry = scm_car (p);
74 if (Score *newscore = Score::unsmob (entry))
75 * t = scm_cons (newscore->clone ()->unprotect (), SCM_EOL);
76 else if (Page_marker *marker = Page_marker::unsmob (entry))
77 * t = scm_cons (marker->clone ()->unprotect (), SCM_EOL);
80 /* This entry is a markup list */
81 *t = scm_cons (entry, SCM_EOL);
87 for (SCM p = s.bookparts_; scm_is_pair (p); p = scm_cdr (p))
89 Book *newpart = Book::unsmob (scm_car (p))->clone ();
91 *t = scm_cons (newpart->self_scm (), SCM_EOL);
93 newpart->unprotect ();
100 return Input::unsmob (input_location_);
112 scm_gc_mark (paper_->self_scm ());
113 scm_gc_mark (scores_);
114 scm_gc_mark (bookparts_);
115 scm_gc_mark (input_location_);
121 Book::add_score (SCM s)
123 scores_ = scm_cons (s, scores_);
127 Book::set_parent (Book *parent)
131 paper_ = new Output_def ();
132 paper_->unprotect ();
134 paper_->parent_ = parent->paper_;
135 /* Copy the header block of the parent */
136 if (ly_is_module (parent->header_))
138 SCM tmp_header = ly_make_module (false);
139 ly_module_copy (tmp_header, parent->header_);
140 if (ly_is_module (header_))
141 ly_module_copy (tmp_header, header_);
142 header_ = tmp_header;
146 /* Before an explicit \bookpart is encountered, scores are added to the book.
147 * But once a bookpart is added, the previous scores shall be collected into
151 Book::add_scores_to_bookpart ()
153 if (scm_is_pair (scores_))
155 /* If scores have been added to this book, add them to a child
157 Book *part = new Book;
158 part->set_parent (this);
159 part->scores_ = scores_;
160 bookparts_ = scm_cons (part->self_scm (), bookparts_);
167 Book::add_bookpart (SCM b)
169 add_scores_to_bookpart ();
170 Book *part = Book::unsmob (b);
171 part->set_parent (this);
172 bookparts_ = scm_cons (b, bookparts_);
178 for (SCM s = scores_; scm_is_pair (s); s = scm_cdr (s))
179 if (Score *score = Score::unsmob (scm_car (s)))
180 if (score->error_found_)
183 for (SCM part = bookparts_; scm_is_pair (part); part = scm_cdr (part))
184 if (Book *bookpart = Book::unsmob (scm_car (part)))
185 if (bookpart->error_found ())
192 Book::process (Output_def *default_paper,
193 Output_def *default_layout)
195 return process (default_paper, default_layout, 0);
199 Book::process_bookparts (Paper_book *output_paper_book, Output_def *paper, Output_def *layout)
201 add_scores_to_bookpart ();
202 for (SCM p = scm_reverse (bookparts_); scm_is_pair (p); p = scm_cdr (p))
204 if (Book *book = Book::unsmob (scm_car (p)))
206 Paper_book *paper_book_part = book->process (paper, layout, output_paper_book);
209 output_paper_book->add_bookpart (paper_book_part->self_scm ());
210 paper_book_part->unprotect ();
214 /* In a Paper_book, bookparts are stored in straight order */
215 output_paper_book->bookparts_ = scm_reverse_x (output_paper_book->bookparts_, SCM_EOL);
219 Book::process_score (SCM s, Paper_book *output_paper_book, Output_def *layout)
221 if (Score *score = Score::unsmob (scm_car (s)))
224 ->book_rendering (output_paper_book->paper_, layout);
226 while (scm_is_pair (outputs))
228 Music_output *output = Music_output::unsmob (scm_car (outputs));
230 if (Performance *perf = dynamic_cast<Performance *> (output))
231 output_paper_book->add_performance (perf->self_scm ());
232 else if (Paper_score *pscore = dynamic_cast<Paper_score *> (output))
234 if (ly_is_module (score->get_header ()))
235 output_paper_book->add_score (score->get_header ());
236 output_paper_book->add_score (pscore->self_scm ());
239 outputs = scm_cdr (outputs);
242 else if (Text_interface::is_markup_list (scm_car (s))
243 || Page_marker::is_smob (scm_car (s)))
244 output_paper_book->add_score (scm_car (s));
250 /* Concatenate all score or book part outputs into a Paper_book
253 Book::process (Output_def *default_paper,
254 Output_def *default_layout,
255 Paper_book *parent_part)
257 Output_def *paper = paper_ ? paper_ : default_paper;
259 /* If top book, recursively check score errors */
260 if (!parent_part && error_found ())
266 Paper_book *paper_book = new Paper_book ();
267 Real scale = scm_to_double (paper->c_variable ("output-scale"));
268 Output_def *scaled_bookdef = scale_output_def (paper, scale);
269 paper_book->paper_ = scaled_bookdef;
272 paper_book->parent_ = parent_part;
273 paper_book->paper_->parent_ = parent_part->paper_;
275 paper_book->header_ = header_;
276 scaled_bookdef->unprotect ();
278 if (scm_is_pair (bookparts_))
280 /* Process children book parts */
281 process_bookparts (paper_book, paper, default_layout);
285 paper_book->paper_->normalize ();
287 /* Render in order of parsing. */
288 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
290 process_score (s, paper_book, default_layout);