]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context-scheme.cc
Fix merging problem
[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 #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 "output-def.hh"
16 #include "translator-group.hh"
17 #include "warn.hh"
18
19 LY_DEFINE (ly_format_output, "ly:format-output",
20            1, 0, 0, (SCM context),
21            "Given a Global context in its final state, "
22            "process it and return the @code{Music_output} object in its final state.")
23 {
24   Global_context *g = dynamic_cast<Global_context *> (unsmob_context (context));
25   SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
26
27   SCM output = g->get_output ();
28   progress_indication ("\n");
29
30   if (Music_output *od = unsmob_music_output (output))
31     od->process ();
32   
33   return output;
34 }
35
36 LY_DEFINE (ly_make_global_translator, "ly:make-global-translator",
37           1, 0, 0, (SCM global),
38           "Create a translator group and connect it to the global context\n"
39           "@var{global}. The translator group is returned.")
40 {
41   Global_context *g = dynamic_cast<Global_context *> (unsmob_context (global));
42   SCM_ASSERT_TYPE (g, global, SCM_ARG1, __FUNCTION__, "Global context");
43
44   Translator_group *tg = new Translator_group ();
45   tg->connect_to_context (g);
46   g->implementation_ = tg;
47
48   return tg->unprotect ();
49 }
50
51 LY_DEFINE (ly_make_global_context, "ly:make-global-context",
52            1, 0, 0, (SCM output_def),
53            "Set up a global interpretation context, using the output\n"
54            "block @var{output_def}.\n"
55            "The context is returned.\n"
56            )
57 {
58   Output_def *odef = unsmob_output_def (output_def);
59
60   SCM_ASSERT_TYPE (odef, output_def, SCM_ARG1, __FUNCTION__,
61                    "Output definition");
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   Music *music = unsmob_music (mus);
81   Global_context *g = dynamic_cast<Global_context *> (unsmob_context (ctx));
82   SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
83   SCM_ASSERT_TYPE (g, ctx, SCM_ARG2, __FUNCTION__, "Global context");
84
85   if (!music
86       || !music->get_length ().to_bool ())
87     {
88       warning (_ ("no music found in score"));
89       return SCM_BOOL_F;
90     }
91
92   Cpu_timer timer;
93
94   message (_ ("Interpreting music... "));
95
96   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
97   Music_iterator *iter = unsmob_iterator (protected_iter);
98
99   iter->init_context (music, g);
100   iter->construct_children ();
101
102   if (!iter->ok ())
103     {
104       warning (_ ("no music found in score"));
105       /* todo: should throw exception. */
106       return SCM_BOOL_F;
107     }
108
109   g->run_iterator_on_me (iter);
110
111   iter->quit ();
112   scm_remember_upto_here_1 (protected_iter);
113
114   send_stream_event (g, "Finish", 0, 0);
115
116   if (be_verbose_global)
117     message (_f ("elapsed time: %.2f seconds", timer.read ()));
118
119   return ctx;
120 }
121
122 LY_DEFINE (ly_run_translator, "ly:run-translator",
123            2, 1, 0, (SCM mus, SCM output_def),
124            "Process @var{mus} according to @var{output_def}. \n"
125            "An interpretation context is set up,\n"
126            "and @var{mus} is interpreted with it.  \n"
127            "The context is returned in its final state.\n"
128            "\n\n"
129            "Optionally, this routine takes an Object-key to\n"
130            "to uniquely identify the Score block containing it.\n")
131 {
132   SCM glob = ly_make_global_context (output_def);
133   ly_make_global_translator (glob);
134   ly_interpret_music_expression (mus, glob);
135   return glob;
136 }