]> git.donarmstrong.com Git - lilypond.git/blob - lily/book.cc
Merge branch 'master' into nested-bookparts
[lilypond.git] / lily / book.cc
1 /*
2   book.cc -- implement Book
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "book.hh"
10
11 #include <cstdio>
12 using namespace std;
13
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"
24
25 #include "ly-smobs.icc"
26
27 Book::Book ()
28 {
29   paper_ = 0;
30   header_ = SCM_EOL;
31   scores_ = SCM_EOL;
32   bookparts_ = SCM_EOL;
33   input_location_ = SCM_EOL;
34   smobify_self ();
35
36   input_location_ = make_input (Input ());
37 }
38
39 Book::Book (Book const &s)
40 {
41   paper_ = 0;
42   header_ = SCM_EOL;
43   scores_ = SCM_EOL;
44   bookparts_ = SCM_EOL;
45   input_location_ = SCM_EOL;
46   smobify_self ();
47
48   if (s.paper_)
49     {
50       paper_ = s.paper_->clone ();
51       paper_->unprotect ();
52     }
53   
54   input_location_ = make_input (*s.origin ());
55
56   header_ = ly_make_anonymous_module (false);
57   if (ly_is_module (s.header_))
58     ly_module_copy (header_, s.header_);
59   
60   SCM *t = &scores_;
61   for (SCM p = s.scores_; scm_is_pair (p); p = scm_cdr (p))
62     {
63       Score *newscore = unsmob_score (scm_car (p))->clone ();
64
65       *t = scm_cons (newscore->self_scm (), SCM_EOL);
66       t = SCM_CDRLOC (*t);
67       newscore->unprotect ();
68     }
69
70   t = &bookparts_;
71   for (SCM p = s.bookparts_; scm_is_pair (p); p = scm_cdr (p))
72     {
73       Book *newpart = unsmob_book (scm_car (p))->clone ();
74
75       *t = scm_cons (newpart->self_scm (), SCM_EOL);
76       t = SCM_CDRLOC (*t);
77       newpart->unprotect ();
78     }
79 }
80
81 Input *
82 Book::origin () const
83 {
84   return unsmob_input (input_location_);
85 }
86
87 Book::~Book ()
88 {
89 }
90
91 IMPLEMENT_SMOBS (Book);
92 IMPLEMENT_DEFAULT_EQUAL_P (Book);
93
94 SCM
95 Book::mark_smob (SCM s)
96 {
97   Book *book = (Book *) SCM_CELL_WORD_1 (s);
98
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_);
104   
105   return book->header_;
106 }
107
108 int
109 Book::print_smob (SCM, SCM p, scm_print_state*)
110 {
111   scm_puts ("#<Book>", p);
112   return 1;
113 }
114
115 void
116 Book::add_score (SCM s)
117 {
118   scores_ = scm_cons (s, scores_);
119 }
120
121 void
122 Book::set_parent (Book *parent)
123 {
124   if (!paper_)
125     {
126       paper_ = new Output_def ();
127       paper_->unprotect ();
128     }
129   paper_->parent_ = parent->paper_;
130
131   if ((header_ == SCM_EOL) && (scm_is_null (parent->bookparts_)))
132     {
133       /* If this is the first part, and it has no header, copy the 
134        * parent header */
135       header_ = ly_make_anonymous_module (false);
136       if (ly_is_module (parent->header_))
137         ly_module_copy (header_, parent->header_);
138     }
139 }
140
141 void
142 Book::add_bookpart ()
143 {
144   if (scm_is_pair (scores_))
145     {
146       /* If scores have been added to this book, add them to a child 
147        * book part */
148       Book *part = new Book;
149       part->set_parent (this);
150       part->scores_ = scores_;
151       bookparts_ = scm_cons (part->self_scm (), bookparts_);
152       part->unprotect ();
153       scores_ = SCM_EOL;
154     }
155 }
156
157 void
158 Book::add_bookpart (SCM b)
159 {
160   add_bookpart ();
161   Book *part = unsmob_book (b);
162   part->set_parent (this);
163   bookparts_ = scm_cons (b, bookparts_);
164 }
165
166 bool
167 Book::error_found ()
168 {
169   for (SCM s = scores_; scm_is_pair (s); s = scm_cdr (s))
170     if (Score *score = unsmob_score (scm_car (s)))
171       if (score->error_found_)
172         return true;
173   
174   for (SCM part = bookparts_; scm_is_pair (part); part = scm_cdr (part))
175     if (Book *bookpart = unsmob_book (scm_car (part)))
176       if (bookpart->error_found ())
177         return true;
178
179   return false;
180 }
181
182 Paper_book *
183 Book::process (Output_def *default_paper,
184                Output_def *default_layout)
185 {
186   return process (default_paper, default_layout, 0);
187 }
188
189 /* Concatenate all score or book part outputs into a Paper_book
190  */
191 Paper_book *
192 Book::process (Output_def *default_paper,
193                Output_def *default_layout,
194                Output_def *parent_paper)
195 {
196   Output_def *paper = paper_ ? paper_ : default_paper;
197
198   /* If top book, recursively check score errors */
199   if (!parent_paper && error_found ())
200     return 0;
201
202   if (!paper)
203     return 0;
204
205   Paper_book *paper_book = new Paper_book ();
206   Real scale = scm_to_double (paper->c_variable ("output-scale"));
207   Output_def *scaled_bookdef = scale_output_def (paper, scale);
208   paper_book->paper_ = scaled_bookdef;
209   if (parent_paper)
210     paper_book->paper_->parent_ = parent_paper;
211   paper_book->header_ = header_;
212
213   if (scm_is_pair (bookparts_))
214     {
215       /* Process children book parts */
216       add_bookpart ();
217       for (SCM p = scm_reverse (bookparts_); scm_is_pair (p); p = scm_cdr (p))
218         {
219           if (Book *book = unsmob_book (scm_car (p)))
220             {
221               Paper_book *paper_book_part = book
222                 ->process (paper, default_layout, paper_book->paper_);
223               if (paper_book_part)
224                 paper_book->add_bookpart (paper_book_part->self_scm ());
225             }
226         }
227     }
228   else
229     {
230       /* Process scores */
231       /* Render in order of parsing.  */
232       for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
233         {
234           if (Score *score = unsmob_score (scm_car (s)))
235             {
236               SCM outputs = score
237                 ->book_rendering (paper_book->paper_, default_layout);
238               
239               while (scm_is_pair (outputs))
240                 {
241                   Music_output *output = unsmob_music_output (scm_car (outputs));
242                   
243                   if (Performance *perf = dynamic_cast<Performance *> (output))
244                     paper_book->add_performance (perf->self_scm ());
245                   else if (Paper_score *pscore = dynamic_cast<Paper_score *> (output))
246                     {
247                       if (ly_is_module (score->get_header ()))
248                         paper_book->add_score (score->get_header ());
249                       paper_book->add_score (pscore->self_scm ());
250                     }
251                   
252                   outputs = scm_cdr (outputs);
253                 }
254             }
255           else if (Text_interface::is_markup_list (scm_car (s))
256                    || unsmob_page_marker (scm_car (s)))
257             paper_book->add_score (scm_car (s));
258           else
259             assert (0);
260         }
261     }
262
263   return paper_book;
264 }