2 paper-book.cc -- implement Paper_book
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "ly-module.hh"
12 #include "paper-book.hh"
13 #include "paper-def.hh"
14 #include "paper-outputter.hh"
15 #include "paper-line.hh"
16 #include "paper-score.hh"
22 stencil2line (Stencil *stil, bool is_title = false)
26 z = scm_permanent_object (ly_offset2scm (Offset (0, 0)));
27 Offset dim = Offset (stil->extent (X_AXIS).length (),
28 stil->extent (Y_AXIS).length ());
29 Paper_line *pl = new Paper_line (dim, scm_cons (stil->smobbed_copy (),
31 -10001 * is_title, is_title);
33 return scm_gc_unprotect_object (pl->self_scm ());
37 Paper_book::Paper_book ()
45 Paper_book::~Paper_book ()
49 #include "ly-smobs.icc"
51 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
52 IMPLEMENT_SMOBS (Paper_book)
53 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
56 Paper_book::mark_smob (SCM smob)
58 Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
59 for (int i = 0; i < b->headers_.size (); i++)
60 scm_gc_mark (b->headers_[i]);
61 for (int i = 0; i < b->global_headers_.size (); i++)
62 scm_gc_mark (b->global_headers_[i]);
63 for (int i = 0; i < b->papers_.size (); i++)
64 scm_gc_mark (b->papers_[i]->self_scm ());
65 for (int i = 0; i < b->scores_.size (); i++)
66 scm_gc_mark (b->scores_[i]);
68 scm_gc_mark (b->copyright_);
74 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
76 Paper_book *b = (Paper_book*) ly_cdr (smob);
78 scm_puts ("#<", port);
79 scm_puts (classname (b), port);
81 //scm_puts (b->, port);
87 Paper_book::output (String outname)
90 // FIXME: no end-output?
93 /* Generate all stencils to trigger font loads. */
94 SCM pages = this->pages ();
96 Paper_def *paper = papers_[0];
97 Paper_outputter *out = paper->get_paper_outputter (outname);
98 int page_count = scm_ilength (pages);
99 out->output_header (paper, scopes (0), page_count, false);
103 int paper_count = papers_.size ();
104 for (int i = 1; i < paper_count; i ++)
106 SCM fonts = papers_[i]->font_descriptions ();
107 out->output_scheme (scm_list_3 (ly_symbol2scm ("define-fonts"),
108 papers_[i]->self_scm (),
110 ly_quote_scm (ly_list_qsort_uniq_x (fonts))));
114 for (SCM s = pages; s != SCM_EOL; s = ly_cdr (s))
116 Page *p = unsmob_page (ly_car (s));
117 progress_indication ("[" + to_string (p->number_));
118 out->output_page (p, ly_cdr (s) == SCM_EOL);
119 progress_indication ("]");
122 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
123 progress_indication ("\n");
127 Paper_book::scopes (int i)
129 SCM scopes = SCM_EOL;
131 scopes = scm_cons (headers_[i], scopes);
132 if (global_headers_.size ()
133 && global_headers_[i] && global_headers_[i] != headers_[i])
134 scopes = scm_cons (global_headers_[i], scopes);
139 Paper_book::title (int i)
141 SCM user_title = ly_scheme_function ("user-title");
142 SCM book_title = ly_scheme_function ("book-title");
143 SCM score_title = ly_scheme_function ("score-title");
144 SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
145 : ly_symbol2scm ("scoreTitle"));
148 SCM scopes = this->scopes (i);
149 SCM s = ly_modules_lookup (scopes, field);
150 if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
151 title = unsmob_stencil (scm_call_2 (user_title,
152 papers_[0]->self_scm (),
153 scm_variable_ref (s)));
155 title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
156 papers_[0]->self_scm (),
159 title->align_to (Y_AXIS, UP);
165 Paper_book::classic_output (String outname)
167 int count = scores_.size ();
168 Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
169 out->output_header (papers_.top (), scopes (count - 1), 0, true);
171 Paper_line *first = unsmob_paper_line (scm_vector_ref (scores_.top (),
173 Offset o (0, -0.5 * first->dim ()[Y_AXIS]);
174 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
175 for (int i = 0; i < line_count; i++)
177 /* In classic compatibility TeX tracks how large things are, and
178 advances the Y pos for us. If we advance it too, we get too
181 FIXME: vague... why is TeX is different from other ouput
182 backends, why not fix the TeX backend? -- jcn */
183 if (output_format_global == "tex")
186 out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
187 &o, i == line_count - 1);
190 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
191 progress_indication ("\n");
194 /* calculate book height, #lines, stencils. */
198 int score_count = scores_.size ();
200 /* Calculate the full book height. Hmm, can't we cache system
201 heights while making stencils? */
203 for (int i = 0; i < score_count; i++)
205 Stencil *title = this->title (i);
207 height_ += title->extent (Y_AXIS).length ();
209 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
210 for (int j = 0; j < line_count; j++)
212 SCM s = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
213 height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
217 Paper_def *paper = papers_[0];
218 SCM scopes = this->scopes (0);
220 SCM make_tagline = paper->c_variable ("make-tagline");
221 tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
223 if (Stencil *s = unsmob_stencil (tagline_))
224 tag_height = s->extent (Y_AXIS).length ();
225 height_ += tag_height;
227 SCM make_copyright = paper->c_variable ("make-copyright");
228 copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
229 Real copy_height = 0;
230 if (Stencil *s = unsmob_stencil (copyright_))
231 copy_height = s->extent (Y_AXIS).length ();
232 height_ += copy_height;
238 int score_count = scores_.size ();
240 for (int i = 0; i < score_count; i++)
242 if (Stencil *title = this->title (i))
243 lines = ly_snoc (stencil2line (title, true), lines);
244 lines = scm_append (scm_list_2 (lines, scm_vector_to_list (scores_[i])));
248 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
249 unsmob_paper_line (ly_car (s))->number_ = ++i;
257 Page::page_count_ = 0;
258 Paper_def *paper = papers_[0];
259 Page *page = new Page (paper, 1);
261 Real text_height = page->text_height ();
263 Real copy_height = 0;
264 if (Stencil *s = unsmob_stencil (copyright_))
265 copy_height = s->extent (Y_AXIS).length ();
268 if (Stencil *s = unsmob_stencil (tagline_))
269 tag_height = s->extent (Y_AXIS).length ();
272 SCM proc = paper->c_variable ("page-breaking");
273 SCM breaks = scm_apply_0 (proc, scm_list_n (all, scm_make_real (height_),
274 scm_make_real (text_height),
275 scm_make_real (-copy_height),
276 scm_make_real (-tag_height),
279 /* Copyright on first page. */
280 if (unsmob_stencil (copyright_))
281 page->copyright_ = copyright_;
284 int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
286 for (int i = 0; i < page_count; i++)
289 page = new Page (paper, i + 1);
291 int next = i + 1 < page_count
292 ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
293 while ((!next && all != SCM_EOL) || line <= next)
295 SCM s = ly_car (all);
296 page->lines_ = ly_snoc (s, page->lines_);
297 page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
302 pages = scm_cons (page->self_scm (), pages);
305 /* Tagline on last page. */
306 if (unsmob_stencil (tagline_))
307 page->tagline_ = tagline_;
309 return scm_reverse (pages);
313 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
314 Real first, Real last)
317 int page_count = int (book_height / text_height + 0.5);
318 SCM breaks = SCM_EOL;
319 Real page_height = text_height + first;
322 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
324 Paper_line *pl = unsmob_paper_line (ly_car (s));
325 if (!pl->is_title () && h < page_height)
327 h += pl->dim ()[Y_AXIS];
328 if (!pl->is_title () && h > page_height)
330 breaks = ly_snoc (scm_int2num (number), breaks);
332 page_height = text_height + (page_number == page_count) * last;
335 if (ly_cdr (s) == SCM_EOL)
336 breaks = ly_snoc (scm_int2num (pl->number_), breaks);
339 return scm_vector (breaks);
342 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
343 5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
344 "Return a vector with line numbers of page breaks.")
346 SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
347 SCM_ASSERT_TYPE (ly_c_number_p (book), book, SCM_ARG2, __FUNCTION__, "real");
348 SCM_ASSERT_TYPE (ly_c_number_p (text), text, SCM_ARG2, __FUNCTION__, "real");
349 SCM_ASSERT_TYPE (ly_c_number_p (first), first, SCM_ARG2, __FUNCTION__, "real");
350 SCM_ASSERT_TYPE (ly_c_number_p (last), last, SCM_ARG2, __FUNCTION__, "real");
352 return c_ragged_page_breaks (lines,
353 ly_scm2double (book), ly_scm2double (text),
354 ly_scm2double (first), ly_scm2double (last));