]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context-scheme.cc
03c992303d7377885eada7c6625cfb4ac1dc9f16
[lilypond.git] / lily / global-context-scheme.cc
1 /*
2   global-context-scheme.cc -- implement Global_context bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
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"
19
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.")
25 {
26   Global_context *g = dynamic_cast<Global_context *> (unsmob_context (context));
27   
28   LY_ASSERT_TYPE (unsmob_global_context, context, 1)
29
30   SCM output = g->get_output ();
31   progress_indication ("\n");
32
33   if (Music_output *od = unsmob_music_output (output))
34     od->process ();
35   
36   return output;
37 }
38
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.")
43 {
44   Global_context *g = dynamic_cast<Global_context *> (unsmob_context (global));
45   LY_ASSERT_TYPE (unsmob_global_context, global, 1)
46
47   Translator_group *tg = new Translator_group ();
48   tg->connect_to_context (g);
49   g->implementation_ = tg;
50
51   return tg->unprotect ();
52 }
53
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.")
58 {
59   LY_ASSERT_SMOB (Output_def, output_def, 1);
60   Output_def *odef = unsmob_output_def (output_def); 
61
62   Global_context *glob = new Global_context (odef);
63
64   if (!glob)
65     {
66       programming_error ("no toplevel translator");
67       return SCM_BOOL_F;
68     }
69
70   return glob->unprotect ();
71 }
72
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.")
77 {
78   LY_ASSERT_SMOB (Music, mus, 1);
79   LY_ASSERT_TYPE (unsmob_global_context, ctx, 2);
80
81   Music *music = unsmob_music (mus);
82   if (!music
83       || !music->get_length ().to_bool ())
84     {
85       warning (_ ("no music found in score"));
86       return SCM_BOOL_F;
87     }
88
89   Global_context *g = dynamic_cast<Global_context *> (unsmob_context (ctx));
90
91   Cpu_timer timer;
92
93   message (_ ("Interpreting music... "));
94
95   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
96   Music_iterator *iter = unsmob_iterator (protected_iter);
97
98   iter->init_context (music, g);
99   iter->construct_children ();
100
101   if (!iter->ok ())
102     {
103       warning (_ ("no music found in score"));
104       /* todo: should throw exception. */
105       return SCM_BOOL_F;
106     }
107
108   g->run_iterator_on_me (iter);
109
110   iter->quit ();
111   scm_remember_upto_here_1 (protected_iter);
112
113   send_stream_event (g, "Finish", 0, 0);
114
115   if (be_verbose_global)
116     message (_f ("elapsed time: %.2f seconds", timer.read ()));
117
118   return ctx;
119 }
120
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.")
130 {
131   LY_ASSERT_SMOB (Music, mus, 1);
132   LY_ASSERT_SMOB (Output_def, output_def, 2);
133
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;
138 }