]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* scm/lily.scm (get-output-module): New function.
[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_header (paper, get_scopes (0), pages->size ());
221
222   int page_count = pages->size ();
223   for (int i = 0; i < page_count; i++)
224     (*pages)[i]->output (out, i + 1 == page_count);
225
226   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
227   progress_indication ("\n");
228 }
229
230 SCM
231 Paper_book::get_scopes (int i)
232 {
233   SCM scopes = SCM_EOL;
234   if (headers_[i])
235     scopes = scm_cons (headers_[i], scopes);
236   if (global_headers_[i] && global_headers_[i] != headers_[i])
237     scopes = scm_cons (global_headers_[i], scopes);
238   return scopes;
239 }
240
241 Stencil*
242 Paper_book::get_title (int i)
243 {
244   SCM user_title = scm_primitive_eval (ly_symbol2scm ("user-title"));
245   SCM book_title = scm_primitive_eval (ly_symbol2scm ("book-title"));
246   SCM score_title = scm_primitive_eval (ly_symbol2scm ("score-title"));
247   SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
248                : ly_symbol2scm ("scoreTitle"));
249
250   Stencil *title = 0;
251   SCM scopes = get_scopes (i);
252   SCM s = ly_modules_lookup (scopes, field);
253   if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
254     title = unsmob_stencil (scm_call_2 (user_title,
255                                         papers_[0]->self_scm (),
256                                         scm_variable_ref (s)));
257   else
258     title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
259                                         papers_[0]->self_scm (),
260                                         scopes));
261   if (title)
262     title->align_to (Y_AXIS, UP);
263   
264   return title;
265 }
266
267 /* calculate book height, #lines, stencils.  */
268 void
269 Paper_book::init ()
270 {
271   int score_count = scores_.size ();
272
273   /* Calculate the full book height.  Hmm, can't we cache system
274      heights while making stencils?  */
275   height_ = 0;
276   for (int i = 0; i < score_count; i++)
277     {
278       Stencil *title = get_title (i);
279       if (title)
280         height_ += title->extent (Y_AXIS).length ();
281
282       int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
283       for (int j = 0; j < line_count; j++)
284         {
285           SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
286           height_ += ly_scm2offset (ly_car (line))[Y_AXIS];
287         }
288     }
289
290   Paper_def *paper = papers_[0];
291   SCM scopes = get_scopes (0);
292
293   SCM make_tagline = scm_primitive_eval (ly_symbol2scm ("make-tagline"));
294   tagline_ = scm_call_2 (make_tagline, paper->smobbed_copy (), scopes);
295   Real tag_height = 0;
296   if (Stencil *s = unsmob_stencil (tagline_))
297     tag_height = s->extent (Y_AXIS).length ();
298   height_ += tag_height;
299
300   SCM make_copyright = scm_primitive_eval (ly_symbol2scm ("make-copyright"));
301   copyright_ = scm_call_2 (make_copyright, paper->smobbed_copy (), scopes);
302   Real copy_height = 0;
303   if (Stencil *s = unsmob_stencil (copyright_))
304     copy_height = s->extent (Y_AXIS).length ();
305   height_ += copy_height;
306 }
307
308 /* Ideas:
309    - real page breaking algorithm (Gourlay?)
310      Hmmughr, Gourlay uses Grobs, columns etc -- looks like it needs serious
311      refactoring before it can be applied to Page breaking...
312    - override: # pages, or pageBreakLines= #'(3 3 4), ?  */
313 Link_array<Page>*
314 Paper_book::get_pages ()
315 {
316   init ();
317   Page::page_count_ = 0;
318   Paper_def *paper = papers_[0];
319   Page *page = new Page (paper, 1);
320
321   Real text_height = page->text_height ();
322   Real page_frac = height_ / text_height;
323   int page_count = (int) ceil (page_frac);
324   if (unsmob_stencil (copyright_))
325     page->copyright_ = copyright_;
326   if (unsmob_stencil (tagline_) && page_count == 1)
327     page->tagline_ = tagline_;
328
329   /* Attempt to fill pages better using FUDGE kludge.  */
330   Real r = page_frac - (int) page_frac;
331   Real cramp_fudge = page_count > 1 ? (r / (page_count - 1)) * text_height : 0;
332   Real expand_fudge = - ((1 - r) / page_count) * text_height;
333   
334   Link_array<Page>* pages = 0;
335   if ((page_count > 1 && (r / (page_count - 1)) < MAX_CRAMP))
336     {
337       /* Just a little more space (<MAX_CRAMP) per page is needed,
338          cramp onto one page less.  */
339       pages = fill_pages (page, page_count - 1, cramp_fudge);
340       if (pages->size () != page_count - 1)
341         {
342           /* Cramping failed.  */
343           page = pages->get (0);
344           delete pages;
345           pages = 0;
346         }
347     }      
348   if (!pages && ((1 - r) / page_count) < 1 - MIN_COVERAGE)
349     {
350       /* There is space left, but not so much that paged would have too
351          little blackness (< MIN_COVERAGE), distribute evenly.  */
352       pages = fill_pages (page, page_count, expand_fudge);
353       bool badly_covered = false;
354       if (pages->size () == page_count)
355         for (int i = 0; i < page_count; i++)
356           {
357             Page *p = (*pages)[i];
358             Real coverage = p ->height_ / p->text_height ();
359             if (coverage < MIN_COVERAGE)
360               {
361                 badly_covered = true;
362                 break;
363               }
364           }
365       if (pages->size () != page_count || badly_covered)
366         {
367           /* expanding failed.  */
368           page = pages->get (0);
369           delete pages;
370           pages = 0;
371         }
372     }
373   
374   if (!pages)
375     /* Fudging failed; just fill pages.  */
376     pages = fill_pages (page, page_count, 0);
377   return pages;
378 }
379
380 /* Simplistic page breaking:
381    add lines until HEIGHT > PAGE.TEXT_HEIGHT_ + FUDGE  */
382 Link_array<Page>*
383 Paper_book::fill_pages (Page *page, int page_count, Real fudge)
384 {
385   int page_number = 1;
386   int score_count = scores_.size ();
387   Paper_def *paper = papers_[0];
388   Link_array<Page> *pages = new Link_array<Page>;
389   page->lines_ = SCM_EOL;
390   page->height_ = 0;
391   page->line_count_ = 0;
392   Real text_height = page->text_height ();
393   for (int i = 0; i < score_count; i++)
394     {
395       Real h = 0;
396       Stencil *title = get_title (i);
397       if (title)
398         h = title->extent (Y_AXIS).length ();
399
400       int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
401       for (int j = 0; j < line_count; j++)
402         {
403           SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
404           h += ly_scm2offset (ly_car (line))[Y_AXIS];
405           Real fill = (page->height_ - text_height) / text_height;
406           // Real fill_h = (page->height_ + h - text_height) / text_height;
407           Real fudged_fill = (page->height_ - (text_height + fudge))
408             / (text_height + fudge);
409           Real fudged_fill_h = ((page->height_ + h) - (text_height + fudge))
410             / (text_height + fudge);
411           if (fill > -MAX_CRAMP
412               || (fudged_fill > -MAX_CRAMP
413                   && (fudge < 0
414                       || !(fudged_fill_h > 0
415                            && abs (fudged_fill_h) < 4 * abs (fudged_fill)))))
416             {
417               pages->push (page);
418               page = new Page (paper, ++page_number);
419               if (unsmob_stencil (tagline_) && page_number == page_count)
420                 page->tagline_ = tagline_;
421               text_height = page->text_height ();
422             }
423           if (j == 0 && title)
424             page->lines_ = ly_snoc (title2line (title), page->lines_);
425           page->lines_ = ly_snoc (line, page->lines_);
426           page->line_count_++;
427           page->height_ += h;
428           h = 0;
429         }
430     }
431
432   pages->push (page);
433   return pages;
434 }
435
436 void
437 Paper_book::classic_output (String outname)
438 {
439   int count = scores_.size ();
440   Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
441   out->output_header (papers_.top (), get_scopes (count - 1), 0);
442
443   int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
444   for (int i = 0; i < line_count; i++)
445     out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
446                       0, i == line_count - 1);
447   
448   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
449   progress_indication ("\n");
450 }
451
452
453 #include "ly-smobs.icc"
454
455 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
456 IMPLEMENT_SMOBS (Paper_book)
457 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
458
459 SCM
460 Paper_book::mark_smob (SCM smob)
461 {
462   Paper_book *pb = (Paper_book*) SCM_CELL_WORD_1 (smob);
463   for (int i = 0; i < pb->headers_.size (); i++)
464     scm_gc_mark (pb->headers_[i]);
465   for (int i = 0; i < pb->global_headers_.size (); i++)
466     scm_gc_mark (pb->global_headers_[i]);
467   for (int i = 0; i < pb->papers_.size (); i++)
468     scm_gc_mark (pb->papers_[i]->self_scm ());
469   for (int i = 0; i < pb->scores_.size (); i++)
470     scm_gc_mark (pb->scores_[i]);
471   return SCM_EOL;
472 }
473
474 int
475 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
476 {
477   Paper_book *b = (Paper_book*) ly_cdr (smob);
478      
479   scm_puts ("#<", port);
480   scm_puts (classname (b), port);
481   scm_puts (" ", port);
482   //scm_puts (b->, port);
483   scm_puts (">", port);
484   return 1;
485 }