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