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