Workaround for broken MusicXML files (percussion clef in MuseScore)
[lilypond.git] / lily / global-context-scheme.cc
blobd672178a54c3fda156dcc294ac348839596edcda
1 /*
2 global-context-scheme.cc -- implement Global_context bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "cpu-timer.hh"
10 #include "global-context.hh"
11 #include "international.hh"
12 #include "main.hh"
13 #include "music-iterator.hh"
14 #include "music-output.hh"
15 #include "music.hh"
16 #include "output-def.hh"
17 #include "translator-group.hh"
18 #include "warn.hh"
20 LY_DEFINE (ly_format_output, "ly:format-output",
21 1, 0, 0, (SCM context),
22 "Given a global context in its final state,"
23 " process it and return the @code{Music_output} object"
24 " in its final state.")
26 Global_context *g = dynamic_cast<Global_context *> (unsmob_context (context));
28 LY_ASSERT_TYPE (unsmob_global_context, context, 1)
30 SCM output = g->get_output ();
31 progress_indication ("\n");
33 if (Music_output *od = unsmob_music_output (output))
34 od->process ();
36 return output;
39 LY_DEFINE (ly_make_global_translator, "ly:make-global-translator",
40 1, 0, 0, (SCM global),
41 "Create a translator group and connect it to the global context"
42 " @var{global}. The translator group is returned.")
44 Global_context *g = dynamic_cast<Global_context *> (unsmob_context (global));
45 LY_ASSERT_TYPE (unsmob_global_context, global, 1)
47 Translator_group *tg = new Translator_group ();
48 tg->connect_to_context (g);
49 g->implementation_ = tg;
51 return tg->unprotect ();
54 LY_DEFINE (ly_make_global_context, "ly:make-global-context",
55 1, 0, 0, (SCM output_def),
56 "Set up a global interpretation context, using the output"
57 " block @var{output_def}. The context is returned.")
59 LY_ASSERT_SMOB (Output_def, output_def, 1);
60 Output_def *odef = unsmob_output_def (output_def);
62 Global_context *glob = new Global_context (odef);
64 if (!glob)
66 programming_error ("no toplevel translator");
67 return SCM_BOOL_F;
70 return glob->unprotect ();
73 LY_DEFINE (ly_interpret_music_expression, "ly:interpret-music-expression",
74 2, 0, 0, (SCM mus, SCM ctx),
75 "Interpret the music expression @var{mus} in the global context"
76 " @var{ctx}. The context is returned in its final state.")
78 LY_ASSERT_SMOB (Music, mus, 1);
79 LY_ASSERT_TYPE (unsmob_global_context, ctx, 2);
81 Music *music = unsmob_music (mus);
82 if (!music
83 || !music->get_length ().to_bool ())
85 warning (_ ("no music found in score"));
86 return SCM_BOOL_F;
89 Global_context *g = dynamic_cast<Global_context *> (unsmob_context (ctx));
91 Cpu_timer timer;
93 message (_ ("Interpreting music... "));
95 SCM protected_iter = Music_iterator::get_static_get_iterator (music);
96 Music_iterator *iter = unsmob_iterator (protected_iter);
98 iter->init_context (music, g);
99 iter->construct_children ();
101 if (!iter->ok ())
103 warning (_ ("no music found in score"));
104 /* todo: should throw exception. */
105 return SCM_BOOL_F;
108 g->run_iterator_on_me (iter);
110 iter->quit ();
111 scm_remember_upto_here_1 (protected_iter);
113 send_stream_event (g, "Finish", 0, 0);
115 if (be_verbose_global)
116 message (_f ("elapsed time: %.2f seconds", timer.read ()));
118 return ctx;
121 LY_DEFINE (ly_run_translator, "ly:run-translator",
122 2, 1, 0, (SCM mus, SCM output_def),
123 "Process @var{mus} according to @var{output-def}. An"
124 " interpretation context is set up, and @var{mus} is"
125 " interpreted with it. The context is returned in its"
126 " final state.\n"
127 "\n"
128 "Optionally, this routine takes an object-key to"
129 " to uniquely identify the score block containing it.")
131 LY_ASSERT_SMOB (Music, mus, 1);
132 LY_ASSERT_SMOB (Output_def, output_def, 2);
134 SCM glob = ly_make_global_context (output_def);
135 ly_make_global_translator (glob);
136 ly_interpret_music_expression (mus, glob);
137 return glob;