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 ());
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_[i] && global_headers_[i] != headers_[i])
277 scopes = scm_cons (global_headers_[i], scopes);
282 Paper_book::title (int i)
284 SCM user_title = ly_scheme_function ("user-title");
285 SCM book_title = ly_scheme_function ("book-title");
286 SCM score_title = ly_scheme_function ("score-title");
287 SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
288 : ly_symbol2scm ("scoreTitle"));
291 SCM scopes = this->scopes (i);
292 SCM s = ly_modules_lookup (scopes, field);
293 if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
294 title = unsmob_stencil (scm_call_2 (user_title,
295 papers_[0]->self_scm (),
296 scm_variable_ref (s)));
298 title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
299 papers_[0]->self_scm (),
302 title->align_to (Y_AXIS, UP);
308 Paper_book::classic_output (String outname)
310 int count = scores_.size ();
311 Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
312 out->output_header (papers_.top (), scopes (count - 1), 0);
314 Paper_line *first = unsmob_paper_line (scm_vector_ref (scores_.top (),
316 Offset o (0, -0.5 * first->dim ()[Y_AXIS]);
317 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
318 for (int i = 0; i < line_count; i++)
319 out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
320 &o, i == line_count - 1);
322 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
323 progress_indication ("\n");
327 /* calculate book height, #lines, stencils. */
331 int score_count = scores_.size ();
333 /* Calculate the full book height. Hmm, can't we cache system
334 heights while making stencils? */
336 for (int i = 0; i < score_count; i++)
338 Stencil *title = this->title (i);
340 height_ += title->extent (Y_AXIS).length ();
342 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
343 for (int j = 0; j < line_count; j++)
345 SCM s = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
346 height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
350 Paper_def *paper = papers_[0];
351 SCM scopes = this->scopes (0);
353 SCM make_tagline = ly_scheme_function ("make-tagline");
354 tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
356 if (Stencil *s = unsmob_stencil (tagline_))
357 tag_height = s->extent (Y_AXIS).length ();
358 height_ += tag_height;
360 SCM make_copyright = ly_scheme_function ("make-copyright");
361 copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
362 Real copy_height = 0;
363 if (Stencil *s = unsmob_stencil (copyright_))
364 copy_height = s->extent (Y_AXIS).length ();
365 height_ += copy_height;
371 int score_count = scores_.size ();
373 for (int i = 0; i < score_count; i++)
375 if (Stencil *title = this->title (i))
376 lines = ly_snoc (stencil2line (title, true), lines);
377 lines = scm_append (scm_list_2 (lines, scm_vector_to_list (scores_[i])));
381 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
382 unsmob_paper_line (ly_car (s))->number_ = ++i;
390 Page::page_count_ = 0;
391 Paper_def *paper = papers_[0];
392 Page *page = new Page (paper, 1);
394 Real text_height = page->text_height ();
396 Real copy_height = 0;
397 if (Stencil *s = unsmob_stencil (copyright_))
398 copy_height = s->extent (Y_AXIS).length ();
401 if (Stencil *s = unsmob_stencil (tagline_))
402 tag_height = s->extent (Y_AXIS).length ();
405 SCM proc = paper->get_scmvar ("page-breaking");
406 SCM breaks = scm_apply_0 (proc, scm_list_n (all, scm_make_real (height_),
407 scm_make_real (text_height),
408 scm_make_real (-copy_height),
409 scm_make_real (-tag_height),
412 /* Copyright on first page. */
413 if (unsmob_stencil (copyright_))
414 page->copyright_ = copyright_;
416 Link_array<Page> *pages = new Link_array<Page>;
417 int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
419 for (int i = 0; i < page_count; i++)
422 page = new Page (paper, i+1);
423 int next = i + 1 < page_count
424 ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
425 while ((!next && all != SCM_EOL) || line <= next)
427 SCM s = ly_car (all);
428 page->lines_ = ly_snoc (s, page->lines_);
429 page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
437 /* Tagline on last page. */
438 if (unsmob_stencil (tagline_))
439 page->tagline_ = tagline_;
445 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
446 Real first, Real last)
449 int page_count = int (book_height / text_height + 0.5);
450 SCM breaks = SCM_EOL;
451 Real page_height = text_height + first;
454 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
456 Paper_line *pl = unsmob_paper_line (ly_car (s));
457 if (!pl->is_title () && h < page_height)
459 h += pl->dim ()[Y_AXIS];
460 if (!pl->is_title () && h > page_height)
462 breaks = ly_snoc (scm_int2num (number), breaks);
464 page_height = text_height + (page_number == page_count) * last;
467 if (ly_cdr (s) == SCM_EOL)
468 breaks = ly_snoc (scm_int2num (pl->number_), breaks);
471 return scm_vector (breaks);
474 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
475 5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
476 "Return a vector with line numbers of page breaks.")
478 SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
479 SCM_ASSERT_TYPE (is_number (book), book, SCM_ARG2, __FUNCTION__, "real");
480 SCM_ASSERT_TYPE (is_number (text), text, SCM_ARG2, __FUNCTION__, "real");
481 SCM_ASSERT_TYPE (is_number (first), first, SCM_ARG2, __FUNCTION__, "real");
482 SCM_ASSERT_TYPE (is_number (last), last, SCM_ARG2, __FUNCTION__, "real");
484 return c_ragged_page_breaks (lines,
485 ly_scm2double (book), ly_scm2double (text),
486 ly_scm2double (first), ly_scm2double (last));