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