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