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