]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
(DECLARE_BASE_SMOBS): add methods
[lilypond.git] / lily / score.cc
1 /*
2   score.cc -- implement Score
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 "score.hh"
10
11 #include <cstdio>
12
13 #include "book.hh"
14 #include "cpu-timer.hh"
15 #include "global-context.hh"
16 #include "lily-parser.hh"
17 #include "lilypond-key.hh"
18 #include "main.hh"
19 #include "music.hh"
20 #include "output-def.hh"
21 #include "paper-book.hh"
22 #include "paper-score.hh"
23 #include "warn.hh"
24
25
26 #include "music.hh"
27 #include "ly-smobs.icc"
28
29 Score::Score ()
30   : Input ()
31 {
32   header_ = SCM_EOL;
33   music_ = SCM_EOL;
34   texts_ = SCM_EOL;
35   error_found_ = false;
36   smobify_self ();
37 }
38
39 Score::~Score ()
40 {
41 }
42
43 IMPLEMENT_SMOBS (Score);
44 IMPLEMENT_DEFAULT_EQUAL_P (Score);
45 IMPLEMENT_TYPE_P (Score, "ly:score?");
46
47 SCM
48 Score::mark_smob (SCM s)
49 {
50   Score *sc = (Score *) SCM_CELL_WORD_1 (s);
51
52   scm_gc_mark (sc->header_);
53   scm_gc_mark (sc->texts_);
54   for (int i = sc->defs_.size (); i--;)
55     scm_gc_mark (sc->defs_[i]->self_scm ());
56   return sc->music_;
57 }
58
59 int
60 Score::print_smob (SCM, SCM p, scm_print_state*)
61 {
62   scm_puts ("#<Score>", p);
63
64   return 1;
65 }
66
67 Score::Score (Score const &s)
68   : Input (s)
69 {
70   header_ = SCM_EOL;
71   music_ = SCM_EOL;
72   texts_ = SCM_EOL;
73   error_found_ = s.error_found_;
74   smobify_self ();
75
76   Music *m = unsmob_music (s.music_);
77   if (m)
78     {
79       Music *mclone =  m->clone ();
80       music_ = mclone->unprotect ();
81     }
82   else
83     music_ = SCM_EOL;
84
85   
86   for (int i = 0, n = s.defs_.size (); i < n; i++)
87     {
88       Output_def * copy = s.defs_[i]->clone ();
89       defs_.push (copy);
90       copy->unprotect ();
91     }
92   header_ = ly_make_anonymous_module (false);
93   if (ly_is_module (s.header_))
94     ly_module_copy (header_, s.header_);
95
96   texts_ = s.texts_;
97 }
98
99 void
100 default_rendering (SCM music, SCM outdef,
101                    SCM book_outputdef,
102                    SCM header,
103                    SCM outname,
104                    SCM key)
105 {
106   SCM scaled_def = outdef;
107   SCM scaled_bookdef = book_outputdef;
108
109   Output_def *bpd = unsmob_output_def (book_outputdef);
110
111   /* ugh.  */
112   if (bpd->c_variable ("is-paper") == SCM_BOOL_T)
113     {
114       Real scale = scm_to_double (bpd->c_variable ("outputscale"));
115
116       Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
117       Output_def *bdef = scale_output_def (bpd, scale);
118       def->parent_ = bdef;
119
120       scaled_def = def->self_scm ();
121       scaled_bookdef = bdef->self_scm();
122
123       def->unprotect();
124       bdef->unprotect ();  
125     }
126
127   SCM context = ly_run_translator (music, scaled_def, key);
128
129   SCM output_as_scm = ly_format_output (context);
130   Music_output *output = unsmob_music_output (output_as_scm);
131       
132   if (Paper_score *pscore = dynamic_cast<Paper_score*> (output))
133     {
134       /* ugh, this is strange, Paper_book without a Book object. */
135       Paper_book *paper_book = new Paper_book ();
136       paper_book->header_ = header;
137       paper_book->paper_ = unsmob_output_def (scaled_bookdef);
138
139       if (ly_is_module (header))
140         paper_book->add_score (header);
141
142       SCM systems = pscore->get_paper_systems ();
143       paper_book->add_score (systems);
144
145       paper_book->classic_output (outname);
146       paper_book->unprotect ();
147     }
148
149   scm_remember_upto_here_1 (scaled_def);
150   scm_remember_upto_here_1 (output_as_scm);
151   scm_remember_upto_here_1 (scaled_bookdef);
152 }
153
154 /*
155   Format score, return list of Music_output objects. 
156
157   LAYOUTBOOK should be scaled already.
158 */
159 SCM
160 Score::book_rendering (Output_def *layoutbook,
161                        Output_def *default_def,
162                        Object_key *book_key)
163 {
164   if (error_found_)
165     return SCM_EOL;
166
167   SCM scaled_bookdef = SCM_EOL;
168   Real scale = 1.0;
169
170   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
171     scale = scm_to_double (layoutbook->c_variable ("outputscale"));
172
173   SCM outputs = SCM_EOL;
174   SCM *tail = &outputs;
175   
176   int outdef_count = defs_.size ();
177
178   Object_key *key = new Lilypond_general_key (book_key, user_key_, 0);
179   SCM scm_key = key->unprotect ();
180
181   for (int i = 0; !i || i < outdef_count; i++)
182     {
183       Output_def *def = outdef_count ? defs_[i] : default_def;
184       SCM scaled = SCM_EOL;
185
186       if (def->c_variable ("is-layout") == SCM_BOOL_T)
187         {
188           def = scale_output_def (def, scale);
189           def->parent_ = layoutbook;
190
191           scaled = def->unprotect ();
192         }
193
194       /* TODO: fix or junk --no-layout.  */
195       SCM context = ly_run_translator (music_, def->self_scm (), scm_key);
196       if (dynamic_cast<Global_context *> (unsmob_context (context)))
197         {
198           SCM s = ly_format_output (context);
199           
200           *tail = scm_cons (s, SCM_EOL);
201           tail = SCM_CDRLOC(*tail);
202         }
203
204       scm_remember_upto_here_1 (scaled);
205     }
206
207   scm_remember_upto_here_1 (scm_key);
208   scm_remember_upto_here_1 (scaled_bookdef);
209   return outputs;
210 }
211
212 void
213 Score::set_music (SCM music)
214 {
215   if (unsmob_music (music_))
216     {
217       unsmob_music (music)->origin ()->error (_ ("already have music in score"));
218       unsmob_music (music_)->origin ()->error (_ ("this is the previous music"));
219     }
220   Music *m = unsmob_music (music);
221   if (m && to_boolean (m->get_property ("error-found")))
222     {
223       m->origin ()->error (_ ("errors found, ignoring music expression"));
224
225       this->error_found_ = this->error_found_
226         || to_boolean (m->get_property ("error-found"));
227     }
228
229   if (this->error_found_)
230     this->music_ = SCM_EOL;
231   else
232     this->music_ = music;
233 }
234
235 SCM
236 Score::get_music () const
237 {
238   return music_;
239 }