]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
release: 1.3.44
[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 "dictionary-iter.hh"
17 #include "virtual-methods.hh"
18 #include "paper-outputter.hh"
19 #include "paper-stream.hh"
20 #include "molecule.hh"
21 #include "array.hh"
22 #include "string-convert.hh"
23 #include "debug.hh"
24 #include "lookup.hh"
25 #include "main.hh"
26 #include "scope.hh"
27 #include "identifier.hh"
28 #include "lily-version.hh"
29
30
31 /*
32   Ugh, this is messy.
33  */
34
35 Paper_outputter::Paper_outputter ()
36 {
37   molecules_ = gh_cons (SCM_EOL, SCM_EOL);
38   last_cons_ = molecules_;
39 }
40
41
42 void
43 Paper_outputter::output_header ()
44 {
45   if (safe_global_b)
46     {
47       gh_define ("security-paranoia", SCM_BOOL_T);      
48     }
49
50   SCM exp = gh_list (ly_symbol2scm ((String (output_global_ch) + "-scm").ch_C()),
51                      ly_quote_scm (ly_symbol2scm ("all-definitions")),
52                      SCM_UNDEFINED);
53   exp = scm_eval (exp);
54   scm_eval (exp);
55   
56   String creator;
57   if (no_timestamps_global_b)
58     creator = gnu_lilypond_str ();
59   else
60     creator = gnu_lilypond_version_str ();
61   
62   String generate;
63   if (no_timestamps_global_b)
64     generate = ".";
65   else
66     {
67       generate = _ (", at ");
68       time_t t (time (0));
69       generate += ctime (&t);
70       generate = generate.left_str (generate.length_i () - 1);
71     }
72
73   SCM args_scm = 
74     gh_list (ly_str02scm (creator.ch_l ()),
75              ly_str02scm (generate.ch_l ()), SCM_UNDEFINED);
76
77
78   SCM scm = gh_cons (ly_symbol2scm ("header"), args_scm);
79   output_scheme (scm);
80 }
81
82
83
84 void
85 Paper_outputter::output_comment (String str)
86 {
87   output_scheme (gh_list (ly_symbol2scm ("comment"),
88                           ly_str02scm ((char*)str.ch_C()),
89                           SCM_UNDEFINED)
90                  );
91 }
92
93
94 void
95 Paper_outputter::output_scheme (SCM scm)
96 {
97   SCM c = gh_cons (scm,gh_cdr (last_cons_));
98   gh_set_cdr_x(last_cons_, c);
99   last_cons_ = c;
100 }
101
102
103 void
104 Paper_outputter::dump_onto (Paper_stream *ps)
105 {
106   if (String (output_global_ch) == "scm")
107 #if 1  // both are fine
108     {
109       /*
110         default to stdin
111        */
112       int fd = 1;
113       if (ofstream* of = dynamic_cast<ofstream*> (ps->os))
114         fd = of->rdbuf ()->fd ();
115       SCM port = scm_fdes_to_port (fd, "a", SCM_EOL);
116
117       /*
118          lilypond -f scm x.ly
119          guile -s x.scm
120        */
121       scm_display (gh_str02scm (
122         ";;; Usage: guile -s x.scm > x.tex\n"
123         "(primitive-load-path 'lily.scm)\n"
124         "(scm-as-output)\n"
125         ";(scm-tex-output)\n"
126         ";(scm-ps-output)\n"
127         "(map (lambda (x) (display (eval x))) '(\n"
128         ), port);
129
130       SCM newline = gh_str02scm ("\n");
131       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
132         {
133           scm_write (gh_car (s), port);
134           scm_display (newline, port);
135           scm_flush (port);
136         }
137       scm_display (gh_str02scm ("))"), port);
138       scm_display (newline, port);
139       scm_flush (port);
140       scm_close_port (port);
141     }
142 #else
143     {
144       /*
145          lilypond -f scm x.ly
146          guile -s x.scm
147        */
148       if (output_global_ch == String ("scm"))
149         *ps << ""
150           ";;; Usage: guile -s x.scm > x.tex\n"
151           "(primitive-load-path 'lily.scm)\n"
152           "(scm-tex-output)\n"
153           ";(scm-ps-output)\n"
154           "(map (lambda (x) (display (eval x))) '(\n"
155         ;
156       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
157         {
158           SCM result =  scm_eval (scm_listify (ly_symbol2scm ("scm->string"),
159                                                ly_quote_scm (gh_car (s)), SCM_UNDEFINED));
160           
161           *ps << ly_scm2string (result);
162         }
163       *ps << "))";
164     }
165 #endif
166   
167   else
168     {
169       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
170         {
171           SCM result = scm_eval (gh_car (s));
172           char *c=gh_scm2newstr (result, NULL);
173           
174           *ps << c;
175           free (c);
176         }
177   }
178 }
179
180 void
181 Paper_outputter::output_scope (Scope *scope, String prefix)
182 {
183   for (Scope_iter i (*scope); i.ok (); i++)
184     {
185       if (dynamic_cast<String_identifier*> (i.val ()))
186         {
187           String val = *i.val()->access_content_String (false);
188
189           output_String_def (prefix + i.key (), val);
190         }
191       else if(dynamic_cast<Real_identifier*> (i.val ()))
192         {
193           Real val  = *i.val ()->access_content_Real (false);
194
195           output_Real_def (prefix + i.key (), val);       
196         }
197       else if (dynamic_cast<int_identifier*> (i.val ()))
198         {
199           int val  = *i.val ()->access_content_int (false);       
200           
201           output_int_def (prefix + i.key (), val);        
202         }
203     }
204 }
205
206 void
207 Paper_outputter::output_version ()
208 {
209   String id_str = "Lily was here";
210   if (no_timestamps_global_b)
211     id_str += ".";
212   else
213     id_str += String (", ") + version_str ();
214
215   output_String_def ( "mudelatagline", id_str);
216   output_String_def ( "LilyPondVersion", version_str ());
217 }
218
219
220
221 void
222 Paper_outputter::output_font_def (int i, String str)
223 {
224   SCM scm = gh_list (ly_symbol2scm ("font-def"),
225                      gh_int2scm (i),
226                      ly_str02scm (str.ch_l ()),
227                      SCM_UNDEFINED);
228
229   output_scheme (scm);
230 }
231
232 void
233 Paper_outputter::output_Real_def (String k, Real v)
234 {
235   
236   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
237                      ly_str02scm (k.ch_l ()),
238                      ly_str02scm (to_str(v).ch_l ()),
239                      SCM_UNDEFINED);
240   output_scheme (scm);
241
242   //  gh_define (k.ch_l (), gh_double2scm (v));
243 }
244
245 void
246 Paper_outputter::output_String_def (String k, String v)
247 {
248   
249   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
250                      ly_str02scm (k.ch_l ()),
251                      ly_str02scm (v.ch_l ()),
252                      SCM_UNDEFINED);
253   output_scheme (scm);
254
255   // gh_define (k.ch_l (), ly_str02scm (v.ch_l ()));
256 }
257
258 void
259 Paper_outputter::output_int_def (String k, int v)
260 {
261   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
262                      ly_str02scm (k.ch_l ()),
263                      ly_str02scm (to_str (v).ch_l ()),
264                      SCM_UNDEFINED);
265   output_scheme (scm);
266
267   // gh_define (k.ch_l (), gh_int2scm (v));
268 }
269
270
271
272
273