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