]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
* ps/lilyponddefs.ps: Remove automatic page layout.
[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--2004 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 "stencil.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-module.hh"
27 #include "paper-book.hh"
28 #include "input-smob.hh"  // output_expr
29
30
31 Paper_outputter::Paper_outputter (String name)
32 {
33   if (safe_global_b)
34     scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);      
35   
36   file_ = scm_open_file (scm_makfrom0str (name.to_str0 ()),
37                          scm_makfrom0str ("w"));
38
39   static SCM find_dumper;
40   if (!find_dumper)
41     find_dumper = scm_c_eval_string ("find-dumper");
42
43   output_func_
44     = scm_call_1 (find_dumper,
45                   scm_makfrom0str (output_format_global.to_str0 ()));
46
47   String creator = gnu_lilypond_version_string ();
48   creator += " (http://lilypond.org)";
49   time_t t (time (0));
50   String time_stamp = ctime (&t);
51   time_stamp = time_stamp.left_string (time_stamp.length () - 1)
52     + " " + *tzname;
53   output_scheme (scm_list_3 (ly_symbol2scm ("header"),
54                              scm_makfrom0str (creator.to_str0 ()),
55                              scm_makfrom0str (time_stamp.to_str0 ())));
56 }
57
58 Paper_outputter::~Paper_outputter ()
59 {
60   scm_close_port (file_);
61   file_ = SCM_EOL;
62 }
63
64 void
65 Paper_outputter::output_scheme (SCM scm)
66 {
67   gh_call2 (output_func_, scm, file_);
68 }
69
70 void
71 Paper_outputter::output_metadata (SCM scopes, Paper_def *paper)
72 {
73   SCM fields = SCM_EOL;
74   for (int i = dump_header_fieldnames_global.size (); i--; )
75     fields
76       = gh_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
77                  fields);
78   output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
79                              paper->self_scm (),
80                              scm_list_n (ly_symbol2scm ("quote"),
81                                          scopes, SCM_UNDEFINED),
82                              scm_list_n (ly_symbol2scm ("quote"),
83                                          fields, SCM_UNDEFINED),
84                              scm_makfrom0str (basename_.to_str0 ()), 
85                              SCM_UNDEFINED));
86 }
87
88 void
89 Paper_outputter::output_header (Paper_def *paper)
90 {
91   output_music_output_def (paper);
92   output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
93   output_scheme (scm_list_2 (ly_symbol2scm ("define-fonts"),
94                              ly_quote_scm (paper->font_descriptions ())));
95 }
96
97 void
98 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
99 {
100   Offset dim = ly_scm2offset (ly_car (line));
101   if (dim[Y_AXIS] > 50 CM)
102     {
103       programming_error ("Improbable system height.");
104       dim[Y_AXIS] = 50 CM;
105     }
106
107   if (output_format_global != PAGE_LAYOUT)
108     output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
109                                gh_double2scm (dim[X_AXIS]),
110                                gh_double2scm (dim[Y_AXIS])));
111   else
112     {
113       output_scheme (scm_list_3 (ly_symbol2scm ("new-start-system"),
114                                  ly_quote_scm (ly_offset2scm (*origin)),
115                                  ly_quote_scm (ly_offset2scm (dim))));
116       (*origin)[Y_AXIS] += dim[Y_AXIS];
117     }
118
119   SCM between = SCM_EOL;
120   for (SCM s = ly_cdr (line); gh_pair_p (s); s = ly_cdr (s))
121     {
122       Stencil *stil = unsmob_stencil (ly_cdar (s));
123       SCM head = ly_caar (s);
124       
125       if (head == ly_symbol2scm ("between-system-string"))
126         {
127           between = stil->get_expr ();
128           continue;
129         }
130
131       if (stil)
132         output_expr (stil->get_expr (), ly_scm2offset (head));
133     }
134
135   if (is_last)
136     output_scheme (scm_list_1 (ly_symbol2scm ("stop-last-system")));
137   else
138     {
139       output_scheme (scm_list_1 (ly_symbol2scm ("stop-system")));
140       if (output_format_global != PAGE_LAYOUT && between != SCM_EOL)
141         output_scheme (between);
142     }
143 }
144
145 void
146 Paper_outputter::output_music_output_def (Music_output_def* odef)
147 {
148   output_scheme (scm_list_n (ly_symbol2scm ("output-paper-def"),
149                              odef->self_scm (), SCM_UNDEFINED));
150 }
151
152 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil  */
153 void
154 Paper_outputter::output_expr (SCM expr, Offset o)
155 {
156   while (1)
157     {
158       if (!gh_pair_p (expr))
159         return;
160   
161       SCM head =ly_car (expr);
162       if (unsmob_input (head))
163         {
164           Input * ip = unsmob_input (head);
165       
166           output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
167                                      scm_makfrom0str (ip->file_string ().to_str0 ()),
168                                      gh_int2scm (ip->line_number ()),
169                                      gh_int2scm (ip->column_number ()),
170                                      SCM_UNDEFINED));
171           expr = ly_cadr (expr);
172         }
173       else  if (head ==  ly_symbol2scm ("no-origin"))
174         {
175           output_scheme (scm_list_n (head, SCM_UNDEFINED));
176           expr = ly_cadr (expr);
177         }
178       else if (head == ly_symbol2scm ("translate-stencil"))
179         {
180           o += ly_scm2offset (ly_cadr (expr));
181           expr = ly_caddr (expr);
182         }
183       else if (head == ly_symbol2scm ("combine-stencil"))
184         {
185           output_expr (ly_cadr (expr), o);
186           expr = ly_caddr (expr);
187         }
188       else
189         {
190           output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
191                                      gh_double2scm (o[X_AXIS]),
192                                      gh_double2scm (o[Y_AXIS]),
193                                      expr,
194                                      SCM_UNDEFINED));
195           return;
196         }
197     }
198 }
199