]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / lily / score.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "score.hh"
21
22 #include <cstdio>
23
24 using namespace std;
25
26 #include "book.hh"
27 #include "cpu-timer.hh"
28 #include "global-context.hh"
29 #include "international.hh"
30 #include "lily-parser.hh"
31 #include "main.hh"
32 #include "music.hh"
33 #include "music.hh"
34 #include "output-def.hh"
35 #include "paper-book.hh"
36 #include "paper-score.hh"
37 #include "warn.hh"
38
39 #include "ly-smobs.icc"
40
41 Input *
42 Score::origin () const
43 {
44   return unsmob_input (input_location_);
45 }
46
47
48 Score::Score ()
49 {
50   header_ = SCM_EOL;
51   music_ = SCM_EOL;
52   input_location_ = SCM_EOL;
53
54   error_found_ = false;
55
56   smobify_self ();
57   input_location_ = make_input (Input ());
58 }
59
60 Score::~Score ()
61 {
62 }
63
64 IMPLEMENT_SMOBS (Score);
65 IMPLEMENT_DEFAULT_EQUAL_P (Score);
66 IMPLEMENT_TYPE_P (Score, "ly:score?");
67
68 SCM
69 Score::mark_smob (SCM s)
70 {
71   Score *sc = (Score *) SCM_CELL_WORD_1 (s);
72
73   scm_gc_mark (sc->header_);
74   for (vsize i = sc->defs_.size (); i--;)
75     scm_gc_mark (sc->defs_[i]->self_scm ());
76
77   scm_gc_mark (sc->input_location_);
78   return sc->music_;
79 }
80
81 int
82 Score::print_smob (SCM, SCM p, scm_print_state*)
83 {
84   scm_puts ("#<Score>", p);
85
86   return 1;
87 }
88
89 Score::Score (Score const &s)
90 {
91   header_ = SCM_EOL;
92   music_ = SCM_EOL;
93   input_location_ = SCM_EOL;
94   error_found_ = s.error_found_;
95
96   smobify_self ();
97   input_location_ = make_input (*s.origin ()); 
98
99   Music *m = unsmob_music (s.music_);
100   if (m)
101     {
102       Music *mclone = m->clone ();
103       music_ = mclone->unprotect ();
104     }
105   else
106     music_ = SCM_EOL;
107
108   for (vsize i = 0, n = s.defs_.size (); i < n; i++)
109     {
110       Output_def *copy = s.defs_[i]->clone ();
111       defs_.push_back (copy);
112       copy->unprotect ();
113     }
114   header_ = ly_make_anonymous_module (false);
115   if (ly_is_module (s.header_))
116     ly_module_copy (header_, s.header_);
117 }
118
119
120 /*
121   Format score, return list of Music_output objects.
122
123   LAYOUTBOOK should be scaled already.
124 */
125 SCM
126 Score::book_rendering (Output_def *layoutbook,
127                        Output_def *default_def)
128 {
129   if (error_found_)
130     return SCM_EOL;
131
132   SCM scaled_bookdef = SCM_EOL;
133   Real scale = 1.0;
134
135   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
136     scale = scm_to_double (layoutbook->c_variable ("output-scale"));
137
138   SCM outputs = SCM_EOL;
139   SCM *tail = &outputs;
140
141   int outdef_count = defs_.size ();
142
143   for (int i = 0; !i || i < outdef_count; i++)
144     {
145       Output_def *def = outdef_count ? defs_[i] : default_def;
146       SCM scaled = SCM_EOL;
147
148       if (def->c_variable ("is-layout") == SCM_BOOL_T)
149         {
150           def = scale_output_def (def, scale);
151           def->parent_ = layoutbook;
152
153           scaled = def->unprotect ();
154         }
155
156       /* TODO: fix or junk --no-layout.  */
157       SCM context = ly_run_translator (music_, def->self_scm ());
158       if (dynamic_cast<Global_context *> (unsmob_context (context)))
159         {
160           SCM s = ly_format_output (context);
161
162           *tail = scm_cons (s, SCM_EOL);
163           tail = SCM_CDRLOC (*tail);
164         }
165
166       scm_remember_upto_here_1 (scaled);
167     }
168
169   scm_remember_upto_here_1 (scaled_bookdef);
170   return outputs;
171 }
172
173 void
174 Score::set_music (SCM music)
175 {
176   if (unsmob_music (music_))
177     {
178       unsmob_music (music)->origin ()->error (_ ("already have music in score"));
179       unsmob_music (music_)->origin ()->error (_ ("this is the previous music"));
180     }
181   Music *m = unsmob_music (music);
182   if (m && to_boolean (m->get_property ("error-found")))
183     {
184       m->origin ()->error (_ ("errors found, ignoring music expression"));
185
186       this->error_found_ = this->error_found_
187         || to_boolean (m->get_property ("error-found"));
188     }
189
190   if (this->error_found_)
191     this->music_ = SCM_EOL;
192   else
193     this->music_ = music;
194 }
195
196 SCM
197 Score::get_music () const
198 {
199   return music_;
200 }
201
202 void
203 Score::add_output_def (Output_def *def)
204 {
205   defs_.push_back (def);
206 }
207
208 SCM
209 Score::get_header () const
210 {
211   return header_;
212 }
213
214 void
215 Score::set_header (SCM module)
216 {
217   assert (ly_is_module (module));
218   header_ = module;
219 }