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 ();
233 Paper_book::title (int i)
235 SCM user_title = bookpaper_->lookup_variable (ly_symbol2scm ("user-title"));
236 SCM book_title = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
237 SCM score_title = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
238 SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
239 : ly_symbol2scm ("scoreTitle"));
244 SCM scopes = SCM_EOL;
245 if (ly_c_module_p (header_))
246 scopes = scm_cons (header_, scopes);
248 if (ly_c_module_p (score_lines_[i].header_))
249 scopes = scm_cons (score_lines_[i].header_, scopes);
252 SCM s = ly_modules_lookup (scopes, field, SCM_BOOL_F);
254 title = *unsmob_stencil (scm_call_2 (user_title,
255 bookpaper_->self_scm (),
258 title = *unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
259 bookpaper_->self_scm (),
261 if (!title.is_empty ())
262 title.align_to (Y_AXIS, UP);
267 /* calculate book height, #lines, stencils. */
271 int score_count = score_lines_.size ();
273 /* Calculate the full book height. Hmm, can't we cache system
274 heights while making stencils? */
276 for (int i = 0; i < score_count; i++)
278 Stencil title = this->title (i);
279 if (!title.is_empty ())
280 height_ += title.extent (Y_AXIS).length ();
282 int line_count = SCM_VECTOR_LENGTH (score_lines_[i].lines_);
283 for (int j = 0; j < line_count; j++)
285 SCM s = scm_vector_ref ((SCM) score_lines_[i].lines_, scm_int2num (j));
286 height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
290 Output_def *paper = bookpaper_;
292 SCM scopes = SCM_EOL;
293 if (ly_c_module_p (header_))
294 scopes = scm_cons (header_, scopes);
296 SCM make_tagline = paper->c_variable ("make-tagline");
297 tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
299 if (Stencil *s = unsmob_stencil (tagline_))
300 tag_height = s->extent (Y_AXIS).length ();
301 height_ += tag_height;
303 SCM make_copyright = paper->c_variable ("make-copyright");
304 copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
305 Real copy_height = 0;
306 if (Stencil *s = unsmob_stencil (copyright_))
307 copy_height = s->extent (Y_AXIS).length ();
308 height_ += copy_height;
314 if (ly_c_pair_p (lines_))
317 int score_count = score_lines_.size ();
318 for (int i = 0; i < score_count; i++)
320 Stencil title = this->title (i);
321 if (!title.is_empty ())
322 lines_ = scm_cons (stencil2line (title, true), lines_);
324 lines_ = scm_append (scm_list_2 (scm_vector_to_list (score_lines_[i].lines_), lines_));
327 lines_ = scm_reverse (lines_);
330 for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
331 unsmob_paper_line (ly_car (s))->number_ = ++i;
338 if (ly_c_pair_p (pages_))
342 Output_def *paper = bookpaper_;
343 Page *page = new Page (paper, 1);
345 Real text_height = page->text_height ();
347 Real copy_height = 0;
348 if (Stencil *s = unsmob_stencil (copyright_))
349 copy_height = s->extent (Y_AXIS).length ();
352 if (Stencil *s = unsmob_stencil (tagline_))
353 tag_height = s->extent (Y_AXIS).length ();
356 SCM proc = paper->c_variable ("page-breaking");
357 SCM breaks = scm_apply_0 (proc, scm_list_n (all, scm_make_real (height_),
358 scm_make_real (text_height),
359 scm_make_real (-copy_height),
360 scm_make_real (-tag_height),
363 /* Copyright on first page. */
364 if (unsmob_stencil (copyright_))
365 page->copyright_ = copyright_;
367 int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
370 for (int i = 0; i < page_count; i++)
373 page = new Page (paper, i + 1);
375 int next = i + 1 < page_count
376 ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
377 while ((!next && all != SCM_EOL) || line <= next)
379 SCM s = ly_car (all);
380 page->lines_ = ly_snoc (s, page->lines_);
381 page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
386 if (i == page_count-1)
387 page->is_last_ = true;
389 pages_ = scm_cons (page->self_scm (), pages_);
392 /* Tagline on last page. */
393 if (unsmob_stencil (tagline_))
394 page->tagline_ = tagline_;
396 pages_ = scm_reverse (pages_);
401 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
402 Real first, Real last)
405 int page_count = int (book_height / text_height + 0.5);
406 SCM breaks = SCM_EOL;
407 Real page_height = text_height + first;
410 for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
412 Paper_line *pl = unsmob_paper_line (ly_car (s));
413 if (!pl->is_title () && h < page_height)
415 h += pl->dim ()[Y_AXIS];
416 if (!pl->is_title () && h > page_height)
418 breaks = ly_snoc (scm_int2num (number), breaks);
420 page_height = text_height + (page_number == page_count) * last;
423 if (ly_cdr (s) == SCM_EOL)
424 breaks = ly_snoc (scm_int2num (pl->number_), breaks);
427 return scm_vector (breaks);
430 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
431 5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
432 "Return a vector with line numbers of page breaks.")
434 SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
435 SCM_ASSERT_TYPE (ly_c_number_p (book), book, SCM_ARG2, __FUNCTION__, "real");
436 SCM_ASSERT_TYPE (ly_c_number_p (text), text, SCM_ARG2, __FUNCTION__, "real");
437 SCM_ASSERT_TYPE (ly_c_number_p (first), first, SCM_ARG2, __FUNCTION__, "real");
438 SCM_ASSERT_TYPE (ly_c_number_p (last), last, SCM_ARG2, __FUNCTION__, "real");
440 return c_ragged_page_breaks (lines,
441 ly_scm2double (book), ly_scm2double (text),
442 ly_scm2double (first), ly_scm2double (last));
445 /****************************************************************/
447 Score_lines::Score_lines ()
454 Score_lines::gc_mark ()
456 scm_gc_mark (lines_);
457 scm_gc_mark (header_);