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