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