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-score.hh"
20 static Real const MIN_COVERAGE = 0.66;
21 static Real const MAX_CRAMP = 0.05;
24 stencil2line (Stencil* stil)
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 return scm_cons (ly_offset2scm (dim),
32 scm_list_1 (scm_cons (z, stil->smobbed_copy ())));
36 title2line (Stencil* stil)
38 SCM s = stencil2line (stil);
39 /* whugh, add marker recognise titles. */
40 return scm_cons (scm_cons (ly_symbol2scm ("title"), ly_car (s)), ly_cdr (s));
43 /* Simplistic page interface */
48 static int page_count_;
53 Protected_scm header_;
54 Protected_scm footer_;
55 Protected_scm copyright_;
56 Protected_scm tagline_;
58 Stencil *get_header () { return unsmob_stencil (header_); }
59 Stencil *get_copyright () { return unsmob_stencil (copyright_); }
60 Stencil *get_tagline () { return unsmob_stencil (tagline_); }
61 Stencil *get_footer () { return unsmob_stencil (footer_); }
63 /* actual height filled with text. */
66 // HMMM all this size stuff to paper/paper-outputter?
76 /* available area for text. */
79 Page (Paper_def*, int);
80 void output (Paper_outputter*, bool);
83 int Page::page_count_ = 0;
85 Page::Page (Paper_def *paper, int number)
95 hsize_ = paper->get_realvar (ly_symbol2scm ("hsize"));
96 vsize_ = paper->get_realvar (ly_symbol2scm ("vsize"));
97 top_margin_ = paper->get_realvar (ly_symbol2scm ("top-margin"));
98 bottom_margin_ = paper->get_realvar (ly_symbol2scm ("bottom-margin"));
99 head_sep_ = paper->get_realvar (ly_symbol2scm ("head-sep"));
100 foot_sep_ = paper->get_realvar (ly_symbol2scm ("foot-sep"));
101 text_width_ = paper->get_realvar (ly_symbol2scm ("linewidth"));
102 left_margin_ = (hsize_ - text_width_) / 2;
104 copyright_ = SCM_EOL;
107 SCM make_header = scm_primitive_eval (ly_symbol2scm ("make-header"));
108 SCM make_footer = scm_primitive_eval (ly_symbol2scm ("make-footer"));
110 header_ = scm_call_2 (make_header, paper_->smobbed_copy (),
111 scm_int2num (number_));
112 // FIXME: why does this (generates Stencil) not trigger font load?
114 get_header ()->align_to (Y_AXIS, UP);
116 footer_ = scm_call_2 (make_footer, paper_->smobbed_copy (),
117 scm_int2num (number_));
119 get_footer ()->align_to (Y_AXIS, UP);
123 Page::output (Paper_outputter *out, bool is_last)
125 progress_indication ("[" + to_string (number_));
126 out->output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
127 Offset o (left_margin_, top_margin_);
128 Real vfill = line_count_ > 1 ? (text_height () - height_) / (line_count_ - 1)
131 Real coverage = height_ / text_height ();
132 if (coverage < MIN_COVERAGE)
133 /* Do not space out a badly filled page. This is too simplistic
134 (ie broken), because this should not vary too much between
135 (subsequent?) pages in a book. */
140 out->output_line (stencil2line (get_header ()), &o, false);
141 o[Y_AXIS] += head_sep_;
143 for (SCM s = lines_; gh_pair_p (s); s = ly_cdr (s))
145 SCM line = ly_car (s);
146 SCM offset = ly_car (line);
147 if (ly_car (offset) == ly_symbol2scm ("title"))
148 line = scm_cons (ly_cdr (offset), ly_cdr (line));
149 out->output_line (line, &o,
150 is_last && gh_pair_p (ly_cdr (s)) && !get_copyright ()
151 && !get_tagline () && !get_footer ());
152 if (gh_pair_p (ly_cdr (s)) && ly_car (offset) != ly_symbol2scm ("title"))
157 if (get_copyright () || get_tagline () || get_footer ())
158 o[Y_AXIS] += foot_sep_;
160 o[Y_AXIS] = vsize_ - bottom_margin_;
161 if (get_copyright ())
162 o[Y_AXIS] -= get_copyright ()->extent (Y_AXIS).length ();
164 o[Y_AXIS] -= get_tagline ()->extent (Y_AXIS).length ();
166 o[Y_AXIS] -= get_footer ()->extent (Y_AXIS).length ();
169 if (get_copyright ())
170 out->output_line (stencil2line (get_copyright ()), &o,
171 is_last && !get_tagline () && !get_footer ());
173 out->output_line (stencil2line (get_tagline ()), &o,
174 is_last && !get_footer ());
176 out->output_line (stencil2line (get_footer ()), &o, is_last);
177 out->output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
178 gh_bool2scm (is_last && !get_footer ())));
179 progress_indication ("]");
185 Real h = vsize_ - top_margin_ - bottom_margin_;
187 h -= get_header ()->extent (Y_AXIS).length () + head_sep_;
188 if (get_copyright () || get_tagline () || get_footer ())
190 if (get_copyright ())
191 h -= get_copyright ()->extent (Y_AXIS).length ();
193 h -= get_tagline ()->extent (Y_AXIS).length ();
195 h -= get_footer ()->extent (Y_AXIS).length ();
199 /****************************************************************/
201 /* Current global paper book. Gives default_rendering access from
202 input-file-results. */
203 Paper_book *paper_book;
205 Paper_book::Paper_book ()
207 copyright_ = SCM_EOL;
213 Paper_book::~Paper_book ()
218 Paper_book::output (String outname)
220 if (!papers_.size ())
223 /* Generate all stencils to trigger font loads. */
224 Link_array<Page> *pages = get_pages ();
226 Paper_def *paper = papers_[0];
227 Paper_outputter *out = paper->get_paper_outputter (outname);
228 out->output_header (paper, get_scopes (0), pages->size ());
230 int page_count = pages->size ();
231 for (int i = 0; i < page_count; i++)
232 (*pages)[i]->output (out, i + 1 == page_count);
234 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
235 progress_indication ("\n");
239 Paper_book::get_scopes (int i)
241 SCM scopes = SCM_EOL;
243 scopes = scm_cons (headers_[i], scopes);
244 if (global_headers_[i] && global_headers_[i] != headers_[i])
245 scopes = scm_cons (global_headers_[i], scopes);
250 Paper_book::get_title (int i)
252 SCM user_title = scm_primitive_eval (ly_symbol2scm ("user-title"));
253 SCM book_title = scm_primitive_eval (ly_symbol2scm ("book-title"));
254 SCM score_title = scm_primitive_eval (ly_symbol2scm ("score-title"));
255 SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
256 : ly_symbol2scm ("scoreTitle"));
259 SCM scopes = get_scopes (i);
260 SCM s = ly_modules_lookup (scopes, field);
261 if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
262 title = unsmob_stencil (scm_call_2 (user_title,
263 papers_[0]->self_scm (),
264 scm_variable_ref (s)));
266 title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
267 papers_[0]->self_scm (),
270 title->align_to (Y_AXIS, UP);
275 /* calculate book height, #lines, stencils. */
279 int score_count = scores_.size ();
281 /* Calculate the full book height. Hmm, can't we cache system
282 heights while making stencils? */
284 for (int i = 0; i < score_count; i++)
286 Stencil *title = get_title (i);
288 height_ += title->extent (Y_AXIS).length ();
290 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
291 for (int j = 0; j < line_count; j++)
293 SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
294 height_ += ly_scm2offset (ly_car (line))[Y_AXIS];
298 Paper_def *paper = papers_[0];
299 SCM scopes = get_scopes (0);
301 SCM make_tagline = scm_primitive_eval (ly_symbol2scm ("make-tagline"));
302 tagline_ = scm_call_2 (make_tagline, paper->smobbed_copy (), scopes);
304 if (Stencil *s = unsmob_stencil (tagline_))
305 tag_height = s->extent (Y_AXIS).length ();
306 height_ += tag_height;
308 SCM make_copyright = scm_primitive_eval (ly_symbol2scm ("make-copyright"));
309 copyright_ = scm_call_2 (make_copyright, paper->smobbed_copy (), scopes);
310 Real copy_height = 0;
311 if (Stencil *s = unsmob_stencil (copyright_))
312 copy_height = s->extent (Y_AXIS).length ();
313 height_ += copy_height;
317 - real page breaking algorithm (Gourlay?)
318 Hmmughr, Gourlay uses Grobs, columns etc -- looks like it needs serious
319 refactoring before it can be applied to Page breaking...
320 - override: # pages, or pageBreakLines= #'(3 3 4), ? */
322 Paper_book::get_pages ()
325 Page::page_count_ = 0;
326 Paper_def *paper = papers_[0];
327 Page *page = new Page (paper, 1);
329 Real text_height = page->text_height ();
330 Real page_frac = height_ / text_height;
331 int page_count = (int) ceil (page_frac);
332 if (unsmob_stencil (copyright_))
333 page->copyright_ = copyright_;
334 if (unsmob_stencil (tagline_) && page_count == 1)
335 page->tagline_ = tagline_;
337 /* Attempt to fill pages better using FUDGE kludge. */
338 Real r = page_frac - (int) page_frac;
339 Real cramp_fudge = page_count > 1 ? (r / (page_count - 1)) * text_height : 0;
340 Real expand_fudge = - ((1 - r) / page_count) * text_height;
342 Link_array<Page>* pages = 0;
343 if ((page_count > 1 && (r / (page_count - 1)) < MAX_CRAMP))
345 /* Just a little more space (<MAX_CRAMP) per page is needed,
346 cramp onto one page less. */
347 pages = fill_pages (page, page_count - 1, cramp_fudge);
348 if (pages->size () != page_count - 1)
350 /* Cramping failed. */
351 page = pages->get (0);
356 if (!pages && ((1 - r) / page_count) < 1 - MIN_COVERAGE)
358 /* There is space left, but not so much that paged would have too
359 little blackness (< MIN_COVERAGE), distribute evenly. */
360 pages = fill_pages (page, page_count, expand_fudge);
361 bool badly_covered = false;
362 if (pages->size () == page_count)
363 for (int i = 0; i < page_count; i++)
365 Page *p = (*pages)[i];
366 Real coverage = p ->height_ / p->text_height ();
367 if (coverage < MIN_COVERAGE)
369 badly_covered = true;
373 if (pages->size () != page_count || badly_covered)
375 /* expanding failed. */
376 page = pages->get (0);
383 /* Fudging failed; just fill pages. */
384 pages = fill_pages (page, page_count, 0);
388 /* Simplistic page breaking:
389 add lines until HEIGHT > PAGE.TEXT_HEIGHT_ + FUDGE */
391 Paper_book::fill_pages (Page *page, int page_count, Real fudge)
394 int score_count = scores_.size ();
395 Paper_def *paper = papers_[0];
396 Link_array<Page> *pages = new Link_array<Page>;
397 page->lines_ = SCM_EOL;
399 page->line_count_ = 0;
400 Real text_height = page->text_height ();
401 for (int i = 0; i < score_count; i++)
404 Stencil *title = get_title (i);
406 h = title->extent (Y_AXIS).length ();
408 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
409 for (int j = 0; j < line_count; j++)
411 SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
412 h += ly_scm2offset (ly_car (line))[Y_AXIS];
413 Real fill = (page->height_ - text_height) / text_height;
414 // Real fill_h = (page->height_ + h - text_height) / text_height;
415 Real fudged_fill = (page->height_ - (text_height + fudge))
416 / (text_height + fudge);
417 Real fudged_fill_h = ((page->height_ + h) - (text_height + fudge))
418 / (text_height + fudge);
419 if (fill > -MAX_CRAMP
420 || (fudged_fill > -MAX_CRAMP
422 || !(fudged_fill_h > 0
423 && abs (fudged_fill_h) < 4 * abs (fudged_fill)))))
426 page = new Page (paper, ++page_number);
427 if (unsmob_stencil (tagline_) && page_number == page_count)
428 page->tagline_ = tagline_;
429 text_height = page->text_height ();
432 page->lines_ = ly_snoc (title2line (title), page->lines_);
433 page->lines_ = ly_snoc (line, page->lines_);
445 Paper_book::classic_output (String outname)
447 int count = scores_.size ();
448 Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
449 out->output_header (papers_.top (), get_scopes (count - 1), 0);
451 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
452 for (int i = 0; i < line_count; i++)
453 out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
454 0, i == line_count - 1);
456 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
457 progress_indication ("\n");
461 #include "ly-smobs.icc"
463 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
464 IMPLEMENT_SMOBS (Paper_book)
465 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
468 Paper_book::mark_smob (SCM smob)
470 Paper_book *pb = (Paper_book*) SCM_CELL_WORD_1 (smob);
471 for (int i = 0; i < pb->headers_.size (); i++)
472 scm_gc_mark (pb->headers_[i]);
473 for (int i = 0; i < pb->global_headers_.size (); i++)
474 scm_gc_mark (pb->global_headers_[i]);
475 for (int i = 0; i < pb->papers_.size (); i++)
476 scm_gc_mark (pb->papers_[i]->self_scm ());
477 for (int i = 0; i < pb->scores_.size (); i++)
478 scm_gc_mark (pb->scores_[i]);
480 scm_gc_mark (pb->copyright_);
487 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
489 Paper_book *b = (Paper_book*) ly_cdr (smob);
491 scm_puts ("#<", port);
492 scm_puts (classname (b), port);
493 scm_puts (" ", port);
494 //scm_puts (b->, port);
495 scm_puts (">", port);