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