]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
release: 1.3.118
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include <time.h>
11 #include <fstream.h>
12 #include <math.h>
13 #include <iostream.h>
14
15 #include "dimensions.hh"
16 #include "virtual-methods.hh"
17 #include "paper-outputter.hh"
18 #include "paper-stream.hh"
19 #include "molecule.hh"
20 #include "array.hh"
21 #include "string-convert.hh"
22 #include "debug.hh"
23 #include "font-metric.hh"
24 #include "main.hh"
25 #include "scope.hh"
26
27 #include "lily-version.hh"
28 #include "paper-def.hh"
29 #include "file-results.hh"
30
31
32 /*
33   Ugh, this is messy.
34  */
35
36 Paper_outputter::Paper_outputter (String name)
37 {
38   stream_p_ =  new Paper_stream (name);
39
40  /*
41    lilypond -f scm x.ly
42    guile -s x.scm
43   */
44   verbatim_scheme_b_ =  output_global_ch == String ("scm");
45
46   if (verbatim_scheme_b_)
47     {
48         *stream_p_ << ""
49           ";;; Usage: guile -s x.scm > x.tex\n"
50           "(primitive-load-path 'standalone.scm)\n"
51           ";(scm-tex-output)\n"
52           "(scm-ps-output)\n"
53           "(map (lambda (x) (display (ly-eval x))) '(\n"
54         ;
55     }
56
57 }
58
59 Paper_outputter::~Paper_outputter ()
60 {
61   if (verbatim_scheme_b_)
62     {
63       *stream_p_ << "))";
64     }
65   delete stream_p_;
66 }
67
68
69 void
70 Paper_outputter::output_header ()
71 {
72   if (safe_global_b)
73     {
74       gh_define ("security-paranoia", SCM_BOOL_T);      
75     }
76
77   SCM exp = gh_list (ly_symbol2scm ((String (output_global_ch) + "-scm").ch_C()),
78                      ly_quote_scm (ly_symbol2scm ("all-definitions")),
79                      SCM_UNDEFINED);
80   exp = scm_eval2 (exp, SCM_EOL);
81   scm_eval2 (exp, SCM_EOL);
82   
83   String creator;
84   if (no_timestamps_global_b)
85     creator = gnu_lilypond_str ();
86   else
87     creator = gnu_lilypond_version_str ();
88   
89   String generate;
90   if (no_timestamps_global_b)
91     generate = ".";
92   else
93     {
94       generate = _ (", at ");
95       time_t t (time (0));
96       generate += ctime (&t);
97       generate = generate.left_str (generate.length_i () - 1);
98     }
99
100   SCM args_scm = 
101     gh_list (ly_str02scm (creator.ch_l ()),
102              ly_str02scm (generate.ch_l ()), SCM_UNDEFINED);
103
104
105   SCM scm = gh_cons (ly_symbol2scm ("header"), args_scm);
106   output_scheme (scm);
107 }
108
109
110
111 void
112 Paper_outputter::output_comment (String str)
113 {
114   output_scheme (gh_list (ly_symbol2scm ("comment"),
115                           ly_str02scm ((char*)str.ch_C()),
116                           SCM_UNDEFINED)
117                  );
118 }
119
120
121 void
122 Paper_outputter::output_scheme (SCM scm)
123 {
124   /*
125     we don't rename dump_scheme, because we might in the future want
126     to remember Scheme. We don't now, because it sucks up a lot of memory.
127   */
128   dump_scheme (scm);
129 }
130
131
132 /*
133   UGH.
134
135   Should probably change interface to do less eval ( symbol ), and more
136   apply (procedure, args)
137  */
138 void
139 Paper_outputter::dump_scheme (SCM s)
140 {
141   if  (verbatim_scheme_b_)
142     {
143       *stream_p_ << ly_scm2string (ly_write2scm (s));
144     }
145   else
146     {
147       SCM result = scm_eval2 (s, SCM_EOL);
148       char *c=gh_scm2newstr (result, NULL);
149   
150       *stream_p_ << c;
151       free (c);
152     }
153 }
154
155 void
156 Paper_outputter::output_scope (Scope *scope, String prefix)
157 {
158   SCM al = scope->to_alist ();
159   for (SCM s = al ; gh_pair_p (s); s = gh_cdr (s))
160     {
161       SCM k = gh_caar (s);
162       SCM v = gh_cdar (s);
163       String s = ly_symbol2string (k);
164
165       
166       if (gh_string_p (v))
167         {
168           output_String_def (prefix + s, ly_scm2string (v));
169         }
170       else if (scm_integer_p (v) == SCM_BOOL_T)
171         {
172           output_int_def (prefix + s, gh_scm2int (v));    
173         }
174       else if (gh_number_p (v))
175         {
176           output_Real_def (prefix + s, gh_scm2double (v));        
177         }
178     }
179 }
180
181 void
182 Paper_outputter::output_version ()
183 {
184   String id_str = "Lily was here";
185   if (no_timestamps_global_b)
186     id_str += ".";
187   else
188     id_str += String (", ") + version_str ();
189
190   output_String_def ( "lilypondtagline", id_str);
191   output_String_def ( "LilyPondVersion", version_str ());
192 }
193
194
195
196
197 void
198 Paper_outputter::output_Real_def (String k, Real v)
199 {
200   
201   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
202                      ly_str02scm (k.ch_l ()),
203                      ly_str02scm (to_str(v).ch_l ()),
204                      SCM_UNDEFINED);
205   output_scheme (scm);
206 }
207
208 void
209 Paper_outputter::output_String_def (String k, String v)
210 {
211   
212   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
213                      ly_str02scm (k.ch_l ()),
214                      ly_str02scm (v.ch_l ()),
215                      SCM_UNDEFINED);
216   output_scheme (scm);
217 }
218
219 void
220 Paper_outputter::output_int_def (String k, int v)
221 {
222   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
223                      ly_str02scm (k.ch_l ()),
224                      ly_str02scm (to_str (v).ch_l ()),
225                      SCM_UNDEFINED);
226   output_scheme (scm);
227 }
228
229 void
230 Paper_outputter::output_string (SCM str)
231 {
232   *stream_p_ <<  ly_scm2string (str);
233 }
234
235 void
236 Paper_outputter::write_header_field_to_file (String filename, String key, String value)
237 {
238   if (filename != "-")
239     filename += String (".") + key;
240   progress_indication (_f ("writing header field %s to %s...",
241                            key,
242                            filename == "-" ? String ("<stdout>") : filename));
243   
244   ostream *os = open_file_stream (filename);
245   *os << value;
246   close_file_stream (os);
247   progress_indication ("\n");
248 }
249
250 void
251 Paper_outputter::write_header_fields_to_file (Scope * header)
252 {
253   if (global_dumped_header_fieldnames.size ())
254     {
255       SCM fields = header->to_alist ();
256       for (int i = 0; i < global_dumped_header_fieldnames.size (); i++)
257         {
258           String key = global_dumped_header_fieldnames[i];
259           SCM val = gh_assoc (ly_symbol2scm (key.ch_C ()), fields);
260           String s;
261           /* Only write header field to file if it exists */
262           if (gh_pair_p (val))
263             {
264               s = ly_scm2string (gh_cdr (val));
265               /* Always write header field file, even if string is empty ... */
266               write_header_field_to_file (basename_, key, s);
267             }
268         }
269     }
270 }