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 "output-def.hh"
14 #include "paper-outputter.hh"
15 #include "paper-line.hh"
16 #include "paper-score.hh"
23 stencil2line (Stencil stil, bool is_title = false)
27 z = scm_permanent_object (ly_offset2scm (Offset (0, 0)));
28 Offset dim = Offset (stil.extent (X_AXIS).length (),
29 stil.extent (Y_AXIS).length ());
30 Paper_line *pl = new Paper_line (dim, scm_cons (stil.smobbed_copy (),
32 -10001 * is_title, is_title);
34 return scm_gc_unprotect_object (pl->self_scm ());
38 Paper_book::Paper_book ()
50 Paper_book::~Paper_book ()
54 #include "ly-smobs.icc"
56 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
57 IMPLEMENT_SMOBS (Paper_book)
58 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
61 Paper_book::mark_smob (SCM smob)
63 Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
64 for (int i = 0; i < b->score_lines_.size (); i++)
65 b->score_lines_[i].gc_mark ();
67 scm_gc_mark (b->copyright_);
69 scm_gc_mark (b->bookpaper_->self_scm ());
70 scm_gc_mark (b->header_);
71 scm_gc_mark (b->pages_);
72 scm_gc_mark (b->lines_);
77 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
79 Paper_book *b = (Paper_book*) ly_cdr (smob);
81 scm_puts ("#<", port);
82 scm_puts (classname (b), port);
84 //scm_puts (b->, port);
90 split_string (String s, char c)
99 s = s.nomid_string (0, 1);
106 rv.push (s.cut_string (0, i));
107 s = s.nomid_string (0, i);
116 SCM fields = SCM_EOL;
117 for (int i = dump_header_fieldnames_global.size (); i--; )
119 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
125 TODO: there is too much code dup, and the interface is not
129 Paper_book::output (String outname)
131 if (!score_lines_.size ())
132 // FIXME: no end-output?
135 /* Generate all stencils to trigger font loads. */
138 Array<String> output_formats = split_string (output_format_global, ',');
140 for (int i = 0; i < output_formats.size (); i++)
142 String format = output_formats[i];
143 Paper_outputter *out = get_paper_outputter (outname + "." + output_formats[i], format);
148 SCM scopes = SCM_EOL;
149 if (ly_c_module_p (header_))
150 scopes = scm_cons (header_, scopes);
152 String func_nm = output_format_global;
153 func_nm = "output-framework-" + func_nm;
155 SCM func = ly_scheme_function (func_nm.to_str0 ());
156 scm_apply_0 (func, scm_list_n (out->self_scm (),
160 scm_makfrom0str (outname.to_str0 ()),
164 scm_gc_unprotect_object (out->self_scm ());
170 Paper_book::classic_output (String outname)
172 String format = "tex";
173 Paper_outputter *out = get_paper_outputter (outname + "." + format, format);
175 /* Generate all stencils to trigger font loads. */
180 SCM scopes = SCM_EOL;
181 if (ly_c_module_p (header_))
182 scopes = scm_cons (header_, scopes);
184 if (ly_c_module_p (score_lines_[0].header_))
185 scopes = scm_cons (score_lines_[0].header_, scopes);
188 String func_nm = output_format_global;
189 func_nm = "output-classic-framework-" + func_nm;
191 SCM func = ly_scheme_function (func_nm.to_str0 ());
192 scm_apply_0 (func, scm_list_n (out->self_scm (),
196 scm_makfrom0str (outname.to_str0 ()),
200 progress_indication ("\n");
206 LY_DEFINE(ly_paper_book_pages, "ly:paper-book-pages",
209 "Return pages in book PB.")
211 return unsmob_paper_book(pb)->pages ();
215 LY_DEFINE(ly_paper_book_lines, "ly:paper-book-lines",
218 "Return lines in book PB.")
220 return unsmob_paper_book (pb)->lines ();
224 LY_DEFINE(ly_paper_book_book_paper, "ly:paper-book-book-paper",
227 "Return pages in book PB.")
229 return unsmob_paper_book(pb)->bookpaper_->self_scm ();
234 TODO: resurrect more complex user-tweaks for titling .
238 Paper_book::book_title ()
240 SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
243 SCM scopes = SCM_EOL;
244 if (ly_c_module_p (header_))
245 scopes = scm_cons (header_, scopes);
249 if (ly_c_procedure_p (title_func))
250 tit = scm_call_2 (title_func,
251 bookpaper_->self_scm (),
254 if (unsmob_stencil (tit))
255 title = *unsmob_stencil (tit);
257 if (!title.is_empty ())
258 title.align_to (Y_AXIS, UP);
266 Paper_book::score_title (int i)
268 SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
273 SCM scopes = SCM_EOL;
274 if (ly_c_module_p (header_))
275 scopes = scm_cons (header_, scopes);
277 if (ly_c_module_p (score_lines_[i].header_))
278 scopes = scm_cons (score_lines_[i].header_, scopes);
282 if (ly_c_procedure_p (title_func))
283 tit =scm_call_2 (title_func,
284 bookpaper_->self_scm (),
287 if (unsmob_stencil (tit))
288 title = *unsmob_stencil (tit);
291 if (!title.is_empty ())
292 title.align_to (Y_AXIS, UP);
300 /* calculate book height, #lines, stencils. */
304 int score_count = score_lines_.size ();
306 /* Calculate the full book height. Hmm, can't we cache system
307 heights while making stencils? */
309 Stencil btitle = book_title ();
310 if (!btitle.is_empty ())
311 height_ += btitle.extent (Y_AXIS).length ();
313 for (int i = 0; i < score_count; i++)
315 Stencil title = score_title (i);
316 if (!title.is_empty ())
317 height_ += title.extent (Y_AXIS).length ();
319 int line_count = SCM_VECTOR_LENGTH (score_lines_[i].lines_);
320 for (int j = 0; j < line_count; j++)
322 SCM s = scm_vector_ref ((SCM) score_lines_[i].lines_, scm_int2num (j));
323 height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
327 Output_def *paper = bookpaper_;
329 SCM scopes = SCM_EOL;
330 if (ly_c_module_p (header_))
331 scopes = scm_cons (header_, scopes);
333 SCM make_tagline = paper->c_variable ("make-tagline");
334 tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
336 if (Stencil *s = unsmob_stencil (tagline_))
337 tag_height = s->extent (Y_AXIS).length ();
338 height_ += tag_height;
340 SCM make_copyright = paper->c_variable ("make-copyright");
341 copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
342 Real copy_height = 0;
343 if (Stencil *s = unsmob_stencil (copyright_))
344 copy_height = s->extent (Y_AXIS).length ();
345 height_ += copy_height;
351 if (ly_c_pair_p (lines_))
354 Stencil title = book_title ();
355 if (!title.is_empty ())
356 lines_ = scm_cons (stencil2line (title, true), lines_);
358 int score_count = score_lines_.size ();
359 for (int i = 0; i < score_count; i++)
361 Stencil title = score_title (i);
362 if (!title.is_empty ())
363 lines_ = scm_cons (stencil2line (title, true), lines_);
365 SCM line_list = scm_vector_to_list (score_lines_[i].lines_); // guh.
366 lines_ = scm_append (scm_list_2 (scm_reverse (line_list), lines_));
369 lines_ = scm_reverse (lines_);
372 for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
373 unsmob_paper_line (ly_car (s))->number_ = ++i;
380 if (ly_c_pair_p (pages_))
384 Output_def *paper = bookpaper_;
385 Page *page = new Page (paper, 1);
387 Real text_height = page->text_height ();
389 Real copy_height = 0;
390 if (Stencil *s = unsmob_stencil (copyright_))
391 copy_height = s->extent (Y_AXIS).length ();
394 if (Stencil *s = unsmob_stencil (tagline_))
395 tag_height = s->extent (Y_AXIS).length ();
398 SCM proc = paper->c_variable ("page-breaking");
399 SCM breaks = scm_apply_0 (proc, scm_list_n (all, scm_make_real (height_),
400 scm_make_real (text_height),
401 scm_make_real (-copy_height),
402 scm_make_real (-tag_height),
405 /* Copyright on first page. */
406 if (unsmob_stencil (copyright_))
407 page->copyright_ = copyright_;
409 int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
412 for (int i = 0; i < page_count; i++)
415 page = new Page (paper, i + 1);
417 int next = i + 1 < page_count
418 ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
419 while ((!next && all != SCM_EOL) || line <= next)
421 SCM s = ly_car (all);
422 page->lines_ = ly_snoc (s, page->lines_);
423 page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
428 if (i == page_count-1)
429 page->is_last_ = true;
431 pages_ = scm_cons (page->self_scm (), pages_);
434 /* Tagline on last page. */
435 if (unsmob_stencil (tagline_))
436 page->tagline_ = tagline_;
438 pages_ = scm_reverse (pages_);
443 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
444 Real first, Real last)
447 int page_count = int (book_height / text_height + 0.5);
448 SCM breaks = SCM_EOL;
449 Real page_height = text_height + first;
452 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
454 Paper_line *pl = unsmob_paper_line (ly_car (s));
455 if (!pl->is_title () && h < page_height)
457 h += pl->dim ()[Y_AXIS];
458 if (!pl->is_title () && h > page_height)
460 breaks = ly_snoc (scm_int2num (number), breaks);
462 page_height = text_height + (page_number == page_count) * last;
465 if (ly_cdr (s) == SCM_EOL)
466 breaks = ly_snoc (scm_int2num (pl->number_), breaks);
469 return scm_vector (breaks);
472 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
473 5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
474 "Return a vector with line numbers of page breaks.")
476 SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
477 SCM_ASSERT_TYPE (ly_c_number_p (book), book, SCM_ARG2, __FUNCTION__, "real");
478 SCM_ASSERT_TYPE (ly_c_number_p (text), text, SCM_ARG2, __FUNCTION__, "real");
479 SCM_ASSERT_TYPE (ly_c_number_p (first), first, SCM_ARG2, __FUNCTION__, "real");
480 SCM_ASSERT_TYPE (ly_c_number_p (last), last, SCM_ARG2, __FUNCTION__, "real");
482 return c_ragged_page_breaks (lines,
483 ly_scm2double (book), ly_scm2double (text),
484 ly_scm2double (first), ly_scm2double (last));
487 /****************************************************************/
489 Score_lines::Score_lines ()
496 Score_lines::gc_mark ()
498 scm_gc_mark (lines_);
499 scm_gc_mark (header_);