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