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"
22 Ugh. the Function of the Paper_book class is unclear. Trim this
28 Paper_book::Paper_book ()
40 Paper_book::~Paper_book ()
44 #include "ly-smobs.icc"
46 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
47 IMPLEMENT_SMOBS (Paper_book)
48 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
51 Paper_book::mark_smob (SCM smob)
53 Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
54 for (int i = 0; i < b->score_lines_.size (); i++)
55 b->score_lines_[i].gc_mark ();
57 scm_gc_mark (b->copyright_);
59 scm_gc_mark (b->bookpaper_->self_scm ());
60 scm_gc_mark (b->header_);
61 scm_gc_mark (b->pages_);
62 scm_gc_mark (b->lines_);
67 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
69 Paper_book *b = (Paper_book*) ly_cdr (smob);
71 scm_puts ("#<", port);
72 scm_puts (classname (b), port);
74 //scm_puts (b->, port);
80 split_string (String s, char c)
89 s = s.nomid_string (0, 1);
96 rv.push (s.cut_string (0, i));
97 s = s.nomid_string (0, i);
106 SCM fields = SCM_EOL;
107 for (int i = dump_header_fieldnames_global.size (); i--; )
109 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
115 TODO: there is too much code dup, and the interface is not
119 Paper_book::output (String outname)
121 if (!score_lines_.size ())
124 /* Generate all stencils to trigger font loads. */
127 Array<String> output_formats = split_string (output_format_global, ',');
129 for (int i = 0; i < output_formats.size (); i++)
131 String format = output_formats[i];
132 Paper_outputter *out = get_paper_outputter (outname + "." + output_formats[i], format);
137 SCM scopes = SCM_EOL;
138 if (ly_c_module_p (header_))
139 scopes = scm_cons (header_, scopes);
141 String func_nm = format;
142 func_nm = "output-framework-" + func_nm;
144 SCM func = ly_scheme_function (func_nm.to_str0 ());
145 scm_apply_0 (func, scm_list_n (out->self_scm (),
149 scm_makfrom0str (outname.to_str0 ()),
153 scm_gc_unprotect_object (out->self_scm ());
159 Paper_book::classic_output (String outname)
162 /* Generate all stencils to trigger font loads. */
167 SCM scopes = SCM_EOL;
168 if (ly_c_module_p (header_))
169 scopes = scm_cons (header_, scopes);
171 if (ly_c_module_p (score_lines_[0].header_))
172 scopes = scm_cons (score_lines_[0].header_, scopes);
176 Array<String> output_formats = split_string (output_format_global, ',');
178 for (int i = 0; i < output_formats.size (); i++)
180 String format = output_formats[i];
181 String func_nm = format;
182 func_nm = "output-classic-framework-" + func_nm;
184 SCM func = ly_scheme_function (func_nm.to_str0 ());
186 Paper_outputter *out = get_paper_outputter (outname + "." + format, format);
188 scm_apply_0 (func, scm_list_n (out->self_scm (),
192 scm_makfrom0str (outname.to_str0 ()),
196 scm_gc_unprotect_object (out->self_scm ());
199 progress_indication ("\n");
205 LY_DEFINE(ly_paper_book_pages, "ly:paper-book-pages",
208 "Return pages in book PB.")
210 return unsmob_paper_book(pb)->pages ();
214 LY_DEFINE(ly_paper_book_lines, "ly:paper-book-lines",
217 "Return lines in book PB.")
219 return unsmob_paper_book (pb)->lines ();
223 LY_DEFINE(ly_paper_book_book_paper, "ly:paper-book-book-paper",
226 "Return pages in book PB.")
228 return unsmob_paper_book(pb)->bookpaper_->self_scm ();
233 TODO: resurrect more complex user-tweaks for titling .
237 Paper_book::book_title ()
239 SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
242 SCM scopes = SCM_EOL;
243 if (ly_c_module_p (header_))
244 scopes = scm_cons (header_, scopes);
248 if (ly_c_procedure_p (title_func))
249 tit = scm_call_2 (title_func,
250 bookpaper_->self_scm (),
253 if (unsmob_stencil (tit))
254 title = *unsmob_stencil (tit);
256 if (!title.is_empty ())
257 title.align_to (Y_AXIS, UP);
265 Paper_book::score_title (int i)
267 SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
272 SCM scopes = SCM_EOL;
273 if (ly_c_module_p (header_))
274 scopes = scm_cons (header_, scopes);
276 if (ly_c_module_p (score_lines_[i].header_))
277 scopes = scm_cons (score_lines_[i].header_, scopes);
281 if (ly_c_procedure_p (title_func))
282 tit =scm_call_2 (title_func,
283 bookpaper_->self_scm (),
286 if (unsmob_stencil (tit))
287 title = *unsmob_stencil (tit);
290 if (!title.is_empty ())
291 title.align_to (Y_AXIS, UP);
301 if (ly_c_pair_p (lines_))
304 Stencil title = book_title ();
305 if (!title.is_empty ())
307 Paper_line *pl = new Paper_line (title, -10001, true);
309 lines_ = scm_cons (pl->self_scm (), lines_);
310 scm_gc_unprotect_object (pl->self_scm ());
313 int score_count = score_lines_.size ();
314 for (int i = 0; i < score_count; i++)
316 Stencil title = score_title (i);
317 if (!title.is_empty ())
319 Paper_line *pl = new Paper_line (title, -10001, true);
320 lines_ = scm_cons (pl->self_scm (), lines_);
321 scm_gc_unprotect_object (pl->self_scm ());
324 if (scm_vector_p (score_lines_[i].lines_) == SCM_BOOL_T)
326 SCM line_list = scm_vector_to_list (score_lines_[i].lines_); // guh.
327 lines_ = scm_append (scm_list_2 (scm_reverse (line_list), lines_));
331 lines_ = scm_reverse (lines_);
334 for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
335 unsmob_paper_line (ly_car (s))->number_ = ++i;
341 make_tagline (Output_def*paper, SCM scopes)
343 SCM make_tagline = paper->c_variable ("make-tagline");
344 SCM tagline = scm_call_2 (make_tagline, paper->self_scm (), scopes);
349 make_copyright (Output_def *paper, SCM scopes)
351 SCM make_copyright = paper->c_variable ("make-copyright");
352 SCM copyright = scm_call_2 (make_copyright, paper->self_scm (), scopes);
359 if (ly_c_pair_p (pages_))
362 Output_def *paper = bookpaper_;
363 Page *page = new Page (paper, 1);
365 Real text_height = page->text_height ();
367 Real copy_height = 0;
368 if (Stencil *s = unsmob_stencil (copyright_))
369 copy_height = s->extent (Y_AXIS).length ();
372 if (Stencil *s = unsmob_stencil (tagline_))
373 tag_height = s->extent (Y_AXIS).length ();
376 SCM proc = paper->c_variable ("page-breaking");
377 SCM breaks = scm_apply_0 (proc, scm_list_n (all,
379 scm_make_real (text_height),
380 scm_make_real (-copy_height),
381 scm_make_real (-tag_height),
386 UGH - move this out of C++.
388 SCM scopes = SCM_EOL;
389 if (ly_c_module_p (header_))
390 scopes = scm_cons (header_, scopes);
392 tagline_ = make_tagline (bookpaper_, scopes);
393 copyright_ = make_tagline (bookpaper_, scopes);
395 int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
398 for (int i = 0; i < page_count; i++)
401 page = new Page (paper, i + 1);
403 int next = i + 1 < page_count
404 ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
405 while ((!next && all != SCM_EOL) || line <= next)
407 SCM s = ly_car (all);
408 page->lines_ = ly_snoc (s, page->lines_);
409 page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
414 if (i == page_count-1)
415 page->is_last_ = true;
417 pages_ = scm_cons (page->self_scm (), pages_);
420 pages_ = scm_reverse (pages_);
427 c_ragged_page_breaks (SCM lines,
430 Real first, Real last)
434 Real book_height =0.;
435 for (SCM s = lines ; ly_c_pair_p (s); s = ly_cdr (s))
437 book_height += unsmob_paper_line (ly_car (s))->dim ()[Y_AXIS];
441 UGH. following stuff should go out of C++.
443 SCM scopes = SCM_EOL;
444 if (ly_c_module_p (book->header_))
445 scopes = scm_cons (book->header_, scopes);
448 SCM tag = make_tagline (book->bookpaper_, scopes);
449 if (unsmob_stencil (tag))
451 book_height += unsmob_stencil (tag)->extent (Y_AXIS).length ();
454 SCM cr = make_copyright (book->bookpaper_, scopes);
455 if (unsmob_stencil (cr))
457 book_height += unsmob_stencil (cr)->extent (Y_AXIS).length ();
460 int page_count = int (book_height / text_height + 0.5); // ceil?
461 SCM breaks = SCM_EOL;
462 Real page_height = text_height + first;
465 for (SCM s = lines; ly_c_pair_p (s); s = ly_cdr (s))
467 Paper_line *pl = unsmob_paper_line (ly_car (s));
468 if (!pl->is_title () && h < page_height)
470 h += pl->dim ()[Y_AXIS];
471 if (!pl->is_title () && h > page_height)
473 breaks = ly_snoc (scm_int2num (number), breaks);
475 page_height = text_height + (page_number == page_count) * last;
478 if (ly_cdr (s) == SCM_EOL)
479 breaks = ly_snoc (scm_int2num (pl->number_), breaks);
482 return scm_vector (breaks);
485 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
486 5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
487 "Return a vector with line numbers of page breaks.")
489 Paper_book* b = unsmob_paper_book (book);
491 SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
492 SCM_ASSERT_TYPE (b, book, SCM_ARG2, __FUNCTION__, "Paper_book");
493 SCM_ASSERT_TYPE (ly_c_number_p (text), text, SCM_ARG3, __FUNCTION__, "number");
494 SCM_ASSERT_TYPE (ly_c_number_p (first), first, SCM_ARG4, __FUNCTION__, "number");
495 SCM_ASSERT_TYPE (ly_c_number_p (last), last, SCM_ARG5, __FUNCTION__, "number");
497 return c_ragged_page_breaks (lines, b,
498 ly_scm2double (text),
499 ly_scm2double (first), ly_scm2double (last));
502 /****************************************************************/
504 Score_lines::Score_lines ()
511 Score_lines::gc_mark ()
513 scm_gc_mark (lines_);
514 scm_gc_mark (header_);