]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
* scripts/convert-ly.py (conv): add rule for breakAlignOrder.
[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 "paper-line.hh"
29 #include "input-smob.hh"  // output_expr
30
31
32 Paper_outputter::Paper_outputter (String name)
33 {
34   if (safe_global_b)
35     scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);      
36   
37   file_ = scm_open_file (scm_makfrom0str (name.to_str0 ()),
38                          scm_makfrom0str ("w"));
39
40   if (output_format_global == PAGE_LAYOUT)
41     {
42       output_func_ = SCM_EOL;
43       String name = "scm output-" + output_format_global;
44       if (safe_global_b)
45         {
46           /* In safe mode, start from a GUILE safe-module and import
47              all symbols from the output module.  */
48           scm_c_use_module ("ice-9 safe");
49           SCM msm = scm_primitive_eval (ly_symbol2scm ("make-safe-module"));
50           output_module_ = scm_call_0 (msm);
51           ly_import_module (output_module_,
52                             scm_c_resolve_module (name.to_str0 ()));
53         }
54       else
55         output_module_ = scm_c_resolve_module (name.to_str0 ());
56
57       /* FIXME: output-lib should be module, that can be imported.  */
58 #define IMPORT_LESS 1 // only import the list of IMPORTS
59 #if IMPORT_LESS
60       scm_c_use_module ("lily");
61       scm_c_use_module ("ice-9 regex");
62       scm_c_use_module ("srfi srfi-13");
63 #endif
64       char const *imports[] = {
65         "lilypond-version",          /* from lily */
66         "ly:output-def-scope",
67         "ly:gulp-file",
68         "ly:number->string",
69         "ly:ragged-page-breaks",
70         "ly:optimal-page-breaks",
71         
72         "ly:number-pair->string",    /* output-lib.scm */
73         "ly:numbers->string",
74         "ly:inexact->string",
75         
76         "assoc-get",
77 #if IMPORT_LESS 
78         "string-index",              /* from srfi srfi-13 */
79         "string-join",
80         "regexp-substitute/global",  /* from (ice9 regex) */
81 #endif  
82         0,
83       };
84       
85       for (int i = 0; imports[i]; i++)
86         {
87           SCM s = ly_symbol2scm (imports[i]);
88           scm_module_define (output_module_, s, scm_primitive_eval (s));
89         }
90 #ifndef IMPORT_LESS  // rather crude, esp for safe-mode let's not
91       SCM m = scm_set_current_module (output_module_);
92       /* not present in current module*/
93       scm_c_use_module ("ice-9 regex");
94       scm_c_use_module ("srfi srfi-13");
95       /* Need only a few of these, see above
96          scm_c_use_module ("lily"); */
97       scm_set_current_module (m);
98 #endif
99     }
100   else
101     {
102       output_func_
103         = scm_call_1 (ly_scheme_function ("find-dumper"),
104                       scm_makfrom0str (output_format_global.to_str0 ()));
105       output_module_ = SCM_EOL;
106     }
107 }
108
109 Paper_outputter::~Paper_outputter ()
110 {
111   scm_close_port (file_);
112   file_ = SCM_EOL;
113 }
114
115 void
116 Paper_outputter::output_scheme (SCM scm)
117 {
118   if (output_format_global == PAGE_LAYOUT)
119     scm_display (scm_eval (scm, output_module_), file_);
120   else
121     gh_call2 (output_func_, scm, file_);
122 }
123
124 void
125 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
126 {
127   SCM fields = SCM_EOL;
128   for (int i = dump_header_fieldnames_global.size (); i--; )
129     fields
130       = gh_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
131                  fields);
132   output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
133                              paper->self_scm (),
134                              ly_quote_scm (scopes),
135                              ly_quote_scm (fields),
136                              scm_makfrom0str (basename_.to_str0 ()), 
137                              SCM_UNDEFINED));
138 }
139
140 void
141 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count)
142 {
143   String creator = gnu_lilypond_version_string ();
144   creator += " (http://lilypond.org)";
145   time_t t (time (0));
146   String time_stamp = ctime (&t);
147   time_stamp = time_stamp.left_string (time_stamp.length () - 1)
148     + " " + *tzname;
149   output_scheme (scm_list_4 (ly_symbol2scm ("header"),
150                              scm_makfrom0str (creator.to_str0 ()),
151                              scm_makfrom0str (time_stamp.to_str0 ()),
152                              scm_int2num (page_count)));
153
154   output_metadata (paper, scopes);
155   output_music_output_def (paper);
156
157   output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
158   output_scheme (scm_list_2 (ly_symbol2scm ("define-fonts"),
159                              ly_quote_scm (paper->font_descriptions ())));
160 }
161
162 void
163 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
164 {
165   Paper_line *pl = unsmob_paper_line (line);
166   Offset dim = pl->dim ();
167   if (dim[Y_AXIS] > 50 CM)
168     {
169       programming_error ("Improbable system height.");
170       dim[Y_AXIS] = 50 CM;
171     }
172
173   if (output_format_global != PAGE_LAYOUT)
174     output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
175                                gh_double2scm (dim[X_AXIS]),
176                                gh_double2scm (dim[Y_AXIS])));
177   else
178     {
179       output_scheme (scm_list_3 (ly_symbol2scm ("new-start-system"),
180                                  ly_quote_scm (ly_offset2scm (*origin)),
181                                  ly_quote_scm (ly_offset2scm (dim))));
182       (*origin)[Y_AXIS] += dim[Y_AXIS];
183     }
184
185   SCM between = SCM_EOL;
186   for (SCM s = pl->stencils (); gh_pair_p (s); s = ly_cdr (s))
187     {
188       Stencil *stil = unsmob_stencil (ly_car (s));
189       if (stil)
190         output_expr (stil->get_expr (), Offset (0,0));
191       /* Only if !PAGE_LAYOUT */
192       else if (ly_caar (s) == ly_symbol2scm ("between-system-string"))
193         between = ly_cdar (s);
194     }
195
196   if (is_last)
197     output_scheme (scm_list_1 (ly_symbol2scm ("stop-last-system")));
198   else
199     {
200       output_scheme (scm_list_1 (ly_symbol2scm ("stop-system")));
201       if (output_format_global != PAGE_LAYOUT && between != SCM_EOL)
202         output_scheme (between);
203     }
204 }
205
206 void
207 Paper_outputter::output_music_output_def (Music_output_def* odef)
208 {
209   output_scheme (scm_list_n (ly_symbol2scm ("output-paper-def"),
210                              odef->self_scm (), SCM_UNDEFINED));
211 }
212
213 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil  */
214 void
215 Paper_outputter::output_expr (SCM expr, Offset o)
216 {
217   while (1)
218     {
219       if (!gh_pair_p (expr))
220         return;
221   
222       SCM head =ly_car (expr);
223       if (unsmob_input (head))
224         {
225           Input * ip = unsmob_input (head);
226       
227           output_scheme (scm_list_n (ly_symbol2scm ("define-origin"),
228                                      scm_makfrom0str (ip->file_string ().to_str0 ()),
229                                      gh_int2scm (ip->line_number ()),
230                                      gh_int2scm (ip->column_number ()),
231                                      SCM_UNDEFINED));
232           expr = ly_cadr (expr);
233         }
234       else  if (head ==  ly_symbol2scm ("no-origin"))
235         {
236           output_scheme (scm_list_n (head, SCM_UNDEFINED));
237           expr = ly_cadr (expr);
238         }
239       else if (head == ly_symbol2scm ("translate-stencil"))
240         {
241           o += ly_scm2offset (ly_cadr (expr));
242           expr = ly_caddr (expr);
243         }
244       else if (head == ly_symbol2scm ("combine-stencil"))
245         {
246           output_expr (ly_cadr (expr), o);
247           expr = ly_caddr (expr);
248         }
249       else
250         {
251           output_scheme (scm_list_n (ly_symbol2scm ("placebox"),
252                                      gh_double2scm (o[X_AXIS]),
253                                      gh_double2scm (o[Y_AXIS]),
254                                      expr,
255                                      SCM_UNDEFINED));
256           return;
257         }
258     }
259 }
260