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"
22 #include "paper-book.hh"
23 #include "paper-def.hh"
24 #include "paper-line.hh"
25 #include "paper-outputter.hh"
26 #include "file-name.hh"
27 #include "scm-hash.hh"
29 #include "string-convert.hh"
33 extern SCM stencil2line (Stencil* stil, bool is_title = false);
35 Paper_outputter::Paper_outputter (String filename)
38 file_ = scm_open_file (scm_makfrom0str (filename.to_str0 ()),
39 scm_makfrom0str ("w"));
42 scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);
44 String module_name = "scm output-" + output_format_global;
47 /* In safe mode, start from a GUILE safe-module and import
48 all symbols from the output module. */
49 scm_c_use_module ("ice-9 safe");
50 SCM msm = scm_primitive_eval (ly_symbol2scm ("make-safe-module"));
51 output_module_ = scm_call_0 (msm);
52 ly_import_module (output_module_,
53 scm_c_resolve_module (module_name.to_str0 ()));
56 output_module_ = scm_c_resolve_module (module_name.to_str0 ());
58 /* FIXME: output-lib should be module, that can be imported. */
59 #define IMPORT_LESS 1 // only import the list of IMPORTS
61 scm_c_use_module ("lily");
62 scm_c_use_module ("ice-9 regex");
63 scm_c_use_module ("srfi srfi-1");
64 scm_c_use_module ("srfi srfi-13");
66 char const *imports[] = {
67 "lilypond-version", /* from lily */
68 "ly:output-def-scope",
71 "ly:ragged-page-breaks",
72 "ly:optimal-page-breaks",
74 "ly:number-pair->string", /* output-lib.scm */
80 "remove", /* from srfi srfi-1 */
81 "string-index", /* from srfi srfi-13 */
83 "regexp-substitute/global", /* from (ice9 regex) */
88 for (int i = 0; imports[i]; i++)
90 SCM s = ly_symbol2scm (imports[i]);
91 scm_module_define (output_module_, s, scm_primitive_eval (s));
93 #ifndef IMPORT_LESS // rather crude, esp for safe-mode let's not
94 SCM m = scm_set_current_module (output_module_);
95 /* not present in current module*/
96 scm_c_use_module ("ice-9 regex");
97 scm_c_use_module ("srfi srfi-13");
98 /* Need only a few of these, see above
99 scm_c_use_module ("lily"); */
100 scm_set_current_module (m);
104 Paper_outputter::~Paper_outputter ()
106 scm_close_port (file_);
111 Paper_outputter::output_scheme (SCM scm)
113 scm_display (scm_eval (scm, output_module_), file_);
117 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
119 SCM fields = SCM_EOL;
120 for (int i = dump_header_fieldnames_global.size (); i--; )
122 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
125 File_name file_name (filename_);
127 String basename = file_name.to_string ();
128 output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
130 ly_quote_scm (scopes),
131 ly_quote_scm (fields),
132 scm_makfrom0str (basename.to_str0 ()),
137 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count,
140 String creator = gnu_lilypond_version_string ();
141 creator += " (http://lilypond.org)";
143 String time_stamp = ctime (&t);
144 time_stamp = time_stamp.left_string (time_stamp.length () - 1)
146 output_scheme (scm_list_n (ly_symbol2scm ("header"),
147 scm_makfrom0str (creator.to_str0 ()),
148 scm_makfrom0str (time_stamp.to_str0 ()),
150 scm_int2num (page_count),
151 ly_bool2scm (is_classic),
154 output_metadata (paper, scopes);
155 output_music_output_def (paper);
157 output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
159 /* TODO: maybe have Scheme extract the fonts directly from \paper ?
161 Alternatively, we could simply load the fonts on demand in the
162 output, and do away with this define-fonts step. */
163 SCM fonts = paper->font_descriptions ();
164 output_scheme (scm_list_3 (ly_symbol2scm ("define-fonts"),
167 ly_quote_scm (ly_list_qsort_uniq_x (fonts))));
171 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
173 Paper_line *p = unsmob_paper_line (line);
174 Offset dim = p->dim ();
175 if (dim[Y_AXIS] > 50 CM)
177 programming_error (to_string ("Improbable line height: %f",
182 output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
183 ly_quote_scm (ly_offset2scm (*origin)),
184 ly_quote_scm (ly_offset2scm (dim))));
186 #if 1 /* FIXME: how stupid is this memorywise? */
187 output_stencil (unsmob_stencil (p->to_stencil ()));
189 for (SCM s = p->stencils (); ly_c_pair_p (s); s = ly_cdr (s))
190 output_expr (unsmob_stencil (ly_car (s))->expr (), Offset (0, 0));
193 (*origin)[Y_AXIS] += dim[Y_AXIS];
194 output_scheme (scm_list_2 (ly_symbol2scm ("stop-system"),
195 ly_bool2scm (is_last)));
199 Paper_outputter::output_page (Page *p, bool is_last)
201 output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
203 #if 0 /* FIXME: how stupid is this, memorywise? */
205 output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
206 ly_quote_scm (ly_offset2scm (Offset (0, 0))),
207 ly_quote_scm (ly_offset2scm (Offset (0, 0)))));
209 output_stencil (unsmob_stencil (p->to_stencil ()));
211 output_scheme (scm_list_2 (ly_symbol2scm ("stop-system"), SCM_BOOL_T));
213 Offset o (p->left_margin_, p->top_margin_);
214 Real vfill = (p->line_count_ > 1
215 ? (p->text_height () - p->height_) / (p->line_count_ - 1)
218 Real coverage = p->height_ / p->text_height ();
219 if (coverage < p->MIN_COVERAGE_)
220 /* Do not space out a badly filled page. This is too simplistic
221 (ie broken), because this should not vary too much between
222 (subsequent?) pages in a book. */
225 if (unsmob_stencil (p->header_))
227 output_line (stencil2line (unsmob_stencil (p->header_)), &o, false);
228 o[Y_AXIS] += p->head_sep_;
230 for (SCM s = p->lines_; s != SCM_EOL; s = ly_cdr (s))
232 SCM line = ly_car (s);
233 output_line (line, &o,
234 is_last && ly_cdr (s) != SCM_EOL
235 && !unsmob_stencil (p->copyright_)
236 && !unsmob_stencil (p->tagline_)
237 && !unsmob_stencil (p->footer_));
239 /* Do not put vfill between title and its music, */
240 if (scm_pair_p (ly_cdr (s))
241 && (!unsmob_paper_line (line)->is_title () || vfill < 0))
243 /* rather put extra just before the title. */
244 if (ly_cdr (s) != SCM_EOL
245 && (unsmob_paper_line (ly_cadr (s))->is_title () && vfill > 0))
249 o[Y_AXIS] = p->vsize_ - p->bottom_margin_;
250 if (unsmob_stencil (p->copyright_))
251 o[Y_AXIS] -= unsmob_stencil (p->copyright_)->extent (Y_AXIS).length ();
252 if (unsmob_stencil (p->tagline_))
253 o[Y_AXIS] -= unsmob_stencil (p->tagline_)->extent (Y_AXIS).length ();
254 if (unsmob_stencil (p->footer_))
255 o[Y_AXIS] -= unsmob_stencil (p->footer_)->extent (Y_AXIS).length ();
257 if (unsmob_stencil (p->copyright_))
258 output_line (stencil2line (unsmob_stencil (p->copyright_)), &o,
260 && !unsmob_stencil (p->tagline_)
261 && !unsmob_stencil (p->footer_));
262 if (unsmob_stencil (p->tagline_))
263 output_line (stencil2line (unsmob_stencil (p->tagline_)), &o,
264 is_last && !unsmob_stencil (p->footer_));
265 if (unsmob_stencil (p->footer_))
266 output_line (stencil2line (unsmob_stencil (p->footer_)), &o, is_last);
269 output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
271 && !unsmob_stencil (p->footer_))));
275 Paper_outputter::output_music_output_def (Music_output_def *odef)
277 output_scheme (scm_list_2 (ly_symbol2scm ("output-paper-def"),
282 Paper_outputter::output_stencil (Stencil *stil)
284 output_expr (stil->expr (), stil->origin ());
287 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil */
289 Paper_outputter::output_expr (SCM expr, Offset o)
293 if (!ly_c_pair_p (expr))
296 SCM head =ly_car (expr);
297 if (unsmob_input (head))
299 Input *ip = unsmob_input (head);
300 output_scheme (scm_list_4 (ly_symbol2scm ("define-origin"),
301 scm_makfrom0str (ip->file_string ()
303 scm_int2num (ip->line_number ()),
304 scm_int2num (ip->column_number ())));
305 expr = ly_cadr (expr);
307 else if (head == ly_symbol2scm ("no-origin"))
309 output_scheme (scm_list_1 (head));
310 expr = ly_cadr (expr);
312 else if (head == ly_symbol2scm ("translate-stencil"))
314 o += ly_scm2offset (ly_cadr (expr));
315 expr = ly_caddr (expr);
317 else if (head == ly_symbol2scm ("combine-stencil"))
319 output_expr (ly_cadr (expr), o);
320 expr = ly_caddr (expr);
324 output_scheme (scm_list_4 (ly_symbol2scm ("placebox"),
325 scm_make_real (o[X_AXIS]),
326 scm_make_real (o[Y_AXIS]),