]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
Web-ja: update introduction
[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 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 "ly-module.hh"
32 #include "main.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 unsmob<Input> (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 * const Score::type_p_name_ = "ly:score?";
63
64 SCM
65 Score::mark_smob () const
66 {
67   scm_gc_mark (header_);
68   for (vsize i = defs_.size (); i--;)
69     scm_gc_mark (defs_[i]->self_scm ());
70
71   scm_gc_mark (input_location_);
72   return music_;
73 }
74
75 Score::Score (Score const &s)
76   : Smob<Score> ()
77 {
78   header_ = SCM_EOL;
79   music_ = SCM_EOL;
80   input_location_ = SCM_EOL;
81   error_found_ = s.error_found_;
82
83   smobify_self ();
84   input_location_ = s.origin ()->smobbed_copy ();
85
86   Music *m = unsmob<Music> (s.music_);
87   if (m)
88     {
89       Music *mclone = m->clone ();
90       music_ = mclone->unprotect ();
91     }
92   else
93     music_ = SCM_EOL;
94
95   for (vsize i = 0, n = s.defs_.size (); i < n; i++)
96     {
97       Output_def *copy = s.defs_[i]->clone ();
98       defs_.push_back (copy);
99       copy->unprotect ();
100     }
101   header_ = ly_make_module (false);
102   if (ly_is_module (s.header_))
103     ly_module_copy (header_, s.header_);
104 }
105
106 /*
107   Format score, return list of Music_output objects.
108
109   LAYOUTBOOK should be scaled already.
110 */
111 SCM
112 Score::book_rendering (Output_def *layoutbook,
113                        Output_def *default_def)
114 {
115   if (error_found_)
116     return SCM_EOL;
117
118   Real scale = 1.0;
119
120   if (layoutbook && to_boolean (layoutbook->c_variable ("is-paper")))
121     scale = scm_to_double (layoutbook->c_variable ("output-scale"));
122
123   SCM outputs = SCM_EOL;
124
125   int outdef_count = defs_.size ();
126
127   for (int i = 0; !i || i < outdef_count; i++)
128     {
129       Output_def *def = outdef_count ? defs_[i] : default_def;
130       SCM scaled = def->self_scm ();
131
132       if (to_boolean (def->c_variable ("is-layout")))
133         {
134           def = scale_output_def (def, scale);
135           def->parent_ = layoutbook;
136
137           scaled = def->unprotect ();
138         }
139
140       /* TODO: fix or junk --no-layout.  */
141       SCM context = ly_run_translator (music_, scaled);
142       if (unsmob<Global_context> (context))
143         {
144           SCM s = ly_format_output (context);
145
146           outputs = scm_cons (s, outputs);
147         }
148
149       scm_remember_upto_here_1 (scaled);
150     }
151
152   return scm_reverse_x (outputs, SCM_EOL);
153 }
154
155 void
156 Score::set_music (SCM music)
157 {
158   if (unsmob<Music> (music_))
159     {
160       unsmob<Music> (music)->origin ()->non_fatal_error
161         (_ ("already have music in score"));
162       unsmob<Music> (music_)->origin ()->non_fatal_error
163         (_ ("this is the previous music"));
164     }
165   Music *m = unsmob<Music> (music);
166   if (m && to_boolean (m->get_property ("error-found")))
167     {
168       m->origin ()->non_fatal_error
169         (_ ("errors found, ignoring music expression"));
170
171       error_found_ = true;
172     }
173
174   if (error_found_)
175     music_ = SCM_EOL;
176   else
177     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 }