]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
Run `make grand-replace'.
[lilypond.git] / lily / score.cc
1 /*
2   score.cc -- implement Score
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "score.hh"
10
11 #include <cstdio>
12
13 using namespace std;
14
15 #include "book.hh"
16 #include "cpu-timer.hh"
17 #include "global-context.hh"
18 #include "international.hh"
19 #include "lily-parser.hh"
20 #include "main.hh"
21 #include "music.hh"
22 #include "music.hh"
23 #include "output-def.hh"
24 #include "paper-book.hh"
25 #include "paper-score.hh"
26 #include "warn.hh"
27
28 #include "ly-smobs.icc"
29
30 Input *
31 Score::origin () const
32 {
33   return unsmob_input (input_location_);
34 }
35
36
37 Score::Score ()
38 {
39   header_ = SCM_EOL;
40   music_ = SCM_EOL;
41   input_location_ = SCM_EOL;
42
43   error_found_ = false;
44
45   smobify_self ();
46   input_location_ = make_input (Input ());
47 }
48
49 Score::~Score ()
50 {
51 }
52
53 IMPLEMENT_SMOBS (Score);
54 IMPLEMENT_DEFAULT_EQUAL_P (Score);
55 IMPLEMENT_TYPE_P (Score, "ly:score?");
56
57 SCM
58 Score::mark_smob (SCM s)
59 {
60   Score *sc = (Score *) SCM_CELL_WORD_1 (s);
61
62   scm_gc_mark (sc->header_);
63   for (vsize i = sc->defs_.size (); i--;)
64     scm_gc_mark (sc->defs_[i]->self_scm ());
65
66   scm_gc_mark (sc->input_location_);
67   return sc->music_;
68 }
69
70 int
71 Score::print_smob (SCM, SCM p, scm_print_state*)
72 {
73   scm_puts ("#<Score>", p);
74
75   return 1;
76 }
77
78 Score::Score (Score const &s)
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_ = make_input (*s.origin ()); 
87
88   Music *m = unsmob_music (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_anonymous_module (false);
104   if (ly_is_module (s.header_))
105     ly_module_copy (header_, s.header_);
106 }
107
108
109 /*
110   Format score, return list of Music_output objects.
111
112   LAYOUTBOOK should be scaled already.
113 */
114 SCM
115 Score::book_rendering (Output_def *layoutbook,
116                        Output_def *default_def)
117 {
118   if (error_found_)
119     return SCM_EOL;
120
121   SCM scaled_bookdef = SCM_EOL;
122   Real scale = 1.0;
123
124   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
125     scale = scm_to_double (layoutbook->c_variable ("output-scale"));
126
127   SCM outputs = SCM_EOL;
128   SCM *tail = &outputs;
129
130   int outdef_count = defs_.size ();
131
132   for (int i = 0; !i || i < outdef_count; i++)
133     {
134       Output_def *def = outdef_count ? defs_[i] : default_def;
135       SCM scaled = SCM_EOL;
136
137       if (def->c_variable ("is-layout") == SCM_BOOL_T)
138         {
139           def = scale_output_def (def, scale);
140           def->parent_ = layoutbook;
141
142           scaled = def->unprotect ();
143         }
144
145       /* TODO: fix or junk --no-layout.  */
146       SCM context = ly_run_translator (music_, def->self_scm ());
147       if (dynamic_cast<Global_context *> (unsmob_context (context)))
148         {
149           SCM s = ly_format_output (context);
150
151           *tail = scm_cons (s, SCM_EOL);
152           tail = SCM_CDRLOC (*tail);
153         }
154
155       scm_remember_upto_here_1 (scaled);
156     }
157
158   scm_remember_upto_here_1 (scaled_bookdef);
159   return outputs;
160 }
161
162 void
163 Score::set_music (SCM music)
164 {
165   if (unsmob_music (music_))
166     {
167       unsmob_music (music)->origin ()->error (_ ("already have music in score"));
168       unsmob_music (music_)->origin ()->error (_ ("this is the previous music"));
169     }
170   Music *m = unsmob_music (music);
171   if (m && to_boolean (m->get_property ("error-found")))
172     {
173       m->origin ()->error (_ ("errors found, ignoring music expression"));
174
175       this->error_found_ = this->error_found_
176         || to_boolean (m->get_property ("error-found"));
177     }
178
179   if (this->error_found_)
180     this->music_ = SCM_EOL;
181   else
182     this->music_ = music;
183 }
184
185 SCM
186 Score::get_music () const
187 {
188   return music_;
189 }
190
191 void
192 Score::add_output_def (Output_def *def)
193 {
194   defs_.push_back (def);
195 }
196
197 SCM
198 Score::get_header () const
199 {
200   return header_;
201 }
202
203 void
204 Score::set_header (SCM module)
205 {
206   assert (ly_is_module (module));
207   header_ = module;
208 }