]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
* lily/modified-font-metric.cc (text_dimension): try
[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 LY_DEFINE (ly_run_translator, "ly:run-translator", 
97            2, 1, 0, (SCM mus, SCM output_def, SCM key),
98            "Process @var{mus} according to @var{output_def}. \n"
99            "An interpretation context is set up,\n"
100            "and @var{mus} is interpreted with it.  \n"
101            "The context is returned in its final state.\n"
102
103            "\n\nOptionally, this routine takes an Object-key to\n"
104            "to uniquely identify the Score block containing it.\n")
105 {
106   Output_def *odef = unsmob_output_def (output_def);
107   Music *music = unsmob_music (mus);
108
109   if (!music
110       || !music->get_length ().to_bool ())
111     {
112       warning (_ ("Need music in a score"));
113       return SCM_BOOL_F;
114     }
115   
116   SCM_ASSERT_TYPE (music, mus, SCM_ARG1,
117                    __FUNCTION__, "Music");
118   SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__,
119                    "Output definition");
120   
121   Cpu_timer timer;
122   
123   Global_context *trans = new Global_context (odef, music->get_length (), unsmob_key (key) );
124   if (!trans)
125     {
126       programming_error ("no toplevel translator");
127       return SCM_BOOL_F;
128     }
129
130   progress_indication (_ ("Interpreting music... "));
131   
132   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
133   Music_iterator * iter = unsmob_iterator (protected_iter);
134   iter->init_translator (music, trans);
135
136   iter->construct_children ();
137
138   if (!iter->ok ())
139     {
140       warning (_ ("Need music in a score"));
141       /* todo: should throw exception. */
142       return SCM_BOOL_F;
143     }
144
145   trans->run_iterator_on_me (iter);
146   iter->quit ();
147   scm_remember_upto_here_1 (protected_iter);
148   trans->finish ();
149
150   if (verbose_global_b)
151     progress_indication (_f ("elapsed time: %.2f seconds",  timer.read ()));
152   
153   return scm_gc_unprotect_object (trans->self_scm ());
154 }
155
156 LY_DEFINE (ly_format_output, "ly:format-output",
157            2, 0, 0, (SCM context, SCM outname),
158            "Given a Score context in its final state,"
159            "process it and return the (rendered) result.")
160 {
161   Global_context *g = dynamic_cast<Global_context*> (unsmob_context (context));
162   SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
163   SCM_ASSERT_TYPE (scm_is_string (outname), outname, SCM_ARG2, __FUNCTION__, "output file name");
164
165   Music_output *output = g->get_output ();
166   progress_indication ("\n");
167   /* ugh, midi still wants outname  */
168   return output->process (ly_scm2string (outname));
169 }
170
171 void
172 default_rendering (SCM music, SCM outdef,
173                    SCM book_outputdef,
174                    SCM header, SCM outname,
175                    SCM key)
176 {
177   SCM scaled_def = outdef;
178   SCM scaled_bookdef = book_outputdef;
179   
180   Output_def *bpd = unsmob_output_def (book_outputdef);
181
182   /* ugh.  */
183   if (bpd->c_variable ("is-paper") == SCM_BOOL_T)
184     {
185       Real scale = scm_to_double (bpd->c_variable ("outputscale"));
186       
187       Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
188       scaled_def = def->self_scm ();
189
190       scaled_bookdef = scale_output_def (bpd, scale)->self_scm ();
191       unsmob_output_def (scaled_def)->parent_
192         = unsmob_output_def (scaled_bookdef);
193       
194       scm_gc_unprotect_object (scaled_bookdef);
195       scm_gc_unprotect_object (scaled_def);
196     }
197   
198   SCM context = ly_run_translator (music, scaled_def, key);
199   if (Global_context *g = dynamic_cast<Global_context*>
200       (unsmob_context (context)))
201     {
202       SCM systems = ly_format_output (context, outname);
203       Music_output *output = g->get_output ();
204       if (systems != SCM_UNDEFINED)
205         {
206           /* ugh, this is strange, Paper_book without a Book object. */
207           Paper_book *paper_book = new Paper_book ();
208           paper_book->header_ = header;
209           paper_book->paper_ = unsmob_output_def (scaled_bookdef);
210           
211           Score_systems sc;
212           sc.systems_ = systems;
213           sc.header_ = header;
214
215           paper_book->score_systems_.push (sc);
216           
217           paper_book->classic_output (ly_scm2string (outname));
218           scm_gc_unprotect_object (paper_book->self_scm ());
219         }
220       delete output;
221     }
222
223   scm_remember_upto_here_1 (scaled_def);
224   scm_remember_upto_here_1 (scaled_bookdef);
225 }
226
227 /*
228 Format score, return systems. OUTNAME is still passed to create a midi
229 file.
230
231 LAYOUTBOOK should be scaled already.
232
233 */
234 SCM
235 Score::book_rendering (String outname,
236                        Output_def *layoutbook,
237                        Output_def *default_def,
238                        Object_key *book_key)
239 {
240   if (error_found_)
241     return SCM_EOL;
242    
243   SCM scaled_bookdef = SCM_EOL;
244   Real scale = 1.0;
245
246   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
247     scale = scm_to_double (layoutbook->c_variable ("outputscale"));
248   
249   SCM out = scm_makfrom0str (outname.to_str0 ());
250   SCM systems = SCM_EOL;
251   int outdef_count = defs_.size ();
252
253   Object_key * key = new Lilypond_general_key (book_key, user_key_, 0);
254   SCM scm_key = key->self_scm();
255   scm_gc_unprotect_object (scm_key);
256   
257   for (int i = 0; !i || i < outdef_count; i++)
258     {
259       Output_def *def = outdef_count ? defs_[i] : default_def;
260       SCM scaled = SCM_EOL;
261       if (def->c_variable ("is-layout") == SCM_BOOL_T)
262         {
263           def = scale_output_def (def, scale);
264           def->parent_ = layoutbook;
265           scaled = def->self_scm ();
266           scm_gc_unprotect_object (scaled);
267         }
268
269       /* TODO: fix or junk --no-layout.  */
270       SCM context = ly_run_translator (music_, def->self_scm (), scm_key);
271       if (dynamic_cast<Global_context*> (unsmob_context (context)))
272         {
273           SCM s = ly_format_output (context, out);
274           if (s != SCM_UNDEFINED)
275             systems = s;
276         }
277
278       scm_remember_upto_here_1 (scaled);
279     }
280
281   scm_remember_upto_here_1 (scm_key);
282   scm_remember_upto_here_1 (scaled_bookdef);
283   return systems;
284 }
285
286
287
288
289 LY_DEFINE (ly_score_embedded_format, "ly:score-embedded-format",
290            2, 1, 0, (SCM score, SCM layout, SCM key),
291            "Run @var{score} through @var{layout}, an output definition, "
292            "scaled to correct outputscale already, "
293            "return a list of layout-lines. "
294            "\nTake optional Object_key argument."
295            )
296 {
297   Score * sc = unsmob_score (score);
298   Output_def *od = unsmob_output_def (layout);
299
300   if (sc->error_found_)
301     {
302       return SCM_EOL;
303     }
304   
305   SCM_ASSERT_TYPE (sc, score, SCM_ARG1, __FUNCTION__, "Score");
306   SCM_ASSERT_TYPE (od, layout, SCM_ARG2, __FUNCTION__, "Output_def");
307
308   Output_def * score_def  = 0;
309
310   /* UGR, FIXME, these are default \layout blocks once again.  They
311      suck. */
312   for (int i = 0; !score_def && i < sc->defs_.size (); i++)
313     if (sc->defs_[i]->c_variable ("is-layout") == SCM_BOOL_T)
314       score_def = sc->defs_[i];
315
316   if (!score_def)
317     return scm_c_make_vector (0, SCM_EOL);
318       
319   score_def = score_def->clone ();
320   SCM prot = score_def->self_scm ();
321   scm_gc_unprotect_object (prot);
322
323   /* TODO: SCORE_DEF should be scaled according to OD->parent_ or OD
324      itself. */
325   score_def->parent_ = od;
326   
327   SCM context = ly_run_translator (sc->get_music (), score_def->self_scm (),
328                                    key);
329   SCM lines = ly_format_output (context, scm_makfrom0str ("<embedded>"));
330   
331   scm_remember_upto_here_1 (prot);
332   return lines;
333 }
334
335 void
336 Score::set_music (SCM music, SCM parser)
337 {
338   /* URG? */
339   SCM check_funcs = ly_lily_module_constant ("toplevel-music-functions");
340   for (; scm_is_pair (check_funcs); check_funcs = scm_cdr (check_funcs))
341     music = scm_call_2 (scm_car (check_funcs), music, parser);
342
343   if (unsmob_music (music_))
344     {
345       unsmob_music (music)->origin ()->error (_("Already have music in score"));
346       unsmob_music (music_)->origin ()->error (_("This is the previous music"));
347     }
348   Music * m = unsmob_music (music);
349   if (m && to_boolean (m->get_property ("error-found")))
350     {
351       m->origin()->error (_("Error found in this music expression. Ignoring it"));
352       
353       this->error_found_ = this->error_found_ || to_boolean (m->get_property ("error-found"));
354       
355     }
356
357   if (this->error_found_)
358     this->music_ = SCM_EOL; 
359   else
360     this->music_ = music;
361
362 }
363
364 SCM
365 Score::get_music () const
366 {
367   return music_;
368 }