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