]> git.donarmstrong.com Git - lilypond.git/blob - lily/book.cc
Web-ja: update introduction
[lilypond.git] / lily / book.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 #include "ly-module.hh"
36
37 Book::Book ()
38 {
39   paper_ = 0;
40   header_ = SCM_EOL;
41   scores_ = SCM_EOL;
42   bookparts_ = SCM_EOL;
43   input_location_ = SCM_EOL;
44   smobify_self ();
45
46   input_location_ = Input ().smobbed_copy ();
47 }
48
49 Book::Book (Book const &s)
50   : Smob<Book> ()
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_ = s.origin ()->smobbed_copy ();
66
67   header_ = ly_make_module (false);
68   if (ly_is_module (s.header_))
69     ly_module_copy (header_, s.header_);
70   SCM *t = &scores_;
71   for (SCM p = s.scores_; scm_is_pair (p); p = scm_cdr (p))
72     {
73       SCM entry = scm_car (p);
74
75       if (Score *newscore = unsmob<Score> (entry))
76         * t = scm_cons (newscore->clone ()->unprotect (), SCM_EOL);
77       else if (Page_marker *marker = unsmob<Page_marker> (entry))
78         * t = scm_cons (marker->clone ()->unprotect (), SCM_EOL);
79       else
80         {
81           /* This entry is a markup list */
82           *t = scm_cons (entry, SCM_EOL);
83         }
84       t = SCM_CDRLOC (*t);
85     }
86
87   t = &bookparts_;
88   for (SCM p = s.bookparts_; scm_is_pair (p); p = scm_cdr (p))
89     {
90       Book *newpart = unsmob<Book> (scm_car (p))->clone ();
91
92       *t = scm_cons (newpart->self_scm (), SCM_EOL);
93       t = SCM_CDRLOC (*t);
94       newpart->unprotect ();
95     }
96 }
97
98 Input *
99 Book::origin () const
100 {
101   return unsmob<Input> (input_location_);
102 }
103
104 Book::~Book ()
105 {
106 }
107
108
109 SCM
110 Book::mark_smob () const
111 {
112   if (paper_)
113     scm_gc_mark (paper_->self_scm ());
114   scm_gc_mark (scores_);
115   scm_gc_mark (bookparts_);
116   scm_gc_mark (input_location_);
117
118   return header_;
119 }
120
121 void
122 Book::add_score (SCM s)
123 {
124   scores_ = scm_cons (s, scores_);
125 }
126
127 void
128 Book::set_parent (Book *parent)
129 {
130   if (!paper_)
131     {
132       paper_ = new Output_def ();
133       paper_->unprotect ();
134     }
135   paper_->parent_ = parent->paper_;
136   /* Copy the header block of the parent */
137   if (ly_is_module (parent->header_))
138     {
139       SCM tmp_header = ly_make_module (false);
140       ly_module_copy (tmp_header, parent->header_);
141       if (ly_is_module (header_))
142         ly_module_copy (tmp_header, header_);
143       header_ = tmp_header;
144     }
145 }
146
147 /* Before an explicit \bookpart is encountered, scores are added to the book.
148  * But once a bookpart is added, the previous scores shall be collected into
149  * a new bookpart.
150  */
151 void
152 Book::add_scores_to_bookpart ()
153 {
154   if (scm_is_pair (scores_))
155     {
156       /* If scores have been added to this book, add them to a child
157        * book part */
158       Book *part = new Book;
159       part->set_parent (this);
160       part->scores_ = scores_;
161       bookparts_ = scm_cons (part->self_scm (), bookparts_);
162       part->unprotect ();
163       scores_ = SCM_EOL;
164     }
165 }
166
167 void
168 Book::add_bookpart (SCM b)
169 {
170   add_scores_to_bookpart ();
171   Book *part = unsmob<Book> (b);
172   part->set_parent (this);
173   bookparts_ = scm_cons (b, bookparts_);
174 }
175
176 bool
177 Book::error_found ()
178 {
179   for (SCM s = scores_; scm_is_pair (s); s = scm_cdr (s))
180     if (Score *score = unsmob<Score> (scm_car (s)))
181       if (score->error_found_)
182         return true;
183
184   for (SCM part = bookparts_; scm_is_pair (part); part = scm_cdr (part))
185     if (Book *bookpart = unsmob<Book> (scm_car (part)))
186       if (bookpart->error_found ())
187         return true;
188
189   return false;
190 }
191
192 Paper_book *
193 Book::process (Output_def *default_paper,
194                Output_def *default_layout)
195 {
196   return process (default_paper, default_layout, 0);
197 }
198
199 void
200 Book::process_bookparts (Paper_book *output_paper_book, Output_def *paper, Output_def *layout)
201 {
202   add_scores_to_bookpart ();
203   for (SCM p = scm_reverse (bookparts_); scm_is_pair (p); p = scm_cdr (p))
204     {
205       if (Book *book = unsmob<Book> (scm_car (p)))
206         {
207           Paper_book *paper_book_part = book->process (paper, layout, output_paper_book);
208           if (paper_book_part)
209             {
210               output_paper_book->add_bookpart (paper_book_part->self_scm ());
211               paper_book_part->unprotect ();
212             }
213         }
214     }
215   /* In a Paper_book, bookparts are stored in straight order */
216   output_paper_book->bookparts_ = scm_reverse_x (output_paper_book->bookparts_, SCM_EOL);
217 }
218
219 void
220 Book::process_score (SCM s, Paper_book *output_paper_book, Output_def *layout)
221 {
222   if (Score *score = unsmob<Score> (scm_car (s)))
223     {
224       SCM outputs = score
225                     ->book_rendering (output_paper_book->paper_, layout);
226
227       while (scm_is_pair (outputs))
228         {
229           Music_output *output = unsmob<Music_output> (scm_car (outputs));
230
231           if (Performance *perf = dynamic_cast<Performance *> (output))
232             {
233               output_paper_book->add_performance (perf->self_scm ());
234               // Associate the performance with a \header block (if there is
235               // one in effect in the scope of the current score), to make the
236               // header metadata accessible when outputting the performance.
237               if (ly_is_module (score->get_header ()))
238                 perf->set_header (score->get_header ());
239               else if (ly_is_module (output_paper_book->header_))
240                 perf->set_header (output_paper_book->header_);
241               else if (ly_is_module (output_paper_book->header_0_))
242                 perf->set_header (output_paper_book->header_0_);
243             }
244           else if (Paper_score *pscore = dynamic_cast<Paper_score *> (output))
245             {
246               if (ly_is_module (score->get_header ()))
247                 output_paper_book->add_score (score->get_header ());
248               output_paper_book->add_score (pscore->self_scm ());
249             }
250
251           outputs = scm_cdr (outputs);
252         }
253     }
254   else if (Text_interface::is_markup_list (scm_car (s))
255            || unsmob<Page_marker> (scm_car (s)))
256     output_paper_book->add_score (scm_car (s));
257   else
258     assert (0);
259
260 }
261
262 /* Concatenate all score or book part outputs into a Paper_book
263  */
264 Paper_book *
265 Book::process (Output_def *default_paper,
266                Output_def *default_layout,
267                Paper_book *parent_part)
268 {
269   Output_def *paper = paper_ ? paper_ : default_paper;
270
271   /* If top book, recursively check score errors */
272   if (!parent_part && error_found ())
273     return 0;
274
275   if (!paper)
276     return 0;
277
278   Paper_book *paper_book = new Paper_book ();
279   Real scale = scm_to_double (paper->c_variable ("output-scale"));
280   Output_def *scaled_bookdef = scale_output_def (paper, scale);
281   paper_book->paper_ = scaled_bookdef;
282   if (parent_part)
283     {
284       paper_book->parent_ = parent_part;
285       paper_book->paper_->parent_ = parent_part->paper_;
286     }
287   paper_book->header_ = header_;
288   scaled_bookdef->unprotect ();
289
290   if (scm_is_pair (bookparts_))
291     {
292       /* Process children book parts */
293       process_bookparts (paper_book, paper, default_layout);
294     }
295   else
296     {
297       paper_book->paper_->normalize ();
298       /* Process scores */
299       /* Render in order of parsing.  */
300       for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
301         {
302           process_score (s, paper_book, default_layout);
303         }
304     }
305
306   return paper_book;
307 }