]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* scm/output-ps.scm (header): Papersize from paper.
[lilypond.git] / lily / paper-book.cc
1 /*
2   paper-book.cc -- implement Paper_book
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include <stdio.h>
10 #include <math.h>
11
12 #include "ly-module.hh"
13 #include "main.hh"
14 #include "paper-book.hh"
15 #include "paper-def.hh"
16 #include "paper-outputter.hh"
17 #include "paper-line.hh"
18 #include "paper-score.hh"
19 #include "stencil.hh"
20
21 static Real const MIN_COVERAGE = 0.66;
22
23 static SCM
24 stencil2line (Stencil* stil, bool is_title = false)
25 {
26   static SCM z;
27   if (!z)
28     z = scm_permanent_object (ly_offset2scm (Offset (0, 0)));
29   Offset dim = Offset (stil->extent (X_AXIS).length (),
30                        stil->extent (Y_AXIS).length ());
31   Paper_line *pl = new Paper_line (dim, scm_cons (stil->smobbed_copy (),
32                                                   SCM_EOL),
33                                    -10001 * is_title, is_title);
34
35   return scm_gc_unprotect_object (pl->self_scm ());
36 }
37
38 /* Simplistic page interface */
39 class Page
40 {
41 public:
42   Paper_def *paper_;
43   static int page_count_;
44   int number_;
45   int line_count_;
46
47   Protected_scm lines_;
48   Protected_scm header_;
49   Protected_scm footer_;
50   Protected_scm copyright_;
51   Protected_scm tagline_;
52   
53   Stencil *get_header () { return unsmob_stencil (header_); }
54   Stencil *get_copyright () { return unsmob_stencil (copyright_); }
55   Stencil *get_tagline () { return unsmob_stencil (tagline_); }
56   Stencil *get_footer () { return unsmob_stencil (footer_); }
57
58   /* actual height filled with text.  */
59   Real height_;
60   
61   // HMMM all this size stuff to paper/paper-outputter?
62   Real hsize_;
63   Real vsize_;
64   Real left_margin_;
65   Real top_margin_;
66   Real bottom_margin_;
67   Real foot_sep_;
68   Real head_sep_;
69   Real text_width_;
70
71   /* available area for text.  */
72   Real text_height ();
73
74   Page (Paper_def*, int);
75   void output (Paper_outputter*, bool);
76 };
77
78 int Page::page_count_ = 0;
79
80 Page::Page (Paper_def *paper, int number)
81 {
82   paper_ = paper;
83   number_ = number;
84   page_count_++;
85   
86   height_ = 0;
87   lines_ = SCM_EOL;
88   line_count_ = 0;
89
90   hsize_ = paper->get_dimension (ly_symbol2scm ("hsize"));
91   vsize_ = paper->get_dimension (ly_symbol2scm ("vsize"));
92   top_margin_ = paper->get_dimension (ly_symbol2scm ("top-margin"));
93   bottom_margin_ = paper->get_dimension (ly_symbol2scm ("bottom-margin"));
94   head_sep_ = paper->get_dimension (ly_symbol2scm ("head-sep"));
95   foot_sep_ = paper->get_dimension (ly_symbol2scm ("foot-sep"));
96   text_width_ = paper->get_dimension (ly_symbol2scm ("linewidth"));
97   left_margin_ = (hsize_ - text_width_) / 2;
98   
99   copyright_ = SCM_EOL;
100   tagline_ = SCM_EOL;
101   
102   SCM make_header = ly_scheme_function ("make-header");
103   SCM make_footer = ly_scheme_function ("make-footer");
104
105   header_ = scm_call_2 (make_header, paper_->self_scm (),
106                         scm_int2num (number_));
107   if (get_header ())
108     get_header ()->align_to (Y_AXIS, UP);
109     
110   footer_ = scm_call_2 (make_footer, paper_->self_scm (),
111                         scm_int2num (number_));
112   if (get_footer ())
113     get_footer ()->align_to (Y_AXIS, UP);
114 }
115
116 void
117 Page::output (Paper_outputter *out, bool is_last)
118 {
119   progress_indication ("[" + to_string (number_));
120   out->output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
121   Offset o (left_margin_, top_margin_);
122   Real vfill = line_count_ > 1 ? (text_height () - height_) / (line_count_ - 1)
123     : 0;
124
125   Real coverage = height_ / text_height ();
126   if (coverage < MIN_COVERAGE)
127     /* Do not space out a badly filled page.  This is too simplistic
128        (ie broken), because this should not vary too much between
129        (subsequent?) pages in a book.  */
130     vfill = 0;
131
132   if (get_header ())
133     {
134       out->output_line (stencil2line (get_header ()), &o, false);
135       o[Y_AXIS] += head_sep_;
136     }
137   for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
138     {
139       SCM line = ly_car (s);
140       out->output_line (line, &o,
141                         is_last && ly_cdr (s) != SCM_EOL && !get_copyright ()
142                         && !get_tagline () && !get_footer ());
143       
144       /* Do not put vfill between title and its music, */
145       if (scm_pair_p (ly_cdr (s))
146           && (!unsmob_paper_line (line)->is_title () || vfill < 0))
147         o[Y_AXIS] += vfill;
148       /* rather put extra just before the title.  */
149       if (ly_cdr (s) != SCM_EOL
150           && (unsmob_paper_line (ly_cadr (s))->is_title () && vfill > 0))
151         o[Y_AXIS] += vfill;
152     }
153
154   o[Y_AXIS] = vsize_ - bottom_margin_;
155   if (get_copyright ())
156     o[Y_AXIS] -= get_copyright ()->extent (Y_AXIS).length ();
157   if (get_tagline ())
158     o[Y_AXIS] -= get_tagline ()->extent (Y_AXIS).length ();
159   if (get_footer ())
160     o[Y_AXIS] -= get_footer ()->extent (Y_AXIS).length ();
161
162   if (get_copyright ())
163     out->output_line (stencil2line (get_copyright ()), &o,
164                       is_last && !get_tagline () && !get_footer ());
165   if (get_tagline ())
166     out->output_line (stencil2line (get_tagline ()), &o,
167                       is_last && !get_footer ());
168   if (get_footer ())
169     out->output_line (stencil2line (get_footer ()), &o, is_last);
170   out->output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
171                                   ly_bool2scm (is_last && !get_footer ())));
172   progress_indication ("]");
173 }
174
175 Real
176 Page::text_height ()
177 {
178   Real h = vsize_ - top_margin_ - bottom_margin_;
179   if (get_header ())
180     h -= get_header ()->extent (Y_AXIS).length () + head_sep_;
181   if (get_copyright () || get_tagline () || get_footer ())
182     h -= foot_sep_;
183   if (get_copyright ())
184     h -= get_copyright ()->extent (Y_AXIS).length ();
185   if (get_tagline ())
186     h -= get_tagline ()->extent (Y_AXIS).length ();
187   if (get_footer ())
188     h -= get_footer ()->extent (Y_AXIS).length ();
189   return h;
190 }
191
192 /****************************************************************/
193
194 Paper_book::Paper_book ()
195 {
196   copyright_ = SCM_EOL;
197   tagline_ = SCM_EOL;
198   
199   smobify_self ();
200 }
201
202 Paper_book::~Paper_book ()
203 {
204 }
205
206 #include "ly-smobs.icc"
207
208 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
209 IMPLEMENT_SMOBS (Paper_book)
210 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
211
212 SCM
213 Paper_book::mark_smob (SCM smob)
214 {
215   Paper_book *pb = (Paper_book*) SCM_CELL_WORD_1 (smob);
216   for (int i = 0; i < pb->headers_.size (); i++)
217     scm_gc_mark (pb->headers_[i]);
218   for (int i = 0; i < pb->global_headers_.size (); i++)
219     scm_gc_mark (pb->global_headers_[i]);
220   for (int i = 0; i < pb->papers_.size (); i++)
221     scm_gc_mark (pb->papers_[i]->self_scm ());
222   for (int i = 0; i < pb->scores_.size (); i++)
223     scm_gc_mark (pb->scores_[i]);
224
225   scm_gc_mark (pb->copyright_);
226   
227   return pb->tagline_;
228 }
229
230 int
231 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
232 {
233   Paper_book *b = (Paper_book*) ly_cdr (smob);
234      
235   scm_puts ("#<", port);
236   scm_puts (classname (b), port);
237   scm_puts (" ", port);
238   //scm_puts (b->, port);
239   scm_puts (">", port);
240   return 1;
241 }
242
243 void
244 Paper_book::output (String outname)
245 {
246   if (!papers_.size ())
247     return;
248     
249   /* Generate all stencils to trigger font loads.  */
250   Link_array<Page> *pages = this->pages ();
251
252   Paper_def *paper = papers_[0];
253   Paper_outputter *out = paper->get_paper_outputter (outname);
254   out->output_header (paper, scopes (0), pages->size (), false);
255
256   int page_count = pages->size ();
257   for (int i = 0; i < page_count; i++)
258     (*pages)[i]->output (out, i + 1 == page_count);
259
260   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
261
262   /* Ugh.  */
263   for (int i = pages->size (); i--;)
264     delete pages->elem (i);
265   delete pages;
266   
267   progress_indication ("\n");
268 }
269
270 SCM
271 Paper_book::scopes (int i)
272 {
273   SCM scopes = SCM_EOL;
274   if (headers_[i])
275     scopes = scm_cons (headers_[i], scopes);
276   if (global_headers_[i] && global_headers_[i] != headers_[i])
277     scopes = scm_cons (global_headers_[i], scopes);
278   return scopes;
279 }
280
281 Stencil*
282 Paper_book::title (int i)
283 {
284   SCM user_title = ly_scheme_function ("user-title");
285   SCM book_title = ly_scheme_function ("book-title");
286   SCM score_title = ly_scheme_function ("score-title");
287   SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
288                : ly_symbol2scm ("scoreTitle"));
289
290   Stencil *title = 0;
291   SCM scopes = this->scopes (i);
292   SCM s = ly_modules_lookup (scopes, field);
293   if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
294     title = unsmob_stencil (scm_call_2 (user_title,
295                                         papers_[0]->self_scm (),
296                                         scm_variable_ref (s)));
297   else
298     title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
299                                         papers_[0]->self_scm (),
300                                         scopes));
301   if (title)
302     title->align_to (Y_AXIS, UP);
303   
304   return title;
305 }
306
307 void
308 Paper_book::classic_output (String outname)
309 {
310   int count = scores_.size ();
311   Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
312   out->output_header (papers_.top (), scopes (count - 1), 0, true);
313
314   Paper_line *first = unsmob_paper_line (scm_vector_ref (scores_.top (),
315                                                          scm_int2num (0)));
316   Offset o (0, -0.5 * first->dim ()[Y_AXIS]);
317   int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
318   for (int i = 0; i < line_count; i++)
319     out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
320                       &o, i == line_count - 1);
321   
322   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
323   progress_indication ("\n");
324 }
325
326
327 /* calculate book height, #lines, stencils.  */
328 void
329 Paper_book::init ()
330 {
331   int score_count = scores_.size ();
332
333   /* Calculate the full book height.  Hmm, can't we cache system
334      heights while making stencils?  */
335   height_ = 0;
336   for (int i = 0; i < score_count; i++)
337     {
338       Stencil *title = this->title (i);
339       if (title)
340         height_ += title->extent (Y_AXIS).length ();
341
342       int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
343       for (int j = 0; j < line_count; j++)
344         {
345           SCM s = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
346           height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
347         }
348     }
349
350   Paper_def *paper = papers_[0];
351   SCM scopes = this->scopes (0);
352
353   SCM make_tagline = ly_scheme_function ("make-tagline");
354   tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
355   Real tag_height = 0;
356   if (Stencil *s = unsmob_stencil (tagline_))
357     tag_height = s->extent (Y_AXIS).length ();
358   height_ += tag_height;
359
360   SCM make_copyright = ly_scheme_function ("make-copyright");
361   copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
362   Real copy_height = 0;
363   if (Stencil *s = unsmob_stencil (copyright_))
364     copy_height = s->extent (Y_AXIS).length ();
365   height_ += copy_height;
366 }
367
368 SCM
369 Paper_book::lines ()
370 {
371   int score_count = scores_.size ();
372   SCM lines = SCM_EOL;
373   for (int i = 0; i < score_count; i++)
374     {
375       if (Stencil *title = this->title (i))
376         lines = ly_snoc (stencil2line (title, true), lines);
377       lines = scm_append (scm_list_2 (lines, scm_vector_to_list (scores_[i])));
378     }
379   //debug helper; ughr
380   int i = 0;
381   for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
382     unsmob_paper_line (ly_car (s))->number_ = ++i;
383   return lines;
384 }
385
386 Link_array<Page>*
387 Paper_book::pages ()
388 {
389   init ();
390   Page::page_count_ = 0;
391   Paper_def *paper = papers_[0];
392   Page *page = new Page (paper, 1);
393
394   Real text_height = page->text_height ();
395
396   Real copy_height = 0;
397   if (Stencil *s = unsmob_stencil (copyright_))
398     copy_height = s->extent (Y_AXIS).length ();
399
400   Real tag_height = 0;
401   if (Stencil *s = unsmob_stencil (tagline_))
402     tag_height = s->extent (Y_AXIS).length ();
403
404   SCM all = lines ();
405   SCM proc = paper->get_scmvar ("page-breaking");
406   SCM breaks = scm_apply_0 (proc, scm_list_n (all, scm_make_real (height_),
407                                               scm_make_real (text_height),
408                                               scm_make_real (-copy_height),
409                                               scm_make_real (-tag_height),
410                                               SCM_UNDEFINED));
411
412   /* Copyright on first page.  */
413   if (unsmob_stencil (copyright_))
414     page->copyright_ = copyright_;
415
416   Link_array<Page> *pages = new Link_array<Page>;
417   int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
418   int line = 1;
419   for (int i = 0; i < page_count; i++)
420     {
421       if (i)
422         page = new Page (paper, i+1);
423       int next = i + 1 < page_count
424         ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
425       while ((!next && all != SCM_EOL) || line <= next)
426         {
427           SCM s = ly_car (all);
428           page->lines_ = ly_snoc (s, page->lines_);
429           page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
430           page->line_count_++;
431           all = ly_cdr (all);
432           line++;
433         }
434       pages->push (page);
435     }
436
437   /* Tagline on last page.  */
438   if (unsmob_stencil (tagline_))
439     page->tagline_ = tagline_;
440
441   return pages;
442 }
443
444 static SCM
445 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
446                       Real first, Real last)
447 {
448   int page_number = 0;
449   int page_count = int (book_height / text_height + 0.5);
450   SCM breaks = SCM_EOL;
451   Real page_height = text_height + first;
452   Real h = 0;
453   int number = 0;
454   for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
455     {
456       Paper_line *pl = unsmob_paper_line (ly_car (s));
457       if (!pl->is_title () && h < page_height)
458         number++;
459       h += pl->dim ()[Y_AXIS];
460       if (!pl->is_title () && h > page_height)
461         {
462           breaks = ly_snoc (scm_int2num (number), breaks);
463           page_number++;
464           page_height = text_height + (page_number == page_count) * last;
465           h = 0;
466         }
467       if (ly_cdr (s) == SCM_EOL)
468         breaks = ly_snoc (scm_int2num (pl->number_), breaks);
469     }
470
471   return scm_vector (breaks);
472 }
473
474 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
475            5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
476            "Return a vector with line numbers of page breaks.")
477 {
478   SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
479   SCM_ASSERT_TYPE (is_number (book), book, SCM_ARG2, __FUNCTION__, "real");
480   SCM_ASSERT_TYPE (is_number (text), text, SCM_ARG2, __FUNCTION__, "real");
481   SCM_ASSERT_TYPE (is_number (first), first, SCM_ARG2, __FUNCTION__, "real");
482   SCM_ASSERT_TYPE (is_number (last), last, SCM_ARG2, __FUNCTION__, "real");
483
484   return c_ragged_page_breaks (lines,
485                                ly_scm2double (book), ly_scm2double (text),
486                                ly_scm2double (first), ly_scm2double (last));
487 }