2 paper-book.cc -- implement Paper_book
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
12 #include "ly-module.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"
21 static Real const MIN_COVERAGE = 0.66;
24 stencil2line (Stencil* stil, bool is_title = false)
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 (),
33 -10001 * is_title, is_title);
35 return scm_gc_unprotect_object (pl->self_scm ());
38 /* Simplistic page interface */
43 static int page_count_;
48 Protected_scm header_;
49 Protected_scm footer_;
50 Protected_scm copyright_;
51 Protected_scm tagline_;
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_); }
58 /* actual height filled with text. */
61 // HMMM all this size stuff to paper/paper-outputter?
71 /* available area for text. */
74 Page (Paper_def*, int);
75 void output (Paper_outputter*, bool);
78 int Page::page_count_ = 0;
80 Page::Page (Paper_def *paper, int number)
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;
102 SCM make_header = ly_scheme_function ("make-header");
103 SCM make_footer = ly_scheme_function ("make-footer");
105 header_ = scm_call_2 (make_header, paper_->self_scm (),
106 scm_int2num (number_));
108 get_header ()->align_to (Y_AXIS, UP);
110 footer_ = scm_call_2 (make_footer, paper_->self_scm (),
111 scm_int2num (number_));
113 get_footer ()->align_to (Y_AXIS, UP);
117 Page::output (Paper_outputter *out, bool is_last)
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)
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. */
134 out->output_line (stencil2line (get_header ()), &o, false);
135 o[Y_AXIS] += head_sep_;
137 for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
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 ());
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))
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))
154 o[Y_AXIS] = vsize_ - bottom_margin_;
155 if (get_copyright ())
156 o[Y_AXIS] -= get_copyright ()->extent (Y_AXIS).length ();
158 o[Y_AXIS] -= get_tagline ()->extent (Y_AXIS).length ();
160 o[Y_AXIS] -= get_footer ()->extent (Y_AXIS).length ();
162 if (get_copyright ())
163 out->output_line (stencil2line (get_copyright ()), &o,
164 is_last && !get_tagline () && !get_footer ());
166 out->output_line (stencil2line (get_tagline ()), &o,
167 is_last && !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 ("]");
178 Real h = vsize_ - top_margin_ - bottom_margin_;
180 h -= get_header ()->extent (Y_AXIS).length () + head_sep_;
181 if (get_copyright () || get_tagline () || get_footer ())
183 if (get_copyright ())
184 h -= get_copyright ()->extent (Y_AXIS).length ();
186 h -= get_tagline ()->extent (Y_AXIS).length ();
188 h -= get_footer ()->extent (Y_AXIS).length ();
192 /****************************************************************/
194 Paper_book::Paper_book ()
196 copyright_ = SCM_EOL;
202 Paper_book::~Paper_book ()
206 #include "ly-smobs.icc"
208 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
209 IMPLEMENT_SMOBS (Paper_book)
210 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
213 Paper_book::mark_smob (SCM smob)
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]);
225 scm_gc_mark (pb->copyright_);
231 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
233 Paper_book *b = (Paper_book*) ly_cdr (smob);
235 scm_puts ("#<", port);
236 scm_puts (classname (b), port);
237 scm_puts (" ", port);
238 //scm_puts (b->, port);
239 scm_puts (">", port);
244 Paper_book::output (String outname)
246 if (!papers_.size ())
249 /* Generate all stencils to trigger font loads. */
250 Link_array<Page> *pages = this->pages ();
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);
256 int page_count = pages->size ();
257 for (int i = 0; i < page_count; i++)
258 (*pages)[i]->output (out, i + 1 == page_count);
260 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
263 for (int i = pages->size (); i--;)
264 delete pages->elem (i);
267 progress_indication ("\n");
271 Paper_book::scopes (int i)
273 SCM scopes = SCM_EOL;
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);
283 Paper_book::title (int i)
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"));
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)));
299 title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
300 papers_[0]->self_scm (),
303 title->align_to (Y_AXIS, UP);
309 Paper_book::classic_output (String outname)
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);
315 Paper_line *first = unsmob_paper_line (scm_vector_ref (scores_.top (),
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++)
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
326 if (output_format_global == "tex")
330 out->output_line (scm_vector_ref ((SCM) scores_.top (),
332 &o, i == line_count - 1);
335 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
336 progress_indication ("\n");
340 /* calculate book height, #lines, stencils. */
344 int score_count = scores_.size ();
346 /* Calculate the full book height. Hmm, can't we cache system
347 heights while making stencils? */
349 for (int i = 0; i < score_count; i++)
351 Stencil *title = this->title (i);
353 height_ += title->extent (Y_AXIS).length ();
355 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
356 for (int j = 0; j < line_count; j++)
358 SCM s = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
359 height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
363 Paper_def *paper = papers_[0];
364 SCM scopes = this->scopes (0);
366 SCM make_tagline = ly_scheme_function ("make-tagline");
367 tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
369 if (Stencil *s = unsmob_stencil (tagline_))
370 tag_height = s->extent (Y_AXIS).length ();
371 height_ += tag_height;
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;
384 int score_count = scores_.size ();
386 for (int i = 0; i < score_count; i++)
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])));
394 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
395 unsmob_paper_line (ly_car (s))->number_ = ++i;
403 Page::page_count_ = 0;
404 Paper_def *paper = papers_[0];
405 Page *page = new Page (paper, 1);
407 Real text_height = page->text_height ();
409 Real copy_height = 0;
410 if (Stencil *s = unsmob_stencil (copyright_))
411 copy_height = s->extent (Y_AXIS).length ();
414 if (Stencil *s = unsmob_stencil (tagline_))
415 tag_height = s->extent (Y_AXIS).length ();
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),
425 /* Copyright on first page. */
426 if (unsmob_stencil (copyright_))
427 page->copyright_ = copyright_;
429 Link_array<Page> *pages = new Link_array<Page>;
430 int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
432 for (int i = 0; i < page_count; 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)
440 SCM s = ly_car (all);
441 page->lines_ = ly_snoc (s, page->lines_);
442 page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
450 /* Tagline on last page. */
451 if (unsmob_stencil (tagline_))
452 page->tagline_ = tagline_;
458 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
459 Real first, Real last)
462 int page_count = int (book_height / text_height + 0.5);
463 SCM breaks = SCM_EOL;
464 Real page_height = text_height + first;
467 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
469 Paper_line *pl = unsmob_paper_line (ly_car (s));
470 if (!pl->is_title () && h < page_height)
472 h += pl->dim ()[Y_AXIS];
473 if (!pl->is_title () && h > page_height)
475 breaks = ly_snoc (scm_int2num (number), breaks);
477 page_height = text_height + (page_number == page_count) * last;
480 if (ly_cdr (s) == SCM_EOL)
481 breaks = ly_snoc (scm_int2num (pl->number_), breaks);
484 return scm_vector (breaks);
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.")
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");
497 return c_ragged_page_breaks (lines,
498 ly_scm2double (book), ly_scm2double (text),
499 ly_scm2double (first), ly_scm2double (last));