]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
release: 1.3.38
[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 Paper_outputter::Paper_outputter ()
32 {
33   molecules_ = gh_cons (SCM_EOL, SCM_EOL);
34   last_cons_ = molecules_;
35 }
36
37
38 void
39 Paper_outputter::output_header ()
40 {
41   if (safe_global_b)
42     {
43       gh_define ("security-paranoia", SCM_BOOL_T);      
44     }
45
46   SCM exp = gh_list (ly_symbol2scm ((String (output_global_ch) + "-scm").ch_C()),
47                      ly_quote_scm (ly_symbol2scm ("all-definitions")),
48                      SCM_UNDEFINED);
49   exp = scm_eval (exp);
50   scm_eval (exp);
51   
52   String creator;
53   if (no_timestamps_global_b)
54     creator = gnu_lilypond_str ();
55   else
56     creator = gnu_lilypond_version_str ();
57   
58   String generate;
59   if (no_timestamps_global_b)
60     generate = ".";
61   else
62     {
63       generate = _ (", at ");
64       time_t t (time (0));
65       generate += ctime (&t);
66       generate = generate.left_str (generate.length_i () - 1);
67     }
68
69   SCM args_scm = 
70     gh_list (ly_str02scm (creator.ch_l ()),
71              ly_str02scm (generate.ch_l ()), SCM_UNDEFINED);
72
73
74   SCM scm = gh_cons (ly_symbol2scm ("header"), args_scm);
75   output_scheme (scm);
76 }
77
78 void
79 Paper_outputter::output_molecule (SCM expr, Offset o, char const *nm)
80 {
81 #if 0
82   if (flower_dstream)
83     {
84       output_comment (nm);
85     }
86 #endif
87   
88   SCM offset_sym = ly_symbol2scm ("translate-molecule");
89   SCM combine_sym = ly_symbol2scm ("combine-molecule");
90 enter:
91
92   if (!gh_pair_p (expr))
93     return;
94   
95   SCM head =gh_car (expr);
96   if (head == offset_sym)
97     {
98       o += ly_scm2offset (gh_cadr (expr));
99       expr = gh_caddr (expr);
100       goto enter;
101     }
102   else if (head == combine_sym)
103     {
104       output_molecule (gh_cadr (expr), o, nm);
105       expr = gh_caddr (expr);
106       goto enter;               // tail recursion
107     }
108   else
109     {
110       output_scheme (gh_list (ly_symbol2scm ("placebox"),
111                               gh_double2scm (o[X_AXIS]),
112                               gh_double2scm (o[Y_AXIS]),
113                               expr,
114                               SCM_UNDEFINED));
115
116     }
117 }
118
119 void
120 Paper_outputter::output_comment (String str)
121 {
122   output_scheme (gh_list (ly_symbol2scm ("comment"),
123                           ly_str02scm ((char*)str.ch_C()),
124                           SCM_UNDEFINED)
125                  );
126 }
127
128
129 void
130 Paper_outputter::output_scheme (SCM scm)
131 {
132   SCM c = gh_cons (scm,gh_cdr (last_cons_));
133   gh_set_cdr_x(last_cons_, c);
134   last_cons_ = c;
135 }
136
137
138 void
139 Paper_outputter::dump_onto (Paper_stream *ps)
140 {
141   if (String (output_global_ch) == "scm")
142 #if 1  // both are fine
143     {
144       /*
145         default to stdin
146        */
147       int fd = 1;
148       if (ofstream* of = dynamic_cast<ofstream*> (ps->os))
149         fd = of->rdbuf ()->fd ();
150       SCM port = scm_fdes_to_port (fd, "a", SCM_EOL);
151
152       /*
153          lilypond -f scm x.ly
154          guile -s x.scm
155        */
156       scm_display (gh_str02scm (
157         ";;; Usage: guile -s x.scm > x.tex\n"
158         "(primitive-load-path 'lily.scm)\n"
159         "(scm-as-output)\n"
160         ";(scm-tex-output)\n"
161         ";(scm-ps-output)\n"
162         "(map (lambda (x) (display (eval x))) '(\n"
163         ), port);
164
165       SCM newline = gh_str02scm ("\n");
166       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
167         {
168           scm_write (gh_car (s), port);
169           scm_display (newline, port);
170           scm_flush (port);
171         }
172       scm_display (gh_str02scm ("))"), port);
173       scm_display (newline, port);
174       scm_flush (port);
175       scm_close_port (port);
176     }
177 #else
178     {
179       /*
180          lilypond -f scm x.ly
181          guile -s x.scm
182        */
183       if (output_global_ch == String ("scm"))
184         *ps << ""
185           ";;; Usage: guile -s x.scm > x.tex\n"
186           "(primitive-load-path 'lily.scm)\n"
187           "(scm-tex-output)\n"
188           ";(scm-ps-output)\n"
189           "(map (lambda (x) (display (eval x))) '(\n"
190         ;
191       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
192         {
193           SCM result =  scm_eval (scm_listify (ly_symbol2scm ("scm->string"),
194                                                ly_quote_scm (gh_car (s)), SCM_UNDEFINED));
195           
196           *ps << ly_scm2string (result);
197         }
198       *ps << "))";
199     }
200 #endif
201   
202   else
203     {
204       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
205         {
206           SCM result = scm_eval (gh_car (s));
207           char *c=gh_scm2newstr (result, NULL);
208           
209           *ps << c;
210           free (c);
211         }
212   }
213 }
214
215 void
216 Paper_outputter::output_scope (Scope *scope, String prefix)
217 {
218   for (Scope_iter i (*scope); i.ok (); i++)
219     {
220       if (dynamic_cast<String_identifier*> (i.val ()))
221         {
222           String val = *i.val()->access_content_String (false);
223
224           output_String_def (prefix + i.key (), val);
225         }
226       else if(dynamic_cast<Real_identifier*> (i.val ()))
227         {
228           Real val  = *i.val ()->access_content_Real (false);
229
230           output_Real_def (prefix + i.key (), val);       
231         }
232       else if (dynamic_cast<int_identifier*> (i.val ()))
233         {
234           int val  = *i.val ()->access_content_int (false);       
235           
236           output_int_def (prefix + i.key (), val);        
237         }
238     }
239 }
240
241 void
242 Paper_outputter::output_version ()
243 {
244   String id_str = "Lily was here";
245   if (no_timestamps_global_b)
246     id_str += ".";
247   else
248     id_str += String (", ") + version_str ();
249
250   output_String_def ( "mudelatagline", id_str);
251   output_String_def ( "LilyPondVersion", version_str ());
252 }
253
254 void
255 Paper_outputter::start_line (Real height)
256 {
257   if (height > 50 CM)
258     {
259       programming_error ("Improbable system height");
260       height = 50 CM;
261     }
262   SCM scm = gh_list (ly_symbol2scm ("start-line"),
263                      gh_double2scm (height),
264                      SCM_UNDEFINED);
265   output_scheme (scm);
266 }
267
268 void
269 Paper_outputter::output_font_def (int i, String str)
270 {
271   SCM scm = gh_list (ly_symbol2scm ("font-def"),
272                      gh_int2scm (i),
273                      ly_str02scm (str.ch_l ()),
274                      SCM_UNDEFINED);
275
276   output_scheme (scm);
277 }
278
279 void
280 Paper_outputter::output_Real_def (String k, Real v)
281 {
282   
283   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
284                      ly_str02scm (k.ch_l ()),
285                      ly_str02scm (to_str(v).ch_l ()),
286                      SCM_UNDEFINED);
287   output_scheme (scm);
288
289   //  gh_define (k.ch_l (), gh_double2scm (v));
290 }
291
292 void
293 Paper_outputter::output_String_def (String k, String v)
294 {
295   
296   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
297                      ly_str02scm (k.ch_l ()),
298                      ly_str02scm (v.ch_l ()),
299                      SCM_UNDEFINED);
300   output_scheme (scm);
301
302   // gh_define (k.ch_l (), ly_str02scm (v.ch_l ()));
303 }
304
305 void
306 Paper_outputter::output_int_def (String k, int v)
307 {
308   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
309                      ly_str02scm (k.ch_l ()),
310                      ly_str02scm (to_str (v).ch_l ()),
311                      SCM_UNDEFINED);
312   output_scheme (scm);
313
314   // gh_define (k.ch_l (), gh_int2scm (v));
315 }
316
317
318
319 void
320 Paper_outputter::stop_line ()
321 {
322   SCM scm = gh_list (ly_symbol2scm ("stop-line"), SCM_UNDEFINED);
323   output_scheme (scm);
324 }
325
326 void
327 Paper_outputter::stop_last_line ()
328 {
329   SCM scm = gh_list (ly_symbol2scm ("stop-last-line"), SCM_UNDEFINED);
330   output_scheme (scm);
331   scm = gh_list (ly_symbol2scm ("end-output"), SCM_UNDEFINED);
332   output_scheme (scm);
333 }
334
335