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