]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
4c1f865fd25735656f6a7bc592ecb5eb6092be58
[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--2001 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_format_global == "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 = scm_list_n (ly_symbol2scm ((output_format_global + "-scm").ch_C ()),
78                      ly_quote_scm (ly_symbol2scm ("all-definitions")),
79                      SCM_UNDEFINED);
80   exp = scm_primitive_eval (exp);
81   scm_primitive_eval (exp);
82   
83   String creator = gnu_lilypond_version_str ();
84   
85   String       generate = _ (", at ");
86   time_t t (time (0));
87   generate += ctime (&t);
88   generate = generate.left_str (generate.length_i () - 1);
89   
90   /*
91     Make fixed length time stamps
92    */
93   generate = generate + to_str (' ' * (120 - generate.length_i ())>? 0)  ;
94   
95   SCM args_scm = 
96     scm_list_n (ly_str02scm (creator.ch_l ()),
97              ly_str02scm (generate.ch_l ()), SCM_UNDEFINED);
98
99
100   SCM scm = gh_cons (ly_symbol2scm ("header"), args_scm);
101   output_scheme (scm);
102 }
103
104
105
106 void
107 Paper_outputter::output_comment (String str)
108 {
109   output_scheme (scm_list_n (ly_symbol2scm ("comment"),
110                           ly_str02scm ((char*)str.ch_C ()),
111                           SCM_UNDEFINED)
112                  );
113 }
114
115
116 void
117 Paper_outputter::output_scheme (SCM scm)
118 {
119   /*
120     we don't rename dump_scheme, because we might in the future want
121     to remember Scheme. We don't now, because it sucks up a lot of memory.
122   */
123   dump_scheme (scm);
124 }
125
126 void flatten_write (SCM x, Paper_stream*ps)
127 {
128   if (ly_pair_p (x))
129     {
130       flatten_write (ly_car (x),ps);
131       flatten_write (ly_cdr (x),ps);
132     }
133   else if (gh_string_p (x))
134     {
135       *ps  << String ( SCM_STRING_CHARS(x)) ;
136     }
137 }
138
139
140 /*
141   UGH.
142
143   Should probably change interface to do less eval (symbol), and more
144   apply (procedure, args)
145  */
146 void
147 Paper_outputter::dump_scheme (SCM s)
148 {
149   if (verbatim_scheme_b_)
150     {
151       *stream_p_ << ly_scm2string (ly_write2scm (s));
152     }
153   else
154     {
155       SCM result = scm_primitive_eval (s);
156       flatten_write (result, stream_p_);
157     }
158 }
159
160 void
161 Paper_outputter::output_scope (Scope *scope, String prefix)
162 {
163   SCM al = scope->to_alist ();
164   for (SCM s = al ; gh_pair_p (s); s = ly_cdr (s))
165     {
166       SCM k = ly_caar (s);
167       SCM v = ly_cdar (s);
168       String s = ly_symbol2string (k);
169
170       
171       if (gh_string_p (v))
172         {
173           output_String_def (prefix + s, ly_scm2string (v));
174         }
175       else if (scm_exact_p (v) == SCM_BOOL_T)
176         {
177           output_int_def (prefix + s, gh_scm2int (v));    
178         }
179       else if (gh_number_p (v))
180         {
181           output_Real_def (prefix + s, gh_scm2double (v));        
182         }
183     }
184 }
185
186 void
187 Paper_outputter::output_version ()
188 {
189   String id_str = "Lily was here";
190   id_str += String_convert::pad_to (String (", ") + version_str (), 40);
191
192   output_String_def ("lilypondtagline", id_str);
193   output_String_def ("LilyPondVersion", version_str ());
194 }
195
196
197
198
199 void
200 Paper_outputter::output_Real_def (String k, Real v)
201 {
202   
203   SCM scm = scm_list_n (ly_symbol2scm ("lily-def"),
204                      ly_str02scm (k.ch_l ()),
205                      ly_str02scm (to_str (v).ch_l ()),
206                      SCM_UNDEFINED);
207   output_scheme (scm);
208 }
209
210 void
211 Paper_outputter::output_String_def (String k, String v)
212 {
213   
214   SCM scm = scm_list_n (ly_symbol2scm ("lily-def"),
215                      ly_str02scm (k.ch_l ()),
216                      ly_str02scm (v.ch_l ()),
217                      SCM_UNDEFINED);
218   output_scheme (scm);
219 }
220
221 void
222 Paper_outputter::output_int_def (String k, int v)
223 {
224   SCM scm = scm_list_n (ly_symbol2scm ("lily-def"),
225                      ly_str02scm (k.ch_l ()),
226                      ly_str02scm (to_str (v).ch_l ()),
227                      SCM_UNDEFINED);
228   output_scheme (scm);
229 }
230
231 void
232 Paper_outputter::output_string (SCM str)
233 {
234   *stream_p_ <<  ly_scm2string (str);
235 }
236
237 void
238 Paper_outputter::write_header_field_to_file (String filename, String key, String value)
239 {
240   if (filename != "-")
241     filename += String (".") + key;
242   progress_indication (_f ("writing header field `%s' to `%s'...",
243                            key,
244                            filename == "-" ? String ("<stdout>") : filename));
245   
246   ostream *os = open_file_stream (filename);
247   *os << value;
248   close_file_stream (os);
249   progress_indication ("\n");
250 }
251
252 void
253 Paper_outputter::write_header_fields_to_file (Scope * header)
254 {
255   if (dump_header_fieldnames_global.size ())
256     {
257       SCM fields = header->to_alist ();
258       for (int i = 0; i < dump_header_fieldnames_global.size (); i++)
259         {
260           String key = dump_header_fieldnames_global[i];
261           SCM val = gh_assoc (ly_symbol2scm (key.ch_C ()), fields);
262           String s;
263           /* Only write header field to file if it exists */
264           if (gh_pair_p (val))
265             {
266               s = ly_scm2string (ly_cdr (val));
267               /* Always write header field file, even if string is empty ... */
268               write_header_field_to_file (basename_, key, s);
269             }
270         }
271     }
272 }