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