]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
patch::: 1.1.1.jcn1: Re: geil
[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--1998 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 "paper-outputter.hh"
13 #include "paper-stream.hh"
14 #include "molecule.hh"
15 #include "atom.hh"
16 #include "array.hh"
17 #include "string-convert.hh"
18 #include "debug.hh"
19 #include "lookup.hh"
20 #include "main.hh"
21
22 Paper_outputter::Paper_outputter (Paper_stream *s)
23 {
24   outstream_l_ = s;
25   output_header ();
26 }
27
28 Paper_outputter::~Paper_outputter ()
29 {
30   SCM scm =
31     ly_append (ly_lambda_o (),
32     ly_list1 (ly_append (ly_func_o ("end-output"), SCM_EOL)));
33
34   output_scheme (scm);
35 }
36
37 void
38 Paper_outputter::output_header ()
39 {
40   String creator;
41   if (no_timestamps_global_b)
42     creator = "GNU LilyPond\n";
43   else
44     creator = get_version_str ();
45   String generate;
46   if (no_timestamps_global_b)
47     generate = ".";
48   else
49     {
50       generate = _ (", at ");
51       time_t t (time (0));
52       generate += ctime (&t);
53       //urg
54     }
55
56   SCM args_scm = 
57     gh_cons (gh_str02scm (creator.ch_l ()),
58     gh_cons (gh_str02scm (generate.ch_l ()), SCM_EOL));
59
60 #ifndef NPRINT
61   DOUT << "output_header\n";
62   if (check_debug && !monitor->silent_b ("Guile"))
63     {
64       gh_display (args_scm); gh_newline ();
65     }
66 #endif
67
68   SCM scm =
69     ly_append (ly_lambda_o (),
70     ly_list1 (ly_append (ly_func_o ("header"), args_scm)));
71
72   output_scheme (scm);
73 }
74
75 void
76 Paper_outputter::output_molecule (Molecule const*m, Offset o, char const *nm)
77 {
78   if (check_debug)
79     *outstream_l_ << String ("\n%start: ") << nm << "\n";
80
81   for (PCursor <Atom*> i (m->atoms_); i.ok (); i++)
82     {
83       Offset a_off = i->offset ();
84       a_off += o;
85
86       if (!i->lambda_)
87         {
88           // urg
89           i->lambda_ = ly_append (ly_lambda_o (), 
90             ly_list1 (ly_func_o ("empty")));
91         }
92
93       switch_to_font (i->font_);
94
95 #ifndef NPRINT
96       if (check_debug && !monitor->silent_b ("Guile"))
97         {
98           DOUT << i->str_ << "\n";
99           gh_display (i->lambda_); gh_newline ();
100         }
101 #endif
102
103       SCM args_scm = 
104         gh_cons (gh_double2scm (a_off.x ()), 
105         gh_cons (gh_double2scm (a_off.y ()), 
106         gh_cons (i->lambda_, SCM_EOL)));
107
108 #ifndef NPRINT
109       if (check_debug && !monitor->silent_b ("Guile"))
110         {
111           gh_display (args_scm); gh_newline ();
112         }
113 #endif
114
115       SCM box_scm =
116         ly_append (ly_lambda_o (),
117         ly_list1 (ly_append (ly_func_o ("placebox"), args_scm)));
118
119       output_scheme (box_scm);
120     }
121 }
122
123 void
124 Paper_outputter::output_comment (String str)
125 {
126   // urg
127   *outstream_l_ << "% " << str << "\n";
128 }
129
130
131 void
132 Paper_outputter::output_scheme (SCM scm)
133 {
134   String o = String ("\'") + output_global_ch;
135 #ifndef NPRINT
136   if (check_debug && !monitor->silent_b ("Guile"))
137     {
138       gh_display (scm); gh_newline ();
139     }
140 #endif
141   // urg; temporary hack to debug scheme error #unknown
142   if (String (output_global_ch) == "scm")
143     {
144       static SCM port = 0;
145       // urg
146       if (!port)
147         {
148           int fd = 1;
149           ofstream * of = dynamic_cast <ofstream*> (outstream_l_->os);
150           if (of)
151             fd = of->rdbuf()->fd();
152           FILE *file = fdopen (fd, "a");
153           port = scm_standard_stream_to_port (file, "a", "");
154           scm_display (gh_str02scm ("(load 'lily.scm)\n"), port);
155         }
156 #if 0
157       *outstream_l_ << "(display ((eval ";
158       scm_write (scm, port);
159       *outstream_l_ << ") 'tex))\n";
160 #else
161       scm_display (gh_str02scm ("(display ((eval "), port);
162       scm_write (scm, port);
163       scm_display (gh_str02scm (") 'tex))\n"), port);
164       scm_newline (port);
165       scm_fflush (port);
166 #endif
167       return;
168     }
169   SCM str_scm = gh_call1 (ly_eval (scm), gh_eval_str (o.ch_l ()));
170   char* c = gh_scm2newstr (str_scm, NULL);
171 #ifndef NPRINT
172   if (check_debug && !monitor->silent_b ("Guile"))
173     {
174       gh_display (str_scm); gh_newline ();
175     }
176 #endif
177   *outstream_l_ << c;
178   free (c);
179 }
180
181 void
182 Paper_outputter::output_string (String str)
183 {
184   // urg
185   *outstream_l_ << str;
186 }
187
188 void
189 Paper_outputter::switch_to_font (String fontname)
190 {
191   if (fontname.length_i () && (fontname != current_font_))
192     {
193       current_font_ = fontname;
194       int i=0;
195       bool new_b = false;
196       for (; i< font_arr_.size (); i++)
197         if (font_arr_[i] == fontname)
198           {
199             new_b = true;
200             break;
201           }
202
203       if (new_b)
204         {
205           font_arr_.push (fontname);
206           output_font_def (i, fontname);
207         }
208       output_font_switch (i);
209     }
210   return;
211 }
212
213 void
214 Paper_outputter::start_line ()
215 {
216   SCM scm =
217     ly_append (ly_lambda_o (),
218     ly_list1 (ly_append (ly_func_o ("start-line"), SCM_EOL)));
219
220   output_scheme (scm);
221 }
222
223 /*
224    26 fonts ought to be enough for anyone.
225 */
226 void
227 Paper_outputter::output_font_def (int i, String str)
228 {
229   //urg, broken with guile-1.3
230   //return;
231   SCM scm =
232     ly_append (ly_lambda_o (),
233     ly_list1 (ly_append (ly_func_o ("font-def"), 
234     gh_cons (gh_int2scm (i), gh_cons (gh_str02scm (str.ch_l ()), SCM_EOL)))));
235
236   output_scheme (scm);
237 }
238
239 void
240 Paper_outputter::output_font_switch (int i)
241 {
242   //urg, broken with guile-1.2, 1.3
243   //return;
244   SCM scm =
245     ly_append (ly_lambda_o (),
246     ly_list1 (ly_append (ly_func_o ("font-switch"), 
247     gh_cons (gh_int2scm (i), SCM_EOL))));
248
249   output_scheme (scm);
250 }
251
252 void
253 Paper_outputter::stop_line ()
254 {
255   SCM scm =
256     ly_append (ly_lambda_o (),
257     ly_list1 (ly_append (ly_func_o ("stop-line"), SCM_EOL)));
258
259   output_scheme (scm);
260
261   current_font_ = "";
262   font_arr_.clear ();
263 }