]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
*** empty log message ***
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "score.hh"
10
11 #include <cstdio>
12
13 #include "lilypond-key.hh"
14 #include "lily-parser.hh"
15 #include "book.hh"
16 #include "cpu-timer.hh"
17 #include "global-context.hh"
18 #include "ly-smobs.icc"
19 #include "main.hh"
20 #include "music-iterator.hh"
21 #include "output-def.hh"
22 #include "music.hh"
23 #include "paper-book.hh"
24 #include "output-def.hh"
25 #include "paper-score.hh"
26 #include "scm-hash.hh"
27 #include "warn.hh"
28
29 Score::Score ()
30   : Input ()
31 {
32   header_ = SCM_EOL;
33   music_ = SCM_EOL;
34   error_found_ = false;
35   smobify_self ();
36 }
37
38 Score::~Score ()
39 {
40 }
41
42 IMPLEMENT_SMOBS (Score);
43 IMPLEMENT_DEFAULT_EQUAL_P (Score);
44 IMPLEMENT_TYPE_P (Score, "ly:score?");
45
46 SCM
47 Score::mark_smob (SCM s)
48 {
49   Score *sc = (Score*) SCM_CELL_WORD_1 (s);
50
51 #if 0 
52   if (sc->key_)
53     scm_gc_mark (sc->key_->self_scm());
54 #endif
55   
56   if (sc->header_)
57     scm_gc_mark (sc->header_);
58   for (int i = sc->defs_.size (); i--;)
59     scm_gc_mark (sc->defs_[i]->self_scm ());
60   return sc->music_;
61 }
62
63 int
64 Score::print_smob (SCM , SCM p, scm_print_state*)
65 {
66   scm_puts ("#<Score>", p);
67
68   return 1;
69 }
70
71 Score::Score (Score const &s)
72   : Input (s)
73 {
74   music_ = SCM_EOL;
75   error_found_ = s.error_found_;
76   
77   /* FIXME: SCM_EOL? */
78   header_ = 0;
79
80   smobify_self ();
81
82   Music *m = unsmob_music (s.music_);
83   music_ = m ? m->clone ()->self_scm () : SCM_EOL;
84   scm_gc_unprotect_object (music_);
85   
86   for (int i = 0; i < s.defs_.size (); i++)
87     defs_.push (s.defs_[i]->clone ());
88
89   header_ = ly_make_anonymous_module (false);
90   if (ly_c_module_p (s.header_))
91     ly_module_copy (header_, s.header_);
92 }
93
94
95 void
96 default_rendering (SCM music, SCM outdef,
97                    SCM book_outputdef,
98                    SCM header, SCM outname,
99                    SCM key)
100 {
101   SCM scaled_def = outdef;
102   SCM scaled_bookdef = book_outputdef;
103   
104   Output_def *bpd = unsmob_output_def (book_outputdef);
105
106   /* ugh.  */
107   if (bpd->c_variable ("is-paper") == SCM_BOOL_T)
108     {
109       Real scale = scm_to_double (bpd->c_variable ("outputscale"));
110       
111       Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
112       scaled_def = def->self_scm ();
113
114       scaled_bookdef = scale_output_def (bpd, scale)->self_scm ();
115       unsmob_output_def (scaled_def)->parent_
116         = unsmob_output_def (scaled_bookdef);
117       
118       scm_gc_unprotect_object (scaled_bookdef);
119       scm_gc_unprotect_object (scaled_def);
120     }
121   
122   SCM context = ly_run_translator (music, scaled_def, key);
123   if (Global_context *g = dynamic_cast<Global_context*>
124       (unsmob_context (context)))
125     {
126       SCM systems = ly_format_output (context, outname);
127       Music_output *output = g->get_output ();
128       if (systems != SCM_UNDEFINED)
129         {
130           /* ugh, this is strange, Paper_book without a Book object. */
131           Paper_book *paper_book = new Paper_book ();
132           paper_book->header_ = header;
133           paper_book->paper_ = unsmob_output_def (scaled_bookdef);
134           
135           Score_systems sc;
136           sc.systems_ = systems;
137           sc.header_ = header;
138
139           paper_book->score_systems_.push (sc);
140           
141           paper_book->classic_output (ly_scm2string (outname));
142           scm_gc_unprotect_object (paper_book->self_scm ());
143         }
144       delete output;
145     }
146
147   scm_remember_upto_here_1 (scaled_def);
148   scm_remember_upto_here_1 (scaled_bookdef);
149 }
150
151 /*
152 Format score, return systems. OUTNAME is still passed to create a midi
153 file.
154
155 LAYOUTBOOK should be scaled already.
156
157 */
158 SCM
159 Score::book_rendering (String outname,
160                        Output_def *layoutbook,
161                        Output_def *default_def,
162                        Object_key *book_key)
163 {
164   if (error_found_)
165     return SCM_EOL;
166    
167   SCM scaled_bookdef = SCM_EOL;
168   Real scale = 1.0;
169
170   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
171     scale = scm_to_double (layoutbook->c_variable ("outputscale"));
172   
173   SCM out = scm_makfrom0str (outname.to_str0 ());
174   SCM systems = SCM_EOL;
175   int outdef_count = defs_.size ();
176
177   Object_key * key = new Lilypond_general_key (book_key, user_key_, 0);
178   SCM scm_key = key->self_scm();
179   scm_gc_unprotect_object (scm_key);
180   
181   for (int i = 0; !i || i < outdef_count; i++)
182     {
183       Output_def *def = outdef_count ? defs_[i] : default_def;
184       SCM scaled = SCM_EOL;
185       if (def->c_variable ("is-layout") == SCM_BOOL_T)
186         {
187           def = scale_output_def (def, scale);
188           def->parent_ = layoutbook;
189           scaled = def->self_scm ();
190           scm_gc_unprotect_object (scaled);
191         }
192
193       /* TODO: fix or junk --no-layout.  */
194       SCM context = ly_run_translator (music_, def->self_scm (), scm_key);
195       if (dynamic_cast<Global_context*> (unsmob_context (context)))
196         {
197           SCM s = ly_format_output (context, out);
198           if (s != SCM_UNDEFINED)
199             systems = s;
200         }
201
202       scm_remember_upto_here_1 (scaled);
203     }
204
205   scm_remember_upto_here_1 (scm_key);
206   scm_remember_upto_here_1 (scaled_bookdef);
207   return systems;
208 }
209
210
211
212
213
214 void
215 Score::set_music (SCM music, SCM parser)
216 {
217   /* URG? */
218   SCM check_funcs = ly_lily_module_constant ("toplevel-music-functions");
219   for (; scm_is_pair (check_funcs); check_funcs = scm_cdr (check_funcs))
220     music = scm_call_2 (scm_car (check_funcs), music, parser);
221
222   if (unsmob_music (music_))
223     {
224       unsmob_music (music)->origin ()->error (_("Already have music in score"));
225       unsmob_music (music_)->origin ()->error (_("This is the previous music"));
226     }
227   Music * m = unsmob_music (music);
228   if (m && to_boolean (m->get_property ("error-found")))
229     {
230       m->origin()->error (_("Error found in this music expression. Ignoring it"));
231       
232       this->error_found_ = this->error_found_ || to_boolean (m->get_property ("error-found"));
233       
234     }
235
236   if (this->error_found_)
237     this->music_ = SCM_EOL; 
238   else
239     this->music_ = music;
240
241 }
242
243 SCM
244 Score::get_music () const
245 {
246   return music_;
247 }