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