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