]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
* scm/output-tex.scm (font-command): use ly: functions to
[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 <time.h>
11 #include <math.h>
12
13 #include "dimensions.hh"
14 #include "virtual-methods.hh"
15 #include "paper-outputter.hh"
16 #include "stencil.hh"
17 #include "array.hh"
18 #include "string-convert.hh"
19 #include "warn.hh"
20 #include "font-metric.hh"
21 #include "main.hh"
22 #include "scm-hash.hh"
23 #include "lily-version.hh"
24 #include "paper-def.hh"
25 #include "input-file-results.hh"
26 #include "ly-module.hh"
27 #include "paper-book.hh"
28 #include "paper-line.hh"
29 #include "input-smob.hh"  // output_expr
30
31
32 Paper_outputter::Paper_outputter (String name)
33 {
34   if (safe_global_b)
35     scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);      
36   
37   file_ = scm_open_file (scm_makfrom0str (name.to_str0 ()),
38                          scm_makfrom0str ("w"));
39
40   if (output_format_global == PAGE_LAYOUT)
41     {
42       output_func_ = SCM_EOL;
43       String name = "scm output-" + output_format_global;
44       if (safe_global_b)
45         {
46           /* In safe mode, start from a GUILE safe-module and import
47              all symbols from the output module.  */
48           scm_c_use_module ("ice-9 safe");
49           SCM msm = scm_primitive_eval (ly_symbol2scm ("make-safe-module"));
50           output_module_ = scm_call_0 (msm);
51           ly_import_module (output_module_,
52                             scm_c_resolve_module (name.to_str0 ()));
53         }
54       else
55         output_module_ = scm_c_resolve_module (name.to_str0 ());
56
57       /* FIXME: output-lib should be module, that can be imported.  */
58 #define IMPORT_LESS 1 // only import the list of IMPORTS
59 #if IMPORT_LESS
60       scm_c_use_module ("lily");
61       scm_c_use_module ("ice-9 regex");
62       scm_c_use_module ("srfi srfi-13");
63 #endif
64       char const *imports[] = {
65         "lilypond-version",          /* from lily */
66         "ly:output-def-scope",
67         "ly:gulp-file",
68         "ly:number->string",
69         "ly:ragged-page-breaks",
70         "ly:optimal-page-breaks",
71         
72         "ly:number-pair->string",    /* output-lib.scm */
73         "ly:numbers->string",
74         "ly:inexact->string",
75         
76         "assoc-get",
77 #if IMPORT_LESS 
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   else
101     {
102       output_func_
103         = scm_call_1 (ly_scheme_function ("find-dumper"),
104                       scm_makfrom0str (output_format_global.to_str0 ()));
105       output_module_ = SCM_EOL;
106     }
107 }
108
109 Paper_outputter::~Paper_outputter ()
110 {
111   scm_close_port (file_);
112   file_ = SCM_EOL;
113 }
114
115 void
116 Paper_outputter::output_scheme (SCM scm)
117 {
118   if (output_format_global == PAGE_LAYOUT)
119     scm_display (scm_eval (scm, output_module_), file_);
120   else
121     gh_call2 (output_func_, scm, file_);
122 }
123
124 void
125 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
126 {
127   SCM fields = SCM_EOL;
128   for (int i = dump_header_fieldnames_global.size (); i--; )
129     fields
130       = gh_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
131                  fields);
132   output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
133                              paper->self_scm (),
134                              ly_quote_scm (scopes),
135                              ly_quote_scm (fields),
136                              scm_makfrom0str (basename_.to_str0 ()), 
137                              SCM_UNDEFINED));
138 }
139
140 void
141 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count)
142 {
143   String creator = gnu_lilypond_version_string ();
144   creator += " (http://lilypond.org)";
145   time_t t (time (0));
146   String time_stamp = ctime (&t);
147   time_stamp = time_stamp.left_string (time_stamp.length () - 1)
148     + " " + *tzname;
149   output_scheme (scm_list_4 (ly_symbol2scm ("header"),
150                              scm_makfrom0str (creator.to_str0 ()),
151                              scm_makfrom0str (time_stamp.to_str0 ()),
152                              scm_int2num (page_count)));
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   /*
160     TODO: maybe have Scheme extract the fonts directly from \paper?
161
162     Alternatively, we could simply load the fonts on demand in the
163     output, and do away with this define-fonts step.
164    */
165   output_scheme (scm_list_3 (ly_symbol2scm ("define-fonts"),
166                              paper->self_scm (),
167                              ly_quote_scm (paper->font_descriptions ())));
168 }
169
170 void
171 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
172 {
173   Paper_line *pl = unsmob_paper_line (line);
174   Offset dim = pl->dim ();
175   if (dim[Y_AXIS] > 50 CM)
176     {
177       programming_error ("Improbable system height.");
178       dim[Y_AXIS] = 50 CM;
179     }
180
181   if (output_format_global != PAGE_LAYOUT)
182     output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
183                                gh_double2scm (dim[X_AXIS]),
184                                gh_double2scm (dim[Y_AXIS])));
185   else
186     {
187       output_scheme (scm_list_3 (ly_symbol2scm ("new-start-system"),
188                                  ly_quote_scm (ly_offset2scm (*origin)),
189                                  ly_quote_scm (ly_offset2scm (dim))));
190       (*origin)[Y_AXIS] += dim[Y_AXIS];
191     }
192
193   SCM between = SCM_EOL;
194   for (SCM s = pl->stencils (); gh_pair_p (s); s = ly_cdr (s))
195     {
196       Stencil *stil = unsmob_stencil (ly_car (s));
197       if (stil)
198         output_expr (stil->get_expr (), Offset (0,0));
199       /* Only if !PAGE_LAYOUT */
200       else if (ly_caar (s) == ly_symbol2scm ("between-system-string"))
201         between = ly_cdar (s);
202     }
203
204   if (is_last)
205     output_scheme (scm_list_1 (ly_symbol2scm ("stop-last-system")));
206   else
207     {
208       output_scheme (scm_list_1 (ly_symbol2scm ("stop-system")));
209       if (output_format_global != PAGE_LAYOUT && between != SCM_EOL)
210         output_scheme (between);
211     }
212 }
213
214 void
215 Paper_outputter::output_music_output_def (Music_output_def* odef)
216 {
217   output_scheme (scm_list_n (ly_symbol2scm ("output-paper-def"),
218                              odef->self_scm (), SCM_UNDEFINED));
219 }
220
221 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil  */
222 void
223 Paper_outputter::output_expr (SCM expr, Offset o)
224 {
225   while (1)
226     {
227       if (!gh_pair_p (expr))
228         return;
229   
230       SCM head =ly_car (expr);
231       if (unsmob_input (head))
232         {
233           Input * ip = unsmob_input (head);
234       
235           output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
236                                      scm_makfrom0str (ip->file_string ().to_str0 ()),
237                                      gh_int2scm (ip->line_number ()),
238                                      gh_int2scm (ip->column_number ()),
239                                      SCM_UNDEFINED));
240           expr = ly_cadr (expr);
241         }
242       else  if (head ==  ly_symbol2scm ("no-origin"))
243         {
244           output_scheme (scm_list_n (head, SCM_UNDEFINED));
245           expr = ly_cadr (expr);
246         }
247       else if (head == ly_symbol2scm ("translate-stencil"))
248         {
249           o += ly_scm2offset (ly_cadr (expr));
250           expr = ly_caddr (expr);
251         }
252       else if (head == ly_symbol2scm ("combine-stencil"))
253         {
254           output_expr (ly_cadr (expr), o);
255           expr = ly_caddr (expr);
256         }
257       else
258         {
259           output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
260                                      gh_double2scm (o[X_AXIS]),
261                                      gh_double2scm (o[Y_AXIS]),
262                                      expr,
263                                      SCM_UNDEFINED));
264           return;
265         }
266     }
267 }
268