]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
b89d0a86b6b06a2c3fc7b0ad31630256e0498786
[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-score.hh"
18 #include "stencil.hh"
19
20 static Real const MIN_COVERAGE = 0.66;
21 static Real const MAX_CRAMP = 0.05;
22
23 static SCM
24 stencil2line (Stencil* stil)
25 {
26   static SCM z = ly_offset2scm (Offset (0, 0));
27   Offset dim = Offset (stil->extent (X_AXIS).length (),
28                        stil->extent (Y_AXIS).length ());
29   return scm_cons (ly_offset2scm (dim),
30                    scm_list_1 (scm_cons (z, stil->smobbed_copy ())));
31 }
32
33 static SCM
34 title2line (Stencil* stil)
35 {
36   SCM s = stencil2line (stil);
37   /* whugh, add marker recognise titles.  */
38   return scm_cons (scm_cons (ly_symbol2scm ("title"), ly_car (s)), ly_cdr (s));
39 }
40
41 /* Simplistic page interface */
42 class Page
43 {
44 public:
45   Paper_def *paper_;
46   static int page_count_;
47   int number_;
48   int line_count_;
49
50   Protected_scm lines_;
51   Protected_scm header_;
52   Protected_scm footer_;
53   Protected_scm copyright_;
54   Protected_scm tagline_;
55   
56   Stencil *get_header () { return unsmob_stencil (header_); }
57   Stencil *get_copyright () { return unsmob_stencil (copyright_); }
58   Stencil *get_tagline () { return unsmob_stencil (tagline_); }
59   Stencil *get_footer () { return unsmob_stencil (footer_); }
60
61   /* actual height filled with text.  */
62   Real height_;
63   
64   // HMMM all this size stuff to paper/paper-outputter?
65   Real hsize_;
66   Real vsize_;
67   Real left_margin_;
68   Real top_margin_;
69   Real bottom_margin_;
70   Real foot_sep_;
71   Real head_sep_;
72   Real text_width_;
73
74   /* available area for text.  */
75   Real text_height ();
76
77   Page (Paper_def*, int);
78   void output (Paper_outputter*, bool);
79 };
80
81 int Page::page_count_ = 0;
82
83 Page::Page (Paper_def *paper, int number)
84 {
85   paper_ = paper;
86   number_ = number;
87   page_count_++;
88   
89   height_ = 0;
90   lines_ = SCM_EOL;
91   line_count_ = 0;
92
93   hsize_ = paper->get_realvar (ly_symbol2scm ("hsize"));
94   vsize_ = paper->get_realvar (ly_symbol2scm ("vsize"));
95   top_margin_ = paper->get_realvar (ly_symbol2scm ("top-margin"));
96   bottom_margin_ = paper->get_realvar (ly_symbol2scm ("bottom-margin"));
97   head_sep_ = paper->get_realvar (ly_symbol2scm ("head-sep"));
98   foot_sep_ = paper->get_realvar (ly_symbol2scm ("foot-sep"));
99   text_width_ = paper->get_realvar (ly_symbol2scm ("linewidth"));
100   left_margin_ = (hsize_ - text_width_) / 2;
101   
102   copyright_ = SCM_EOL;
103   tagline_ = SCM_EOL;
104   
105   SCM make_header = scm_primitive_eval (ly_symbol2scm ("make-header"));
106   SCM make_footer = scm_primitive_eval (ly_symbol2scm ("make-footer"));
107
108   header_ = scm_call_2 (make_header, paper_->smobbed_copy (),
109                         scm_int2num (number_));
110   // FIXME: why does this (generates Stencil) not trigger font load?
111   if (get_header ())
112     get_header ()->align_to (Y_AXIS, UP);
113     
114   footer_ = scm_call_2 (make_footer, paper_->smobbed_copy (),
115                         scm_int2num (number_));
116   if (get_footer ())
117     get_footer ()->align_to (Y_AXIS, UP);
118 }
119
120 void
121 Page::output (Paper_outputter *out, bool is_last)
122 {
123   progress_indication ("[" + to_string (number_));
124   out->output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
125   Offset o (left_margin_, top_margin_);
126   Real vfill = line_count_ > 1 ? (text_height () - height_) / (line_count_ - 1)
127     : 0;
128
129   Real coverage = height_ / text_height ();
130   if (coverage < MIN_COVERAGE)
131     /* Do not space out a badly filled page.  This is too simplistic
132        (ie broken), because this should not vary too much between
133        (subsequent?) pages in a book.  */
134     vfill = 0;
135
136   if (get_header ())
137     {
138       out->output_line (stencil2line (get_header ()), &o, false);
139       o[Y_AXIS] += head_sep_;
140     }
141   for (SCM s = lines_; gh_pair_p (s); s = ly_cdr (s))
142     {
143       SCM line = ly_car (s);
144       SCM offset = ly_car (line);
145       if (ly_car (offset) == ly_symbol2scm ("title"))
146         line = scm_cons (ly_cdr (offset), ly_cdr (line));
147       out->output_line (line, &o,
148                         is_last && gh_pair_p (ly_cdr (s)) && !get_copyright ()
149                         && !get_tagline () && !get_footer ());
150       if (gh_pair_p (ly_cdr (s)) && ly_car (offset) != ly_symbol2scm ("title"))
151         o[Y_AXIS] += vfill; 
152     }
153
154 #if 0
155   if (get_copyright () || get_tagline () || get_footer ())
156     o[Y_AXIS] += foot_sep_;
157 #else
158   o[Y_AXIS] = vsize_ - bottom_margin_;
159   if (get_copyright ())
160     o[Y_AXIS] -= get_copyright ()->extent (Y_AXIS).length ();
161   if (get_tagline ())
162     o[Y_AXIS] -= get_tagline ()->extent (Y_AXIS).length ();
163   if (get_footer ())
164     o[Y_AXIS] -= get_footer ()->extent (Y_AXIS).length ();
165 #endif
166
167   if (get_copyright ())
168     out->output_line (stencil2line (get_copyright ()), &o,
169                       is_last && !get_tagline () && !get_footer ());
170   if (get_tagline ())
171     out->output_line (stencil2line (get_tagline ()), &o,
172                       is_last && !get_footer ());
173   if (get_footer ())
174     out->output_line (stencil2line (get_footer ()), &o, is_last);
175   out->output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
176                                   gh_bool2scm (is_last && !get_footer ())));
177   progress_indication ("]");
178 }
179
180 Real
181 Page::text_height ()
182 {
183   Real h = vsize_ - top_margin_ - bottom_margin_;
184   if (get_header ())
185     h -= get_header ()->extent (Y_AXIS).length () + head_sep_;
186   if (get_copyright () || get_tagline () || get_footer ())
187     h -= foot_sep_;
188   if (get_copyright ())
189     h -= get_copyright ()->extent (Y_AXIS).length ();
190   if (get_tagline ())
191     h -= get_tagline ()->extent (Y_AXIS).length ();
192   if (get_footer ())
193     h -= get_footer ()->extent (Y_AXIS).length ();
194   return h;
195 }
196
197 /****************************************************************/
198
199 /* Current global paper book.  Gives default_rendering access from
200    input-file-results.  */
201 Paper_book *paper_book;
202
203 Paper_book::Paper_book ()
204 {
205   smobify_self ();
206 }
207
208 Paper_book::~Paper_book ()
209 {
210 }
211
212 void
213 Paper_book::output (String outname)
214 {
215   /* Generate all stencils to trigger font loads.  */
216   Link_array<Page> *pages = get_pages ();
217
218   Paper_def *paper = papers_[0];
219   Paper_outputter *out = paper->get_paper_outputter (outname);
220   out->output_metadata (get_scopes (0), paper);
221   out->output_header (paper);
222
223   int page_count = pages->size ();
224   for (int i = 0; i < page_count; i++)
225     (*pages)[i]->output (out, i + 1 == page_count);
226
227   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
228   progress_indication ("\n");
229 }
230
231 SCM
232 Paper_book::get_scopes (int i)
233 {
234   SCM scopes = SCM_EOL;
235   if (headers_[i])
236     scopes = scm_cons (headers_[i], scopes);
237   if (global_headers_[i] && global_headers_[i] != headers_[i])
238     scopes = scm_cons (global_headers_[i], scopes);
239   return scopes;
240 }
241
242 Stencil*
243 Paper_book::get_title (int i)
244 {
245   SCM user_title = scm_primitive_eval (ly_symbol2scm ("user-title"));
246   SCM book_title = scm_primitive_eval (ly_symbol2scm ("book-title"));
247   SCM score_title = scm_primitive_eval (ly_symbol2scm ("score-title"));
248   SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
249                : ly_symbol2scm ("scoreTitle"));
250
251   Stencil *title = 0;
252   SCM scopes = get_scopes (i);
253   SCM s = ly_modules_lookup (scopes, field);
254   if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
255     title = unsmob_stencil (scm_call_2 (user_title,
256                                         papers_[0]->self_scm (),
257                                         scm_variable_ref (s)));
258   else
259     title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
260                                         papers_[0]->self_scm (),
261                                         scopes));
262   if (title)
263     title->align_to (Y_AXIS, UP);
264   
265   return title;
266 }
267
268 /* calculate book height, #lines, stencils.  */
269 void
270 Paper_book::init ()
271 {
272   int score_count = scores_.size ();
273
274   /* Calculate the full book height.  Hmm, can't we cache system
275      heights while making stencils?  */
276   height_ = 0;
277   for (int i = 0; i < score_count; i++)
278     {
279       Stencil *title = get_title (i);
280       if (title)
281         height_ += title->extent (Y_AXIS).length ();
282
283       int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
284       for (int j = 0; j < line_count; j++)
285         {
286           SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
287           height_ += ly_scm2offset (ly_car (line))[Y_AXIS];
288         }
289     }
290
291   Paper_def *paper = papers_[0];
292   SCM scopes = get_scopes (0);
293
294   SCM make_tagline = scm_primitive_eval (ly_symbol2scm ("make-tagline"));
295   tagline_ = scm_call_2 (make_tagline, paper->smobbed_copy (), scopes);
296   Real tag_height = 0;
297   if (Stencil *s = unsmob_stencil (tagline_))
298     tag_height = s->extent (Y_AXIS).length ();
299   height_ += tag_height;
300
301   SCM make_copyright = scm_primitive_eval (ly_symbol2scm ("make-copyright"));
302   copyright_ = scm_call_2 (make_copyright, paper->smobbed_copy (), scopes);
303   Real copy_height = 0;
304   if (Stencil *s = unsmob_stencil (copyright_))
305     copy_height = s->extent (Y_AXIS).length ();
306   height_ += copy_height;
307 }
308
309 /* Ideas:
310    - real page breaking algorithm (Gourlay?)
311      Hmmughr, Gourlay uses Grobs, columns etc -- looks like it needs serious
312      refactoring before it can be applied to Page breaking...
313    - override: # pages, or pageBreakLines= #'(3 3 4), ?  */
314 Link_array<Page>*
315 Paper_book::get_pages ()
316 {
317   init ();
318   Page::page_count_ = 0;
319   Paper_def *paper = papers_[0];
320   Page *page = new Page (paper, 1);
321
322   Real text_height = page->text_height ();
323   Real page_frac = height_ / text_height;
324   int page_count = (int) ceil (page_frac);
325   if (unsmob_stencil (copyright_))
326     page->copyright_ = copyright_;
327   if (unsmob_stencil (tagline_) && page_count == 1)
328     page->tagline_ = tagline_;
329
330   /* Attempt to fill pages better using FUDGE kludge.  */
331   Real r = page_frac - (int) page_frac;
332   Real cramp_fudge = page_count > 1 ? (r / (page_count - 1)) * text_height : 0;
333   Real expand_fudge = - ((1 - r) / page_count) * text_height;
334   
335   Link_array<Page>* pages = 0;
336   if ((page_count > 1 && (r / (page_count - 1)) < MAX_CRAMP))
337     {
338       /* Just a little more space (<MAX_CRAMP) per page is needed,
339          cramp onto one page less.  */
340       pages = fill_pages (page, page_count - 1, cramp_fudge);
341       if (pages->size () != page_count - 1)
342         {
343           /* Cramping failed.  */
344           page = pages->get (0);
345           delete pages;
346           pages = 0;
347         }
348     }      
349   if (!pages && ((1 - r) / page_count) < 1 - MIN_COVERAGE)
350     {
351       /* There is space left, but not so much that paged would have too
352          little blackness (< MIN_COVERAGE), distribute evenly.  */
353       pages = fill_pages (page, page_count, expand_fudge);
354       bool badly_covered = false;
355       if (pages->size () == page_count)
356         for (int i = 0; i < page_count; i++)
357           {
358             Page *p = (*pages)[i];
359             Real coverage = p ->height_ / p->text_height ();
360             if (coverage < MIN_COVERAGE)
361               {
362                 badly_covered = true;
363                 break;
364               }
365           }
366       if (pages->size () != page_count || badly_covered)
367         {
368           /* expanding failed.  */
369           page = pages->get (0);
370           delete pages;
371           pages = 0;
372         }
373     }
374   
375   if (!pages)
376     /* Fudging failed; just fill pages.  */
377     pages = fill_pages (page, page_count, 0);
378   return pages;
379 }
380
381 /* Simplistic page breaking:
382    add lines until HEIGHT > PAGE.TEXT_HEIGHT_ + FUDGE  */
383 Link_array<Page>*
384 Paper_book::fill_pages (Page *page, int page_count, Real fudge)
385 {
386   int page_number = 1;
387   int score_count = scores_.size ();
388   Paper_def *paper = papers_[0];
389   Link_array<Page> *pages = new Link_array<Page>;
390   page->lines_ = SCM_EOL;
391   page->height_ = 0;
392   page->line_count_ = 0;
393   Real text_height = page->text_height ();
394   for (int i = 0; i < score_count; i++)
395     {
396       Real h = 0;
397       Stencil *title = get_title (i);
398       if (title)
399         h = title->extent (Y_AXIS).length ();
400
401       int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
402       for (int j = 0; j < line_count; j++)
403         {
404           SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
405           h += ly_scm2offset (ly_car (line))[Y_AXIS];
406           Real fill = (page->height_ - text_height) / text_height;
407           // Real fill_h = (page->height_ + h - text_height) / text_height;
408           Real fudged_fill = (page->height_ - (text_height + fudge))
409             / (text_height + fudge);
410           Real fudged_fill_h = ((page->height_ + h) - (text_height + fudge))
411             / (text_height + fudge);
412           if (fill > -MAX_CRAMP
413               || (fudged_fill > -MAX_CRAMP
414                   && (fudge < 0
415                       || !(fudged_fill_h > 0
416                            && abs (fudged_fill_h) < 4 * abs (fudged_fill)))))
417             {
418               pages->push (page);
419               page = new Page (paper, ++page_number);
420               if (unsmob_stencil (tagline_) && page_number == page_count)
421                 page->tagline_ = tagline_;
422               text_height = page->text_height ();
423             }
424           if (j == 0 && title)
425             page->lines_ = ly_snoc (title2line (title), page->lines_);
426           page->lines_ = ly_snoc (line, page->lines_);
427           page->line_count_++;
428           page->height_ += h;
429           h = 0;
430         }
431     }
432
433   pages->push (page);
434   return pages;
435 }
436
437 void
438 Paper_book::classic_output (String outname)
439 {
440   Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
441   int count = scores_.size ();
442   
443   out->output_metadata (get_scopes (count - 1), papers_.top ());
444   out->output_header (papers_.top ());
445
446   int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
447   for (int i = 0; i < line_count; i++)
448     out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
449                       0, i == line_count - 1);
450   
451   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
452   progress_indication ("\n");
453 }
454
455
456 #include "ly-smobs.icc"
457
458 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
459 IMPLEMENT_SMOBS (Paper_book)
460 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
461
462 SCM
463 Paper_book::mark_smob (SCM smob)
464 {
465   Paper_book *pb = (Paper_book*) SCM_CELL_WORD_1 (smob);
466   for (int i = 0; i < pb->headers_.size (); i++)
467     scm_gc_mark (pb->headers_[i]);
468   for (int i = 0; i < pb->global_headers_.size (); i++)
469     scm_gc_mark (pb->global_headers_[i]);
470   for (int i = 0; i < pb->papers_.size (); i++)
471     scm_gc_mark (pb->papers_[i]->self_scm ());
472   for (int i = 0; i < pb->scores_.size (); i++)
473     scm_gc_mark (pb->scores_[i]);
474   return SCM_EOL;
475 }
476
477 int
478 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
479 {
480   Paper_book *b = (Paper_book*) ly_cdr (smob);
481      
482   scm_puts ("#<", port);
483   scm_puts (classname (b), port);
484   scm_puts (" ", port);
485   //scm_puts (b->, port);
486   scm_puts (">", port);
487   return 1;
488 }