]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
134b19bc4c01f30d6c1f545d431bfc8219b1e5a4
[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 "input-smob.hh"  // output_expr
29
30
31 Paper_outputter::Paper_outputter (String name)
32 {
33   if (safe_global_b)
34     scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);      
35   
36   file_ = scm_open_file (scm_makfrom0str (name.to_str0 ()),
37                          scm_makfrom0str ("w"));
38
39   if (output_format_global == PAGE_LAYOUT)
40     {
41       output_func_ = SCM_UNDEFINED;
42       String name = "scm output-" + output_format_global;
43       if (safe_global_b)
44         {
45           /* In safe mode, start from a GUILE safe-module and import
46              all symbols from the output module.  */
47           scm_c_use_module ("ice-9 safe");
48           SCM msm = scm_primitive_eval (ly_symbol2scm ("make-safe-module"));
49           output_module_ = scm_call_0 (msm);
50           ly_import_module (output_module_,
51                             scm_c_resolve_module (name.to_str0 ()));
52         }
53       else
54         output_module_ = scm_c_resolve_module (name.to_str0 ());
55
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-13");
61 #endif
62       char const *imports[] = {
63         "lilypond-version",          /* from lily */
64         "ly:output-def-scope",
65         "ly:gulp-file",
66         "ly:number->string",
67         "assoc-get",
68         "number-pair->string",
69         "inexact->string",
70         "numbers->string",
71 #if IMPORT_LESS 
72         "string-index",              /* from srfi srfi-13 */
73         "regexp-substitute/global",  /* from (ice9 regex) */
74 #endif  
75         0,
76       };
77       
78       for (int i = 0; imports[i]; i++)
79         {
80           SCM s = ly_symbol2scm (imports[i]);
81           scm_module_define (output_module_, s, scm_primitive_eval (s));
82         }
83 #ifndef IMPORT_LESS  // rather crude, esp for safe-mode let's not
84       SCM m = scm_set_current_module (output_module_);
85       /* not present in current module*/
86       scm_c_use_module ("ice-9 regex");
87       scm_c_use_module ("srfi srfi-13");
88       /* Need only a few of these, see above
89          scm_c_use_module ("lily"); */
90       scm_set_current_module (m);
91 #endif
92     }
93   else
94     {
95       static SCM find_dumper;
96       if (!find_dumper)
97         find_dumper = scm_c_eval_string ("find-dumper");
98       
99       output_func_
100         = scm_call_1 (find_dumper,
101                       scm_makfrom0str (output_format_global.to_str0 ()));
102       output_module_ = SCM_UNDEFINED;
103     }
104 }
105
106 Paper_outputter::~Paper_outputter ()
107 {
108   scm_close_port (file_);
109   file_ = SCM_EOL;
110 }
111
112 void
113 Paper_outputter::output_scheme (SCM scm)
114 {
115   if (output_format_global == PAGE_LAYOUT)
116     scm_display (scm_eval (scm, output_module_), file_);
117   else
118     gh_call2 (output_func_, scm, file_);
119 }
120
121 void
122 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
123 {
124   SCM fields = SCM_EOL;
125   for (int i = dump_header_fieldnames_global.size (); i--; )
126     fields
127       = gh_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
128                  fields);
129   output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
130                              paper->self_scm (),
131                              scm_list_n (ly_symbol2scm ("quote"),
132                                          scopes, SCM_UNDEFINED),
133                              scm_list_n (ly_symbol2scm ("quote"),
134                                          fields, SCM_UNDEFINED),
135                              scm_makfrom0str (basename_.to_str0 ()), 
136                              SCM_UNDEFINED));
137 }
138
139 void
140 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count)
141 {
142   String creator = gnu_lilypond_version_string ();
143   creator += " (http://lilypond.org)";
144   time_t t (time (0));
145   String time_stamp = ctime (&t);
146   time_stamp = time_stamp.left_string (time_stamp.length () - 1)
147     + " " + *tzname;
148   output_scheme (scm_list_4 (ly_symbol2scm ("header"),
149                              scm_makfrom0str (creator.to_str0 ()),
150                              scm_makfrom0str (time_stamp.to_str0 ()),
151                              scm_int2num (page_count)));
152
153   output_metadata (paper, scopes);
154   output_music_output_def (paper);
155
156   output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
157   output_scheme (scm_list_2 (ly_symbol2scm ("define-fonts"),
158                              ly_quote_scm (paper->font_descriptions ())));
159 }
160
161 void
162 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
163 {
164   Offset dim = ly_scm2offset (ly_car (line));
165   if (dim[Y_AXIS] > 50 CM)
166     {
167       programming_error ("Improbable system height.");
168       dim[Y_AXIS] = 50 CM;
169     }
170
171   if (output_format_global != PAGE_LAYOUT)
172     output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
173                                gh_double2scm (dim[X_AXIS]),
174                                gh_double2scm (dim[Y_AXIS])));
175   else
176     {
177       output_scheme (scm_list_3 (ly_symbol2scm ("new-start-system"),
178                                  ly_quote_scm (ly_offset2scm (*origin)),
179                                  ly_quote_scm (ly_offset2scm (dim))));
180       (*origin)[Y_AXIS] += dim[Y_AXIS];
181     }
182
183   SCM between = SCM_EOL;
184   for (SCM s = ly_cdr (line); gh_pair_p (s); s = ly_cdr (s))
185     {
186       Stencil *stil = unsmob_stencil (ly_cdar (s));
187       SCM head = ly_caar (s);
188       
189       if (head == ly_symbol2scm ("between-system-string"))
190         {
191           between = stil->get_expr ();
192           continue;
193         }
194
195       if (stil)
196         output_expr (stil->get_expr (), ly_scm2offset (head));
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