]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
* lily/output-def-scheme.cc: new file.
[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--2004 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-module.hh"
19 #include "ly-smobs.icc"
20 #include "main.hh"
21 #include "music-iterator.hh"
22 #include "output-def.hh"
23 #include "music.hh"
24 #include "paper-book.hh"
25 #include "output-def.hh"
26 #include "paper-score.hh"
27 #include "scm-hash.hh"
28 #include "warn.hh"
29
30 Score::Score ()
31   : Input ()
32 {
33   header_ = SCM_EOL;
34   music_ = SCM_EOL;
35   error_found_ = false;
36   smobify_self ();
37 }
38
39 Score::~Score ()
40 {
41 }
42
43 IMPLEMENT_SMOBS (Score);
44 IMPLEMENT_DEFAULT_EQUAL_P (Score);
45 IMPLEMENT_TYPE_P (Score, "ly:score?");
46
47 SCM
48 Score::mark_smob (SCM s)
49 {
50   Score *sc = (Score*) SCM_CELL_WORD_1 (s);
51
52 #if 0 
53   if (sc->key_)
54     scm_gc_mark (sc->key_->self_scm());
55 #endif
56   
57   if (sc->header_)
58     scm_gc_mark (sc->header_);
59   for (int i = sc->defs_.size (); i--;)
60     scm_gc_mark (sc->defs_[i]->self_scm ());
61   return sc->music_;
62 }
63
64 int
65 Score::print_smob (SCM , SCM p, scm_print_state*)
66 {
67   scm_puts ("#<Score>", p);
68
69   return 1;
70 }
71
72 Score::Score (Score const &s)
73   : Input (s)
74 {
75   music_ = SCM_EOL;
76   error_found_ = s.error_found_;
77   
78   /* FIXME: SCM_EOL? */
79   header_ = 0;
80
81   smobify_self ();
82
83   Music *m = unsmob_music (s.music_);
84   music_ = m ? m->clone ()->self_scm () : SCM_EOL;
85   scm_gc_unprotect_object (music_);
86   
87   for (int i = 0; i < s.defs_.size (); i++)
88     defs_.push (s.defs_[i]->clone ());
89
90   header_ = ly_make_anonymous_module (false);
91   if (ly_c_module_p (s.header_))
92     ly_module_copy (header_, s.header_);
93 }
94
95
96 void
97 default_rendering (SCM music, SCM outdef,
98                    SCM book_outputdef,
99                    SCM header, SCM outname,
100                    SCM key)
101 {
102   SCM scaled_def = outdef;
103   SCM scaled_bookdef = book_outputdef;
104   
105   Output_def *bpd = unsmob_output_def (book_outputdef);
106
107   /* ugh.  */
108   if (bpd->c_variable ("is-paper") == SCM_BOOL_T)
109     {
110       Real scale = scm_to_double (bpd->c_variable ("outputscale"));
111       
112       Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
113       scaled_def = def->self_scm ();
114
115       scaled_bookdef = scale_output_def (bpd, scale)->self_scm ();
116       unsmob_output_def (scaled_def)->parent_
117         = unsmob_output_def (scaled_bookdef);
118       
119       scm_gc_unprotect_object (scaled_bookdef);
120       scm_gc_unprotect_object (scaled_def);
121     }
122   
123   SCM context = ly_run_translator (music, scaled_def, key);
124   if (Global_context *g = dynamic_cast<Global_context*>
125       (unsmob_context (context)))
126     {
127       SCM systems = ly_format_output (context, outname);
128       Music_output *output = g->get_output ();
129       if (systems != SCM_UNDEFINED)
130         {
131           /* ugh, this is strange, Paper_book without a Book object. */
132           Paper_book *paper_book = new Paper_book ();
133           paper_book->header_ = header;
134           paper_book->paper_ = unsmob_output_def (scaled_bookdef);
135           
136           Score_systems sc;
137           sc.systems_ = systems;
138           sc.header_ = header;
139
140           paper_book->score_systems_.push (sc);
141           
142           paper_book->classic_output (ly_scm2string (outname));
143           scm_gc_unprotect_object (paper_book->self_scm ());
144         }
145       delete output;
146     }
147
148   scm_remember_upto_here_1 (scaled_def);
149   scm_remember_upto_here_1 (scaled_bookdef);
150 }
151
152 /*
153 Format score, return systems. OUTNAME is still passed to create a midi
154 file.
155
156 LAYOUTBOOK should be scaled already.
157
158 */
159 SCM
160 Score::book_rendering (String outname,
161                        Output_def *layoutbook,
162                        Output_def *default_def,
163                        Object_key *book_key)
164 {
165   if (error_found_)
166     return SCM_EOL;
167    
168   SCM scaled_bookdef = SCM_EOL;
169   Real scale = 1.0;
170
171   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
172     scale = scm_to_double (layoutbook->c_variable ("outputscale"));
173   
174   SCM out = scm_makfrom0str (outname.to_str0 ());
175   SCM systems = SCM_EOL;
176   int outdef_count = defs_.size ();
177
178   Object_key * key = new Lilypond_general_key (book_key, user_key_, 0);
179   SCM scm_key = key->self_scm();
180   scm_gc_unprotect_object (scm_key);
181   
182   for (int i = 0; !i || i < outdef_count; i++)
183     {
184       Output_def *def = outdef_count ? defs_[i] : default_def;
185       SCM scaled = SCM_EOL;
186       if (def->c_variable ("is-layout") == SCM_BOOL_T)
187         {
188           def = scale_output_def (def, scale);
189           def->parent_ = layoutbook;
190           scaled = def->self_scm ();
191           scm_gc_unprotect_object (scaled);
192         }
193
194       /* TODO: fix or junk --no-layout.  */
195       SCM context = ly_run_translator (music_, def->self_scm (), scm_key);
196       if (dynamic_cast<Global_context*> (unsmob_context (context)))
197         {
198           SCM s = ly_format_output (context, out);
199           if (s != SCM_UNDEFINED)
200             systems = s;
201         }
202
203       scm_remember_upto_here_1 (scaled);
204     }
205
206   scm_remember_upto_here_1 (scm_key);
207   scm_remember_upto_here_1 (scaled_bookdef);
208   return systems;
209 }
210
211
212
213
214 LY_DEFINE (ly_score_embedded_format, "ly:score-embedded-format",
215            2, 1, 0, (SCM score, SCM layout, SCM key),
216            "Run @var{score} through @var{layout}, an output definition, "
217            "scaled to correct outputscale already, "
218            "return a list of layout-lines. "
219            "\nTake optional Object_key argument."
220            )
221 {
222   Score * sc = unsmob_score (score);
223   Output_def *od = unsmob_output_def (layout);
224
225   if (sc->error_found_)
226     {
227       return SCM_EOL;
228     }
229   
230   SCM_ASSERT_TYPE (sc, score, SCM_ARG1, __FUNCTION__, "Score");
231   SCM_ASSERT_TYPE (od, layout, SCM_ARG2, __FUNCTION__, "Output_def");
232
233   Output_def * score_def  = 0;
234
235   /* UGR, FIXME, these are default \layout blocks once again.  They
236      suck. */
237   for (int i = 0; !score_def && i < sc->defs_.size (); i++)
238     if (sc->defs_[i]->c_variable ("is-layout") == SCM_BOOL_T)
239       score_def = sc->defs_[i];
240
241   if (!score_def)
242     return scm_c_make_vector (0, SCM_EOL);
243       
244   score_def = score_def->clone ();
245   SCM prot = score_def->self_scm ();
246   scm_gc_unprotect_object (prot);
247
248   /* TODO: SCORE_DEF should be scaled according to OD->parent_ or OD
249      itself. */
250   score_def->parent_ = od;
251   
252   SCM context = ly_run_translator (sc->get_music (), score_def->self_scm (),
253                                    key);
254   SCM lines = ly_format_output (context, scm_makfrom0str ("<embedded>"));
255   
256   scm_remember_upto_here_1 (prot);
257   return lines;
258 }
259
260 void
261 Score::set_music (SCM music, SCM parser)
262 {
263   /* URG? */
264   SCM check_funcs = ly_lily_module_constant ("toplevel-music-functions");
265   for (; scm_is_pair (check_funcs); check_funcs = scm_cdr (check_funcs))
266     music = scm_call_2 (scm_car (check_funcs), music, parser);
267
268   if (unsmob_music (music_))
269     {
270       unsmob_music (music)->origin ()->error (_("Already have music in score"));
271       unsmob_music (music_)->origin ()->error (_("This is the previous music"));
272     }
273   Music * m = unsmob_music (music);
274   if (m && to_boolean (m->get_property ("error-found")))
275     {
276       m->origin()->error (_("Error found in this music expression. Ignoring it"));
277       
278       this->error_found_ = this->error_found_ || to_boolean (m->get_property ("error-found"));
279       
280     }
281
282   if (this->error_found_)
283     this->music_ = SCM_EOL; 
284   else
285     this->music_ = music;
286
287 }
288
289 SCM
290 Score::get_music () const
291 {
292   return music_;
293 }