lilypond-1.1.44
[lilypond.git] / lily / paper-outputter.cc
blob4337ad92d64ca226b68347a567a4629fc882c38b
1 /*
2 paper-outputter.cc -- implement Paper_outputter
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
10 #include <time.h>
11 #include <fstream.h>
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"
29 Paper_outputter::Paper_outputter (Paper_stream *s)
31 outstream_l_ = s;
32 output_header ();
35 Paper_outputter::~Paper_outputter ()
37 SCM scm = gh_list (ly_symbol ("end-output"), SCM_UNDEFINED);
38 output_scheme (scm);
41 void
42 Paper_outputter::output_header ()
44 if (safe_global_b)
46 ly_set_scm ("security-paranoia", SCM_BOOL_T);
47 // gh_eval_str ("(set! security-paranoia #t)");
49 String s = String ("(eval (") + output_global_ch + "-scm 'all-definitions))";
50 gh_eval_str (s.ch_C());
52 String creator;
53 if (no_timestamps_global_b)
54 creator = gnu_lilypond_str ();
55 else
56 creator = gnu_lilypond_version_str ();
58 String generate;
59 if (no_timestamps_global_b)
60 generate = ".\n";
61 else
63 generate = _ (", at ");
64 time_t t (time (0));
65 generate += ctime (&t);
66 //urg
69 SCM args_scm =
70 gh_list (gh_str02scm (creator.ch_l ()),
71 gh_str02scm (generate.ch_l ()), SCM_UNDEFINED);
73 #ifndef NPRINT
74 DOUT << "output_header\n";
75 if (check_debug && !monitor->silent_b ("Guile"))
77 gh_display (args_scm); gh_newline ();
79 #endif
81 SCM scm = gh_cons (header_scm_sym, args_scm);
82 output_scheme (scm);
85 void
86 Paper_outputter::output_molecule (Molecule const*m, Offset o, char const *nm)
88 if (check_debug)
89 *outstream_l_ << String ("\n%start: ") << nm << "\n";
92 if (check_debug)
94 output_comment (nm);
97 #ifdef ATOM_SMOB
98 for (SCM ptr = m->atom_list_; ptr != SCM_EOL; ptr = SCM_CDR(ptr))
100 Atom *i = Atom::atom_l (SCM_CAR(ptr));
101 #else
102 for (Cons<Atom> *ptr = m->atom_list_; ptr; ptr = ptr->next_)
104 Atom * i = ptr->car_;
105 #endif
106 Offset a_off = i->off_;
107 a_off += o;
109 if (!i->func_)
110 continue;
112 if (a_off.length () > 100 CM)
114 warning (_f("Improbable offset for object type `%s\'", nm));
115 Axis a =X_AXIS;
116 while (a < NO_AXES)
118 if (abs(a_off[a]) > 50 CM)
119 a_off[a] = 50 CM;
120 incr (a);
124 if (i->font_)
126 output_scheme (gh_list (ly_symbol ("select-font"),
127 gh_str02scm (symbol_to_string (i->font_).ch_C()),
128 SCM_UNDEFINED));
131 SCM box_scm
132 = gh_list (placebox_scm_sym,
133 gh_double2scm (a_off.x ()),
134 gh_double2scm (a_off.y ()),
135 SCM(i->func_),
136 SCM_UNDEFINED);
138 output_scheme (box_scm);
142 void
143 Paper_outputter::output_comment (String str)
145 if (String (output_global_ch) == "scm")
147 *outstream_l_ << "; " << str << '\n';
149 else
151 *outstream_l_ << "% " << str << "\n";
156 void
157 Paper_outputter::output_scheme (SCM scm)
159 if (String (output_global_ch) == "scm")
161 static SCM port = 0;
163 // urg
164 if (!port)
166 int fd = 1;
167 ofstream * of = dynamic_cast<ofstream*> (outstream_l_->os);
168 if (of)
169 fd = of->rdbuf()->fd();
170 FILE *file = fdopen (fd, "a");
171 port = scm_standard_stream_to_port (file, "a", "");
172 scm_display (gh_str02scm ("(load 'lily.scm)\n"), port);
175 scm_display (gh_str02scm ("("), port);
176 scm_write (scm, port);
177 scm_display (gh_str02scm (")\n"),port);
178 scm_fflush (port);
180 else
182 SCM result = scm_eval (scm);
183 char *c=gh_scm2newstr (result, NULL);
185 *outstream_l_ << c;
186 free (c);
190 void
191 Paper_outputter::output_scope (Scope *scope, String prefix)
193 for (Scope_iter i (*scope); i.ok (); i++)
195 if (dynamic_cast<String_identifier*> (i.val ()))
197 String val = *i.val()->access_content_String (false);
199 output_String_def (prefix + i.key (), val);
201 else if(dynamic_cast<Real_identifier*> (i.val ()))
203 Real val = *i.val ()->access_content_Real (false);
205 output_Real_def (prefix + i.key (), val);
207 else if (dynamic_cast<int_identifier*> (i.val ()))
209 int val = *i.val ()->access_content_int (false);
211 output_int_def (prefix + i.key (), val);
216 void
217 Paper_outputter::output_version ()
219 String id_str = "Lily was here";
220 if (no_timestamps_global_b)
221 id_str += ".";
222 else
223 id_str += String (", ") + version_str ();
225 output_String_def ( "mudelatagline", id_str);
226 output_String_def ( "LilyPondVersion", version_str ());
229 void
230 Paper_outputter::start_line (Real height)
232 SCM scm = gh_list (ly_symbol ("start-line"),
233 gh_double2scm (height),
234 SCM_UNDEFINED);
235 output_scheme (scm);
238 void
239 Paper_outputter::output_font_def (int i, String str)
241 SCM scm = gh_list (ly_symbol ("font-def"),
242 gh_int2scm (i),
243 gh_str02scm (str.ch_l ()),
244 SCM_UNDEFINED);
246 output_scheme (scm);
249 void
250 Paper_outputter::output_Real_def (String k, Real v)
253 SCM scm = gh_list (ly_symbol ("lily-def"),
254 gh_str02scm (k.ch_l ()),
255 gh_str02scm (to_str(v).ch_l ()),
256 SCM_UNDEFINED);
257 output_scheme (scm);
259 gh_define (k.ch_l (), gh_double2scm (v));
262 void
263 Paper_outputter::output_String_def (String k, String v)
266 SCM scm = gh_list (ly_symbol ("lily-def"),
267 gh_str02scm (k.ch_l ()),
268 gh_str02scm (v.ch_l ()),
269 SCM_UNDEFINED);
270 output_scheme (scm);
272 gh_define (k.ch_l (), gh_str02scm (v.ch_l ()));
275 void
276 Paper_outputter::output_int_def (String k, int v)
278 SCM scm = gh_list (ly_symbol ("lily-def"),
279 gh_str02scm (k.ch_l ()),
280 gh_str02scm (to_str (v).ch_l ()),
281 SCM_UNDEFINED);
282 output_scheme (scm);
284 gh_define (k.ch_l (), gh_int2scm (v));
289 void
290 Paper_outputter::stop_line ()
292 SCM scm = gh_list (ly_symbol ("stop-line"), SCM_UNDEFINED);
293 output_scheme (scm);