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