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