]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[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--2007 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 "lilypond-key.hh"
21 #include "main.hh"
22 #include "music.hh"
23 #include "music.hh"
24 #include "output-def.hh"
25 #include "paper-book.hh"
26 #include "paper-score.hh"
27 #include "warn.hh"
28
29 #include "ly-smobs.icc"
30
31 Input *
32 Score::origin () const
33 {
34   return unsmob_input (input_location_);
35 }
36
37
38 Score::Score ()
39 {
40   header_ = SCM_EOL;
41   music_ = SCM_EOL;
42   input_location_ = SCM_EOL;
43
44   error_found_ = false;
45
46   smobify_self ();
47   input_location_ = make_input (Input ());
48 }
49
50 Score::~Score ()
51 {
52 }
53
54 IMPLEMENT_SMOBS (Score);
55 IMPLEMENT_DEFAULT_EQUAL_P (Score);
56 IMPLEMENT_TYPE_P (Score, "ly:score?");
57
58 SCM
59 Score::mark_smob (SCM s)
60 {
61   Score *sc = (Score *) SCM_CELL_WORD_1 (s);
62
63   scm_gc_mark (sc->header_);
64   for (vsize i = sc->defs_.size (); i--;)
65     scm_gc_mark (sc->defs_[i]->self_scm ());
66
67   scm_gc_mark (sc->input_location_);
68   return sc->music_;
69 }
70
71 int
72 Score::print_smob (SCM, SCM p, scm_print_state*)
73 {
74   scm_puts ("#<Score>", p);
75
76   return 1;
77 }
78
79 Score::Score (Score const &s)
80 {
81   header_ = SCM_EOL;
82   music_ = SCM_EOL;
83   input_location_ = SCM_EOL;
84   error_found_ = s.error_found_;
85
86   smobify_self ();
87   input_location_ = make_input (*s.origin ()); 
88
89   Music *m = unsmob_music (s.music_);
90   if (m)
91     {
92       Music *mclone = m->clone ();
93       music_ = mclone->unprotect ();
94     }
95   else
96     music_ = SCM_EOL;
97
98   for (vsize i = 0, n = s.defs_.size (); i < n; i++)
99     {
100       Output_def *copy = s.defs_[i]->clone ();
101       defs_.push_back (copy);
102       copy->unprotect ();
103     }
104   header_ = ly_make_anonymous_module (false);
105   if (ly_is_module (s.header_))
106     ly_module_copy (header_, s.header_);
107 }
108
109
110 /*
111   Format score, return list of Music_output objects.
112
113   LAYOUTBOOK should be scaled already.
114 */
115 SCM
116 Score::book_rendering (Output_def *layoutbook,
117                        Output_def *default_def)
118 {
119   if (error_found_)
120     return SCM_EOL;
121
122   SCM scaled_bookdef = SCM_EOL;
123   Real scale = 1.0;
124
125   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
126     scale = scm_to_double (layoutbook->c_variable ("output-scale"));
127
128   SCM outputs = SCM_EOL;
129   SCM *tail = &outputs;
130
131   int outdef_count = defs_.size ();
132
133   for (int i = 0; !i || i < outdef_count; i++)
134     {
135       Output_def *def = outdef_count ? defs_[i] : default_def;
136       SCM scaled = SCM_EOL;
137
138       if (def->c_variable ("is-layout") == SCM_BOOL_T)
139         {
140           def = scale_output_def (def, scale);
141           def->parent_ = layoutbook;
142
143           scaled = def->unprotect ();
144         }
145
146       /* TODO: fix or junk --no-layout.  */
147       SCM context = ly_run_translator (music_, def->self_scm ());
148       if (dynamic_cast<Global_context *> (unsmob_context (context)))
149         {
150           SCM s = ly_format_output (context);
151
152           *tail = scm_cons (s, SCM_EOL);
153           tail = SCM_CDRLOC (*tail);
154         }
155
156       scm_remember_upto_here_1 (scaled);
157     }
158
159   scm_remember_upto_here_1 (scaled_bookdef);
160   return outputs;
161 }
162
163 void
164 Score::set_music (SCM music)
165 {
166   if (unsmob_music (music_))
167     {
168       unsmob_music (music)->origin ()->error (_ ("already have music in score"));
169       unsmob_music (music_)->origin ()->error (_ ("this is the previous music"));
170     }
171   Music *m = unsmob_music (music);
172   if (m && to_boolean (m->get_property ("error-found")))
173     {
174       m->origin ()->error (_ ("errors found, ignoring music expression"));
175
176       this->error_found_ = this->error_found_
177         || to_boolean (m->get_property ("error-found"));
178     }
179
180   if (this->error_found_)
181     this->music_ = SCM_EOL;
182   else
183     this->music_ = music;
184 }
185
186 SCM
187 Score::get_music () const
188 {
189   return music_;
190 }
191
192 void
193 Score::add_output_def (Output_def *def)
194 {
195   defs_.push_back (def);
196 }