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>
14 #include "dimensions.hh"
15 #include "font-metric.hh"
16 #include "input-file-results.hh"
17 #include "input-smob.hh"
18 #include "lily-guile.hh"
19 #include "lily-version.hh"
20 #include "ly-module.hh"
22 #include "paper-book.hh"
23 #include "paper-def.hh"
24 #include "paper-line.hh"
25 #include "paper-outputter.hh"
26 #include "scm-hash.hh"
28 #include "string-convert.hh"
32 Paper_outputter::Paper_outputter (String filename)
35 scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);
37 file_ = scm_open_file (scm_makfrom0str (filename.to_str0 ()),
38 scm_makfrom0str ("w"));
40 String module_name = "scm output-" + output_format_global;
43 /* In safe mode, start from a GUILE safe-module and import
44 all symbols from the output module. */
45 scm_c_use_module ("ice-9 safe");
46 SCM msm = scm_primitive_eval (ly_symbol2scm ("make-safe-module"));
47 output_module_ = scm_call_0 (msm);
48 ly_import_module (output_module_,
49 scm_c_resolve_module (module_name.to_str0 ()));
52 output_module_ = scm_c_resolve_module (module_name.to_str0 ());
54 /* FIXME: output-lib should be module, that can be imported. */
55 #define IMPORT_LESS 1 // only import the list of IMPORTS
57 scm_c_use_module ("lily");
58 scm_c_use_module ("ice-9 regex");
59 scm_c_use_module ("srfi srfi-1");
60 scm_c_use_module ("srfi srfi-13");
62 char const *imports[] = {
63 "lilypond-version", /* from lily */
64 "ly:output-def-scope",
67 "ly:ragged-page-breaks",
68 "ly:optimal-page-breaks",
70 "ly:number-pair->string", /* output-lib.scm */
76 "remove", /* from srfi srfi-1 */
77 "string-index", /* from srfi srfi-13 */
79 "regexp-substitute/global", /* from (ice9 regex) */
84 for (int i = 0; imports[i]; i++)
86 SCM s = ly_symbol2scm (imports[i]);
87 scm_module_define (output_module_, s, scm_primitive_eval (s));
89 #ifndef IMPORT_LESS // rather crude, esp for safe-mode let's not
90 SCM m = scm_set_current_module (output_module_);
91 /* not present in current module*/
92 scm_c_use_module ("ice-9 regex");
93 scm_c_use_module ("srfi srfi-13");
94 /* Need only a few of these, see above
95 scm_c_use_module ("lily"); */
96 scm_set_current_module (m);
100 Paper_outputter::~Paper_outputter ()
102 scm_close_port (file_);
107 Paper_outputter::output_scheme (SCM scm)
109 scm_display (scm_eval (scm, output_module_), file_);
113 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
115 SCM fields = SCM_EOL;
116 for (int i = dump_header_fieldnames_global.size (); i--; )
118 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
120 output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
122 ly_quote_scm (scopes),
123 ly_quote_scm (fields),
124 scm_makfrom0str (basename_.to_str0 ()),
129 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count)
131 String creator = gnu_lilypond_version_string ();
132 creator += " (http://lilypond.org)";
134 String time_stamp = ctime (&t);
135 time_stamp = time_stamp.left_string (time_stamp.length () - 1)
137 output_scheme (scm_list_4 (ly_symbol2scm ("header"),
138 scm_makfrom0str (creator.to_str0 ()),
139 scm_makfrom0str (time_stamp.to_str0 ()),
140 scm_int2num (page_count)));
142 output_metadata (paper, scopes);
143 output_music_output_def (paper);
145 output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
147 /* TODO: maybe have Scheme extract the fonts directly from \paper ?
149 Alternatively, we could simply load the fonts on demand in the
150 output, and do away with this define-fonts step. */
151 SCM fonts = paper->font_descriptions ();
152 output_scheme (scm_list_3 (ly_symbol2scm ("define-fonts"),
155 ly_quote_scm (ly_list_qsort_uniq_x (fonts))));
159 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
161 Paper_line *pl = unsmob_paper_line (line);
162 Offset dim = pl->dim ();
163 if (dim[Y_AXIS] > 50 CM)
165 programming_error ("Improbable system height.");
169 output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
170 ly_quote_scm (ly_offset2scm (*origin)),
171 ly_quote_scm (ly_offset2scm (dim))));
173 for (SCM s = pl->stencils (); is_pair (s); s = ly_cdr (s))
174 output_expr (unsmob_stencil (ly_car (s))->get_expr (), Offset (0, 0));
176 output_scheme (scm_list_2 (ly_symbol2scm ("stop-system"),
177 ly_bool2scm (is_last)));
179 (*origin)[Y_AXIS] += dim[Y_AXIS];
183 Paper_outputter::output_music_output_def (Music_output_def* odef)
185 output_scheme (scm_list_n (ly_symbol2scm ("output-paper-def"),
186 odef->self_scm (), SCM_UNDEFINED));
189 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil */
191 Paper_outputter::output_expr (SCM expr, Offset o)
198 SCM head =ly_car (expr);
199 if (unsmob_input (head))
201 Input * ip = unsmob_input (head);
203 output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
204 scm_makfrom0str (ip->file_string ().to_str0 ()),
205 scm_int2num (ip->line_number ()),
206 scm_int2num (ip->column_number ()),
208 expr = ly_cadr (expr);
210 else if (head == ly_symbol2scm ("no-origin"))
212 output_scheme (scm_list_n (head, SCM_UNDEFINED));
213 expr = ly_cadr (expr);
215 else if (head == ly_symbol2scm ("translate-stencil"))
217 o += ly_scm2offset (ly_cadr (expr));
218 expr = ly_caddr (expr);
220 else if (head == ly_symbol2scm ("combine-stencil"))
222 output_expr (ly_cadr (expr), o);
223 expr = ly_caddr (expr);
227 output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
228 scm_make_real (o[X_AXIS]),
229 scm_make_real (o[Y_AXIS]),