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