]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context-scheme.cc
219dc267c1d3762f0a0c19f314f415833001f976
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8 #include "cpu-timer.hh"
9 #include "global-context.hh"
10 #include "international.hh"
11 #include "main.hh"
12 #include "music-iterator.hh"
13 #include "music-output.hh"
14 #include "music.hh"
15 #include "object-key.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 in its final state.")
24 {
25   Global_context *g = dynamic_cast<Global_context *> (unsmob_context (context));
26   SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
27
28   SCM output = g->get_output ();
29   progress_indication ("\n");
30
31   if (Music_output *od = unsmob_music_output (output))
32     od->process ();
33   
34   return output;
35 }
36
37 LY_DEFINE (ly_make_global_translator, "ly:make-global-translator",
38           1, 0, 0, (SCM global),
39           "Create a translator group and connect it to the global context\n"
40           "@var{global}. The translator group is returned.")
41 {
42   Global_context *g = dynamic_cast<Global_context *> (unsmob_context (global));
43   SCM_ASSERT_TYPE (g, global, SCM_ARG1, __FUNCTION__, "Global context");
44
45   Translator_group *tg = new Translator_group ();
46   tg->connect_to_context (g);
47   g->implementation_ = tg;
48
49   return tg->unprotect ();
50 }
51
52 LY_DEFINE (ly_make_global_context, "ly:make-global-context",
53            1, 1, 0, (SCM output_def, SCM key),
54            "Set up a global interpretation context, using the output\n"
55            "block @var{output_def}.\n"
56            "The context is returned.\n"
57
58            "\n\nOptionally, this routine takes an Object-key to\n"
59            "to uniquely identify the Score block containing it.\n")
60 {
61   Output_def *odef = unsmob_output_def (output_def);
62
63   SCM_ASSERT_TYPE (odef, output_def, SCM_ARG1, __FUNCTION__,
64                    "Output definition");
65
66   Global_context *glob = new Global_context (odef, unsmob_key (key));
67
68   if (!glob)
69     {
70       programming_error ("no toplevel translator");
71       return SCM_BOOL_F;
72     }
73
74   return glob->unprotect ();
75 }
76
77 LY_DEFINE (ly_interpret_music_expression, "ly:interpret-music-expression",
78            2, 0, 0, (SCM mus, SCM ctx),
79            "Interpret the music expression @var{mus} in the\n"
80            "global context @var{ctx}. The context is returned in its\n"
81            "final state.\n")
82 {
83   Music *music = unsmob_music (mus);
84   Global_context *g = dynamic_cast<Global_context *> (unsmob_context (ctx));
85   SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
86   SCM_ASSERT_TYPE (g, ctx, SCM_ARG2, __FUNCTION__, "Global context");
87
88   if (!music
89       || !music->get_length ().to_bool ())
90     {
91       warning (_ ("no music found in score"));
92       return SCM_BOOL_F;
93     }
94
95   Cpu_timer timer;
96
97   message (_ ("Interpreting music... "));
98
99   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
100   Music_iterator *iter = unsmob_iterator (protected_iter);
101
102   iter->init_context (music, g);
103   iter->construct_children ();
104
105   if (!iter->ok ())
106     {
107       warning (_ ("no music found in score"));
108       /* todo: should throw exception. */
109       return SCM_BOOL_F;
110     }
111
112   g->run_iterator_on_me (iter);
113
114   iter->quit ();
115   scm_remember_upto_here_1 (protected_iter);
116
117   send_stream_event (g, "Finish", 0, 0);
118
119   if (be_verbose_global)
120     message (_f ("elapsed time: %.2f seconds", timer.read ()));
121
122   return ctx;
123 }
124
125 LY_DEFINE (ly_run_translator, "ly:run-translator",
126            2, 1, 0, (SCM mus, SCM output_def, SCM key),
127            "Process @var{mus} according to @var{output_def}. \n"
128            "An interpretation context is set up,\n"
129            "and @var{mus} is interpreted with it.  \n"
130            "The context is returned in its final state.\n"
131            "\n\n"
132            "Optionally, this routine takes an Object-key to\n"
133            "to uniquely identify the Score block containing it.\n")
134 {
135   SCM glob = ly_make_global_context (output_def, key);
136   ly_make_global_translator (glob);
137   ly_interpret_music_expression (mus, glob);
138   return glob;
139 }