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-smob.hh"
17 #include "lily-guile.hh"
18 #include "lily-version.hh"
19 #include "ly-module.hh"
21 #include "paper-book.hh"
22 #include "paper-def.hh"
23 #include "paper-line.hh"
24 #include "paper-outputter.hh"
25 #include "file-name.hh"
26 #include "scm-hash.hh"
28 #include "string-convert.hh"
32 Paper_outputter::Paper_outputter (String filename)
35 file_ = scm_open_file (scm_makfrom0str (filename.to_str0 ()),
36 scm_makfrom0str ("w"));
39 scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);
41 String module_name = "scm output-" + output_format_global;
44 /* In safe mode, start from a GUILE safe-module and import
45 all symbols from the output module. */
46 scm_c_use_module ("ice-9 safe");
47 SCM msm = scm_primitive_eval (ly_symbol2scm ("make-safe-module"));
48 output_module_ = scm_call_0 (msm);
49 ly_import_module (output_module_,
50 scm_c_resolve_module (module_name.to_str0 ()));
53 output_module_ = scm_c_resolve_module (module_name.to_str0 ());
55 /* FIXME: output-lib should be module, that can be imported. */
56 #define IMPORT_LESS 1 // only import the list of IMPORTS
58 scm_c_use_module ("lily");
59 scm_c_use_module ("ice-9 regex");
60 scm_c_use_module ("srfi srfi-1");
61 scm_c_use_module ("srfi srfi-13");
63 char const *imports[] = {
64 "lilypond-version", /* from lily */
65 "ly:output-def-scope",
68 "ly:ragged-page-breaks",
69 "ly:optimal-page-breaks",
71 "ly:number-pair->string", /* output-lib.scm */
77 "remove", /* from srfi srfi-1 */
78 "string-index", /* from srfi srfi-13 */
80 "regexp-substitute/global", /* from (ice9 regex) */
85 for (int i = 0; imports[i]; i++)
87 SCM s = ly_symbol2scm (imports[i]);
88 scm_module_define (output_module_, s, scm_primitive_eval (s));
90 #ifndef IMPORT_LESS // rather crude, esp for safe-mode let's not
91 SCM m = scm_set_current_module (output_module_);
92 /* not present in current module*/
93 scm_c_use_module ("ice-9 regex");
94 scm_c_use_module ("srfi srfi-13");
95 /* Need only a few of these, see above
96 scm_c_use_module ("lily"); */
97 scm_set_current_module (m);
101 Paper_outputter::~Paper_outputter ()
103 scm_close_port (file_);
108 Paper_outputter::output_scheme (SCM scm)
110 scm_display (scm_eval (scm, output_module_), file_);
114 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
116 SCM fields = SCM_EOL;
117 for (int i = dump_header_fieldnames_global.size (); i--; )
119 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
122 File_name file_name (filename_);
124 String basename = file_name.to_string ();
125 output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
127 ly_quote_scm (scopes),
128 ly_quote_scm (fields),
129 scm_makfrom0str (basename.to_str0 ()),
134 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count,
137 String creator = gnu_lilypond_version_string ();
138 creator += " (http://lilypond.org)";
140 String time_stamp = ctime (&t);
141 time_stamp = time_stamp.left_string (time_stamp.length () - 1)
143 output_scheme (scm_list_n (ly_symbol2scm ("header"),
144 scm_makfrom0str (creator.to_str0 ()),
145 scm_makfrom0str (time_stamp.to_str0 ()),
147 scm_int2num (page_count),
148 ly_bool2scm (is_classic),
151 output_metadata (paper, scopes);
152 output_music_output_def (paper);
154 output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
156 /* TODO: maybe have Scheme extract the fonts directly from \paper ?
158 Alternatively, we could simply load the fonts on demand in the
159 output, and do away with this define-fonts step. */
160 SCM fonts = paper->font_descriptions ();
161 output_scheme (scm_list_3 (ly_symbol2scm ("define-fonts"),
164 ly_quote_scm (ly_list_qsort_uniq_x (fonts))));
168 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
170 Paper_line *pl = unsmob_paper_line (line);
171 Offset dim = pl->dim ();
172 if (dim[Y_AXIS] > 50 CM)
174 programming_error ("Improbable system height.");
178 output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
179 ly_quote_scm (ly_offset2scm (*origin)),
180 ly_quote_scm (ly_offset2scm (dim))));
182 for (SCM s = pl->stencils (); ly_c_pair_p (s); s = ly_cdr (s))
183 output_expr (unsmob_stencil (ly_car (s))->get_expr (), Offset (0, 0));
185 output_scheme (scm_list_2 (ly_symbol2scm ("stop-system"),
186 ly_bool2scm (is_last)));
188 (*origin)[Y_AXIS] += dim[Y_AXIS];
192 Paper_outputter::output_music_output_def (Music_output_def *odef)
194 output_scheme (scm_list_2 (ly_symbol2scm ("output-paper-def"),
199 Paper_outputter::output_stencil (Stencil *stil)
201 output_expr (stil->get_expr (), stil->origin ());
204 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil */
206 Paper_outputter::output_expr (SCM expr, Offset o)
210 if (!ly_c_pair_p (expr))
213 SCM head =ly_car (expr);
214 if (unsmob_input (head))
216 Input *ip = unsmob_input (head);
217 output_scheme (scm_list_4 (ly_symbol2scm ("define-origin"),
218 scm_makfrom0str (ip->file_string ()
220 scm_int2num (ip->line_number ()),
221 scm_int2num (ip->column_number ())));
222 expr = ly_cadr (expr);
224 else if (head == ly_symbol2scm ("no-origin"))
226 output_scheme (scm_list_1 (head));
227 expr = ly_cadr (expr);
229 else if (head == ly_symbol2scm ("translate-stencil"))
231 o += ly_scm2offset (ly_cadr (expr));
232 expr = ly_caddr (expr);
234 else if (head == ly_symbol2scm ("combine-stencil"))
236 output_expr (ly_cadr (expr), o);
237 expr = ly_caddr (expr);
241 output_scheme (scm_list_4 (ly_symbol2scm ("placebox"),
242 scm_make_real (o[X_AXIS]),
243 scm_make_real (o[Y_AXIS]),