]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
*** empty log message ***
[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_UNDEFINED;
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         
70         "number-pair->string",       /* output-lib.scm */
71         "numbers->string",
72         
73         "assoc-get",
74         "inexact->string",           /* insecure guile? */
75 #if IMPORT_LESS 
76         "string-index",              /* from srfi srfi-13 */
77         "regexp-substitute/global",  /* from (ice9 regex) */
78 #endif  
79         0,
80       };
81       
82       for (int i = 0; imports[i]; i++)
83         {
84           SCM s = ly_symbol2scm (imports[i]);
85           scm_module_define (output_module_, s, scm_primitive_eval (s));
86         }
87 #ifndef IMPORT_LESS  // rather crude, esp for safe-mode let's not
88       SCM m = scm_set_current_module (output_module_);
89       /* not present in current module*/
90       scm_c_use_module ("ice-9 regex");
91       scm_c_use_module ("srfi srfi-13");
92       /* Need only a few of these, see above
93          scm_c_use_module ("lily"); */
94       scm_set_current_module (m);
95 #endif
96     }
97   else
98     {
99       static SCM find_dumper;
100       if (!find_dumper)
101         find_dumper = scm_c_eval_string ("find-dumper");
102       
103       output_func_
104         = scm_call_1 (find_dumper,
105                       scm_makfrom0str (output_format_global.to_str0 ()));
106       output_module_ = SCM_UNDEFINED;
107     }
108 }
109
110 Paper_outputter::~Paper_outputter ()
111 {
112   scm_close_port (file_);
113   file_ = SCM_EOL;
114 }
115
116 void
117 Paper_outputter::output_scheme (SCM scm)
118 {
119   if (output_format_global == PAGE_LAYOUT)
120     scm_display (scm_eval (scm, output_module_), file_);
121   else
122     gh_call2 (output_func_, scm, file_);
123 }
124
125 void
126 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
127 {
128   SCM fields = SCM_EOL;
129   for (int i = dump_header_fieldnames_global.size (); i--; )
130     fields
131       = gh_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
132                  fields);
133   output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
134                              paper->self_scm (),
135                              scm_list_n (ly_symbol2scm ("quote"),
136                                          scopes, SCM_UNDEFINED),
137                              scm_list_n (ly_symbol2scm ("quote"),
138                                          fields, SCM_UNDEFINED),
139                              scm_makfrom0str (basename_.to_str0 ()), 
140                              SCM_UNDEFINED));
141 }
142
143 void
144 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count)
145 {
146   String creator = gnu_lilypond_version_string ();
147   creator += " (http://lilypond.org)";
148   time_t t (time (0));
149   String time_stamp = ctime (&t);
150   time_stamp = time_stamp.left_string (time_stamp.length () - 1)
151     + " " + *tzname;
152   output_scheme (scm_list_4 (ly_symbol2scm ("header"),
153                              scm_makfrom0str (creator.to_str0 ()),
154                              scm_makfrom0str (time_stamp.to_str0 ()),
155                              scm_int2num (page_count)));
156
157   output_metadata (paper, scopes);
158   output_music_output_def (paper);
159
160   output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
161   output_scheme (scm_list_2 (ly_symbol2scm ("define-fonts"),
162                              ly_quote_scm (paper->font_descriptions ())));
163 }
164
165 void
166 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
167 {
168   Paper_line *pl = unsmob_paper_line (line);
169   Offset dim = pl->dim ();
170   if (dim[Y_AXIS] > 50 CM)
171     {
172       programming_error ("Improbable system height.");
173       dim[Y_AXIS] = 50 CM;
174     }
175
176   if (output_format_global != PAGE_LAYOUT)
177     output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
178                                gh_double2scm (dim[X_AXIS]),
179                                gh_double2scm (dim[Y_AXIS])));
180   else
181     {
182       output_scheme (scm_list_3 (ly_symbol2scm ("new-start-system"),
183                                  ly_quote_scm (ly_offset2scm (*origin)),
184                                  ly_quote_scm (ly_offset2scm (dim))));
185       (*origin)[Y_AXIS] += dim[Y_AXIS];
186     }
187
188   SCM between = SCM_EOL;
189   for (SCM s = pl->stencils (); gh_pair_p (s); s = ly_cdr (s))
190     {
191       Stencil *stil = unsmob_stencil (ly_car (s));
192       if (stil)
193         output_expr (stil->get_expr (), stil->origin ());
194       /* Only if !PAGE_LAYOUT */
195       else if (ly_caar (s) == ly_symbol2scm ("between-system-string"))
196         between = ly_cdar (s);
197     }
198
199   if (is_last)
200     output_scheme (scm_list_1 (ly_symbol2scm ("stop-last-system")));
201   else
202     {
203       output_scheme (scm_list_1 (ly_symbol2scm ("stop-system")));
204       if (output_format_global != PAGE_LAYOUT && between != SCM_EOL)
205         output_scheme (between);
206     }
207 }
208
209 void
210 Paper_outputter::output_music_output_def (Music_output_def* odef)
211 {
212   output_scheme (scm_list_n (ly_symbol2scm ("output-paper-def"),
213                              odef->self_scm (), SCM_UNDEFINED));
214 }
215
216 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil  */
217 void
218 Paper_outputter::output_expr (SCM expr, Offset o)
219 {
220   while (1)
221     {
222       if (!gh_pair_p (expr))
223         return;
224   
225       SCM head =ly_car (expr);
226       if (unsmob_input (head))
227         {
228           Input * ip = unsmob_input (head);
229       
230           output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
231                                      scm_makfrom0str (ip->file_string ().to_str0 ()),
232                                      gh_int2scm (ip->line_number ()),
233                                      gh_int2scm (ip->column_number ()),
234                                      SCM_UNDEFINED));
235           expr = ly_cadr (expr);
236         }
237       else  if (head ==  ly_symbol2scm ("no-origin"))
238         {
239           output_scheme (scm_list_n (head, SCM_UNDEFINED));
240           expr = ly_cadr (expr);
241         }
242       else if (head == ly_symbol2scm ("translate-stencil"))
243         {
244           o += ly_scm2offset (ly_cadr (expr));
245           expr = ly_caddr (expr);
246         }
247       else if (head == ly_symbol2scm ("combine-stencil"))
248         {
249           output_expr (ly_cadr (expr), o);
250           expr = ly_caddr (expr);
251         }
252       else
253         {
254           output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
255                                      gh_double2scm (o[X_AXIS]),
256                                      gh_double2scm (o[Y_AXIS]),
257                                      expr,
258                                      SCM_UNDEFINED));
259           return;
260         }
261     }
262 }
263