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"
11 #include "paper-book.hh"
12 #include "output-def.hh"
13 #include "paper-outputter.hh"
14 #include "paper-line.hh"
15 #include "paper-score.hh"
21 Ugh. the Function of the Paper_book class is unclear. Trim this
27 Paper_book::Paper_book ()
37 Paper_book::~Paper_book ()
41 #include "ly-smobs.icc"
43 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
44 IMPLEMENT_SMOBS (Paper_book)
45 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
48 Paper_book::mark_smob (SCM smob)
50 Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
51 for (int i = 0; i < b->score_lines_.size (); i++)
52 b->score_lines_[i].gc_mark ();
55 scm_gc_mark (b->bookpaper_->self_scm ());
56 scm_gc_mark (b->header_);
57 scm_gc_mark (b->pages_);
62 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
64 Paper_book *b = (Paper_book*) ly_cdr (smob);
66 scm_puts ("#<", port);
67 scm_puts (classname (b), port);
69 //scm_puts (b->, port);
75 split_string (String s, char c)
84 s = s.nomid_string (0, 1);
91 rv.push (s.cut_string (0, i));
92 s = s.nomid_string (0, i);
101 SCM fields = SCM_EOL;
102 for (int i = dump_header_fieldnames_global.size (); i--; )
104 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
109 LY_DEFINE (ly_output_formats, "ly:output-formats",
111 "Formats passed to --format as a list of strings, "
112 "used for the output.")
114 Array<String> output_formats = split_string (output_format_global, ',');
117 for (int i = 0; i < output_formats.size (); i ++)
118 lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst);
124 TODO: there is too much code dup, and the interface is not
128 Paper_book::output (String outname)
130 if (!score_lines_.size ())
134 /* Generate all stencils to trigger font loads. */
138 SCM formats = ly_output_formats();
139 for (SCM s = formats; ly_c_pair_p (s); s = ly_cdr (s))
141 String format = ly_scm2string (ly_car (s));
143 Paper_outputter *out = get_paper_outputter (outname + "." + format, format);
145 SCM scopes = SCM_EOL;
146 if (ly_c_module_p (header_))
147 scopes = scm_cons (header_, scopes);
149 String func_nm = format;
150 func_nm = "output-framework-" + func_nm;
152 SCM func = ly_scheme_function (func_nm.to_str0 ());
153 scm_apply_0 (func, scm_list_n (out->self_scm (),
157 scm_makfrom0str (outname.to_str0 ()),
161 scm_gc_unprotect_object (out->self_scm ());
167 Paper_book::classic_output (String outname)
170 /* Generate all stencils to trigger font loads. */
175 SCM scopes = SCM_EOL;
176 if (ly_c_module_p (header_))
177 scopes = scm_cons (header_, scopes);
179 if (ly_c_module_p (score_lines_[0].header_))
180 scopes = scm_cons (score_lines_[0].header_, scopes);
184 Array<String> output_formats = split_string (output_format_global, ',');
186 for (int i = 0; i < output_formats.size (); i++)
188 String format = output_formats[i];
189 String func_nm = format;
190 func_nm = "output-classic-framework-" + func_nm;
192 SCM func = ly_scheme_function (func_nm.to_str0 ());
194 Paper_outputter *out = get_paper_outputter (outname + "." + format, format);
196 scm_apply_0 (func, scm_list_n (out->self_scm (),
200 scm_makfrom0str (outname.to_str0 ()),
204 scm_gc_unprotect_object (out->self_scm ());
207 progress_indication ("\n");
213 LY_DEFINE(ly_paper_book_pages, "ly:paper-book-pages",
216 "Return pages in book PB.")
218 return unsmob_paper_book(pb)->pages ();
222 LY_DEFINE(ly_paper_book_scopes, "ly:paper-book-scopes",
225 "Return pages in paper book @var{book}.")
227 Paper_book * pb = unsmob_paper_book(book);
228 SCM_ASSERT_TYPE(pb, book, SCM_ARG1, __FUNCTION__, "Paper_book");
230 SCM scopes = SCM_EOL;
231 if (ly_c_module_p (pb->header_))
232 scopes = scm_cons (pb->header_, scopes);
238 LY_DEFINE(ly_paper_book_lines, "ly:paper-book-lines",
241 "Return lines in book PB.")
243 return unsmob_paper_book (pb)->lines ();
247 LY_DEFINE(ly_paper_book_book_paper, "ly:paper-book-book-paper",
250 "Return pages in book PB.")
252 return unsmob_paper_book(pb)->bookpaper_->self_scm ();
257 TODO: resurrect more complex user-tweaks for titling .
261 Paper_book::book_title ()
263 SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
266 SCM scopes = SCM_EOL;
267 if (ly_c_module_p (header_))
268 scopes = scm_cons (header_, scopes);
272 if (ly_c_procedure_p (title_func))
273 tit = scm_call_2 (title_func,
274 bookpaper_->self_scm (),
277 if (unsmob_stencil (tit))
278 title = *unsmob_stencil (tit);
280 if (!title.is_empty ())
281 title.align_to (Y_AXIS, UP);
289 Paper_book::score_title (int i)
291 SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
296 SCM scopes = SCM_EOL;
297 if (ly_c_module_p (header_))
298 scopes = scm_cons (header_, scopes);
300 if (ly_c_module_p (score_lines_[i].header_))
301 scopes = scm_cons (score_lines_[i].header_, scopes);
305 if (ly_c_procedure_p (title_func))
306 tit = scm_call_2 (title_func,
307 bookpaper_->self_scm (),
310 if (unsmob_stencil (tit))
311 title = *unsmob_stencil (tit);
314 if (!title.is_empty ())
315 title.align_to (Y_AXIS, UP);
324 if (SCM_BOOL_F != lines_)
328 Stencil title = book_title ();
330 if (!title.is_empty ())
332 Paper_line *pl = new Paper_line (title, true);
334 lines_ = scm_cons (pl->self_scm (), lines_);
335 scm_gc_unprotect_object (pl->self_scm ());
338 int score_count = score_lines_.size ();
339 for (int i = 0; i < score_count; i++)
341 Stencil title = score_title (i);
342 if (!title.is_empty ())
344 Paper_line *pl = new Paper_line (title, true);
345 lines_ = scm_cons (pl->self_scm (), lines_);
346 scm_gc_unprotect_object (pl->self_scm ());
349 if (scm_vector_p (score_lines_[i].lines_) == SCM_BOOL_T)
351 SCM line_list = scm_vector_to_list (score_lines_[i].lines_); // guh.
353 line_list = scm_reverse (line_list);
354 lines_ = scm_append (scm_list_2 (line_list, lines_));
358 lines_ = scm_reverse (lines_);
361 Paper_line * last = 0;
362 for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
364 Paper_line * p = unsmob_paper_line (ly_car (s));
367 if (last && last->is_title ())
369 p->penalty_ = 10000; // ugh, hardcoded.
382 if (SCM_BOOL_F != pages_)
387 Output_def *paper = bookpaper_;
389 SCM proc = paper->c_variable ("page-breaking");
390 pages_ = scm_apply_0 (proc, scm_list_n (lines (),
403 c_ragged_page_breaks (SCM lines,
406 Real first, Real last)
410 Real book_height =0.;
411 for (SCM s = lines ; ly_c_pair_p (s); s = ly_cdr (s))
413 book_height += unsmob_paper_line (ly_car (s))->dim ()[Y_AXIS];
416 int page_count = int (book_height / text_height + 0.5); // ceil?
417 SCM breaks = SCM_EOL;
418 Real page_height = text_height + first;
421 for (SCM s = lines; ly_c_pair_p (s); s = ly_cdr (s))
423 Paper_line *pl = unsmob_paper_line (ly_car (s));
424 if (!pl->is_title () && h < page_height)
426 h += pl->dim ()[Y_AXIS];
427 if (!pl->is_title () && h > page_height)
429 breaks = ly_snoc (scm_int2num (number), breaks);
431 page_height = text_height + (page_number == page_count) * last;
434 if (ly_cdr (s) == SCM_EOL)
435 breaks = ly_snoc (scm_int2num (pl->number_), breaks);
438 return scm_vector (breaks);
441 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
442 5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
443 "Return a vector with line numbers of page breaks.")
445 Paper_book* b = unsmob_paper_book (book);
447 SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
448 SCM_ASSERT_TYPE (b, book, SCM_ARG2, __FUNCTION__, "Paper_book");
449 SCM_ASSERT_TYPE (ly_c_number_p (text), text, SCM_ARG3, __FUNCTION__, "number");
450 SCM_ASSERT_TYPE (ly_c_number_p (first), first, SCM_ARG4, __FUNCTION__, "number");
451 SCM_ASSERT_TYPE (ly_c_number_p (last), last, SCM_ARG5, __FUNCTION__, "number");
453 return c_ragged_page_breaks (lines, b,
454 ly_scm2double (text),
455 ly_scm2double (first), ly_scm2double (last));
460 /****************************************************************/
462 Score_lines::Score_lines ()
469 Score_lines::gc_mark ()
471 scm_gc_mark (lines_);
472 scm_gc_mark (header_);