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