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