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