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