]> git.donarmstrong.com Git - lilypond.git/blob - lily/book.cc
* scm/midi.scm (paper-book-write-midis): new function. Write all
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "book.hh"
10
11 #include <cstdio>
12
13 #include "lilypond-key.hh"
14 #include "global-context.hh"
15 #include "main.hh"
16 #include "music-iterator.hh"
17 #include "music-output.hh"
18 #include "music.hh"
19 #include "output-def.hh"
20 #include "paper-book.hh"
21 #include "score.hh"
22 #include "stencil.hh"
23 #include "text-interface.hh"
24 #include "warn.hh"
25
26 #include "performance.hh"
27 #include "paper-score.hh"
28
29 #include "ly-smobs.icc"
30
31 Book::Book ()
32   : Input ()
33 {
34   paper_ = 0;
35   header_ = SCM_EOL;
36   scores_ = SCM_EOL;
37   smobify_self ();
38 }
39
40 Book::~Book ()
41 {
42 }
43
44 IMPLEMENT_SMOBS (Book);
45 IMPLEMENT_DEFAULT_EQUAL_P (Book);
46
47 SCM
48 Book::mark_smob (SCM s)
49 {
50   Book *book = (Book *) SCM_CELL_WORD_1 (s);
51
52 #if 0
53   if (book->key_)
54     scm_gc_mark (book->key_->self_scm ());
55 #endif
56
57   if (book->paper_)
58     scm_gc_mark (book->paper_->self_scm ());
59   scm_gc_mark (book->scores_);
60
61   return book->header_;
62 }
63
64 int
65 Book::print_smob (SCM, SCM p, scm_print_state*)
66 {
67   scm_puts ("#<Book>", p);
68   return 1;
69 }
70
71 void
72 Book::add_score (SCM s)
73 {
74   scores_ = scm_cons (s, scores_);
75 }
76
77 /* Concatenate all score outputs into a Paper_book
78    
79  */
80 Paper_book *
81 Book::process (Output_def *default_paper,
82                Output_def *default_layout)
83 {
84   for (SCM s = scores_; s != SCM_EOL; s = scm_cdr (s))
85     if (Score *score = unsmob_score (scm_car (s)))
86       if (score->error_found_)
87         return 0;
88
89   Output_def *paper = paper_ ? default_paper : paper_;
90   
91   Paper_book *paper_book = new Paper_book ();
92   Real scale = scm_to_double (paper->c_variable ("outputscale"));
93   Output_def *scaled_bookdef = scale_output_def (paper, scale);
94
95   Object_key *key = new Lilypond_general_key (0, user_key_, 0);
96   SCM scm_key = key->self_scm ();
97   scm_gc_unprotect_object (scm_key);
98
99   paper_book->paper_ = scaled_bookdef;
100   scm_gc_unprotect_object (scaled_bookdef->self_scm ());
101
102   paper_book->header_ = header_;
103
104   /* Render in order of parsing.  */
105   for (SCM s = scm_reverse (scores_); s != SCM_EOL; s = scm_cdr (s))
106     {
107       if (Score *score = unsmob_score (scm_car (s)))
108         {
109           SCM outputs = score
110             ->book_rendering (paper_book->paper_, default_layout, key);
111
112           while (scm_is_pair (outputs))
113             {
114               Music_output *output = unsmob_music_output (scm_car (outputs));
115
116               if (Performance *perf = dynamic_cast<Performance *> (output))
117                 {
118                   paper_book->add_performance (perf->self_scm ());
119                 }
120               else if (Paper_score *pscore = dynamic_cast<Paper_score *> (output)) 
121                 {
122                   SCM systems = pscore->get_paper_systems ();
123                   if (ly_is_module (score->header_))
124                     paper_book->add_score (score->header_);
125                   paper_book->add_score (systems);
126                 }
127               
128               outputs = scm_cdr (outputs);
129             }
130         }
131       else if (Text_interface::markup_p (scm_car (s)))
132         paper_book->add_score (scm_car (s));
133       else
134         assert (0);
135     }
136
137   scm_remember_upto_here_1 (scm_key);
138   return paper_book;
139 }