]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
*** empty log message ***
[lilypond.git] / lily / paper-outputter.cc
1 /*
2   paper-outputter.cc -- implement Paper_outputter
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7                  Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include <math.h>
11 #include <time.h>
12
13 #include "array.hh"
14 #include "dimensions.hh"
15 #include "file-name.hh"
16 #include "font-metric.hh"
17 #include "input-smob.hh"
18 #include "lily-guile.hh"
19 #include "lily-version.hh"
20 #include "ly-module.hh"
21 #include "main.hh"
22 #include "output-def.hh"
23 #include "paper-book.hh"
24 #include "paper-outputter.hh"
25 #include "paper-system.hh"
26 #include "scm-hash.hh"
27 #include "stencil.hh"
28 #include "string-convert.hh"
29 #include "warn.hh"
30
31 #include "ly-smobs.icc"
32
33
34 Paper_outputter::Paper_outputter (String filename, String format)
35 {
36   file_ = SCM_EOL;
37   output_module_ = SCM_EOL;
38   smobify_self ();
39   
40   filename_ = filename;
41   String module_name = "scm output-" + format;
42   output_module_ = scm_c_resolve_module (module_name.to_str0 ());
43 }
44
45 Paper_outputter::~Paper_outputter ()
46 {
47 }
48
49
50 IMPLEMENT_SMOBS (Paper_outputter);
51 IMPLEMENT_DEFAULT_EQUAL_P (Paper_outputter);
52
53 SCM
54 Paper_outputter::mark_smob (SCM x)
55 {
56   Paper_outputter *p = (Paper_outputter*) SCM_CELL_WORD_1(x);
57   scm_gc_mark (p->output_module_);
58   return p->file_;
59 }
60
61 int
62 Paper_outputter::print_smob (SCM x, SCM p, scm_print_state*)
63 {
64   (void) x;
65   scm_puts ("#<Paper_outputter>", p);
66   return 1;
67 }
68
69 SCM
70 Paper_outputter::file ()
71 {
72   if (file_ == SCM_EOL)
73     if (filename_ == "-")
74       file_ = scm_current_output_port();
75     else
76       file_ = scm_open_file (scm_makfrom0str (filename_.to_str0 ()),
77                              scm_makfrom0str ("w"));
78   return file_;
79 }
80
81 SCM
82 Paper_outputter::dump_string (SCM scm)
83 {
84   return scm_display (scm, file ());
85 }
86
87 SCM
88 Paper_outputter::scheme_to_string (SCM scm)
89 {
90   return scm_eval (scm, output_module_);
91 }
92
93 void
94 Paper_outputter::output_scheme (SCM scm)
95 {
96   dump_string (scheme_to_string (scm));
97 }
98
99 void
100 paper_outputter_dump (void *po, SCM x)
101 {
102   Paper_outputter *me = (Paper_outputter*) po;
103   me->output_scheme (x);
104 }
105
106 void
107 Paper_outputter::output_stencil (Stencil stil)
108 {
109   interpret_stencil_expression (stil.expr (), paper_outputter_dump,
110                                 (void*) this, Offset (0,0));
111 }
112
113 Paper_outputter *
114 get_paper_outputter (String outname, String f) 
115 {
116   progress_indication (_f ("Layout output to `%s'...",
117                            outname == "-" ? String ("<stdout>") : outname));
118   progress_indication ("\n");
119   return new Paper_outputter (outname, f);
120 }
121
122 /* FIXME: why is output_* wrapper called dump?  */
123 LY_DEFINE (ly_outputter_dump_stencil, "ly:outputter-dump-stencil",
124            2, 0, 0, (SCM outputter, SCM stencil),
125            "Dump stencil @var{expr} onto @var{outputter}.")
126 {
127   Paper_outputter *po = unsmob_outputter (outputter);
128   Stencil *st = unsmob_stencil (stencil);
129   SCM_ASSERT_TYPE (po, outputter, SCM_ARG1, __FUNCTION__, "Paper_outputter");
130   SCM_ASSERT_TYPE (st, stencil, SCM_ARG1, __FUNCTION__, "Paper_outputter");
131   po->output_stencil (*st);
132   return SCM_UNSPECIFIED;
133 }
134
135 LY_DEFINE (ly_outputter_dump_string, "ly:outputter-dump-string",
136            2, 0, 0, (SCM outputter, SCM str),
137            "Dump @var{str} onto @var{outputter}.")
138 {
139   Paper_outputter *po = unsmob_outputter (outputter);
140   SCM_ASSERT_TYPE (po, outputter, SCM_ARG1, __FUNCTION__, "Paper_outputter");
141   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "Paper_outputter");
142   
143   return po->dump_string (str);
144 }
145
146 void
147 Paper_outputter::close ()
148 {
149   if (scm_port_p (file_) == SCM_BOOL_T)
150     scm_close_port (file_);
151 }