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 return pl->self_scm ();
36 /* Simplistic page interface */
41 static int page_count_;
46 Protected_scm header_;
47 Protected_scm footer_;
48 Protected_scm copyright_;
49 Protected_scm tagline_;
51 Stencil *get_header () { return unsmob_stencil (header_); }
52 Stencil *get_copyright () { return unsmob_stencil (copyright_); }
53 Stencil *get_tagline () { return unsmob_stencil (tagline_); }
54 Stencil *get_footer () { return unsmob_stencil (footer_); }
56 /* actual height filled with text. */
59 // HMMM all this size stuff to paper/paper-outputter?
69 /* available area for text. */
72 Page (Paper_def*, int);
73 void output (Paper_outputter*, bool);
76 int Page::page_count_ = 0;
78 Page::Page (Paper_def *paper, int number)
88 hsize_ = paper->get_realvar (ly_symbol2scm ("hsize"));
89 vsize_ = paper->get_realvar (ly_symbol2scm ("vsize"));
90 top_margin_ = paper->get_realvar (ly_symbol2scm ("top-margin"));
91 bottom_margin_ = paper->get_realvar (ly_symbol2scm ("bottom-margin"));
92 head_sep_ = paper->get_realvar (ly_symbol2scm ("head-sep"));
93 foot_sep_ = paper->get_realvar (ly_symbol2scm ("foot-sep"));
94 text_width_ = paper->get_realvar (ly_symbol2scm ("linewidth"));
95 left_margin_ = (hsize_ - text_width_) / 2;
100 SCM make_header = scm_primitive_eval (ly_symbol2scm ("make-header"));
101 SCM make_footer = scm_primitive_eval (ly_symbol2scm ("make-footer"));
103 header_ = scm_call_2 (make_header, paper_->self_scm (),
104 scm_int2num (number_));
105 // FIXME: why does this (generates Stencil) not trigger font load?
107 get_header ()->align_to (Y_AXIS, UP);
109 footer_ = scm_call_2 (make_footer, paper_->self_scm (),
110 scm_int2num (number_));
112 get_footer ()->align_to (Y_AXIS, UP);
116 Page::output (Paper_outputter *out, bool is_last)
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)
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. */
133 out->output_line (stencil2line (get_header ()), &o, false);
134 o[Y_AXIS] += head_sep_;
136 for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
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 ());
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))
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))
153 o[Y_AXIS] = vsize_ - bottom_margin_;
154 if (get_copyright ())
155 o[Y_AXIS] -= get_copyright ()->extent (Y_AXIS).length ();
157 o[Y_AXIS] -= get_tagline ()->extent (Y_AXIS).length ();
159 o[Y_AXIS] -= get_footer ()->extent (Y_AXIS).length ();
161 if (get_copyright ())
162 out->output_line (stencil2line (get_copyright ()), &o,
163 is_last && !get_tagline () && !get_footer ());
165 out->output_line (stencil2line (get_tagline ()), &o,
166 is_last && !get_footer ());
168 out->output_line (stencil2line (get_footer ()), &o, is_last);
169 out->output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
170 gh_bool2scm (is_last && !get_footer ())));
171 progress_indication ("]");
177 Real h = vsize_ - top_margin_ - bottom_margin_;
179 h -= get_header ()->extent (Y_AXIS).length () + head_sep_;
180 if (get_copyright () || get_tagline () || get_footer ())
182 if (get_copyright ())
183 h -= get_copyright ()->extent (Y_AXIS).length ();
185 h -= get_tagline ()->extent (Y_AXIS).length ();
187 h -= get_footer ()->extent (Y_AXIS).length ();
191 /****************************************************************/
193 /* Current global paper book. Gives default_rendering access from
194 input-file-results. */
195 Paper_book *paper_book;
197 Paper_book::Paper_book ()
199 copyright_ = SCM_EOL;
205 Paper_book::~Paper_book ()
209 #include "ly-smobs.icc"
211 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
212 IMPLEMENT_SMOBS (Paper_book)
213 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
216 Paper_book::mark_smob (SCM smob)
218 Paper_book *pb = (Paper_book*) SCM_CELL_WORD_1 (smob);
219 for (int i = 0; i < pb->headers_.size (); i++)
220 scm_gc_mark (pb->headers_[i]);
221 for (int i = 0; i < pb->global_headers_.size (); i++)
222 scm_gc_mark (pb->global_headers_[i]);
223 for (int i = 0; i < pb->papers_.size (); i++)
224 scm_gc_mark (pb->papers_[i]->self_scm ());
225 for (int i = 0; i < pb->scores_.size (); i++)
226 scm_gc_mark (pb->scores_[i]);
228 scm_gc_mark (pb->copyright_);
234 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
236 Paper_book *b = (Paper_book*) ly_cdr (smob);
238 scm_puts ("#<", port);
239 scm_puts (classname (b), port);
240 scm_puts (" ", port);
241 //scm_puts (b->, port);
242 scm_puts (">", port);
247 Paper_book::output (String outname)
249 if (!papers_.size ())
252 /* Generate all stencils to trigger font loads. */
253 Link_array<Page> *pages = this->pages ();
255 Paper_def *paper = papers_[0];
256 Paper_outputter *out = paper->get_paper_outputter (outname);
257 out->output_header (paper, scopes (0), pages->size ());
259 int page_count = pages->size ();
260 for (int i = 0; i < page_count; i++)
261 (*pages)[i]->output (out, i + 1 == page_count);
263 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
264 progress_indication ("\n");
268 Paper_book::scopes (int i)
270 SCM scopes = SCM_EOL;
272 scopes = scm_cons (headers_[i], scopes);
273 if (global_headers_[i] && global_headers_[i] != headers_[i])
274 scopes = scm_cons (global_headers_[i], scopes);
279 Paper_book::title (int i)
281 SCM user_title = scm_primitive_eval (ly_symbol2scm ("user-title"));
282 SCM book_title = scm_primitive_eval (ly_symbol2scm ("book-title"));
283 SCM score_title = scm_primitive_eval (ly_symbol2scm ("score-title"));
284 SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
285 : ly_symbol2scm ("scoreTitle"));
288 SCM scopes = this->scopes (i);
289 SCM s = ly_modules_lookup (scopes, field);
290 if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
291 title = unsmob_stencil (scm_call_2 (user_title,
292 papers_[0]->self_scm (),
293 scm_variable_ref (s)));
295 title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
296 papers_[0]->self_scm (),
299 title->align_to (Y_AXIS, UP);
305 Paper_book::classic_output (String outname)
307 int count = scores_.size ();
308 Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
309 out->output_header (papers_.top (), scopes (count - 1), 0);
311 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
312 for (int i = 0; i < line_count; i++)
313 out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
314 0, i == line_count - 1);
316 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
317 progress_indication ("\n");
321 /* calculate book height, #lines, stencils. */
325 int score_count = scores_.size ();
327 /* Calculate the full book height. Hmm, can't we cache system
328 heights while making stencils? */
330 for (int i = 0; i < score_count; i++)
332 Stencil *title = this->title (i);
334 height_ += title->extent (Y_AXIS).length ();
336 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
337 for (int j = 0; j < line_count; j++)
339 SCM s = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
340 height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
344 Paper_def *paper = papers_[0];
345 SCM scopes = this->scopes (0);
347 SCM make_tagline = scm_primitive_eval (ly_symbol2scm ("make-tagline"));
348 tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
350 if (Stencil *s = unsmob_stencil (tagline_))
351 tag_height = s->extent (Y_AXIS).length ();
352 height_ += tag_height;
354 SCM make_copyright = scm_primitive_eval (ly_symbol2scm ("make-copyright"));
355 copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
356 Real copy_height = 0;
357 if (Stencil *s = unsmob_stencil (copyright_))
358 copy_height = s->extent (Y_AXIS).length ();
359 height_ += copy_height;
365 int score_count = scores_.size ();
367 for (int i = 0; i < score_count; i++)
369 if (Stencil *title = this->title (i))
370 lines = ly_snoc (stencil2line (title, true), lines);
371 lines = scm_append (scm_list_2 (lines, scm_vector_to_list (scores_[i])));
375 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
376 unsmob_paper_line (ly_car (s))->number_ = ++i;
384 Page::page_count_ = 0;
385 Paper_def *paper = papers_[0];
386 Page *page = new Page (paper, 1);
388 Real text_height = page->text_height ();
390 Real copy_height = 0;
391 if (Stencil *s = unsmob_stencil (copyright_))
392 copy_height = s->extent (Y_AXIS).length ();
395 if (Stencil *s = unsmob_stencil (tagline_))
396 tag_height = s->extent (Y_AXIS).length ();
399 SCM proc = paper->get_scmvar ("page-breaking");
400 SCM breaks = scm_apply_0 (proc, scm_list_n (all,
401 gh_double2scm (height_),
402 gh_double2scm (text_height),
403 gh_double2scm (-copy_height),
404 gh_double2scm (-tag_height),
407 /* Copyright on first page. */
408 if (unsmob_stencil (copyright_))
409 page->copyright_ = copyright_;
411 Link_array<Page> *pages = new Link_array<Page>;
412 int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
414 for (int i = 0; i < page_count; i++)
417 page = new Page (paper, i+1);
418 int next = i + 1 < page_count
419 ? gh_scm2int (scm_vector_ref (breaks, gh_int2scm (i))) : 0;
420 while ((!next && all != SCM_EOL) || line <= next)
422 SCM s = ly_car (all);
423 page->lines_ = ly_snoc (s, page->lines_);
424 page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
432 /* Tagline on last page. */
433 if (unsmob_stencil (tagline_))
434 page->tagline_ = tagline_;
439 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
440 Real first, Real last)
443 int page_count = int (book_height / text_height + 0.5);
444 SCM breaks = SCM_EOL;
445 Real page_height = text_height + first;
448 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
450 Paper_line *pl = unsmob_paper_line (ly_car (s));
451 if (!pl->is_title () && h < page_height)
453 h += pl->dim ()[Y_AXIS];
454 if (!pl->is_title () && h > page_height)
456 breaks = ly_snoc (gh_int2scm (number), breaks);
458 page_height = text_height + (page_number == page_count) * last;
461 if (ly_cdr (s) == SCM_EOL)
462 breaks = ly_snoc (gh_int2scm (pl->number_), breaks);
465 return scm_vector (breaks);
468 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
469 5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
470 "Return a vector with line numbers of page breaks.")
472 SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
473 SCM_ASSERT_TYPE (gh_number_p (book), book, SCM_ARG2, __FUNCTION__, "real");
474 SCM_ASSERT_TYPE (gh_number_p (text), text, SCM_ARG2, __FUNCTION__, "real");
475 SCM_ASSERT_TYPE (gh_number_p (first), first, SCM_ARG2, __FUNCTION__, "real");
476 SCM_ASSERT_TYPE (gh_number_p (last), last, SCM_ARG2, __FUNCTION__, "real");
478 return c_ragged_page_breaks (lines,
479 gh_scm2double (book), gh_scm2double (text),
480 gh_scm2double (first), gh_scm2double (last));