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