]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
* lily/stencil-scheme.cc (ly:stencil-expr): Rename (was:
[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-smob.hh"
17 #include "lily-guile.hh"
18 #include "lily-version.hh"
19 #include "ly-module.hh"
20 #include "main.hh"
21 #include "page.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"
28 #include "stencil.hh"
29 #include "string-convert.hh"
30 #include "warn.hh"
31
32 // JUNKME
33 extern SCM stencil2line (Stencil* stil, bool is_title = false);
34
35 Paper_outputter::Paper_outputter (String filename)
36 {
37   filename_ = filename;
38   file_ = scm_open_file (scm_makfrom0str (filename.to_str0 ()),
39                          scm_makfrom0str ("w"));
40
41   if (safe_global_b)
42     scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);      
43
44   String module_name = "scm output-" + output_format_global;
45   if (safe_global_b)
46     {
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 ()));
54     }
55   else
56     output_module_ = scm_c_resolve_module (module_name.to_str0 ());
57   
58   /* FIXME: output-lib should be module, that can be imported.  */
59 #define IMPORT_LESS 1 // only import the list of IMPORTS
60 #if IMPORT_LESS
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");
65 #endif
66   char const *imports[] = {
67     "lilypond-version",          /* from lily */
68     "ly:output-def-scope",
69     "ly:gulp-file",
70     "ly:number->string",
71     "ly:ragged-page-breaks",
72     "ly:optimal-page-breaks",
73     
74     "ly:number-pair->string",    /* output-lib.scm */
75     "ly:numbers->string",
76     "ly:inexact->string",
77     
78     "assoc-get",
79 #if IMPORT_LESS 
80     "remove",                    /* from srfi srfi-1 */
81     "string-index",              /* from srfi srfi-13 */
82     "string-join",
83     "regexp-substitute/global",  /* from (ice9 regex) */
84 #endif  
85     0,
86   };
87       
88   for (int i = 0; imports[i]; i++)
89     {
90       SCM s = ly_symbol2scm (imports[i]);
91       scm_module_define (output_module_, s, scm_primitive_eval (s));
92     }
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);
101 #endif
102 }
103
104 Paper_outputter::~Paper_outputter ()
105 {
106   scm_close_port (file_);
107   file_ = SCM_EOL;
108 }
109
110 void
111 Paper_outputter::output_scheme (SCM scm)
112 {
113   scm_display (scm_eval (scm, output_module_), file_);
114 }
115
116 void
117 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
118 {
119   SCM fields = SCM_EOL;
120   for (int i = dump_header_fieldnames_global.size (); i--; )
121     fields
122       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
123                  fields);
124
125   File_name file_name (filename_);
126   file_name.ext_ = "";
127   String basename = file_name.to_string ();
128   output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
129                              paper->self_scm (),
130                              ly_quote_scm (scopes),
131                              ly_quote_scm (fields),
132                              scm_makfrom0str (basename.to_str0 ()), 
133                              SCM_UNDEFINED));
134 }
135
136 void
137 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count,
138                                 bool is_classic)
139 {
140   String creator = gnu_lilypond_version_string ();
141   creator += " (http://lilypond.org)";
142   time_t t (time (0));
143   String time_stamp = ctime (&t);
144   time_stamp = time_stamp.left_string (time_stamp.length () - 1)
145     + " " + *tzname;
146   output_scheme (scm_list_n (ly_symbol2scm ("header"),
147                              scm_makfrom0str (creator.to_str0 ()),
148                              scm_makfrom0str (time_stamp.to_str0 ()),
149                              paper->self_scm (),
150                              scm_int2num (page_count),
151                              ly_bool2scm (is_classic),
152                              SCM_UNDEFINED));
153
154   output_metadata (paper, scopes);
155   output_music_output_def (paper);
156
157   output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
158
159   /* TODO: maybe have Scheme extract the fonts directly from \paper ?
160           
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"),
165                              paper->self_scm (),
166                              //FIXME:
167                              ly_quote_scm (ly_list_qsort_uniq_x (fonts))));
168 }
169
170 void
171 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
172 {
173   Paper_line *p = unsmob_paper_line (line);
174   Offset dim = p->dim ();
175   if (dim[Y_AXIS] > 50 CM)
176     {
177       programming_error (to_string ("Improbable line height: %f",
178                                     dim[Y_AXIS]));
179       dim[Y_AXIS] = 50 CM;
180     }
181
182   output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
183                              ly_quote_scm (ly_offset2scm (*origin)),
184                              ly_quote_scm (ly_offset2scm (dim))));
185
186   output_stencil (unsmob_stencil (p->to_stencil ()));
187
188   (*origin)[Y_AXIS] += dim[Y_AXIS];
189   output_scheme (scm_list_2 (ly_symbol2scm ("stop-system"),
190                              ly_bool2scm (is_last)));
191 }
192
193 void
194 Paper_outputter::output_page (Page *p, bool is_last)
195 {
196   output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
197
198   output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
199                              ly_quote_scm (ly_offset2scm (Offset (0, 0))),
200                              ly_quote_scm (ly_offset2scm (Offset (0, 0)))));
201
202   output_stencil (unsmob_stencil (p->to_stencil ()));
203
204   output_scheme (scm_list_2 (ly_symbol2scm ("stop-system"), SCM_BOOL_T));
205   output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
206                              ly_bool2scm (is_last
207                                           && !unsmob_stencil (p->footer_))));
208 }
209
210 void
211 Paper_outputter::output_music_output_def (Music_output_def *odef)
212 {
213   output_scheme (scm_list_2 (ly_symbol2scm ("output-paper-def"),
214                               odef->self_scm ()));
215 }
216
217 void
218 Paper_outputter::output_stencil (Stencil *stil)
219 {
220   output_expr (stil->expr (), stil->origin ());
221 }
222
223 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil  */
224 void
225 Paper_outputter::output_expr (SCM expr, Offset o)
226 {
227   while (1)
228     {
229       if (!ly_c_pair_p (expr))
230         return;
231   
232       SCM head =ly_car (expr);
233       if (unsmob_input (head))
234         {
235           Input *ip = unsmob_input (head);
236           output_scheme (scm_list_4 (ly_symbol2scm ("define-origin"),
237                                       scm_makfrom0str (ip->file_string ()
238                                                        .to_str0 ()),
239                                       scm_int2num (ip->line_number ()),
240                                       scm_int2num (ip->column_number ())));
241           expr = ly_cadr (expr);
242         }
243       else  if (head ==  ly_symbol2scm ("no-origin"))
244         {
245           output_scheme (scm_list_1 (head));
246           expr = ly_cadr (expr);
247         }
248       else if (head == ly_symbol2scm ("translate-stencil"))
249         {
250           o += ly_scm2offset (ly_cadr (expr));
251           expr = ly_caddr (expr);
252         }
253       else if (head == ly_symbol2scm ("combine-stencil"))
254         {
255           output_expr (ly_cadr (expr), o);
256           expr = ly_caddr (expr);
257         }
258       else
259         {
260           output_scheme (scm_list_4 (ly_symbol2scm ("placebox"),
261                                      scm_make_real (o[X_AXIS]),
262                                      scm_make_real (o[Y_AXIS]),
263                                      expr));
264           return;
265         }
266     }
267 }