]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
release: 1.3.25
[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 = ".";
57   else
58     {
59       generate = _ (", at ");
60       time_t t (time (0));
61       generate += ctime (&t);
62       generate = generate.left_str (generate.length_i () - 1);
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-as-output)\n"
157         ";(scm-tex-output)\n"
158         ";(scm-ps-output)\n"
159         "(map (lambda (x) (display (eval x))) '(\n"
160         ), port);
161
162       SCM newline = gh_str02scm ("\n");
163       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
164         {
165           scm_write (gh_car (s), port);
166           scm_display (newline, port);
167           scm_flush (port);
168         }
169       scm_display (gh_str02scm ("))"), port);
170       scm_display (newline, port);
171       scm_flush (port);
172       scm_close_port (port);
173     }
174 #else
175     {
176       /*
177          lilypond -f scm x.ly
178          guile -s x.scm
179        */
180       if (output_global_ch == String ("scm"))
181         *ps << ""
182           ";;; Usage: guile -s x.scm > x.tex\n"
183           "(primitive-load-path 'lily.scm)\n"
184           "(scm-tex-output)\n"
185           ";(scm-ps-output)\n"
186           "(map (lambda (x) (display (eval x))) '(\n"
187         ;
188       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
189         {
190           SCM result =  scm_eval (scm_listify (ly_symbol2scm ("scm->string"),
191                                                ly_quote_scm (gh_car (s)), SCM_UNDEFINED));
192           
193           *ps << ly_scm2string (result);
194         }
195       *ps << "))";
196     }
197 #endif
198   
199   else
200     {
201       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
202         {
203           SCM result = scm_eval (gh_car (s));
204           char *c=gh_scm2newstr (result, NULL);
205           
206           *ps << c;
207           free (c);
208         }
209     }
210 }
211
212 void
213 Paper_outputter::output_scope (Scope *scope, String prefix)
214 {
215   for (Scope_iter i (*scope); i.ok (); i++)
216     {
217       if (dynamic_cast<String_identifier*> (i.val ()))
218         {
219           String val = *i.val()->access_content_String (false);
220
221           output_String_def (prefix + i.key (), val);
222         }
223       else if(dynamic_cast<Real_identifier*> (i.val ()))
224         {
225           Real val  = *i.val ()->access_content_Real (false);
226
227           output_Real_def (prefix + i.key (), val);       
228         }
229       else if (dynamic_cast<int_identifier*> (i.val ()))
230         {
231           int val  = *i.val ()->access_content_int (false);       
232           
233           output_int_def (prefix + i.key (), val);        
234         }
235     }
236 }
237
238 void
239 Paper_outputter::output_version ()
240 {
241   String id_str = "Lily was here";
242   if (no_timestamps_global_b)
243     id_str += ".";
244   else
245     id_str += String (", ") + version_str ();
246
247   output_String_def ( "mudelatagline", id_str);
248   output_String_def ( "LilyPondVersion", version_str ());
249 }
250
251 void
252 Paper_outputter::start_line (Real height)
253 {
254   if (height > 50 CM)
255     {
256       programming_error ("Improbable system height");
257       height = 50 CM;
258     }
259   SCM scm = gh_list (ly_symbol2scm ("start-line"),
260                      gh_double2scm (height),
261                      SCM_UNDEFINED);
262   output_scheme (scm);
263 }
264
265 void
266 Paper_outputter::output_font_def (int i, String str)
267 {
268   SCM scm = gh_list (ly_symbol2scm ("font-def"),
269                      gh_int2scm (i),
270                      ly_str02scm (str.ch_l ()),
271                      SCM_UNDEFINED);
272
273   output_scheme (scm);
274 }
275
276 void
277 Paper_outputter::output_Real_def (String k, Real v)
278 {
279   
280   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
281                      ly_str02scm (k.ch_l ()),
282                      ly_str02scm (to_str(v).ch_l ()),
283                      SCM_UNDEFINED);
284   output_scheme (scm);
285
286   gh_define (k.ch_l (), gh_double2scm (v));
287 }
288
289 void
290 Paper_outputter::output_String_def (String k, String v)
291 {
292   
293   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
294                      ly_str02scm (k.ch_l ()),
295                      ly_str02scm (v.ch_l ()),
296                      SCM_UNDEFINED);
297   output_scheme (scm);
298
299   gh_define (k.ch_l (), ly_str02scm (v.ch_l ()));
300 }
301
302 void
303 Paper_outputter::output_int_def (String k, int v)
304 {
305   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
306                      ly_str02scm (k.ch_l ()),
307                      ly_str02scm (to_str (v).ch_l ()),
308                      SCM_UNDEFINED);
309   output_scheme (scm);
310
311   gh_define (k.ch_l (), gh_int2scm (v));
312 }
313
314
315
316 void
317 Paper_outputter::stop_line ()
318 {
319   SCM scm = gh_list (ly_symbol2scm ("stop-line"), SCM_UNDEFINED);
320   output_scheme (scm);
321 }
322
323 void
324 Paper_outputter::stop_last_line ()
325 {
326   SCM scm = gh_list (ly_symbol2scm ("stop-last-line"), SCM_UNDEFINED);
327   output_scheme (scm);
328   scm = gh_list (ly_symbol2scm ("end-output"), SCM_UNDEFINED);
329   output_scheme (scm);
330 }
331
332