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