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