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