-#include "stencil.hh"
-
-static Real const MIN_COVERAGE = 0.66;
-
-static SCM
-stencil2line (Stencil* stil, bool is_title = false)
-{
- static SCM z;
- if (!z)
- z = scm_permanent_object (ly_offset2scm (Offset (0, 0)));
- Offset dim = Offset (stil->extent (X_AXIS).length (),
- stil->extent (Y_AXIS).length ());
- Paper_line *pl = new Paper_line (dim, scm_cons (stil->smobbed_copy (),
- SCM_EOL), is_title);
-
- return scm_gc_unprotect_object (pl->self_scm ());
-}
-
-/* Simplistic page interface */
-class Page
-{
-public:
- Paper_def *paper_;
- static int page_count_;
- int number_;
- int line_count_;
-
- Protected_scm lines_;
- Protected_scm header_;
- Protected_scm footer_;
- Protected_scm copyright_;
- Protected_scm tagline_;
-
- Stencil *get_header () { return unsmob_stencil (header_); }
- Stencil *get_copyright () { return unsmob_stencil (copyright_); }
- Stencil *get_tagline () { return unsmob_stencil (tagline_); }
- Stencil *get_footer () { return unsmob_stencil (footer_); }
-
- /* actual height filled with text. */
- Real height_;
-
- // HMMM all this size stuff to paper/paper-outputter?
- Real hsize_;
- Real vsize_;
- Real left_margin_;
- Real top_margin_;
- Real bottom_margin_;
- Real foot_sep_;
- Real head_sep_;
- Real text_width_;
-
- /* available area for text. */
- Real text_height ();
-
- Page (Paper_def*, int);
- void output (Paper_outputter*, bool);
-};
-
-int Page::page_count_ = 0;
-
-Page::Page (Paper_def *paper, int number)
-{
- paper_ = paper;
- number_ = number;
- page_count_++;
-
- height_ = 0;
- lines_ = SCM_EOL;
- line_count_ = 0;
-
- hsize_ = paper->get_realvar (ly_symbol2scm ("hsize"));
- vsize_ = paper->get_realvar (ly_symbol2scm ("vsize"));
- top_margin_ = paper->get_realvar (ly_symbol2scm ("top-margin"));
- bottom_margin_ = paper->get_realvar (ly_symbol2scm ("bottom-margin"));
- head_sep_ = paper->get_realvar (ly_symbol2scm ("head-sep"));
- foot_sep_ = paper->get_realvar (ly_symbol2scm ("foot-sep"));
- text_width_ = paper->get_realvar (ly_symbol2scm ("linewidth"));
- left_margin_ = (hsize_ - text_width_) / 2;
-
- copyright_ = SCM_EOL;
- tagline_ = SCM_EOL;
-
- SCM make_header = ly_scheme_function ("make-header");
- SCM make_footer = ly_scheme_function ("make-footer");
-
- header_ = scm_call_2 (make_header, paper_->self_scm (),
- scm_int2num (number_));
- // FIXME: why does this (generates Stencil) not trigger font load?
- if (get_header ())
- get_header ()->align_to (Y_AXIS, UP);
-
- footer_ = scm_call_2 (make_footer, paper_->self_scm (),
- scm_int2num (number_));
- if (get_footer ())
- get_footer ()->align_to (Y_AXIS, UP);
-}
-
-void
-Page::output (Paper_outputter *out, bool is_last)
-{
- progress_indication ("[" + to_string (number_));
- out->output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
- Offset o (left_margin_, top_margin_);
- Real vfill = line_count_ > 1 ? (text_height () - height_) / (line_count_ - 1)
- : 0;
-
- Real coverage = height_ / text_height ();
- if (coverage < MIN_COVERAGE)
- /* Do not space out a badly filled page. This is too simplistic
- (ie broken), because this should not vary too much between
- (subsequent?) pages in a book. */
- vfill = 0;
-
- if (get_header ())
- {
- out->output_line (stencil2line (get_header ()), &o, false);
- o[Y_AXIS] += head_sep_;
- }
- for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
- {
- SCM line = ly_car (s);
- out->output_line (line, &o,
- is_last && ly_cdr (s) != SCM_EOL && !get_copyright ()
- && !get_tagline () && !get_footer ());
-
- /* Do not put vfill between title and its music, */
- if (scm_pair_p (ly_cdr (s))
- && (!unsmob_paper_line (line)->is_title () || vfill < 0))
- o[Y_AXIS] += vfill;
- /* rather put extra just before the title. */
- if (ly_cdr (s) != SCM_EOL
- && (unsmob_paper_line (ly_cadr (s))->is_title () && vfill > 0))
- o[Y_AXIS] += vfill;
- }
-
- o[Y_AXIS] = vsize_ - bottom_margin_;
- if (get_copyright ())
- o[Y_AXIS] -= get_copyright ()->extent (Y_AXIS).length ();
- if (get_tagline ())
- o[Y_AXIS] -= get_tagline ()->extent (Y_AXIS).length ();
- if (get_footer ())
- o[Y_AXIS] -= get_footer ()->extent (Y_AXIS).length ();
-
- if (get_copyright ())
- out->output_line (stencil2line (get_copyright ()), &o,
- is_last && !get_tagline () && !get_footer ());
- if (get_tagline ())
- out->output_line (stencil2line (get_tagline ()), &o,
- is_last && !get_footer ());
- if (get_footer ())
- out->output_line (stencil2line (get_footer ()), &o, is_last);
- out->output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
- gh_bool2scm (is_last && !get_footer ())));
- progress_indication ("]");
-}
-
-Real
-Page::text_height ()
-{
- Real h = vsize_ - top_margin_ - bottom_margin_;
- if (get_header ())
- h -= get_header ()->extent (Y_AXIS).length () + head_sep_;
- if (get_copyright () || get_tagline () || get_footer ())
- h -= foot_sep_;
- if (get_copyright ())
- h -= get_copyright ()->extent (Y_AXIS).length ();
- if (get_tagline ())
- h -= get_tagline ()->extent (Y_AXIS).length ();
- if (get_footer ())
- h -= get_footer ()->extent (Y_AXIS).length ();
- return h;
-}
-
-/****************************************************************/