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