2 paper-outputter.cc -- implement Paper_outputter
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
13 #include "dimensions.hh"
14 #include "virtual-methods.hh"
15 #include "paper-outputter.hh"
18 #include "string-convert.hh"
20 #include "font-metric.hh"
22 #include "scm-hash.hh"
23 #include "lily-version.hh"
24 #include "paper-def.hh"
25 #include "input-file-results.hh"
26 #include "ly-module.hh"
27 #include "paper-book.hh"
28 #include "paper-line.hh"
29 #include "input-smob.hh" // output_expr
32 Paper_outputter::Paper_outputter (String name)
35 scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);
37 file_ = scm_open_file (scm_makfrom0str (name.to_str0 ()),
38 scm_makfrom0str ("w"));
40 if (output_format_global == PAGE_LAYOUT)
42 output_func_ = SCM_EOL;
43 String name = "scm output-" + output_format_global;
46 /* In safe mode, start from a GUILE safe-module and import
47 all symbols from the output module. */
48 scm_c_use_module ("ice-9 safe");
49 SCM msm = scm_primitive_eval (ly_symbol2scm ("make-safe-module"));
50 output_module_ = scm_call_0 (msm);
51 ly_import_module (output_module_,
52 scm_c_resolve_module (name.to_str0 ()));
55 output_module_ = scm_c_resolve_module (name.to_str0 ());
57 /* FIXME: output-lib should be module, that can be imported. */
58 #define IMPORT_LESS 1 // only import the list of IMPORTS
60 scm_c_use_module ("lily");
61 scm_c_use_module ("ice-9 regex");
62 scm_c_use_module ("srfi srfi-13");
64 char const *imports[] = {
65 "lilypond-version", /* from lily */
66 "ly:output-def-scope",
70 "number-pair->string", /* output-lib.scm */
74 "inexact->string", /* insecure guile? */
76 "string-index", /* from srfi srfi-13 */
77 "regexp-substitute/global", /* from (ice9 regex) */
82 for (int i = 0; imports[i]; i++)
84 SCM s = ly_symbol2scm (imports[i]);
85 scm_module_define (output_module_, s, scm_primitive_eval (s));
87 #ifndef IMPORT_LESS // rather crude, esp for safe-mode let's not
88 SCM m = scm_set_current_module (output_module_);
89 /* not present in current module*/
90 scm_c_use_module ("ice-9 regex");
91 scm_c_use_module ("srfi srfi-13");
92 /* Need only a few of these, see above
93 scm_c_use_module ("lily"); */
94 scm_set_current_module (m);
100 = scm_call_1 (ly_scheme_function ("find-dumper"),
101 scm_makfrom0str (output_format_global.to_str0 ()));
102 output_module_ = SCM_EOL;
106 Paper_outputter::~Paper_outputter ()
108 scm_close_port (file_);
113 Paper_outputter::output_scheme (SCM scm)
115 if (output_format_global == PAGE_LAYOUT)
116 scm_display (scm_eval (scm, output_module_), file_);
118 gh_call2 (output_func_, scm, file_);
122 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
124 SCM fields = SCM_EOL;
125 for (int i = dump_header_fieldnames_global.size (); i--; )
127 = gh_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
129 output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
131 scm_list_n (ly_symbol2scm ("quote"),
132 scopes, SCM_UNDEFINED),
133 scm_list_n (ly_symbol2scm ("quote"),
134 fields, SCM_UNDEFINED),
135 scm_makfrom0str (basename_.to_str0 ()),
140 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count)
142 String creator = gnu_lilypond_version_string ();
143 creator += " (http://lilypond.org)";
145 String time_stamp = ctime (&t);
146 time_stamp = time_stamp.left_string (time_stamp.length () - 1)
148 output_scheme (scm_list_4 (ly_symbol2scm ("header"),
149 scm_makfrom0str (creator.to_str0 ()),
150 scm_makfrom0str (time_stamp.to_str0 ()),
151 scm_int2num (page_count)));
153 output_metadata (paper, scopes);
154 output_music_output_def (paper);
156 output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
157 output_scheme (scm_list_2 (ly_symbol2scm ("define-fonts"),
158 ly_quote_scm (paper->font_descriptions ())));
162 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
164 Paper_line *pl = unsmob_paper_line (line);
165 Offset dim = pl->dim ();
166 if (dim[Y_AXIS] > 50 CM)
168 programming_error ("Improbable system height.");
172 if (output_format_global != PAGE_LAYOUT)
173 output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
174 gh_double2scm (dim[X_AXIS]),
175 gh_double2scm (dim[Y_AXIS])));
178 output_scheme (scm_list_3 (ly_symbol2scm ("new-start-system"),
179 ly_quote_scm (ly_offset2scm (*origin)),
180 ly_quote_scm (ly_offset2scm (dim))));
181 (*origin)[Y_AXIS] += dim[Y_AXIS];
184 SCM between = SCM_EOL;
185 for (SCM s = pl->stencils (); gh_pair_p (s); s = ly_cdr (s))
187 Stencil *stil = unsmob_stencil (ly_car (s));
189 output_expr (stil->get_expr (), stil->origin ());
190 /* Only if !PAGE_LAYOUT */
191 else if (ly_caar (s) == ly_symbol2scm ("between-system-string"))
192 between = ly_cdar (s);
196 output_scheme (scm_list_1 (ly_symbol2scm ("stop-last-system")));
199 output_scheme (scm_list_1 (ly_symbol2scm ("stop-system")));
200 if (output_format_global != PAGE_LAYOUT && between != SCM_EOL)
201 output_scheme (between);
206 Paper_outputter::output_music_output_def (Music_output_def* odef)
208 output_scheme (scm_list_n (ly_symbol2scm ("output-paper-def"),
209 odef->self_scm (), SCM_UNDEFINED));
212 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil */
214 Paper_outputter::output_expr (SCM expr, Offset o)
218 if (!gh_pair_p (expr))
221 SCM head =ly_car (expr);
222 if (unsmob_input (head))
224 Input * ip = unsmob_input (head);
226 output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
227 scm_makfrom0str (ip->file_string ().to_str0 ()),
228 gh_int2scm (ip->line_number ()),
229 gh_int2scm (ip->column_number ()),
231 expr = ly_cadr (expr);
233 else if (head == ly_symbol2scm ("no-origin"))
235 output_scheme (scm_list_n (head, SCM_UNDEFINED));
236 expr = ly_cadr (expr);
238 else if (head == ly_symbol2scm ("translate-stencil"))
240 o += ly_scm2offset (ly_cadr (expr));
241 expr = ly_caddr (expr);
243 else if (head == ly_symbol2scm ("combine-stencil"))
245 output_expr (ly_cadr (expr), o);
246 expr = ly_caddr (expr);
250 output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
251 gh_double2scm (o[X_AXIS]),
252 gh_double2scm (o[Y_AXIS]),