]> 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--2011 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 Score::Score ()
48 {
49   header_ = SCM_EOL;
50   music_ = SCM_EOL;
51   input_location_ = SCM_EOL;
52
53   error_found_ = false;
54
55   smobify_self ();
56   input_location_ = make_input (Input ());
57 }
58
59 Score::~Score ()
60 {
61 }
62
63 IMPLEMENT_SMOBS (Score);
64 IMPLEMENT_DEFAULT_EQUAL_P (Score);
65 IMPLEMENT_TYPE_P (Score, "ly:score?");
66
67 SCM
68 Score::mark_smob (SCM s)
69 {
70   Score *sc = (Score *) SCM_CELL_WORD_1 (s);
71
72   scm_gc_mark (sc->header_);
73   for (vsize i = sc->defs_.size (); i--;)
74     scm_gc_mark (sc->defs_[i]->self_scm ());
75
76   scm_gc_mark (sc->input_location_);
77   return sc->music_;
78 }
79
80 int
81 Score::print_smob (SCM, SCM p, scm_print_state *)
82 {
83   scm_puts ("#<Score>", p);
84
85   return 1;
86 }
87
88 Score::Score (Score const &s)
89 {
90   header_ = SCM_EOL;
91   music_ = SCM_EOL;
92   input_location_ = SCM_EOL;
93   error_found_ = s.error_found_;
94
95   smobify_self ();
96   input_location_ = make_input (*s.origin ());
97
98   Music *m = unsmob_music (s.music_);
99   if (m)
100     {
101       Music *mclone = m->clone ();
102       music_ = mclone->unprotect ();
103     }
104   else
105     music_ = SCM_EOL;
106
107   for (vsize i = 0, n = s.defs_.size (); i < n; i++)
108     {
109       Output_def *copy = s.defs_[i]->clone ();
110       defs_.push_back (copy);
111       copy->unprotect ();
112     }
113   header_ = ly_make_module (false);
114   if (ly_is_module (s.header_))
115     ly_module_copy (header_, s.header_);
116 }
117
118 /*
119   Format score, return list of Music_output objects.
120
121   LAYOUTBOOK should be scaled already.
122 */
123 SCM
124 Score::book_rendering (Output_def *layoutbook,
125                        Output_def *default_def)
126 {
127   if (error_found_)
128     return SCM_EOL;
129
130   SCM scaled_bookdef = SCM_EOL;
131   Real scale = 1.0;
132
133   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
134     scale = scm_to_double (layoutbook->c_variable ("output-scale"));
135
136   SCM outputs = SCM_EOL;
137   SCM *tail = &outputs;
138
139   int outdef_count = defs_.size ();
140
141   for (int i = 0; !i || i < outdef_count; i++)
142     {
143       Output_def *def = outdef_count ? defs_[i] : default_def;
144       SCM scaled = SCM_EOL;
145
146       if (def->c_variable ("is-layout") == SCM_BOOL_T)
147         {
148           def = scale_output_def (def, scale);
149           def->parent_ = layoutbook;
150
151           scaled = def->unprotect ();
152         }
153
154       /* TODO: fix or junk --no-layout.  */
155       SCM context = ly_run_translator (music_, def->self_scm ());
156       if (dynamic_cast<Global_context *> (unsmob_context (context)))
157         {
158           SCM s = ly_format_output (context);
159
160           *tail = scm_cons (s, SCM_EOL);
161           tail = SCM_CDRLOC (*tail);
162         }
163
164       scm_remember_upto_here_1 (scaled);
165     }
166
167   scm_remember_upto_here_1 (scaled_bookdef);
168   return outputs;
169 }
170
171 void
172 Score::set_music (SCM music)
173 {
174   if (unsmob_music (music_))
175     {
176       unsmob_music (music)->origin ()->error (_ ("already have music in score"));
177       unsmob_music (music_)->origin ()->error (_ ("this is the previous music"));
178     }
179   Music *m = unsmob_music (music);
180   if (m && to_boolean (m->get_property ("error-found")))
181     {
182       m->origin ()->error (_ ("errors found, ignoring music expression"));
183
184       this->error_found_ = this->error_found_
185                            || to_boolean (m->get_property ("error-found"));
186     }
187
188   if (this->error_found_)
189     this->music_ = SCM_EOL;
190   else
191     this->music_ = music;
192 }
193
194 SCM
195 Score::get_music () const
196 {
197   return music_;
198 }
199
200 void
201 Score::add_output_def (Output_def *def)
202 {
203   defs_.push_back (def);
204 }
205
206 SCM
207 Score::get_header () const
208 {
209   return header_;
210 }
211
212 void
213 Score::set_header (SCM module)
214 {
215   assert (ly_is_module (module));
216   header_ = module;
217 }