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