]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / score.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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   Real scale = 1.0;
131
132   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
133     scale = scm_to_double (layoutbook->c_variable ("output-scale"));
134
135   SCM outputs = SCM_EOL;
136
137   int outdef_count = defs_.size ();
138
139   for (int i = 0; !i || i < outdef_count; i++)
140     {
141       Output_def *def = outdef_count ? defs_[i] : default_def;
142       SCM scaled = def->self_scm ();
143
144       if (def->c_variable ("is-layout") == SCM_BOOL_T)
145         {
146           def = scale_output_def (def, scale);
147           def->parent_ = layoutbook;
148
149           scaled = def->unprotect ();
150         }
151
152       /* TODO: fix or junk --no-layout.  */
153       SCM context = ly_run_translator (music_, scaled);
154       if (dynamic_cast<Global_context *> (unsmob_context (context)))
155         {
156           SCM s = ly_format_output (context);
157
158           outputs = scm_cons (s, outputs);
159         }
160
161       scm_remember_upto_here_1 (scaled);
162     }
163
164   return scm_reverse_x (outputs, SCM_EOL);
165 }
166
167 void
168 Score::set_music (SCM music)
169 {
170   if (unsmob_music (music_))
171     {
172       unsmob_music (music)->origin ()->error (_ ("already have music in score"));
173       unsmob_music (music_)->origin ()->error (_ ("this is the previous music"));
174     }
175   Music *m = unsmob_music (music);
176   if (m && to_boolean (m->get_property ("error-found")))
177     {
178       m->origin ()->error (_ ("errors found, ignoring music expression"));
179
180       this->error_found_ = this->error_found_
181                            || to_boolean (m->get_property ("error-found"));
182     }
183
184   if (this->error_found_)
185     this->music_ = SCM_EOL;
186   else
187     this->music_ = music;
188 }
189
190 SCM
191 Score::get_music () const
192 {
193   return music_;
194 }
195
196 void
197 Score::add_output_def (Output_def *def)
198 {
199   defs_.push_back (def);
200 }
201
202 SCM
203 Score::get_header () const
204 {
205   return header_;
206 }
207
208 void
209 Score::set_header (SCM module)
210 {
211   assert (ly_is_module (module));
212   header_ = module;
213 }