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