* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[lilypond.git] / lily / paper-outputter.cc
blob2db6aaa9cc18a23ba025b19a33018015123ab34d
1 /*
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>
8 */
10 #include <time.h>
11 #include <math.h>
13 #include "dimensions.hh"
14 #include "virtual-methods.hh"
15 #include "paper-outputter.hh"
16 #include "stencil.hh"
17 #include "array.hh"
18 #include "string-convert.hh"
19 #include "warn.hh"
20 #include "font-metric.hh"
21 #include "main.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)
34 if (safe_global_b)
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;
44 if (safe_global_b)
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 ()));
54 else
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
59 #if IMPORT_LESS
60 scm_c_use_module ("lily");
61 scm_c_use_module ("ice-9 regex");
62 scm_c_use_module ("srfi srfi-13");
63 #endif
64 char const *imports[] = {
65 "lilypond-version", /* from lily */
66 "ly:output-def-scope",
67 "ly:gulp-file",
68 "ly:number->string",
69 "ly:ragged-page-breaks",
70 "ly:optimal-page-breaks",
72 "ly:number-pair->string", /* output-lib.scm */
73 "ly:numbers->string",
74 "ly:inexact->string",
76 "assoc-get",
77 #if IMPORT_LESS
78 "string-index", /* from srfi srfi-13 */
79 "string-join",
80 "regexp-substitute/global", /* from (ice9 regex) */
81 #endif
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);
98 #endif
100 else
102 output_func_
103 = scm_call_1 (ly_scheme_function ("find-dumper"),
104 scm_makfrom0str (output_format_global.to_str0 ()));
105 output_module_ = SCM_EOL;
109 Paper_outputter::~Paper_outputter ()
111 scm_close_port (file_);
112 file_ = SCM_EOL;
115 void
116 Paper_outputter::output_scheme (SCM scm)
118 if (output_format_global == PAGE_LAYOUT)
119 scm_display (scm_eval (scm, output_module_), file_);
120 else
121 scm_call_2 (output_func_, scm, file_);
124 void
125 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
127 SCM fields = SCM_EOL;
128 for (int i = dump_header_fieldnames_global.size (); i--; )
129 fields
130 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
131 fields);
132 output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
133 paper->self_scm (),
134 ly_quote_scm (scopes),
135 ly_quote_scm (fields),
136 scm_makfrom0str (basename_.to_str0 ()),
137 SCM_UNDEFINED));
140 void
141 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count)
143 String creator = gnu_lilypond_version_string ();
144 creator += " (http://lilypond.org)";
145 time_t t (time (0));
146 String time_stamp = ctime (&t);
147 time_stamp = time_stamp.left_string (time_stamp.length () - 1)
148 + " " + *tzname;
149 output_scheme (scm_list_4 (ly_symbol2scm ("header"),
150 scm_makfrom0str (creator.to_str0 ()),
151 scm_makfrom0str (time_stamp.to_str0 ()),
152 scm_int2num (page_count)));
154 output_metadata (paper, scopes);
155 output_music_output_def (paper);
157 output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
160 TODO: maybe have Scheme extract the fonts directly from \paper?
162 Alternatively, we could simply load the fonts on demand in the
163 output, and do away with this define-fonts step.
165 output_scheme (scm_list_3 (ly_symbol2scm ("define-fonts"),
166 paper->self_scm (),
167 ly_quote_scm (paper->font_descriptions ())));
170 void
171 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
173 Paper_line *pl = unsmob_paper_line (line);
174 Offset dim = pl->dim ();
175 if (dim[Y_AXIS] > 50 CM)
177 programming_error ("Improbable system height.");
178 dim[Y_AXIS] = 50 CM;
181 if (output_format_global != PAGE_LAYOUT)
182 output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
183 scm_make_real (dim[X_AXIS]),
184 scm_make_real (dim[Y_AXIS])));
185 else
187 output_scheme (scm_list_3 (ly_symbol2scm ("new-start-system"),
188 ly_quote_scm (ly_offset2scm (*origin)),
189 ly_quote_scm (ly_offset2scm (dim))));
190 (*origin)[Y_AXIS] += dim[Y_AXIS];
193 SCM between = SCM_EOL;
194 for (SCM s = pl->stencils (); ly_pair_p (s); s = ly_cdr (s))
196 Stencil *stil = unsmob_stencil (ly_car (s));
197 if (stil)
198 output_expr (stil->get_expr (), Offset (0,0));
199 /* Only if !PAGE_LAYOUT */
200 else if (ly_caar (s) == ly_symbol2scm ("between-system-string"))
201 between = ly_cdar (s);
204 if (is_last)
205 output_scheme (scm_list_1 (ly_symbol2scm ("stop-last-system")));
206 else
208 output_scheme (scm_list_1 (ly_symbol2scm ("stop-system")));
209 if (output_format_global != PAGE_LAYOUT && between != SCM_EOL)
210 output_scheme (between);
214 void
215 Paper_outputter::output_music_output_def (Music_output_def* odef)
217 output_scheme (scm_list_n (ly_symbol2scm ("output-paper-def"),
218 odef->self_scm (), SCM_UNDEFINED));
221 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil */
222 void
223 Paper_outputter::output_expr (SCM expr, Offset o)
225 while (1)
227 if (!ly_pair_p (expr))
228 return;
230 SCM head =ly_car (expr);
231 if (unsmob_input (head))
233 Input * ip = unsmob_input (head);
235 output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
236 scm_makfrom0str (ip->file_string ().to_str0 ()),
237 scm_int2num (ip->line_number ()),
238 scm_int2num (ip->column_number ()),
239 SCM_UNDEFINED));
240 expr = ly_cadr (expr);
242 else if (head == ly_symbol2scm ("no-origin"))
244 output_scheme (scm_list_n (head, SCM_UNDEFINED));
245 expr = ly_cadr (expr);
247 else if (head == ly_symbol2scm ("translate-stencil"))
249 o += ly_scm2offset (ly_cadr (expr));
250 expr = ly_caddr (expr);
252 else if (head == ly_symbol2scm ("combine-stencil"))
254 output_expr (ly_cadr (expr), o);
255 expr = ly_caddr (expr);
257 else
259 output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
260 scm_make_real (o[X_AXIS]),
261 scm_make_real (o[Y_AXIS]),
262 expr,
263 SCM_UNDEFINED));
264 return;