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
29 stencil2line (Stencil stil, bool is_title = false)
33 z = scm_permanent_object (ly_offset2scm (Offset (0, 0)));
34 Offset dim = Offset (stil.extent (X_AXIS).length (),
35 stil.extent (Y_AXIS).length ());
36 Paper_line *pl = new Paper_line (dim, scm_cons (stil.smobbed_copy (),
38 -10001 * is_title, is_title);
40 return scm_gc_unprotect_object (pl->self_scm ());
44 Paper_book::Paper_book ()
56 Paper_book::~Paper_book ()
60 #include "ly-smobs.icc"
62 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
63 IMPLEMENT_SMOBS (Paper_book)
64 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
67 Paper_book::mark_smob (SCM smob)
69 Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
70 for (int i = 0; i < b->score_lines_.size (); i++)
71 b->score_lines_[i].gc_mark ();
73 scm_gc_mark (b->copyright_);
75 scm_gc_mark (b->bookpaper_->self_scm ());
76 scm_gc_mark (b->header_);
77 scm_gc_mark (b->pages_);
78 scm_gc_mark (b->lines_);
83 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
85 Paper_book *b = (Paper_book*) ly_cdr (smob);
87 scm_puts ("#<", port);
88 scm_puts (classname (b), port);
90 //scm_puts (b->, port);
96 split_string (String s, char c)
105 s = s.nomid_string (0, 1);
112 rv.push (s.cut_string (0, i));
113 s = s.nomid_string (0, i);
122 SCM fields = SCM_EOL;
123 for (int i = dump_header_fieldnames_global.size (); i--; )
125 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
131 TODO: there is too much code dup, and the interface is not
135 Paper_book::output (String outname)
137 if (!score_lines_.size ())
140 /* Generate all stencils to trigger font loads. */
143 Array<String> output_formats = split_string (output_format_global, ',');
145 for (int i = 0; i < output_formats.size (); i++)
147 String format = output_formats[i];
148 Paper_outputter *out = get_paper_outputter (outname + "." + output_formats[i], format);
153 SCM scopes = SCM_EOL;
154 if (ly_c_module_p (header_))
155 scopes = scm_cons (header_, scopes);
157 String func_nm = format;
158 func_nm = "output-framework-" + func_nm;
160 SCM func = ly_scheme_function (func_nm.to_str0 ());
161 scm_apply_0 (func, scm_list_n (out->self_scm (),
165 scm_makfrom0str (outname.to_str0 ()),
169 scm_gc_unprotect_object (out->self_scm ());
175 Paper_book::classic_output (String outname)
178 /* Generate all stencils to trigger font loads. */
183 SCM scopes = SCM_EOL;
184 if (ly_c_module_p (header_))
185 scopes = scm_cons (header_, scopes);
187 if (ly_c_module_p (score_lines_[0].header_))
188 scopes = scm_cons (score_lines_[0].header_, scopes);
192 Array<String> output_formats = split_string (output_format_global, ',');
194 for (int i = 0; i < output_formats.size (); i++)
196 String format = output_formats[i];
197 String func_nm = format;
198 func_nm = "output-classic-framework-" + func_nm;
200 SCM func = ly_scheme_function (func_nm.to_str0 ());
202 Paper_outputter *out = get_paper_outputter (outname + "." + format, format);
204 scm_apply_0 (func, scm_list_n (out->self_scm (),
208 scm_makfrom0str (outname.to_str0 ()),
212 scm_gc_unprotect_object (out->self_scm ());
215 progress_indication ("\n");
221 LY_DEFINE(ly_paper_book_pages, "ly:paper-book-pages",
224 "Return pages in book PB.")
226 return unsmob_paper_book(pb)->pages ();
230 LY_DEFINE(ly_paper_book_lines, "ly:paper-book-lines",
233 "Return lines in book PB.")
235 return unsmob_paper_book (pb)->lines ();
239 LY_DEFINE(ly_paper_book_book_paper, "ly:paper-book-book-paper",
242 "Return pages in book PB.")
244 return unsmob_paper_book(pb)->bookpaper_->self_scm ();
249 TODO: resurrect more complex user-tweaks for titling .
253 Paper_book::book_title ()
255 SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
258 SCM scopes = SCM_EOL;
259 if (ly_c_module_p (header_))
260 scopes = scm_cons (header_, scopes);
264 if (ly_c_procedure_p (title_func))
265 tit = scm_call_2 (title_func,
266 bookpaper_->self_scm (),
269 if (unsmob_stencil (tit))
270 title = *unsmob_stencil (tit);
272 if (!title.is_empty ())
273 title.align_to (Y_AXIS, UP);
281 Paper_book::score_title (int i)
283 SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
288 SCM scopes = SCM_EOL;
289 if (ly_c_module_p (header_))
290 scopes = scm_cons (header_, scopes);
292 if (ly_c_module_p (score_lines_[i].header_))
293 scopes = scm_cons (score_lines_[i].header_, scopes);
297 if (ly_c_procedure_p (title_func))
298 tit =scm_call_2 (title_func,
299 bookpaper_->self_scm (),
302 if (unsmob_stencil (tit))
303 title = *unsmob_stencil (tit);
306 if (!title.is_empty ())
307 title.align_to (Y_AXIS, UP);
317 if (ly_c_pair_p (lines_))
320 Stencil title = book_title ();
321 if (!title.is_empty ())
322 lines_ = scm_cons (stencil2line (title, true), lines_);
324 int score_count = score_lines_.size ();
325 for (int i = 0; i < score_count; i++)
327 Stencil title = score_title (i);
328 if (!title.is_empty ())
329 lines_ = scm_cons (stencil2line (title, true), lines_);
331 if (scm_vector_p (score_lines_[i].lines_) == SCM_BOOL_T)
333 SCM line_list = scm_vector_to_list (score_lines_[i].lines_); // guh.
334 lines_ = scm_append (scm_list_2 (scm_reverse (line_list), lines_));
338 lines_ = scm_reverse (lines_);
341 for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
342 unsmob_paper_line (ly_car (s))->number_ = ++i;
348 make_tagline (Output_def*paper, SCM scopes)
350 SCM make_tagline = paper->c_variable ("make-tagline");
351 SCM tagline = scm_call_2 (make_tagline, paper->self_scm (), scopes);
356 make_copyright (Output_def *paper, SCM scopes)
358 SCM make_copyright = paper->c_variable ("make-copyright");
359 SCM copyright = scm_call_2 (make_copyright, paper->self_scm (), scopes);
366 if (ly_c_pair_p (pages_))
369 Output_def *paper = bookpaper_;
370 Page *page = new Page (paper, 1);
372 Real text_height = page->text_height ();
374 Real copy_height = 0;
375 if (Stencil *s = unsmob_stencil (copyright_))
376 copy_height = s->extent (Y_AXIS).length ();
379 if (Stencil *s = unsmob_stencil (tagline_))
380 tag_height = s->extent (Y_AXIS).length ();
383 SCM proc = paper->c_variable ("page-breaking");
384 SCM breaks = scm_apply_0 (proc, scm_list_n (all,
386 scm_make_real (text_height),
387 scm_make_real (-copy_height),
388 scm_make_real (-tag_height),
393 UGH - move this out of C++.
395 SCM scopes = SCM_EOL;
396 if (ly_c_module_p (header_))
397 scopes = scm_cons (header_, scopes);
399 tagline_ = make_tagline (bookpaper_, scopes);
400 copyright_ = make_tagline (bookpaper_, scopes);
402 int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
405 for (int i = 0; i < page_count; i++)
408 page = new Page (paper, i + 1);
410 int next = i + 1 < page_count
411 ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
412 while ((!next && all != SCM_EOL) || line <= next)
414 SCM s = ly_car (all);
415 page->lines_ = ly_snoc (s, page->lines_);
416 page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
421 if (i == page_count-1)
422 page->is_last_ = true;
424 pages_ = scm_cons (page->self_scm (), pages_);
427 pages_ = scm_reverse (pages_);
434 c_ragged_page_breaks (SCM lines,
437 Real first, Real last)
441 Real book_height =0.;
442 for (SCM s = lines ; ly_c_pair_p (s); s = ly_cdr (s))
444 book_height += unsmob_paper_line (ly_car (s))->dim ()[Y_AXIS];
448 UGH. following stuff should go out of C++.
450 SCM scopes = SCM_EOL;
451 if (ly_c_module_p (book->header_))
452 scopes = scm_cons (book->header_, scopes);
455 SCM tag = make_tagline (book->bookpaper_, scopes);
456 if (unsmob_stencil (tag))
458 book_height += unsmob_stencil (tag)->extent (Y_AXIS).length ();
461 SCM cr = make_copyright (book->bookpaper_, scopes);
462 if (unsmob_stencil (cr))
464 book_height += unsmob_stencil (cr)->extent (Y_AXIS).length ();
467 int page_count = int (book_height / text_height + 0.5); // ceil?
468 SCM breaks = SCM_EOL;
469 Real page_height = text_height + first;
472 for (SCM s = lines; ly_c_pair_p (s); s = ly_cdr (s))
474 Paper_line *pl = unsmob_paper_line (ly_car (s));
475 if (!pl->is_title () && h < page_height)
477 h += pl->dim ()[Y_AXIS];
478 if (!pl->is_title () && h > page_height)
480 breaks = ly_snoc (scm_int2num (number), breaks);
482 page_height = text_height + (page_number == page_count) * last;
485 if (ly_cdr (s) == SCM_EOL)
486 breaks = ly_snoc (scm_int2num (pl->number_), breaks);
489 return scm_vector (breaks);
492 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
493 5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
494 "Return a vector with line numbers of page breaks.")
496 Paper_book* b = unsmob_paper_book (book);
498 SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
499 SCM_ASSERT_TYPE (b, book, SCM_ARG2, __FUNCTION__, "Paper_book");
500 SCM_ASSERT_TYPE (ly_c_number_p (text), text, SCM_ARG3, __FUNCTION__, "number");
501 SCM_ASSERT_TYPE (ly_c_number_p (first), first, SCM_ARG4, __FUNCTION__, "number");
502 SCM_ASSERT_TYPE (ly_c_number_p (last), last, SCM_ARG5, __FUNCTION__, "number");
504 return c_ragged_page_breaks (lines, b,
505 ly_scm2double (text),
506 ly_scm2double (first), ly_scm2double (last));
509 /****************************************************************/
511 Score_lines::Score_lines ()
518 Score_lines::gc_mark ()
520 scm_gc_mark (lines_);
521 scm_gc_mark (header_);