]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
Distangle classic output routines.
[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--2006 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                        Object_key *book_key)
119 {
120   if (error_found_)
121     return SCM_EOL;
122
123   SCM scaled_bookdef = SCM_EOL;
124   Real scale = 1.0;
125
126   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
127     scale = scm_to_double (layoutbook->c_variable ("output-scale"));
128
129   SCM outputs = SCM_EOL;
130   SCM *tail = &outputs;
131
132   int outdef_count = defs_.size ();
133
134   Object_key *key = new Lilypond_general_key (book_key, user_key_, 0);
135   SCM scm_key = key->unprotect ();
136
137   for (int i = 0; !i || i < outdef_count; i++)
138     {
139       Output_def *def = outdef_count ? defs_[i] : default_def;
140       SCM scaled = SCM_EOL;
141
142       if (def->c_variable ("is-layout") == SCM_BOOL_T)
143         {
144           def = scale_output_def (def, scale);
145           def->parent_ = layoutbook;
146
147           scaled = def->unprotect ();
148         }
149
150       /* TODO: fix or junk --no-layout.  */
151       SCM context = ly_run_translator (music_, def->self_scm (), scm_key);
152       if (dynamic_cast<Global_context *> (unsmob_context (context)))
153         {
154           SCM s = ly_format_output (context);
155
156           *tail = scm_cons (s, SCM_EOL);
157           tail = SCM_CDRLOC (*tail);
158         }
159
160       scm_remember_upto_here_1 (scaled);
161     }
162
163   scm_remember_upto_here_1 (scm_key);
164   scm_remember_upto_here_1 (scaled_bookdef);
165   return outputs;
166 }
167
168 void
169 Score::set_music (SCM music)
170 {
171   if (unsmob_music (music_))
172     {
173       unsmob_music (music)->origin ()->error (_ ("already have music in score"));
174       unsmob_music (music_)->origin ()->error (_ ("this is the previous music"));
175     }
176   Music *m = unsmob_music (music);
177   if (m && to_boolean (m->get_property ("error-found")))
178     {
179       m->origin ()->error (_ ("errors found, ignoring music expression"));
180
181       this->error_found_ = this->error_found_
182         || to_boolean (m->get_property ("error-found"));
183     }
184
185   if (this->error_found_)
186     this->music_ = SCM_EOL;
187   else
188     this->music_ = music;
189 }
190
191 SCM
192 Score::get_music () const
193 {
194   return music_;
195 }
196
197 void
198 Score::add_output_def (Output_def *def)
199 {
200   defs_.push_back (def);
201 }