Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / book.cc
blob86eeda1b39061207c2a4c02ad2bdabaa699d1f13
1 /*
2 book.cc -- implement Book
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "book.hh"
11 #include <cstdio>
12 using namespace std;
14 #include "main.hh"
15 #include "music.hh"
16 #include "output-def.hh"
17 #include "paper-book.hh"
18 #include "score.hh"
19 #include "text-interface.hh"
20 #include "warn.hh"
21 #include "performance.hh"
22 #include "paper-score.hh"
23 #include "page-marker.hh"
25 #include "ly-smobs.icc"
27 Book::Book ()
29 paper_ = 0;
30 header_ = SCM_EOL;
31 scores_ = SCM_EOL;
32 bookparts_ = SCM_EOL;
33 input_location_ = SCM_EOL;
34 smobify_self ();
36 input_location_ = make_input (Input ());
39 Book::Book (Book const &s)
41 paper_ = 0;
42 header_ = SCM_EOL;
43 scores_ = SCM_EOL;
44 bookparts_ = SCM_EOL;
45 input_location_ = SCM_EOL;
46 smobify_self ();
48 if (s.paper_)
50 paper_ = s.paper_->clone ();
51 paper_->unprotect ();
54 input_location_ = make_input (*s.origin ());
56 header_ = ly_make_anonymous_module (false);
57 if (ly_is_module (s.header_))
58 ly_module_copy (header_, s.header_);
60 SCM *t = &scores_;
61 for (SCM p = s.scores_; scm_is_pair (p); p = scm_cdr (p))
63 Score *newscore = unsmob_score (scm_car (p))->clone ();
65 *t = scm_cons (newscore->self_scm (), SCM_EOL);
66 t = SCM_CDRLOC (*t);
67 newscore->unprotect ();
70 t = &bookparts_;
71 for (SCM p = s.bookparts_; scm_is_pair (p); p = scm_cdr (p))
73 Book *newpart = unsmob_book (scm_car (p))->clone ();
75 *t = scm_cons (newpart->self_scm (), SCM_EOL);
76 t = SCM_CDRLOC (*t);
77 newpart->unprotect ();
81 Input *
82 Book::origin () const
84 return unsmob_input (input_location_);
87 Book::~Book ()
91 IMPLEMENT_SMOBS (Book);
92 IMPLEMENT_DEFAULT_EQUAL_P (Book);
94 SCM
95 Book::mark_smob (SCM s)
97 Book *book = (Book *) SCM_CELL_WORD_1 (s);
99 if (book->paper_)
100 scm_gc_mark (book->paper_->self_scm ());
101 scm_gc_mark (book->scores_);
102 scm_gc_mark (book->bookparts_);
103 scm_gc_mark (book->input_location_);
105 return book->header_;
109 Book::print_smob (SCM, SCM p, scm_print_state*)
111 scm_puts ("#<Book>", p);
112 return 1;
115 void
116 Book::add_score (SCM s)
118 scores_ = scm_cons (s, scores_);
121 void
122 Book::set_parent (Book *parent)
124 if (!paper_)
126 paper_ = new Output_def ();
127 paper_->unprotect ();
129 paper_->parent_ = parent->paper_;
130 /* Copy the header block of the parent */
131 if (ly_is_module (parent->header_))
133 SCM tmp_header = ly_make_anonymous_module (false);
134 ly_module_copy (tmp_header, parent->header_);
135 if (ly_is_module (header_))
136 ly_module_copy (tmp_header, header_);
137 header_ = tmp_header;
141 /* Before an explicit \bookpart is encountered, scores are added to the book.
142 * But once a bookpart is added, the previous scores shall be collected into
143 * a new bookpart.
145 void
146 Book::add_scores_to_bookpart ()
148 if (scm_is_pair (scores_))
150 /* If scores have been added to this book, add them to a child
151 * book part */
152 Book *part = new Book;
153 part->set_parent (this);
154 part->scores_ = scores_;
155 bookparts_ = scm_cons (part->self_scm (), bookparts_);
156 part->unprotect ();
157 scores_ = SCM_EOL;
161 void
162 Book::add_bookpart (SCM b)
164 add_scores_to_bookpart ();
165 Book *part = unsmob_book (b);
166 part->set_parent (this);
167 bookparts_ = scm_cons (b, bookparts_);
170 bool
171 Book::error_found ()
173 for (SCM s = scores_; scm_is_pair (s); s = scm_cdr (s))
174 if (Score *score = unsmob_score (scm_car (s)))
175 if (score->error_found_)
176 return true;
178 for (SCM part = bookparts_; scm_is_pair (part); part = scm_cdr (part))
179 if (Book *bookpart = unsmob_book (scm_car (part)))
180 if (bookpart->error_found ())
181 return true;
183 return false;
186 Paper_book *
187 Book::process (Output_def *default_paper,
188 Output_def *default_layout)
190 return process (default_paper, default_layout, 0);
193 void
194 Book::process_bookparts (Paper_book *output_paper_book, Output_def *paper, Output_def *layout)
196 add_scores_to_bookpart ();
197 for (SCM p = scm_reverse (bookparts_); scm_is_pair (p); p = scm_cdr (p))
199 if (Book *book = unsmob_book (scm_car (p)))
201 Paper_book *paper_book_part = book->process (paper, layout, output_paper_book);
202 if (paper_book_part)
204 output_paper_book->add_bookpart (paper_book_part->self_scm ());
205 paper_book_part->unprotect ();
209 /* In a Paper_book, bookparts are stored in straight order */
210 output_paper_book->bookparts_ = scm_reverse_x (output_paper_book->bookparts_, SCM_EOL);
213 void
214 Book::process_score (SCM s, Paper_book *output_paper_book, Output_def *layout)
216 if (Score *score = unsmob_score (scm_car (s)))
218 SCM outputs = score
219 ->book_rendering (output_paper_book->paper_, layout);
221 while (scm_is_pair (outputs))
223 Music_output *output = unsmob_music_output (scm_car (outputs));
225 if (Performance *perf = dynamic_cast<Performance *> (output))
226 output_paper_book->add_performance (perf->self_scm ());
227 else if (Paper_score *pscore = dynamic_cast<Paper_score *> (output))
229 if (ly_is_module (score->get_header ()))
230 output_paper_book->add_score (score->get_header ());
231 output_paper_book->add_score (pscore->self_scm ());
234 outputs = scm_cdr (outputs);
237 else if (Text_interface::is_markup_list (scm_car (s))
238 || unsmob_page_marker (scm_car (s)))
239 output_paper_book->add_score (scm_car (s));
240 else
241 assert (0);
245 /* Concatenate all score or book part outputs into a Paper_book
247 Paper_book *
248 Book::process (Output_def *default_paper,
249 Output_def *default_layout,
250 Paper_book *parent_part)
252 Output_def *paper = paper_ ? paper_ : default_paper;
254 /* If top book, recursively check score errors */
255 if (!parent_part && error_found ())
256 return 0;
258 if (!paper)
259 return 0;
261 Paper_book *paper_book = new Paper_book ();
262 Real scale = scm_to_double (paper->c_variable ("output-scale"));
263 Output_def *scaled_bookdef = scale_output_def (paper, scale);
264 paper_book->paper_ = scaled_bookdef;
265 if (parent_part)
267 paper_book->parent_ = parent_part;
268 paper_book->paper_->parent_ = parent_part->paper_;
270 paper_book->header_ = header_;
271 scaled_bookdef->unprotect ();
273 if (scm_is_pair (bookparts_))
275 /* Process children book parts */
276 process_bookparts (paper_book, paper, default_layout);
278 else
280 paper_book->paper_->normalize ();
281 /* Process scores */
282 /* Render in order of parsing. */
283 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
285 process_score (s, paper_book, default_layout);
289 return paper_book;