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)
26 static SCM z = ly_offset2scm (Offset (0, 0));
27 Offset dim = Offset (stil->extent (X_AXIS).length (),
28 stil->extent (Y_AXIS).length ());
29 return scm_cons (ly_offset2scm (dim),
30 scm_list_1 (scm_cons (z, stil->smobbed_copy ())));
34 title2line (Stencil* stil)
36 SCM s = stencil2line (stil);
37 /* whugh, add marker recognise titles. */
38 return scm_cons (scm_cons (ly_symbol2scm ("title"), ly_car (s)), ly_cdr (s));
41 /* Simplistic page interface */
46 static int page_count_;
51 Protected_scm header_;
52 Protected_scm footer_;
53 Protected_scm copyright_;
54 Protected_scm tagline_;
56 Stencil *get_header () { return unsmob_stencil (header_); }
57 Stencil *get_copyright () { return unsmob_stencil (copyright_); }
58 Stencil *get_tagline () { return unsmob_stencil (tagline_); }
59 Stencil *get_footer () { return unsmob_stencil (footer_); }
61 /* actual height filled with text. */
64 // HMMM all this size stuff to paper/paper-outputter?
74 /* available area for text. */
77 Page (Paper_def*, int);
78 void output (Paper_outputter*, bool);
81 int Page::page_count_ = 0;
83 Page::Page (Paper_def *paper, int number)
93 hsize_ = paper->get_realvar (ly_symbol2scm ("hsize"));
94 vsize_ = paper->get_realvar (ly_symbol2scm ("vsize"));
95 top_margin_ = paper->get_realvar (ly_symbol2scm ("top-margin"));
96 bottom_margin_ = paper->get_realvar (ly_symbol2scm ("bottom-margin"));
97 head_sep_ = paper->get_realvar (ly_symbol2scm ("head-sep"));
98 foot_sep_ = paper->get_realvar (ly_symbol2scm ("foot-sep"));
99 text_width_ = paper->get_realvar (ly_symbol2scm ("linewidth"));
100 left_margin_ = (hsize_ - text_width_) / 2;
102 copyright_ = SCM_EOL;
105 SCM make_header = scm_primitive_eval (ly_symbol2scm ("make-header"));
106 SCM make_footer = scm_primitive_eval (ly_symbol2scm ("make-footer"));
108 header_ = scm_call_2 (make_header, paper_->smobbed_copy (),
109 scm_int2num (number_));
110 // FIXME: why does this (generates Stencil) not trigger font load?
112 get_header ()->align_to (Y_AXIS, UP);
114 footer_ = scm_call_2 (make_footer, paper_->smobbed_copy (),
115 scm_int2num (number_));
117 get_footer ()->align_to (Y_AXIS, UP);
121 Page::output (Paper_outputter *out, bool is_last)
123 progress_indication ("[" + to_string (number_));
124 out->output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
125 Offset o (left_margin_, top_margin_);
126 Real vfill = line_count_ > 1 ? (text_height () - height_) / (line_count_ - 1)
129 Real coverage = height_ / text_height ();
130 if (coverage < MIN_COVERAGE)
131 /* Do not space out a badly filled page. This is too simplistic
132 (ie broken), because this should not vary too much between
133 (subsequent?) pages in a book. */
138 out->output_line (stencil2line (get_header ()), &o, false);
139 o[Y_AXIS] += head_sep_;
141 for (SCM s = lines_; gh_pair_p (s); s = ly_cdr (s))
143 SCM line = ly_car (s);
144 SCM offset = ly_car (line);
145 if (ly_car (offset) == ly_symbol2scm ("title"))
146 line = scm_cons (ly_cdr (offset), ly_cdr (line));
147 out->output_line (line, &o,
148 is_last && gh_pair_p (ly_cdr (s)) && !get_copyright ()
149 && !get_tagline () && !get_footer ());
150 if (gh_pair_p (ly_cdr (s)) && ly_car (offset) != ly_symbol2scm ("title"))
155 if (get_copyright () || get_tagline () || get_footer ())
156 o[Y_AXIS] += foot_sep_;
158 o[Y_AXIS] = vsize_ - bottom_margin_;
159 if (get_copyright ())
160 o[Y_AXIS] -= get_copyright ()->extent (Y_AXIS).length ();
162 o[Y_AXIS] -= get_tagline ()->extent (Y_AXIS).length ();
164 o[Y_AXIS] -= get_footer ()->extent (Y_AXIS).length ();
167 if (get_copyright ())
168 out->output_line (stencil2line (get_copyright ()), &o,
169 is_last && !get_tagline () && !get_footer ());
171 out->output_line (stencil2line (get_tagline ()), &o,
172 is_last && !get_footer ());
174 out->output_line (stencil2line (get_footer ()), &o, is_last);
175 out->output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
176 gh_bool2scm (is_last && !get_footer ())));
177 progress_indication ("]");
183 Real h = vsize_ - top_margin_ - bottom_margin_;
185 h -= get_header ()->extent (Y_AXIS).length () + head_sep_;
186 if (get_copyright () || get_tagline () || get_footer ())
188 if (get_copyright ())
189 h -= get_copyright ()->extent (Y_AXIS).length ();
191 h -= get_tagline ()->extent (Y_AXIS).length ();
193 h -= get_footer ()->extent (Y_AXIS).length ();
197 /****************************************************************/
199 /* Current global paper book. Gives default_rendering access from
200 input-file-results. */
201 Paper_book *paper_book;
203 Paper_book::Paper_book ()
208 Paper_book::~Paper_book ()
213 Paper_book::output (String outname)
215 /* Generate all stencils to trigger font loads. */
216 Link_array<Page> *pages = get_pages ();
218 Paper_def *paper = papers_[0];
219 Paper_outputter *out = paper->get_paper_outputter (outname);
220 out->output_metadata (get_scopes (0), paper);
221 out->output_header (paper);
223 int page_count = pages->size ();
224 for (int i = 0; i < page_count; i++)
225 (*pages)[i]->output (out, i + 1 == page_count);
227 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
228 progress_indication ("\n");
232 Paper_book::get_scopes (int i)
234 SCM scopes = SCM_EOL;
236 scopes = scm_cons (headers_[i], scopes);
237 if (global_headers_[i] && global_headers_[i] != headers_[i])
238 scopes = scm_cons (global_headers_[i], scopes);
243 Paper_book::get_title (int i)
245 SCM user_title = scm_primitive_eval (ly_symbol2scm ("user-title"));
246 SCM book_title = scm_primitive_eval (ly_symbol2scm ("book-title"));
247 SCM score_title = scm_primitive_eval (ly_symbol2scm ("score-title"));
248 SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
249 : ly_symbol2scm ("scoreTitle"));
252 SCM scopes = get_scopes (i);
253 SCM s = ly_modules_lookup (scopes, field);
254 if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
255 title = unsmob_stencil (scm_call_2 (user_title,
256 papers_[0]->self_scm (),
257 scm_variable_ref (s)));
259 title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
260 papers_[0]->self_scm (),
263 title->align_to (Y_AXIS, UP);
268 /* calculate book height, #lines, stencils. */
272 int score_count = scores_.size ();
274 /* Calculate the full book height. Hmm, can't we cache system
275 heights while making stencils? */
277 for (int i = 0; i < score_count; i++)
279 Stencil *title = get_title (i);
281 height_ += title->extent (Y_AXIS).length ();
283 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
284 for (int j = 0; j < line_count; j++)
286 SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
287 height_ += ly_scm2offset (ly_car (line))[Y_AXIS];
291 Paper_def *paper = papers_[0];
292 SCM scopes = get_scopes (0);
294 SCM make_tagline = scm_primitive_eval (ly_symbol2scm ("make-tagline"));
295 tagline_ = scm_call_2 (make_tagline, paper->smobbed_copy (), scopes);
297 if (Stencil *s = unsmob_stencil (tagline_))
298 tag_height = s->extent (Y_AXIS).length ();
299 height_ += tag_height;
301 SCM make_copyright = scm_primitive_eval (ly_symbol2scm ("make-copyright"));
302 copyright_ = scm_call_2 (make_copyright, paper->smobbed_copy (), scopes);
303 Real copy_height = 0;
304 if (Stencil *s = unsmob_stencil (copyright_))
305 copy_height = s->extent (Y_AXIS).length ();
306 height_ += copy_height;
310 - real page breaking algorithm (Gourlay?)
311 Hmmughr, Gourlay uses Grobs, columns etc -- looks like it needs serious
312 refactoring before it can be applied to Page breaking...
313 - override: # pages, or pageBreakLines= #'(3 3 4), ? */
315 Paper_book::get_pages ()
318 Page::page_count_ = 0;
319 Paper_def *paper = papers_[0];
320 Page *page = new Page (paper, 1);
322 Real text_height = page->text_height ();
323 Real page_frac = height_ / text_height;
324 int page_count = (int) ceil (page_frac);
325 if (unsmob_stencil (copyright_))
326 page->copyright_ = copyright_;
327 if (unsmob_stencil (tagline_) && page_count == 1)
328 page->tagline_ = tagline_;
330 /* Attempt to fill pages better using FUDGE kludge. */
331 Real r = page_frac - (int) page_frac;
332 Real cramp_fudge = page_count > 1 ? (r / (page_count - 1)) * text_height : 0;
333 Real expand_fudge = - ((1 - r) / page_count) * text_height;
335 Link_array<Page>* pages = 0;
336 if ((page_count > 1 && (r / (page_count - 1)) < MAX_CRAMP))
338 /* Just a little more space (<MAX_CRAMP) per page is needed,
339 cramp onto one page less. */
340 pages = fill_pages (page, page_count - 1, cramp_fudge);
341 if (pages->size () != page_count - 1)
343 /* Cramping failed. */
344 page = pages->get (0);
349 if (!pages && ((1 - r) / page_count) < 1 - MIN_COVERAGE)
351 /* There is space left, but not so much that paged would have too
352 little blackness (< MIN_COVERAGE), distribute evenly. */
353 pages = fill_pages (page, page_count, expand_fudge);
354 bool badly_covered = false;
355 if (pages->size () == page_count)
356 for (int i = 0; i < page_count; i++)
358 Page *p = (*pages)[i];
359 Real coverage = p ->height_ / p->text_height ();
360 if (coverage < MIN_COVERAGE)
362 badly_covered = true;
366 if (pages->size () != page_count || badly_covered)
368 /* expanding failed. */
369 page = pages->get (0);
376 /* Fudging failed; just fill pages. */
377 pages = fill_pages (page, page_count, 0);
381 /* Simplistic page breaking:
382 add lines until HEIGHT > PAGE.TEXT_HEIGHT_ + FUDGE */
384 Paper_book::fill_pages (Page *page, int page_count, Real fudge)
387 int score_count = scores_.size ();
388 Paper_def *paper = papers_[0];
389 Link_array<Page> *pages = new Link_array<Page>;
390 page->lines_ = SCM_EOL;
392 page->line_count_ = 0;
393 Real text_height = page->text_height ();
394 for (int i = 0; i < score_count; i++)
397 Stencil *title = get_title (i);
399 h = title->extent (Y_AXIS).length ();
401 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
402 for (int j = 0; j < line_count; j++)
404 SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
405 h += ly_scm2offset (ly_car (line))[Y_AXIS];
406 Real fill = (page->height_ - text_height) / text_height;
407 // Real fill_h = (page->height_ + h - text_height) / text_height;
408 Real fudged_fill = (page->height_ - (text_height + fudge))
409 / (text_height + fudge);
410 Real fudged_fill_h = ((page->height_ + h) - (text_height + fudge))
411 / (text_height + fudge);
412 if (fill > -MAX_CRAMP
413 || (fudged_fill > -MAX_CRAMP
415 || !(fudged_fill_h > 0
416 && abs (fudged_fill_h) < 4 * abs (fudged_fill)))))
419 page = new Page (paper, ++page_number);
420 if (unsmob_stencil (tagline_) && page_number == page_count)
421 page->tagline_ = tagline_;
422 text_height = page->text_height ();
425 page->lines_ = ly_snoc (title2line (title), page->lines_);
426 page->lines_ = ly_snoc (line, page->lines_);
438 Paper_book::classic_output (String outname)
440 Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
441 int count = scores_.size ();
443 out->output_metadata (get_scopes (count - 1), papers_.top ());
444 out->output_header (papers_.top ());
446 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
447 for (int i = 0; i < line_count; i++)
448 out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
449 0, i == line_count - 1);
451 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
452 progress_indication ("\n");
456 #include "ly-smobs.icc"
458 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
459 IMPLEMENT_SMOBS (Paper_book)
460 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
463 Paper_book::mark_smob (SCM smob)
465 Paper_book *pb = (Paper_book*) SCM_CELL_WORD_1 (smob);
466 for (int i = 0; i < pb->headers_.size (); i++)
467 scm_gc_mark (pb->headers_[i]);
468 for (int i = 0; i < pb->global_headers_.size (); i++)
469 scm_gc_mark (pb->global_headers_[i]);
470 for (int i = 0; i < pb->papers_.size (); i++)
471 scm_gc_mark (pb->papers_[i]->self_scm ());
472 for (int i = 0; i < pb->scores_.size (); i++)
473 scm_gc_mark (pb->scores_[i]);
478 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
480 Paper_book *b = (Paper_book*) ly_cdr (smob);
482 scm_puts ("#<", port);
483 scm_puts (classname (b), port);
484 scm_puts (" ", port);
485 //scm_puts (b->, port);
486 scm_puts (">", port);