]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
patch::: 1.1.3.jcn2
[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
136   if (String (output_global_ch) == "scm")
137     {
138       static SCM port = 0;
139       // urg
140       if (!port)
141         {
142           int fd = 1;
143           ofstream * of = dynamic_cast <ofstream*> (outstream_l_->os);
144           if (of)
145             fd = of->rdbuf()->fd();
146           FILE *file = fdopen (fd, "a");
147           port = scm_standard_stream_to_port (file, "a", "");
148           scm_display (gh_str02scm ("(load 'lily.scm)\n"), port);
149         }
150
151       scm_display (gh_str02scm ("(display ((eval "), port);
152       scm_write (scm, port);
153       scm_display (gh_str02scm (") 'tex))\n"), port);
154       scm_newline (port);
155       scm_fflush (port);
156
157       return;
158     }
159   else
160     {
161       SCM str_scm = gh_call1 (ly_eval (scm), gh_eval_str (o.ch_l ()));
162       char* c = gh_scm2newstr (str_scm, NULL);
163       *outstream_l_ << c;
164       free (c);
165     }
166 }
167 void
168 Paper_outputter::output_string (String str)
169 {
170   // urg
171   *outstream_l_ << str;
172 }
173
174 void
175 Paper_outputter::switch_to_font (String fontname)
176 {
177   if (fontname.length_i () && (fontname != current_font_))
178     {
179       current_font_ = fontname;
180       int i=0;
181       bool new_b = true;
182       for (; i< font_arr_.size (); i++)
183         if (font_arr_[i] == fontname)
184           {
185             new_b = false;
186             break;
187           }
188
189       if (new_b)
190         {
191           font_arr_.push (fontname);
192           output_font_def (i, fontname);
193         }
194       output_font_switch (i);
195     }
196   return;
197 }
198
199 void
200 Paper_outputter::start_line ()
201 {
202   SCM scm =
203     gh_append2 (ly_lambda_o (),
204                 gh_list (ly_func_o ("start-line"), SCM_UNDEFINED));;
205
206   output_scheme (scm);
207 }
208
209 /*
210    26 fonts ought to be enough for anyone.
211 */
212 void
213 Paper_outputter::output_font_def (int i, String str)
214 {
215   //urg, broken with guile-1.3
216   //return;
217   SCM scm =
218     ly_append (ly_lambda_o (),
219     ly_list1 (ly_append (ly_func_o ("font-def"), 
220     gh_cons (gh_int2scm (i), gh_cons (gh_str02scm (str.ch_l ()), SCM_EOL)))));
221
222   output_scheme (scm);
223 }
224
225 void
226 Paper_outputter::output_font_switch (int i)
227 {
228   //urg, broken with guile-1.2, 1.3
229   //return;
230   SCM scm =
231     ly_append (ly_lambda_o (),
232     ly_list1 (ly_append (ly_func_o ("font-switch"), 
233     gh_cons (gh_int2scm (i), SCM_EOL))));
234
235   output_scheme (scm);
236 }
237
238 void
239 Paper_outputter::stop_line ()
240 {
241   SCM scm =
242     ly_append (ly_lambda_o (),
243     ly_list1 (ly_append (ly_func_o ("stop-line"), SCM_EOL)));
244
245   output_scheme (scm);
246
247   current_font_ = "";
248   font_arr_.clear ();
249 }