]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
Issue 4550 (2/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / score.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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
25 #include "book.hh"
26 #include "cpu-timer.hh"
27 #include "global-context.hh"
28 #include "international.hh"
29 #include "lily-parser.hh"
30 #include "ly-module.hh"
31 #include "main.hh"
32 #include "music.hh"
33 #include "output-def.hh"
34 #include "paper-book.hh"
35 #include "paper-score.hh"
36 #include "warn.hh"
37
38
39 Input *
40 Score::origin () const
41 {
42   return unsmob<Input> (input_location_);
43 }
44
45 Score::Score ()
46 {
47   header_ = SCM_EOL;
48   music_ = SCM_EOL;
49   input_location_ = SCM_EOL;
50
51   error_found_ = false;
52
53   smobify_self ();
54   input_location_ = Input ().smobbed_copy ();
55 }
56
57 Score::~Score ()
58 {
59 }
60
61 const char Score::type_p_name_[] = "ly:score?";
62
63 SCM
64 Score::mark_smob () const
65 {
66   scm_gc_mark (header_);
67   for (vsize i = defs_.size (); i--;)
68     scm_gc_mark (defs_[i]->self_scm ());
69
70   scm_gc_mark (input_location_);
71   return music_;
72 }
73
74 Score::Score (Score const &s)
75   : Smob<Score> ()
76 {
77   header_ = SCM_EOL;
78   music_ = SCM_EOL;
79   input_location_ = SCM_EOL;
80   error_found_ = s.error_found_;
81
82   smobify_self ();
83   input_location_ = s.origin ()->smobbed_copy ();
84
85   Music *m = unsmob<Music> (s.music_);
86   if (m)
87     {
88       Music *mclone = m->clone ();
89       music_ = mclone->unprotect ();
90     }
91   else
92     music_ = SCM_EOL;
93
94   for (vsize i = 0, n = s.defs_.size (); i < n; i++)
95     {
96       Output_def *copy = s.defs_[i]->clone ();
97       defs_.push_back (copy);
98       copy->unprotect ();
99     }
100   header_ = ly_make_module (false);
101   if (ly_is_module (s.header_))
102     ly_module_copy (header_, s.header_);
103 }
104
105 /*
106   Format score, return list of Music_output objects.
107
108   LAYOUTBOOK should be scaled already.
109 */
110 SCM
111 Score::book_rendering (Output_def *layoutbook,
112                        Output_def *default_def)
113 {
114   if (error_found_)
115     return SCM_EOL;
116
117   Real scale = 1.0;
118
119   if (layoutbook && to_boolean (layoutbook->c_variable ("is-paper")))
120     scale = scm_to_double (layoutbook->c_variable ("output-scale"));
121
122   SCM outputs = SCM_EOL;
123
124   int outdef_count = defs_.size ();
125
126   for (int i = 0; !i || i < outdef_count; i++)
127     {
128       Output_def *def = outdef_count ? defs_[i] : default_def;
129       SCM scaled = def->self_scm ();
130
131       if (to_boolean (def->c_variable ("is-layout")))
132         {
133           def = scale_output_def (def, scale);
134           def->parent_ = layoutbook;
135
136           scaled = def->unprotect ();
137         }
138
139       /* TODO: fix or junk --no-layout.  */
140       SCM context = ly_run_translator (music_, scaled);
141       if (unsmob<Global_context> (context))
142         {
143           SCM s = ly_format_output (context);
144
145           outputs = scm_cons (s, outputs);
146         }
147
148       scm_remember_upto_here_1 (scaled);
149     }
150
151   return scm_reverse_x (outputs, SCM_EOL);
152 }
153
154 void
155 Score::set_music (SCM music)
156 {
157   if (unsmob<Music> (music_))
158     {
159       unsmob<Music> (music)->origin ()->non_fatal_error
160         (_ ("already have music in score"));
161       unsmob<Music> (music_)->origin ()->non_fatal_error
162         (_ ("this is the previous music"));
163     }
164   Music *m = unsmob<Music> (music);
165   if (m && to_boolean (m->get_property ("error-found")))
166     {
167       m->origin ()->non_fatal_error
168         (_ ("errors found, ignoring music expression"));
169
170       error_found_ = true;
171     }
172
173   if (error_found_)
174     music_ = SCM_EOL;
175   else
176     music_ = music;
177 }
178
179 SCM
180 Score::get_music () const
181 {
182   return music_;
183 }
184
185 void
186 Score::add_output_def (Output_def *def)
187 {
188   defs_.push_back (def);
189 }
190
191 SCM
192 Score::get_header () const
193 {
194   return header_;
195 }
196
197 void
198 Score::set_header (SCM module)
199 {
200   assert (ly_is_module (module));
201   header_ = module;
202 }