]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-outputter.cc
* lily/book.cc (to_stencil): New method.
[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 <math.h>
11 #include <time.h>
12
13 #include "array.hh"
14 #include "dimensions.hh"
15 #include "font-metric.hh"
16 #include "input-smob.hh"
17 #include "lily-guile.hh"
18 #include "lily-version.hh"
19 #include "ly-module.hh"
20 #include "main.hh"
21 #include "page.hh"
22 #include "paper-book.hh"
23 #include "paper-def.hh"
24 #include "paper-line.hh"
25 #include "paper-outputter.hh"
26 #include "file-name.hh"
27 #include "scm-hash.hh"
28 #include "stencil.hh"
29 #include "string-convert.hh"
30 #include "warn.hh"
31
32 // JUNKME
33 extern SCM stencil2line (Stencil* stil, bool is_title = false);
34
35 Paper_outputter::Paper_outputter (String filename)
36 {
37   filename_ = filename;
38   file_ = scm_open_file (scm_makfrom0str (filename.to_str0 ()),
39                          scm_makfrom0str ("w"));
40
41   if (safe_global_b)
42     scm_define (ly_symbol2scm ("safe-mode?"), SCM_BOOL_T);      
43
44   String module_name = "scm output-" + output_format_global;
45   if (safe_global_b)
46     {
47       /* In safe mode, start from a GUILE safe-module and import
48          all symbols from the output module.  */
49       scm_c_use_module ("ice-9 safe");
50       SCM msm = scm_primitive_eval (ly_symbol2scm ("make-safe-module"));
51       output_module_ = scm_call_0 (msm);
52       ly_import_module (output_module_,
53                         scm_c_resolve_module (module_name.to_str0 ()));
54     }
55   else
56     output_module_ = scm_c_resolve_module (module_name.to_str0 ());
57   
58   /* FIXME: output-lib should be module, that can be imported.  */
59 #define IMPORT_LESS 1 // only import the list of IMPORTS
60 #if IMPORT_LESS
61   scm_c_use_module ("lily");
62   scm_c_use_module ("ice-9 regex");
63   scm_c_use_module ("srfi srfi-1");
64   scm_c_use_module ("srfi srfi-13");
65 #endif
66   char const *imports[] = {
67     "lilypond-version",          /* from lily */
68     "ly:output-def-scope",
69     "ly:gulp-file",
70     "ly:number->string",
71     "ly:ragged-page-breaks",
72     "ly:optimal-page-breaks",
73     
74     "ly:number-pair->string",    /* output-lib.scm */
75     "ly:numbers->string",
76     "ly:inexact->string",
77     
78     "assoc-get",
79 #if IMPORT_LESS 
80     "remove",                    /* from srfi srfi-1 */
81     "string-index",              /* from srfi srfi-13 */
82     "string-join",
83     "regexp-substitute/global",  /* from (ice9 regex) */
84 #endif  
85     0,
86   };
87       
88   for (int i = 0; imports[i]; i++)
89     {
90       SCM s = ly_symbol2scm (imports[i]);
91       scm_module_define (output_module_, s, scm_primitive_eval (s));
92     }
93 #ifndef IMPORT_LESS  // rather crude, esp for safe-mode let's not
94   SCM m = scm_set_current_module (output_module_);
95   /* not present in current module*/
96   scm_c_use_module ("ice-9 regex");
97   scm_c_use_module ("srfi srfi-13");
98   /* Need only a few of these, see above
99      scm_c_use_module ("lily"); */
100   scm_set_current_module (m);
101 #endif
102 }
103
104 Paper_outputter::~Paper_outputter ()
105 {
106   scm_close_port (file_);
107   file_ = SCM_EOL;
108 }
109
110 void
111 Paper_outputter::output_scheme (SCM scm)
112 {
113   scm_display (scm_eval (scm, output_module_), file_);
114 }
115
116 void
117 Paper_outputter::output_metadata (Paper_def *paper, SCM scopes)
118 {
119   SCM fields = SCM_EOL;
120   for (int i = dump_header_fieldnames_global.size (); i--; )
121     fields
122       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
123                  fields);
124
125   File_name file_name (filename_);
126   file_name.ext_ = "";
127   String basename = file_name.to_string ();
128   output_scheme (scm_list_n (ly_symbol2scm ("output-scopes"),
129                              paper->self_scm (),
130                              ly_quote_scm (scopes),
131                              ly_quote_scm (fields),
132                              scm_makfrom0str (basename.to_str0 ()), 
133                              SCM_UNDEFINED));
134 }
135
136 void
137 Paper_outputter::output_header (Paper_def *paper, SCM scopes, int page_count,
138                                 bool is_classic)
139 {
140   String creator = gnu_lilypond_version_string ();
141   creator += " (http://lilypond.org)";
142   time_t t (time (0));
143   String time_stamp = ctime (&t);
144   time_stamp = time_stamp.left_string (time_stamp.length () - 1)
145     + " " + *tzname;
146   output_scheme (scm_list_n (ly_symbol2scm ("header"),
147                              scm_makfrom0str (creator.to_str0 ()),
148                              scm_makfrom0str (time_stamp.to_str0 ()),
149                              paper->self_scm (),
150                              scm_int2num (page_count),
151                              ly_bool2scm (is_classic),
152                              SCM_UNDEFINED));
153
154   output_metadata (paper, scopes);
155   output_music_output_def (paper);
156
157   output_scheme (scm_list_1 (ly_symbol2scm ("header-end")));
158
159   /* TODO: maybe have Scheme extract the fonts directly from \paper ?
160           
161      Alternatively, we could simply load the fonts on demand in the
162      output, and do away with this define-fonts step.  */
163   SCM fonts = paper->font_descriptions ();
164   output_scheme (scm_list_3 (ly_symbol2scm ("define-fonts"),
165                              paper->self_scm (),
166                              //FIXME:
167                              ly_quote_scm (ly_list_qsort_uniq_x (fonts))));
168 }
169
170 void
171 Paper_outputter::output_line (SCM line, Offset *origin, bool is_last)
172 {
173   Paper_line *p = unsmob_paper_line (line);
174   Offset dim = p->dim ();
175   if (dim[Y_AXIS] > 50 CM)
176     {
177       programming_error (to_string ("Improbable line height: %f",
178                                     dim[Y_AXIS]));
179       dim[Y_AXIS] = 50 CM;
180     }
181
182   output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
183                              ly_quote_scm (ly_offset2scm (*origin)),
184                              ly_quote_scm (ly_offset2scm (dim))));
185
186 #if 1 /* FIXME: how stupid is this memorywise?  */
187   output_stencil (unsmob_stencil (p->to_stencil ()));
188 #else
189   for (SCM s = p->stencils (); ly_c_pair_p (s); s = ly_cdr (s))
190     output_expr (unsmob_stencil (ly_car (s))->expr (), Offset (0, 0));
191 #endif
192
193   (*origin)[Y_AXIS] += dim[Y_AXIS];
194   output_scheme (scm_list_2 (ly_symbol2scm ("stop-system"),
195                              ly_bool2scm (is_last)));
196 }
197
198 void
199 Paper_outputter::output_page (Page *p, bool is_last)
200 {
201   output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
202
203 #if 0 /* FIXME: how stupid is this, memorywise?  */
204   
205   output_scheme (scm_list_3 (ly_symbol2scm ("start-system"),
206                              ly_quote_scm (ly_offset2scm (Offset (0, 0))),
207                              ly_quote_scm (ly_offset2scm (Offset (0, 0)))));
208
209   output_stencil (unsmob_stencil (p->to_stencil ()));
210
211   output_scheme (scm_list_2 (ly_symbol2scm ("stop-system"), SCM_BOOL_T));
212 #else
213   Offset o (p->left_margin_, p->top_margin_);
214   Real vfill = (p->line_count_ > 1
215                 ? (p->text_height () - p->height_) / (p->line_count_ - 1)
216                 : 0);
217
218   Real coverage = p->height_ / p->text_height ();
219   if (coverage < p->MIN_COVERAGE_)
220     /* Do not space out a badly filled page.  This is too simplistic
221        (ie broken), because this should not vary too much between
222        (subsequent?) pages in a book.  */
223     vfill = 0;
224
225   if (unsmob_stencil (p->header_))
226     {
227       output_line (stencil2line (unsmob_stencil (p->header_)), &o, false);
228       o[Y_AXIS] += p->head_sep_;
229     }
230   for (SCM s = p->lines_; s != SCM_EOL; s = ly_cdr (s))
231     {
232       SCM line = ly_car (s);
233       output_line (line, &o,
234                    is_last && ly_cdr (s) != SCM_EOL
235                    && !unsmob_stencil (p->copyright_)
236                    && !unsmob_stencil (p->tagline_)
237                    && !unsmob_stencil (p->footer_));
238       
239       /* Do not put vfill between title and its music, */
240       if (scm_pair_p (ly_cdr (s))
241           && (!unsmob_paper_line (line)->is_title () || vfill < 0))
242         o[Y_AXIS] += vfill;
243       /* rather put extra just before the title.  */
244       if (ly_cdr (s) != SCM_EOL
245           && (unsmob_paper_line (ly_cadr (s))->is_title () && vfill > 0))
246         o[Y_AXIS] += vfill;
247     }
248
249   o[Y_AXIS] = p->vsize_ - p->bottom_margin_;
250   if (unsmob_stencil (p->copyright_))
251     o[Y_AXIS] -= unsmob_stencil (p->copyright_)->extent (Y_AXIS).length ();
252   if (unsmob_stencil (p->tagline_))
253     o[Y_AXIS] -= unsmob_stencil (p->tagline_)->extent (Y_AXIS).length ();
254   if (unsmob_stencil (p->footer_))
255     o[Y_AXIS] -= unsmob_stencil (p->footer_)->extent (Y_AXIS).length ();
256
257   if (unsmob_stencil (p->copyright_))
258     output_line (stencil2line (unsmob_stencil (p->copyright_)), &o,
259                  is_last
260                  && !unsmob_stencil (p->tagline_)
261                  && !unsmob_stencil (p->footer_));
262   if (unsmob_stencil (p->tagline_))
263     output_line (stencil2line (unsmob_stencil (p->tagline_)), &o,
264                       is_last && !unsmob_stencil (p->footer_));
265   if (unsmob_stencil (p->footer_))
266     output_line (stencil2line (unsmob_stencil (p->footer_)), &o, is_last);
267 #endif
268   
269   output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
270                              ly_bool2scm (is_last
271                                           && !unsmob_stencil (p->footer_))));
272 }
273
274 void
275 Paper_outputter::output_music_output_def (Music_output_def *odef)
276 {
277   output_scheme (scm_list_2 (ly_symbol2scm ("output-paper-def"),
278                               odef->self_scm ()));
279 }
280
281 void
282 Paper_outputter::output_stencil (Stencil *stil)
283 {
284   output_expr (stil->expr (), stil->origin ());
285 }
286
287 /* TODO: replaceme/rewriteme, see output-ps.scm: output-stencil  */
288 void
289 Paper_outputter::output_expr (SCM expr, Offset o)
290 {
291   while (1)
292     {
293       if (!ly_c_pair_p (expr))
294         return;
295   
296       SCM head =ly_car (expr);
297       if (unsmob_input (head))
298         {
299           Input *ip = unsmob_input (head);
300           output_scheme (scm_list_4 (ly_symbol2scm ("define-origin"),
301                                       scm_makfrom0str (ip->file_string ()
302                                                        .to_str0 ()),
303                                       scm_int2num (ip->line_number ()),
304                                       scm_int2num (ip->column_number ())));
305           expr = ly_cadr (expr);
306         }
307       else  if (head ==  ly_symbol2scm ("no-origin"))
308         {
309           output_scheme (scm_list_1 (head));
310           expr = ly_cadr (expr);
311         }
312       else if (head == ly_symbol2scm ("translate-stencil"))
313         {
314           o += ly_scm2offset (ly_cadr (expr));
315           expr = ly_caddr (expr);
316         }
317       else if (head == ly_symbol2scm ("combine-stencil"))
318         {
319           output_expr (ly_cadr (expr), o);
320           expr = ly_caddr (expr);
321         }
322       else
323         {
324           output_scheme (scm_list_4 (ly_symbol2scm ("placebox"),
325                                      scm_make_real (o[X_AXIS]),
326                                      scm_make_real (o[Y_AXIS]),
327                                      expr));
328           return;
329         }
330     }
331 }