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