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