]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
* scm/output-ps.scm (header): Papersize from paper.
[lilypond.git] / lily / paper-outputter.cc
1 /*
2   paper-outputter.cc -- implement Paper_outputter
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7                  Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include <math.h>
11 #include <time.h>
12
13 #include "array.hh"
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"
21 #include "main.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"
27 #include "stencil.hh"
28 #include "string-convert.hh"
29 #include "warn.hh"
30
31
32 Paper_outputter::Paper_outputter (String filename)
33 {
34   if (safe_global_b)
35     scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);      
36   
37   file_ = scm_open_file (scm_makfrom0str (filename.to_str0 ()),
38                          scm_makfrom0str ("w"));
39
40   String module_name = "scm output-" + output_format_global;
41   if (safe_global_b)
42     {
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 ()));
50     }
51   else
52     output_module_ = scm_c_resolve_module (module_name.to_str0 ());
53   
54   /* FIXME: output-lib should be module, that can be imported.  */
55 #define IMPORT_LESS 1 // only import the list of IMPORTS
56 #if IMPORT_LESS
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");
61 #endif
62   char const *imports[] = {
63     "lilypond-version",          /* from lily */
64     "ly:output-def-scope",
65     "ly:gulp-file",
66     "ly:number->string",
67     "ly:ragged-page-breaks",
68     "ly:optimal-page-breaks",
69     
70     "ly:number-pair->string",    /* output-lib.scm */
71     "ly:numbers->string",
72     "ly:inexact->string",
73     
74     "assoc-get",
75 #if IMPORT_LESS 
76     "remove",                    /* from srfi srfi-1 */
77     "string-index",              /* from srfi srfi-13 */
78     "string-join",
79     "regexp-substitute/global",  /* from (ice9 regex) */
80 #endif  
81     0,
82   };
83       
84   for (int i = 0; imports[i]; i++)
85     {
86       SCM s = ly_symbol2scm (imports[i]);
87       scm_module_define (output_module_, s, scm_primitive_eval (s));
88     }
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);
97 #endif
98 }
99
100 Paper_outputter::~Paper_outputter ()
101 {
102   scm_close_port (file_);
103   file_ = SCM_EOL;
104 }
105
106 void
107 Paper_outputter::output_scheme (SCM scm)
108 {
109   scm_display (scm_eval (scm, output_module_), file_);
110 }
111
112 void
113 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
114 {
115   SCM fields = SCM_EOL;
116   for (int i = dump_header_fieldnames_global.size (); i--; )
117     fields
118       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
119                  fields);
120   output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
121                              paper->self_scm (),
122                              ly_quote_scm (scopes),
123                              ly_quote_scm (fields),
124                              scm_makfrom0str (basename_.to_str0 ()), 
125                              SCM_UNDEFINED));
126 }
127
128 void
129 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count,
130                                 bool is_classic)
131 {
132   String creator = gnu_lilypond_version_string ();
133   creator += " (http://lilypond.org)";
134   time_t t (time (0));
135   String time_stamp = ctime (&t);
136   time_stamp = time_stamp.left_string (time_stamp.length () - 1)
137     + " " + *tzname;
138   output_scheme (scm_list_n (ly_symbol2scm ("header"),
139                              scm_makfrom0str (creator.to_str0 ()),
140                              scm_makfrom0str (time_stamp.to_str0 ()),
141                              paper->self_scm (),
142                              scm_int2num (page_count),
143                              ly_bool2scm (is_classic),
144                              SCM_UNDEFINED));
145
146   output_metadata (paper, scopes);
147   output_music_output_def (paper);
148
149   output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
150
151   /* TODO: maybe have Scheme extract the fonts directly from \paper ?
152           
153      Alternatively, we could simply load the fonts on demand in the
154      output, and do away with this define-fonts step.  */
155   SCM fonts = paper->font_descriptions ();
156   output_scheme (scm_list_3 (ly_symbol2scm ("define-fonts"),
157                              paper->self_scm (),
158                              //FIXME:
159                              ly_quote_scm (ly_list_qsort_uniq_x (fonts))));
160 }
161
162 void
163 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
164 {
165   Paper_line *pl = unsmob_paper_line (line);
166   Offset dim = pl->dim ();
167   if (dim[Y_AXIS] > 50 CM)
168     {
169       programming_error ("Improbable system height.");
170       dim[Y_AXIS] = 50 CM;
171     }
172
173   output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
174                              ly_quote_scm (ly_offset2scm (*origin)),
175                              ly_quote_scm (ly_offset2scm (dim))));
176
177   for (SCM s = pl->stencils (); is_pair (s); s = ly_cdr (s))
178     output_expr (unsmob_stencil (ly_car (s))->get_expr (), Offset (0, 0));
179
180   output_scheme (scm_list_2 (ly_symbol2scm ("stop-system"),
181                              ly_bool2scm (is_last)));
182
183   (*origin)[Y_AXIS] += dim[Y_AXIS];
184 }
185
186 void
187 Paper_outputter::output_music_output_def (Music_output_def* odef)
188 {
189   output_scheme (scm_list_n (ly_symbol2scm ("output-paper-def"),
190                              odef->self_scm (), SCM_UNDEFINED));
191 }
192
193 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil  */
194 void
195 Paper_outputter::output_expr (SCM expr, Offset o)
196 {
197   while (1)
198     {
199       if (!is_pair (expr))
200         return;
201   
202       SCM head =ly_car (expr);
203       if (unsmob_input (head))
204         {
205           Input * ip = unsmob_input (head);
206       
207           output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
208                                      scm_makfrom0str (ip->file_string ().to_str0 ()),
209                                      scm_int2num (ip->line_number ()),
210                                      scm_int2num (ip->column_number ()),
211                                      SCM_UNDEFINED));
212           expr = ly_cadr (expr);
213         }
214       else  if (head ==  ly_symbol2scm ("no-origin"))
215         {
216           output_scheme (scm_list_n (head, SCM_UNDEFINED));
217           expr = ly_cadr (expr);
218         }
219       else if (head == ly_symbol2scm ("translate-stencil"))
220         {
221           o += ly_scm2offset (ly_cadr (expr));
222           expr = ly_caddr (expr);
223         }
224       else if (head == ly_symbol2scm ("combine-stencil"))
225         {
226           output_expr (ly_cadr (expr), o);
227           expr = ly_caddr (expr);
228         }
229       else
230         {
231           output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
232                                      scm_make_real (o[X_AXIS]),
233                                      scm_make_real (o[Y_AXIS]),
234                                      expr,
235                                      SCM_UNDEFINED));
236           return;
237         }
238     }
239 }
240