]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
* lily/source-file.cc (init_port): add an SCM port to the
[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--2002 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 "molecule.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
27
28 /*
29   Ugh, this is messy.
30  */
31 Paper_outputter::Paper_outputter (String name)
32 {
33   if (safe_global_b)
34     {
35       gh_define ("security-paranoia", SCM_BOOL_T);      
36     }
37   
38   file_ = scm_open_file (scm_makfrom0str (name.to_str0 ()),
39                             scm_makfrom0str ("w"));
40   
41   SCM exp = scm_list_n (ly_symbol2scm ("find-dumper"),
42                         scm_makfrom0str (output_format_global.to_str0 ()),
43                         SCM_UNDEFINED);
44
45   output_func_  = scm_primitive_eval (exp);
46 }
47
48 Paper_outputter::~Paper_outputter ()
49 {
50   
51 }
52
53
54 void
55 Paper_outputter::output_header ()
56 {
57   String       generate = _ (", at ");
58   time_t t (time (0));
59   generate += ctime (&t);
60   generate = generate.left_string (generate.length () - 1);
61   
62   /*
63     Make fixed length time stamps
64    */
65   generate = generate + to_string (' ' * (120 - generate.length ())>? 0)  ;
66   String creator = "lelie";
67   
68   SCM args_scm = scm_list_n (scm_makfrom0str (creator.to_str0 ()),
69                              scm_makfrom0str (generate.to_str0 ()), SCM_UNDEFINED);
70
71
72   SCM scm = gh_cons (ly_symbol2scm ("header"), args_scm);
73
74   output_scheme (scm);
75 }
76
77
78
79 void
80 Paper_outputter::output_comment (String str)
81 {
82   output_scheme (scm_list_n (ly_symbol2scm ("comment"),
83                           scm_makfrom0str ((char*)str.to_str0 ()),
84                           SCM_UNDEFINED)
85                  );
86 }
87
88 void
89 Paper_outputter::output_scheme (SCM scm)
90 {
91   gh_call2 (output_func_, scm, file_);
92 }
93
94 void
95 Paper_outputter::output_scope (Scheme_hash_table *scope, String prefix)
96 {
97   SCM al = scope->to_alist ();
98   for (SCM s = al ; gh_pair_p (s); s = ly_cdr (s))
99     {
100       SCM k = ly_caar (s);
101       SCM v = ly_cdar (s);
102       String s = ly_symbol2string (k);
103       
104       if (gh_string_p (v))
105         {
106           output_String_def (prefix + s, ly_scm2string (v));
107         }
108       else if (scm_exact_p (v) == SCM_BOOL_T)
109         {
110           output_int_def (prefix + s, gh_scm2int (v));    
111         }
112       else if (gh_number_p (v))
113         {
114           output_Real_def (prefix + s, gh_scm2double (v));
115         }
116     }
117 }
118
119 void
120 Paper_outputter::output_version ()
121 {
122   String id_string = "Lily was here";
123   id_string += String_convert::pad_to (String (", ") + version_string (), 40);
124
125   output_String_def ("lilypondtagline", id_string);
126   output_String_def ("LilyPondVersion", version_string ());
127   output_String_def ("lilypondpaperunit", String (INTERNAL_UNIT));  
128 }
129
130
131 void
132 Paper_outputter::output_Real_def (String k, Real v)
133 {
134   
135   SCM scm = scm_list_n (ly_symbol2scm ("lily-def"),
136                         scm_makfrom0str (k.get_str0 ()),
137                         scm_makfrom0str (to_string (v).get_str0 ()),
138                         SCM_UNDEFINED);
139   output_scheme (scm);
140 }
141
142 void
143 Paper_outputter::output_String_def (String k, String v)
144 {
145   
146   SCM scm = scm_list_n (ly_symbol2scm ("lily-def"),
147                      scm_makfrom0str (k.get_str0 ()),
148                      scm_makfrom0str (v.get_str0 ()),
149                      SCM_UNDEFINED);
150   output_scheme (scm);
151 }
152
153 void
154 Paper_outputter::output_int_def (String k, int v)
155 {
156   SCM scm = scm_list_n (ly_symbol2scm ("lily-def"),
157                      scm_makfrom0str (k.get_str0 ()),
158                      scm_makfrom0str (to_string (v).get_str0 ()),
159                      SCM_UNDEFINED);
160   output_scheme (scm);
161 }
162
163 void
164 Paper_outputter::write_header_field_to_file (String filename, SCM key, SCM value)
165 {
166   output_scheme (scm_list_n (ly_symbol2scm ("header-to-file"),
167                              scm_makfrom0str (filename.to_str0 ()),
168                              ly_quote_scm (key), value,
169                              SCM_UNDEFINED));
170 }
171
172 void
173 Paper_outputter::write_header_fields_to_file (Scheme_hash_table * header)
174 {
175   if (dump_header_fieldnames_global.size ())
176     {
177       SCM fields = header->to_alist ();
178       for (int i = 0; i < dump_header_fieldnames_global.size (); i++)
179         {
180           String key = dump_header_fieldnames_global[i];
181           SCM val = gh_assoc (ly_symbol2scm (key.to_str0 ()), fields);
182           String s;
183           /* Only write header field to file if it exists */
184           if (gh_pair_p (val))
185             {
186               s = ly_scm2string (ly_cdr (val));
187               /* Always write header field file, even if string is empty ... */
188               write_header_field_to_file (basename_ , ly_car (val), ly_cdr (val));
189             }
190         }
191     }
192 }