]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
patch::: 1.1.8.hwn1
[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
13 #include "virtual-methods.hh"
14 #include "paper-outputter.hh"
15 #include "paper-stream.hh"
16 #include "molecule.hh"
17 #include "atom.hh"
18 #include "array.hh"
19 #include "string-convert.hh"
20 #include "debug.hh"
21 #include "lookup.hh"
22 #include "main.hh"
23
24 Paper_outputter::Paper_outputter (Paper_stream *s)
25 {
26   outstream_l_ = s;
27   output_header ();
28 }
29
30 Paper_outputter::~Paper_outputter ()
31 {
32   SCM scm = gh_list (ly_symbol ("end-output"), SCM_UNDEFINED);
33   output_scheme (scm);
34 }
35
36 void
37 Paper_outputter::output_header ()
38 {
39   String s = String ("(eval (") + output_global_ch + "-scm 'all-definitions))";
40   gh_eval_str (s.ch_C());
41
42   
43   String creator;
44   if (no_timestamps_global_b)
45     creator = "GNU LilyPond\n";
46   else
47     creator = get_version_str ();
48   String generate;
49   if (no_timestamps_global_b)
50     generate = ".";
51   else
52     {
53       generate = _ (", at ");
54       time_t t (time (0));
55       generate += ctime (&t);
56       //urg
57     }
58
59   SCM args_scm = 
60     gh_list (gh_str02scm (creator.ch_l ()),
61              gh_str02scm (generate.ch_l ()), SCM_UNDEFINED);
62
63 #ifndef NPRINT
64   DOUT << "output_header\n";
65   if (check_debug && !monitor->silent_b ("Guile"))
66     {
67       gh_display (args_scm); gh_newline ();
68     }
69 #endif
70
71   SCM scm = gh_cons (ly_symbol ("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 (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         continue; 
88
89       if (check_debug)
90         {
91           output_comment (classname (i.ptr ()->origin_l_));
92
93         }
94       
95       switch_to_font (i->font_);
96
97       SCM args_scm = gh_list (gh_double2scm (a_off.x ()),
98                  gh_double2scm (a_off.y ()), 
99                  i->lambda_.to_SCM (),
100                  SCM_UNDEFINED);
101
102
103       SCM box_scm = gh_cons (ly_symbol ("placebox"), args_scm);
104
105       output_scheme (box_scm);
106     }
107 }
108
109 void
110 Paper_outputter::output_comment (String str)
111 {
112   if (String (output_global_ch) == "scm")
113     {
114       *outstream_l_ << "; " << str << '\n';
115     }
116   else
117     {
118       *outstream_l_ << "% " << str << "\n";
119     }
120 }
121
122
123 void
124 Paper_outputter::output_scheme (SCM scm)
125 {
126   String o = String ("\'") + output_global_ch;
127
128   if (String (output_global_ch) == "scm")
129     {
130       static SCM port = 0;
131
132       // urg
133       if (!port)
134         {
135           int fd = 1;
136           ofstream * of = dynamic_cast<ofstream*> (outstream_l_->os);
137           if (of)
138             fd = of->rdbuf()->fd();
139           FILE *file = fdopen (fd, "a");
140           port = scm_standard_stream_to_port (file, "a", "");
141           scm_display (gh_str02scm ("(load 'lily.scm)\n"), port);
142         }
143
144       scm_display (gh_str02scm ("("), port);
145       scm_write (scm, port);
146       scm_display (gh_str02scm (")\n"),port);
147       scm_fflush (port);
148     }
149   else
150     {
151       SCM result = scm_eval (scm);
152       char *c=gh_scm2newstr (result, NULL);
153
154       *outstream_l_ << c;
155       free (c);
156     }
157 }
158 void
159 Paper_outputter::output_string (String str)
160 {
161   // urg
162   *outstream_l_ << str;
163 }
164
165 void
166 Paper_outputter::switch_to_font (String fontname)
167 {
168   if (fontname.length_i () && fontname != current_font_)
169     {
170       current_font_ = fontname;
171       int i=0;
172
173       for (; i< font_arr_.size (); i++)
174         if (font_arr_[i] == fontname)
175             break;
176
177       if (i == font_arr_.size ())
178         {
179           font_arr_.push (fontname);
180           output_font_def (i, fontname);
181         }
182       output_font_switch (i);
183     }
184   return;
185 }
186
187 void
188 Paper_outputter::start_line ()
189 {
190   SCM scm = gh_list (ly_symbol ("start-line"), SCM_UNDEFINED);
191   output_scheme (scm);
192 }
193
194 void
195 Paper_outputter::output_font_def (int i, String str)
196 {
197   SCM scm =gh_list (ly_symbol ("font-def"),
198                     gh_int2scm (i),
199                     gh_str02scm (str.ch_l ()),
200                     SCM_UNDEFINED);
201                                  
202   output_scheme (scm);
203 }
204
205 void
206 Paper_outputter::output_font_switch (int i)
207 {
208   SCM scm = gh_list (ly_symbol ("font-switch"),
209                      gh_int2scm (i),
210                      SCM_UNDEFINED);
211
212   output_scheme (scm);
213 }
214
215 void
216 Paper_outputter::stop_line ()
217 {
218   SCM scm =    gh_list (ly_symbol ("stop-line"), SCM_UNDEFINED);
219   output_scheme (scm);
220
221   current_font_ = "";
222   font_arr_.clear ();
223 }