]> git.donarmstrong.com Git - lilypond.git/blob - lily/book.cc
Merge with master
[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
24 #include "ly-smobs.icc"
25
26 Book::Book ()
27 {
28   paper_ = 0;
29   header_ = SCM_EOL;
30   scores_ = SCM_EOL;
31   input_location_ = SCM_EOL;
32   smobify_self ();
33
34   input_location_ = make_input (Input ());
35 }
36
37 Book::Book (Book const &s)
38 {
39   paper_ = 0;
40   header_ = SCM_EOL;
41   scores_ = SCM_EOL;
42   input_location_ = SCM_EOL;
43   smobify_self ();
44
45   if (s.paper_)
46     {
47       paper_ = s.paper_->clone ();
48       paper_->unprotect ();
49     }
50   
51   input_location_ = make_input (*s.origin ());
52
53   header_ = ly_make_anonymous_module (false);
54   if (ly_is_module (s.header_))
55     ly_module_copy (header_, s.header_);
56   
57   SCM *t = &scores_;
58   for (SCM p = s.scores_; scm_is_pair (p); p = scm_cdr (p))
59     {
60       Score *newscore = unsmob_score (scm_car (p))->clone ();
61
62       *t = scm_cons (newscore->self_scm (), SCM_EOL);
63       t = SCM_CDRLOC (*t);
64       newscore->unprotect ();
65     }
66 }
67
68 Input *
69 Book::origin () const
70 {
71   return unsmob_input (input_location_);
72 }
73
74 Book::~Book ()
75 {
76 }
77
78 IMPLEMENT_SMOBS (Book);
79 IMPLEMENT_DEFAULT_EQUAL_P (Book);
80
81 SCM
82 Book::mark_smob (SCM s)
83 {
84   Book *book = (Book *) SCM_CELL_WORD_1 (s);
85
86   if (book->paper_)
87     scm_gc_mark (book->paper_->self_scm ());
88   scm_gc_mark (book->scores_);
89   scm_gc_mark (book->input_location_);
90   
91   return book->header_;
92 }
93
94 int
95 Book::print_smob (SCM, SCM p, scm_print_state*)
96 {
97   scm_puts ("#<Book>", p);
98   return 1;
99 }
100
101 void
102 Book::add_score (SCM s)
103 {
104   scores_ = scm_cons (s, scores_);
105 }
106
107
108 /* Concatenate all score outputs into a Paper_book
109  */
110 Paper_book *
111 Book::process (Output_def *default_paper,
112                Output_def *default_layout)
113 {
114   for (SCM s = scores_; scm_is_pair (s); s = scm_cdr (s))
115     if (Score *score = unsmob_score (scm_car (s)))
116       if (score->error_found_)
117         return 0;
118
119   Output_def *paper = paper_ ? paper_ : default_paper;
120   if (!paper)
121     return 0;
122   
123   Paper_book *paper_book = new Paper_book ();
124   Real scale = scm_to_double (paper->c_variable ("output-scale"));
125   Output_def *scaled_bookdef = scale_output_def (paper, scale);
126
127   paper_book->paper_ = scaled_bookdef;
128   scaled_bookdef->unprotect ();
129
130   paper_book->header_ = header_;
131
132   /* Render in order of parsing.  */
133   for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
134     {
135       if (Score *score = unsmob_score (scm_car (s)))
136         {
137           SCM outputs = score
138             ->book_rendering (paper_book->paper_, default_layout);
139
140           while (scm_is_pair (outputs))
141             {
142               Music_output *output = unsmob_music_output (scm_car (outputs));
143
144               if (Performance *perf = dynamic_cast<Performance *> (output))
145                 paper_book->add_performance (perf->self_scm ());
146               else if (Paper_score *pscore = dynamic_cast<Paper_score *> (output))
147                 {
148                   if (ly_is_module (score->header_))
149                     paper_book->add_score (score->header_);
150                   paper_book->add_score (pscore->self_scm ());
151                 }
152
153               outputs = scm_cdr (outputs);
154             }
155         }
156       else if (Text_interface::is_markup (scm_car (s)))
157         paper_book->add_score (scm_car (s));
158       else
159         assert (0);
160     }
161
162   return paper_book;
163 }
164