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