]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
patch::: 1.3.26.hwn3
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include <time.h>
11 #include <fstream.h>
12 #include <math.h>
13 #include <iostream.h>
14
15 #include "dimensions.hh"
16 #include "dictionary-iter.hh"
17 #include "virtual-methods.hh"
18 #include "paper-outputter.hh"
19 #include "paper-stream.hh"
20 #include "molecule.hh"
21 #include "array.hh"
22 #include "string-convert.hh"
23 #include "debug.hh"
24 #include "lookup.hh"
25 #include "main.hh"
26 #include "scope.hh"
27 #include "identifier.hh"
28 #include "lily-version.hh"
29 #include "atom.hh"
30
31 Paper_outputter::Paper_outputter ()
32 {
33   molecules_ = gh_cons (SCM_EOL, SCM_EOL);
34   last_cons_ = molecules_;
35 }
36
37
38 void
39 Paper_outputter::output_header ()
40 {
41   if (safe_global_b)
42     {
43
44       gh_define ("security-paranoia", SCM_BOOL_T);      
45     }
46   String s = String ("(eval (") + output_global_ch + "-scm 'all-definitions))";
47   ly_eval_str (s.ch_C ());
48   
49   String creator;
50   if (no_timestamps_global_b)
51     creator = gnu_lilypond_str ();
52   else
53     creator = gnu_lilypond_version_str ();
54   
55   String generate;
56   if (no_timestamps_global_b)
57     generate = ".";
58   else
59     {
60       generate = _ (", at ");
61       time_t t (time (0));
62       generate += ctime (&t);
63       generate = generate.left_str (generate.length_i () - 1);
64     }
65
66   SCM args_scm = 
67     gh_list (ly_str02scm (creator.ch_l ()),
68              ly_str02scm (generate.ch_l ()), SCM_UNDEFINED);
69
70
71   SCM scm = gh_cons (ly_symbol2scm ("header"), args_scm);
72   output_scheme (scm);
73 }
74
75 void
76 Paper_outputter::output_molecule (Molecule const*m, Offset o, char const *nm)
77 {
78   if (flower_dstream)
79     {
80       output_comment (nm);
81     }
82       
83   for (SCM ptr = gh_cdr (m->atom_list_); ptr != SCM_EOL; ptr = gh_cdr (ptr))
84     {
85       Atom * i = unsmob_atom (gh_car (ptr));
86
87       Offset a_off = i->off_;
88       a_off += o;
89
90       if (!i->func_)
91         continue; 
92
93       Axis a = X_AXIS;
94       while (a < NO_AXES)
95         {
96           if (abs(a_off[a]) > 30 CM
97               || isinf (a_off[a]) || isnan (a_off[a]))
98             {
99               programming_error ("Improbable offset for object: setting to zero");
100               a_off[a] =  0.0;
101             }
102           incr (a);
103         }
104
105         
106       SCM box_scm
107         = gh_list (ly_symbol2scm ("placebox"),
108                    gh_double2scm (a_off[X_AXIS]),
109                    gh_double2scm (a_off[Y_AXIS]),
110                    SCM(i->func_),
111                    SCM_UNDEFINED);
112
113       output_scheme (box_scm);
114     }
115 }
116
117 void
118 Paper_outputter::output_comment (String str)
119 {
120   output_scheme (gh_list (ly_symbol2scm ("comment"),
121                           ly_str02scm ((char*)str.ch_C()),
122                           SCM_UNDEFINED)
123                  );
124 }
125
126
127 void
128 Paper_outputter::output_scheme (SCM scm)
129 {
130   SCM c = gh_cons (scm,gh_cdr (last_cons_));
131   gh_set_cdr_x(last_cons_, c);
132   last_cons_ = c;
133 }
134
135
136 int
137 count_cells (SCM s)
138 {
139   if (Atom * a = unsmob_atom (s))
140     {
141       return 2 + count_cells (a->func_);
142     }
143   else if (gh_pair_p (s))
144     {
145       return 2 + count_cells (gh_car (s))+ count_cells (gh_cdr (s));
146     }
147   else
148     return 1;
149 }
150
151 void
152 Paper_outputter::dump_onto (Paper_stream *ps)
153 {
154   if (String (output_global_ch) == "scm")
155 #if 1  // both are fine
156     {
157       /*
158         default to stdin
159        */
160       int fd = 1;
161       if (ofstream* of = dynamic_cast<ofstream*> (ps->os))
162         fd = of->rdbuf ()->fd ();
163       SCM port = scm_fdes_to_port (fd, "a", SCM_EOL);
164
165       /*
166          lilypond -f scm x.ly
167          guile -s x.scm
168        */
169       scm_display (gh_str02scm (
170         ";;; Usage: guile -s x.scm > x.tex\n"
171         "(primitive-load-path 'lily.scm)\n"
172         "(scm-as-output)\n"
173         ";(scm-tex-output)\n"
174         ";(scm-ps-output)\n"
175         "(map (lambda (x) (display (eval x))) '(\n"
176         ), port);
177
178       SCM newline = gh_str02scm ("\n");
179       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
180         {
181           scm_write (gh_car (s), port);
182           scm_display (newline, port);
183           scm_flush (port);
184         }
185       scm_display (gh_str02scm ("))"), port);
186       scm_display (newline, port);
187       scm_flush (port);
188       scm_close_port (port);
189     }
190 #else
191     {
192       /*
193          lilypond -f scm x.ly
194          guile -s x.scm
195        */
196       if (output_global_ch == String ("scm"))
197         *ps << ""
198           ";;; Usage: guile -s x.scm > x.tex\n"
199           "(primitive-load-path 'lily.scm)\n"
200           "(scm-tex-output)\n"
201           ";(scm-ps-output)\n"
202           "(map (lambda (x) (display (eval x))) '(\n"
203         ;
204       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
205         {
206           SCM result =  scm_eval (scm_listify (ly_symbol2scm ("scm->string"),
207                                                ly_quote_scm (gh_car (s)), SCM_UNDEFINED));
208           
209           *ps << ly_scm2string (result);
210         }
211       *ps << "))";
212     }
213 #endif
214   
215   else
216     {
217       for (SCM s = gh_cdr (molecules_); gh_pair_p (s); s = gh_cdr (s))
218         {
219           SCM result = scm_eval (gh_car (s));
220           char *c=gh_scm2newstr (result, NULL);
221           
222           *ps << c;
223           free (c);
224         }
225       cout << "\nCells in use: " <<  scm_cells_allocated << endl;
226   cout <<    "protects "  << scm_ilength (scm_protects) << endl;
227
228   }
229 }
230
231 void
232 Paper_outputter::output_scope (Scope *scope, String prefix)
233 {
234   for (Scope_iter i (*scope); i.ok (); i++)
235     {
236       if (dynamic_cast<String_identifier*> (i.val ()))
237         {
238           String val = *i.val()->access_content_String (false);
239
240           output_String_def (prefix + i.key (), val);
241         }
242       else if(dynamic_cast<Real_identifier*> (i.val ()))
243         {
244           Real val  = *i.val ()->access_content_Real (false);
245
246           output_Real_def (prefix + i.key (), val);       
247         }
248       else if (dynamic_cast<int_identifier*> (i.val ()))
249         {
250           int val  = *i.val ()->access_content_int (false);       
251           
252           output_int_def (prefix + i.key (), val);        
253         }
254     }
255 }
256
257 void
258 Paper_outputter::output_version ()
259 {
260   String id_str = "Lily was here";
261   if (no_timestamps_global_b)
262     id_str += ".";
263   else
264     id_str += String (", ") + version_str ();
265
266   output_String_def ( "mudelatagline", id_str);
267   output_String_def ( "LilyPondVersion", version_str ());
268 }
269
270 void
271 Paper_outputter::start_line (Real height)
272 {
273   if (height > 50 CM)
274     {
275       programming_error ("Improbable system height");
276       height = 50 CM;
277     }
278   SCM scm = gh_list (ly_symbol2scm ("start-line"),
279                      gh_double2scm (height),
280                      SCM_UNDEFINED);
281   output_scheme (scm);
282 }
283
284 void
285 Paper_outputter::output_font_def (int i, String str)
286 {
287   SCM scm = gh_list (ly_symbol2scm ("font-def"),
288                      gh_int2scm (i),
289                      ly_str02scm (str.ch_l ()),
290                      SCM_UNDEFINED);
291
292   output_scheme (scm);
293 }
294
295 void
296 Paper_outputter::output_Real_def (String k, Real v)
297 {
298   
299   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
300                      ly_str02scm (k.ch_l ()),
301                      ly_str02scm (to_str(v).ch_l ()),
302                      SCM_UNDEFINED);
303   output_scheme (scm);
304
305   //  gh_define (k.ch_l (), gh_double2scm (v));
306 }
307
308 void
309 Paper_outputter::output_String_def (String k, String v)
310 {
311   
312   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
313                      ly_str02scm (k.ch_l ()),
314                      ly_str02scm (v.ch_l ()),
315                      SCM_UNDEFINED);
316   output_scheme (scm);
317
318   // gh_define (k.ch_l (), ly_str02scm (v.ch_l ()));
319 }
320
321 void
322 Paper_outputter::output_int_def (String k, int v)
323 {
324   SCM scm = gh_list (ly_symbol2scm ("lily-def"),
325                      ly_str02scm (k.ch_l ()),
326                      ly_str02scm (to_str (v).ch_l ()),
327                      SCM_UNDEFINED);
328   output_scheme (scm);
329
330   // gh_define (k.ch_l (), gh_int2scm (v));
331 }
332
333
334
335 void
336 Paper_outputter::stop_line ()
337 {
338   SCM scm = gh_list (ly_symbol2scm ("stop-line"), SCM_UNDEFINED);
339   output_scheme (scm);
340 }
341
342 void
343 Paper_outputter::stop_last_line ()
344 {
345   SCM scm = gh_list (ly_symbol2scm ("stop-last-line"), SCM_UNDEFINED);
346   output_scheme (scm);
347   scm = gh_list (ly_symbol2scm ("end-output"), SCM_UNDEFINED);
348   output_scheme (scm);
349 }
350
351