]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
(classic_output): don't advance Offset for
[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 "scm-hash.hh"
26 #include "stencil.hh"
27 #include "string-convert.hh"
28 #include "warn.hh"
29
30
31 Paper_outputter::Paper_outputter (String filename)
32 {
33   if (safe_global_b)
34     scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);      
35   
36   file_ = scm_open_file (scm_makfrom0str (filename.to_str0 ()),
37                          scm_makfrom0str ("w"));
38
39   String module_name = "scm output-" + output_format_global;
40   if (safe_global_b)
41     {
42       /* In safe mode, start from a GUILE safe-module and import
43          all symbols from the output module.  */
44       scm_c_use_module ("ice-9 safe");
45       SCM msm = scm_primitive_eval (ly_symbol2scm ("make-safe-module"));
46       output_module_ = scm_call_0 (msm);
47       ly_import_module (output_module_,
48                         scm_c_resolve_module (module_name.to_str0 ()));
49     }
50   else
51     output_module_ = scm_c_resolve_module (module_name.to_str0 ());
52   
53   /* FIXME: output-lib should be module, that can be imported.  */
54 #define IMPORT_LESS 1 // only import the list of IMPORTS
55 #if IMPORT_LESS
56   scm_c_use_module ("lily");
57   scm_c_use_module ("ice-9 regex");
58   scm_c_use_module ("srfi srfi-1");
59   scm_c_use_module ("srfi srfi-13");
60 #endif
61   char const *imports[] = {
62     "lilypond-version",          /* from lily */
63     "ly:output-def-scope",
64     "ly:gulp-file",
65     "ly:number->string",
66     "ly:ragged-page-breaks",
67     "ly:optimal-page-breaks",
68     
69     "ly:number-pair->string",    /* output-lib.scm */
70     "ly:numbers->string",
71     "ly:inexact->string",
72     
73     "assoc-get",
74 #if IMPORT_LESS 
75     "remove",                    /* from srfi srfi-1 */
76     "string-index",              /* from srfi srfi-13 */
77     "string-join",
78     "regexp-substitute/global",  /* from (ice9 regex) */
79 #endif  
80     0,
81   };
82       
83   for (int i = 0; imports[i]; i++)
84     {
85       SCM s = ly_symbol2scm (imports[i]);
86       scm_module_define (output_module_, s, scm_primitive_eval (s));
87     }
88 #ifndef IMPORT_LESS  // rather crude, esp for safe-mode let's not
89   SCM m = scm_set_current_module (output_module_);
90   /* not present in current module*/
91   scm_c_use_module ("ice-9 regex");
92   scm_c_use_module ("srfi srfi-13");
93   /* Need only a few of these, see above
94      scm_c_use_module ("lily"); */
95   scm_set_current_module (m);
96 #endif
97 }
98
99 Paper_outputter::~Paper_outputter ()
100 {
101   scm_close_port (file_);
102   file_ = SCM_EOL;
103 }
104
105 void
106 Paper_outputter::output_scheme (SCM scm)
107 {
108   scm_display (scm_eval (scm, output_module_), file_);
109 }
110
111 void
112 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
113 {
114   SCM fields = SCM_EOL;
115   for (int i = dump_header_fieldnames_global.size (); i--; )
116     fields
117       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
118                  fields);
119   output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
120                              paper->self_scm (),
121                              ly_quote_scm (scopes),
122                              ly_quote_scm (fields),
123                              scm_makfrom0str (basename_.to_str0 ()), 
124                              SCM_UNDEFINED));
125 }
126
127 void
128 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count,
129                                 bool is_classic)
130 {
131   String creator = gnu_lilypond_version_string ();
132   creator += " (http://lilypond.org)";
133   time_t t (time (0));
134   String time_stamp = ctime (&t);
135   time_stamp = time_stamp.left_string (time_stamp.length () - 1)
136     + " " + *tzname;
137   output_scheme (scm_list_n (ly_symbol2scm ("header"),
138                              scm_makfrom0str (creator.to_str0 ()),
139                              scm_makfrom0str (time_stamp.to_str0 ()),
140                              paper->self_scm (),
141                              scm_int2num (page_count),
142                              ly_bool2scm (is_classic),
143                              SCM_UNDEFINED));
144
145   output_metadata (paper, scopes);
146   output_music_output_def (paper);
147
148   output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
149
150   /* TODO: maybe have Scheme extract the fonts directly from \paper ?
151           
152      Alternatively, we could simply load the fonts on demand in the
153      output, and do away with this define-fonts step.  */
154   SCM fonts = paper->font_descriptions ();
155   output_scheme (scm_list_3 (ly_symbol2scm ("define-fonts"),
156                              paper->self_scm (),
157                              //FIXME:
158                              ly_quote_scm (ly_list_qsort_uniq_x (fonts))));
159 }
160
161 void
162 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
163 {
164   Paper_line *pl = unsmob_paper_line (line);
165   Offset dim = pl->dim ();
166   if (dim[Y_AXIS] > 50 CM)
167     {
168       programming_error ("Improbable system height.");
169       dim[Y_AXIS] = 50 CM;
170     }
171
172   output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
173                              ly_quote_scm (ly_offset2scm (*origin)),
174                              ly_quote_scm (ly_offset2scm (dim))));
175
176   for (SCM s = pl->stencils (); is_pair (s); s = ly_cdr (s))
177     output_expr (unsmob_stencil (ly_car (s))->get_expr (), Offset (0, 0));
178
179   output_scheme (scm_list_2 (ly_symbol2scm ("stop-system"),
180                              ly_bool2scm (is_last)));
181
182   (*origin)[Y_AXIS] += dim[Y_AXIS];
183 }
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