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