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