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