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