2 page.cc -- implement Page
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "dimensions.hh"
10 #include "ly-module.hh"
12 #include "output-def.hh"
13 #include "paper-outputter.hh"
14 #include "paper-line.hh"
17 #include "book-paper-def.hh"
19 int Page::page_count_ = 0;
20 Real Page::MIN_COVERAGE_ = 0.66;
23 Page::bookpaper () const
25 return dynamic_cast<Book_output_def*> ( paper_->parent_);
28 Page::Page (Output_def *paper, int number)
44 header_ = scm_call_2 (paper_->c_variable ("make-header"),
46 scm_int2num (number_));
47 if (unsmob_stencil (header_))
48 unsmob_stencil (header_)->align_to (Y_AXIS, UP);
50 footer_ = scm_call_2 (paper_->c_variable ("make-footer"),
52 scm_int2num (number_));
53 if (unsmob_stencil (footer_))
54 unsmob_stencil (footer_)->align_to (Y_AXIS, UP);
63 #include "ly-smobs.icc"
65 IMPLEMENT_DEFAULT_EQUAL_P (Page)
66 IMPLEMENT_SMOBS (Page)
67 IMPLEMENT_TYPE_P (Page, "ly:page?")
70 Page::mark_smob (SCM smob)
72 Page *p = (Page*) SCM_CELL_WORD_1 (smob);
73 scm_gc_mark (p->header_);
74 scm_gc_mark (p->footer_);
78 scm_gc_mark (p->paper_->self_scm ());
81 scm_gc_mark (p->bookpaper ()->self_scm ());
85 scm_gc_mark (p->copyright_);
86 scm_gc_mark (p->tagline_);
87 //scm_gc_mark (p->lines_);
92 Page::print_smob (SCM smob, SCM port, scm_print_state*)
94 Page *p = (Page*) ly_cdr (smob);
95 scm_puts ("#<", port);
96 scm_puts (classname (p), port);
97 scm_puts (to_string (p->number_).to_str0 (), port);
104 stack_stencils (Stencil a, Stencil b, Offset *origin)
106 Real height = b.extent (Y_AXIS).length ();
109 programming_error (to_string ("Improbable stencil height: %f", height));
116 (*origin)[Y_AXIS] += height;
121 Page::to_stencil () const
123 SCM proc = paper_->lookup_variable (ly_symbol2scm ("page-to-stencil"));
124 return *unsmob_stencil (scm_call_1 (proc, self_scm ()));
129 Page::left_margin () const
131 return (paper_->get_dimension (ly_symbol2scm ("hsize"))
132 - paper_->get_dimension (ly_symbol2scm ("linewidth"))) / 2;
135 LY_DEFINE (ly_page_header_lines_footer_stencil, "ly:page-header-lines-footer-stencil",
137 "Simple header, lines, footer stencil from PAGE.")
139 Page *p = unsmob_page (page);
140 SCM_ASSERT_TYPE (p, page, SCM_ARG1, __FUNCTION__, "page");
143 Offset o (p->left_margin (),
144 p->paper_->get_dimension (ly_symbol2scm ("top-margin")));
146 Real vfill = (p->line_count_ > 1
147 ? (p->text_height () - p->height_) / (p->line_count_ - 1)
150 Real coverage = p->height_ / p->text_height ();
151 if (coverage < p->MIN_COVERAGE_)
152 /* Do not space out a badly filled page. This is too simplistic
153 (ie broken), because this should not vary too much between
154 (subsequent?) pages in a book. */
157 if (Stencil *s = unsmob_stencil (p->header_))
159 stencil = stack_stencils (stencil, *s, &o);
160 o[Y_AXIS] += p->paper_->get_dimension (ly_symbol2scm ("head-sep"));
163 for (SCM s = p->lines_; s != SCM_EOL; s = ly_cdr (s))
165 Paper_line *p = unsmob_paper_line (ly_car (s));
166 stencil = stack_stencils (stencil, p->to_stencil (), &o);
167 /* Do not put vfill between title and its music, */
168 if (ly_cdr (s) != SCM_EOL
169 && (!p->is_title () || vfill < 0))
171 /* rather put extra just before the title. */
172 if (ly_cdr (s) != SCM_EOL
173 && (unsmob_paper_line (ly_cadr (s))->is_title () && vfill > 0))
177 o[Y_AXIS] = p->paper_->get_dimension (ly_symbol2scm ("vsize"))
178 - p->paper_->get_dimension (ly_symbol2scm ("bottom-margin"));
179 if (unsmob_stencil (p->copyright_))
180 o[Y_AXIS] -= unsmob_stencil (p->copyright_)->extent (Y_AXIS).length ();
181 if (unsmob_stencil (p->tagline_))
182 o[Y_AXIS] -= unsmob_stencil (p->tagline_)->extent (Y_AXIS).length ();
183 if (unsmob_stencil (p->footer_))
184 o[Y_AXIS] -= unsmob_stencil (p->footer_)->extent (Y_AXIS).length ();
186 if (Stencil *s = unsmob_stencil (p->copyright_))
187 stencil = stack_stencils (stencil, *s, &o);
188 if (Stencil *s = unsmob_stencil (p->tagline_))
189 stencil = stack_stencils (stencil, *s, &o);
190 if (Stencil *s = unsmob_stencil (p->footer_))
191 stencil = stack_stencils (stencil, *s, &o);
193 return stencil.smobbed_copy ();
197 Page::text_height () const
199 Real h = paper_->get_dimension (ly_symbol2scm ("vsize"))
200 - paper_->get_dimension (ly_symbol2scm ("top-margin"))
201 - paper_->get_dimension (ly_symbol2scm ("bottom-margin"));
202 if (unsmob_stencil (header_))
203 h -= unsmob_stencil (header_)->extent (Y_AXIS).length ()
204 + paper_->get_dimension (ly_symbol2scm ("head-sep"));
205 if (unsmob_stencil (copyright_)
206 || unsmob_stencil (tagline_)
207 || unsmob_stencil (footer_))
208 h -= paper_->get_dimension (ly_symbol2scm ("foot-sep"));
209 if (unsmob_stencil (copyright_))
210 h -= unsmob_stencil (copyright_)->extent (Y_AXIS).length ();
211 if (unsmob_stencil (tagline_))
212 h -= unsmob_stencil (tagline_)->extent (Y_AXIS).length ();
213 if (unsmob_stencil (footer_))
214 h -= unsmob_stencil (footer_)->extent (Y_AXIS).length ();
218 LY_DEFINE (ly_page_paper_lines, "ly:page-paper-lines",
220 "Return paper-lines from PAGE.")
222 Page *p = unsmob_page (page);
223 SCM_ASSERT_TYPE (p, page, SCM_ARG1, __FUNCTION__, "page");