]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
comment
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include <time.h>
11 #include <math.h>
12
13 #include "dimensions.hh"
14 #include "virtual-methods.hh"
15 #include "paper-outputter.hh"
16 #include "stencil.hh"
17 #include "array.hh"
18 #include "string-convert.hh"
19 #include "warn.hh"
20 #include "font-metric.hh"
21 #include "main.hh"
22 #include "scm-hash.hh"
23 #include "lily-version.hh"
24 #include "paper-def.hh"
25 #include "input-file-results.hh"
26 #include "ly-modules.hh"
27
28
29
30 /*
31   TODO: this entire class should be implemented at Scheme level.
32
33
34   
35  */
36 Paper_outputter::Paper_outputter (String name)
37 {
38   if (safe_global_b)
39     {
40       gh_define ("security-paranoia", SCM_BOOL_T);      
41     }
42   
43   file_ = scm_open_file (scm_makfrom0str (name.to_str0 ()),
44                             scm_makfrom0str ("w"));
45
46   /*
47     ugh.
48    */
49   SCM exp = scm_list_n (ly_symbol2scm ("find-dumper"),
50                         scm_makfrom0str (output_format_global.to_str0 ()),
51                         SCM_UNDEFINED);
52
53   output_func_  = scm_primitive_eval (exp);
54 }
55
56 Paper_outputter::~Paper_outputter ()
57 {
58   scm_close_port (file_);
59   file_ = SCM_EOL;
60 }
61
62
63 /*
64   TODO: move this to Scheme  
65  */
66 void
67 Paper_outputter::output_header ()
68 {
69   time_t t (time (0));
70   String generate = ctime (&t);
71   generate = generate.left_string (generate.length () - 1) + " " + *tzname;
72   
73   /* Fixed length time stamp */
74   generate = generate + to_string (' ', (50 - generate.length ()) >? 0);
75   
76   /* Fixed length creator string */
77   String creator = gnu_lilypond_version_string ();
78   creator += " (http://lilypond.org)";
79   creator = creator + to_string (' ', (50 - creator.length ()) >? 0);
80   
81   SCM args_scm = scm_list_n (scm_makfrom0str (creator.to_str0 ()),
82                              scm_makfrom0str (generate.to_str0 ()),
83                              SCM_UNDEFINED);
84
85
86   SCM scm = gh_cons (ly_symbol2scm ("header"), args_scm);
87
88   output_scheme (scm);
89 }
90
91
92
93 void
94 Paper_outputter::output_comment (String str)
95 {
96   output_scheme (scm_list_n (ly_symbol2scm ("comment"),
97                           scm_makfrom0str ((char*)str.to_str0 ()),
98                           SCM_UNDEFINED)
99                  );
100 }
101
102 void
103 Paper_outputter::output_scheme (SCM scm)
104 {
105   gh_call2 (output_func_, scm, file_);
106 }
107
108 void
109 Paper_outputter::output_scope (SCM mod, String prefix)
110 {
111   if (!SCM_MODULEP (mod))
112     return ;
113   
114   SCM al = ly_module_to_alist (mod);
115   for (SCM s = al ; gh_pair_p (s); s = ly_cdr (s))
116     {
117       SCM k = ly_caar (s);
118       SCM v = ly_cdar (s);
119       String s = ly_symbol2string (k);
120       
121       if (gh_string_p (v))
122         {
123           output_String_def (prefix + s, ly_scm2string (v));
124         }
125       else if (scm_integer_p (v) == SCM_BOOL_T && scm_exact_p (v) == SCM_BOOL_T)
126         {
127           output_int_def (prefix + s, gh_scm2int (v));    
128         }
129       else if (gh_number_p (v))
130         {
131           output_Real_def (prefix + s, gh_scm2double (v));
132         }
133     }
134 }
135
136 void
137 Paper_outputter::output_version ()
138 {
139   String id_string = "Engraved by LilyPond";
140   id_string += String_convert::pad_to (String (", ") + version_string (), 40);
141
142   output_String_def ("lilypondtagline", id_string);
143   output_String_def ("LilyPondVersion", version_string ());
144   output_String_def ("lilypondpaperunit", String (INTERNAL_UNIT));  
145 }
146
147
148 void
149 Paper_outputter::output_Real_def (String k, Real v)
150 {
151   
152   SCM scm = scm_list_n (ly_symbol2scm ("lily-def"),
153                         scm_makfrom0str (k.get_str0 ()),
154                         scm_makfrom0str (to_string (v).get_str0 ()),
155                         SCM_UNDEFINED);
156   output_scheme (scm);
157 }
158
159 void
160 Paper_outputter::output_String_def (String k, String v)
161 {
162   
163   SCM scm = scm_list_n (ly_symbol2scm ("lily-def"),
164                      scm_makfrom0str (k.get_str0 ()),
165                      scm_makfrom0str (v.get_str0 ()),
166                      SCM_UNDEFINED);
167   output_scheme (scm);
168 }
169
170 void
171 Paper_outputter::output_int_def (String k, int v)
172 {
173   SCM scm = scm_list_n (ly_symbol2scm ("lily-def"),
174                      scm_makfrom0str (k.get_str0 ()),
175                      scm_makfrom0str (to_string (v).get_str0 ()),
176                      SCM_UNDEFINED);
177   output_scheme (scm);
178 }
179
180 void
181 Paper_outputter::write_header_field_to_file (String filename, SCM key, SCM value)
182 {
183   output_scheme (scm_list_n (ly_symbol2scm ("header-to-file"),
184                              scm_makfrom0str (filename.to_str0 ()),
185                              ly_quote_scm (key), value,
186                              SCM_UNDEFINED));
187 }
188
189 void
190 Paper_outputter::write_header_fields_to_file (SCM mod)
191 {
192   if (ly_module_p (mod)&&
193       dump_header_fieldnames_global.size ())
194     {
195       SCM fields = ly_module_to_alist (mod);
196       for (int i = 0; i < dump_header_fieldnames_global.size (); i++)
197         {
198           String key = dump_header_fieldnames_global[i];
199           SCM val = gh_assoc (ly_symbol2scm (key.to_str0 ()), fields);
200           String s;
201           /* Only write header field to file if it exists */
202           if (gh_pair_p (val) && gh_string_p (ly_cdr (val)))
203             {
204               s = ly_scm2string (ly_cdr (val));
205               /* Always write header field file, even if string is empty ... */
206               write_header_field_to_file (basename_ , ly_car (val), ly_cdr (val));
207             }
208         }
209     }
210 }