2 paper-book.cc -- implement Paper_book
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2007 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "paper-book.hh"
13 #include "output-def.hh"
14 #include "paper-column.hh"
15 #include "paper-score.hh"
16 #include "paper-system.hh"
17 #include "text-interface.hh"
19 #include "program-option.hh"
20 #include "page-marker.hh"
22 #include "ly-smobs.icc"
24 Paper_book::Paper_book ()
31 performances_ = SCM_EOL;
32 systems_ = SCM_BOOL_F;
39 Paper_book::~Paper_book ()
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);
52 scm_gc_mark (b->paper_->self_scm ());
54 scm_gc_mark (b->parent_->self_scm ());
55 scm_gc_mark (b->header_);
56 scm_gc_mark (b->header_0_);
57 scm_gc_mark (b->pages_);
58 scm_gc_mark (b->performances_);
59 scm_gc_mark (b->scores_);
60 scm_gc_mark (b->bookparts_);
65 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
67 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
69 scm_puts ("#<Paper_book>", port);
74 Paper_book::top_paper ()
76 Output_def *paper = paper_;
77 while (paper->parent_)
78 paper = paper->parent_;
86 for (vsize i = dump_header_fieldnames_global.size (); i--;)
88 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
94 Paper_book::add_score (SCM s)
96 scores_ = scm_cons (s, scores_);
100 Paper_book::add_bookpart (SCM p)
102 bookparts_ = scm_cons (p, bookparts_);
106 Paper_book::add_performance (SCM s)
108 performances_ = scm_cons (s, performances_);
112 Paper_book::output_aux (SCM output_channel,
115 int *first_page_number,
116 int *first_performance_number)
118 if (scm_is_pair (performances_))
120 SCM proc = ly_lily_module_constant ("write-performances-midis");
125 scm_long2num (*first_performance_number));
126 *first_performance_number += scm_ilength (performances_);
129 if (scm_is_pair (bookparts_))
131 bool is_first_part = is_first;
132 for (SCM p = scm_reverse (bookparts_); scm_is_pair (p); p = scm_cdr (p))
133 if (Paper_book *pbookpart = unsmob_paper_book (scm_car (p)))
135 bool is_last_part = (is_last && !scm_is_pair (scm_cdr (p)));
136 pbookpart->output_aux (output_channel,
140 first_performance_number);
141 is_first_part = false;
146 if (scores_ == SCM_EOL)
148 paper_->set_variable (ly_symbol2scm ("part-first-page-number"),
149 scm_long2num (*first_page_number));
150 paper_->set_variable (ly_symbol2scm ("part-is-first"),
151 ly_bool2scm (is_first));
152 paper_->set_variable (ly_symbol2scm ("part-is-last"),
153 ly_bool2scm (is_last));
154 /* Generate all stencils to trigger font loads. */
155 *first_page_number += scm_ilength (pages ());
160 Paper_book::output (SCM output_channel)
162 int first_page_number = robust_scm2int (paper_->c_variable ("first-page-number"), 1);
163 int first_performance_number = 0;
164 output_aux (output_channel,
168 &first_performance_number);
170 SCM scopes = SCM_EOL;
171 if (ly_is_module (header_))
172 scopes = scm_cons (header_, scopes);
174 string mod_nm = "scm framework-" + get_output_backend_name ();
176 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
178 if (get_program_option ("print-pages"))
180 SCM func = scm_c_module_lookup (mod, "output-framework");
182 func = scm_variable_ref (func);
183 scm_apply_0 (func, scm_list_n (output_channel,
190 if (get_program_option ("preview"))
192 SCM func = scm_c_module_lookup (mod, "output-preview-framework");
193 func = scm_variable_ref (func);
194 scm_apply_0 (func, scm_list_n (output_channel,
203 Paper_book::classic_output_aux (SCM output,
204 int *first_performance_number)
206 if (scm_is_pair (performances_))
208 SCM proc = ly_lily_module_constant ("write-performances-midis");
212 scm_long2num (*first_performance_number));
213 *first_performance_number += scm_ilength (performances_);
216 /* Generate all stencils to trigger font loads. */
221 Paper_book::classic_output (SCM output)
223 int first_performance_number = 0;
224 classic_output_aux (output, &first_performance_number);
226 SCM scopes = SCM_EOL;
227 if (ly_is_module (header_))
228 scopes = scm_cons (header_, scopes);
230 if (ly_is_module (header_0_))
231 scopes = scm_cons (header_0_, scopes);
233 string format = get_output_backend_name ();
234 string mod_nm = "scm framework-" + format;
236 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
237 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
239 func = scm_variable_ref (func);
240 scm_apply_0 (func, scm_list_n (output,
246 progress_indication ("\n");
249 /* TODO: resurrect more complex user-tweaks for titling? */
251 Paper_book::book_title ()
253 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
256 SCM scopes = SCM_EOL;
257 if (ly_is_module (header_))
258 scopes = scm_cons (header_, scopes);
261 if (ly_is_procedure (title_func))
262 tit = scm_call_2 (title_func,
266 if (unsmob_stencil (tit))
267 title = *unsmob_stencil (tit);
269 if (!title.is_empty ())
270 title.align_to (Y_AXIS, UP);
276 Paper_book::score_title (SCM header)
278 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
282 SCM scopes = SCM_EOL;
283 if (ly_is_module (header_))
284 scopes = scm_cons (header_, scopes);
286 if (ly_is_module (header))
287 scopes = scm_cons (header, scopes);
290 if (ly_is_procedure (title_func))
291 tit = scm_call_2 (title_func,
295 if (unsmob_stencil (tit))
296 title = *unsmob_stencil (tit);
298 if (!title.is_empty ())
299 title.align_to (Y_AXIS, UP);
305 set_page_permission (SCM sys, SCM symbol, SCM permission)
307 if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
309 vector<Grob*> cols = ps->get_columns ();
312 Paper_column *col = dynamic_cast<Paper_column*> (cols.back ());
313 col->set_property (symbol, permission);
314 col->find_prebroken_piece (LEFT)->set_property (symbol, permission);
317 else if (Prob *pb = unsmob_prob (sys))
318 pb->set_property (symbol, permission);
321 /* read the breakbefore property of a score block and set up the preceding
322 system-spec to honour it. That is, SYS should be the system spec that
323 immediately precedes the score (from which HEADER is taken)
324 in the get_system_specs () list */
326 set_system_penalty (SCM sys, SCM header)
328 if (ly_is_module (header))
330 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
331 if (SCM_VARIABLEP (force)
332 && scm_is_bool (SCM_VARIABLE_REF (force)))
334 if (to_boolean (SCM_VARIABLE_REF (force)))
336 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
337 ly_symbol2scm ("force"));
338 set_page_permission (sys, ly_symbol2scm ("line-break-permission"),
339 ly_symbol2scm ("force"));
342 set_page_permission (sys, ly_symbol2scm ("page-break-permission"), SCM_EOL);
348 set_labels (SCM sys, SCM labels)
350 if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
352 vector<Grob*> cols = ps->get_columns ();
355 Paper_column *col = dynamic_cast<Paper_column*> (cols[0]);
356 col->set_property ("labels",
357 scm_append_x (scm_list_2 (col->get_property ("labels"),
359 Paper_column *col_right = dynamic_cast<Paper_column*> (col->find_prebroken_piece (RIGHT));
360 col_right->set_property ("labels",
361 scm_append_x (scm_list_2 (col_right->get_property ("labels"),
365 else if (Prob *pb = unsmob_prob (sys))
366 pb->set_property ("labels",
367 scm_append_x (scm_list_2 (pb->get_property ("labels"),
372 Paper_book::get_score_title (SCM header)
374 Stencil title = score_title (header);
375 if (title.is_empty ())
376 title = score_title (header_);
377 if (!title.is_empty ())
380 TODO: this should come from the \layout {} block, which should
381 override settings from \paper {}
383 SCM props = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
384 Prob *ps = make_paper_system (props);
385 paper_system_set_stencil (ps, title);
387 return ps->self_scm ();
395 Paper_book::get_system_specs ()
397 SCM system_specs = SCM_EOL;
399 Stencil title = book_title ();
400 if (!title.is_empty ())
402 SCM props = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
403 Prob *ps = make_paper_system (props);
404 paper_system_set_stencil (ps, title);
406 system_specs = scm_cons (ps->self_scm (), system_specs);
411 = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
412 paper_->self_scm ());
414 SCM interpret_markup_list = ly_lily_module_constant ("interpret-markup-list");
415 SCM header = SCM_EOL;
416 SCM labels = SCM_EOL;
417 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
419 if (ly_is_module (scm_car (s)))
421 header = scm_car (s);
422 if (header_0_ == SCM_EOL)
425 else if (Page_marker *page_marker = unsmob_page_marker (scm_car (s)))
427 /* page markers are used to set page breaking/turning permission,
428 or to place bookmarking labels */
429 if (scm_is_symbol (page_marker->permission_symbol ()))
431 /* set previous element page break or turn permission */
432 if (scm_is_pair (system_specs))
433 set_page_permission (scm_car (system_specs),
434 page_marker->permission_symbol (),
435 page_marker->permission_value ());
437 if (scm_is_symbol (page_marker->label ()))
439 /* The next element label is to be set */
440 labels = scm_cons (page_marker->label (), labels);
443 else if (Music_output *mop = unsmob_music_output (scm_car (s)))
445 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
447 SCM title = get_score_title (header);
449 if (scm_is_pair (system_specs))
450 set_system_penalty (scm_car (system_specs), header);
452 if (unsmob_prob (title))
454 system_specs = scm_cons (title, system_specs);
455 unsmob_prob (title)->unprotect ();
459 system_specs = scm_cons (pscore->self_scm (), system_specs);
460 if (scm_is_pair (labels))
462 set_labels (scm_car (system_specs), labels);
473 else if (Text_interface::is_markup_list (scm_car (s)))
475 SCM texts = scm_call_3 (interpret_markup_list,
479 for (SCM list = texts ; scm_is_pair (list) ; list = scm_cdr (list))
481 SCM t = scm_car (list);
483 Prob *ps = make_paper_system (SCM_EOL);
484 ps->set_property ("page-break-permission", ly_symbol2scm ("allow"));
485 ps->set_property ("page-turn-permission", ly_symbol2scm ("allow"));
487 paper_system_set_stencil (ps, *unsmob_stencil (t));
488 ps->set_property ("is-title", SCM_BOOL_T);
489 if (scm_is_pair (scm_cdr (list)))
491 /* If an other markup is following, set this markup
492 * next padding and next space to 0, so that baseline-skip
493 * only should be taken into account for lines vertical
495 ps->set_property ("next-padding", scm_double2num (0.0));
496 ps->set_property ("next-space", scm_double2num (0.0));
498 system_specs = scm_cons (ps->self_scm (), system_specs);
501 if (scm_is_pair (labels))
503 set_labels (scm_car (system_specs), labels);
506 // FIXME: figure out penalty.
507 //set_system_penalty (ps, scores_[i].header_);
514 system_specs = scm_reverse_x (system_specs, SCM_EOL);
519 Paper_book::systems ()
521 if (systems_ != SCM_BOOL_F)
525 if (scm_is_pair (bookparts_))
527 for (SCM p = scm_reverse (bookparts_); scm_is_pair (p); p = scm_cdr (p))
528 if (Paper_book *pbookpart = unsmob_paper_book (scm_car (p)))
529 systems_ = scm_append_x (scm_list_2 (systems_, pbookpart->systems ()));
533 SCM specs = get_system_specs ();
534 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
536 if (Paper_score *pscore = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
538 SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
539 system_list = scm_reverse (system_list);
540 systems_ = scm_append (scm_list_2 (system_list, systems_));
544 systems_ = scm_cons (scm_car (s), systems_);
547 systems_ = scm_reverse (systems_);
549 /* backwards compatibility for the old page breaker */
552 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
554 Prob *ps = unsmob_prob (scm_car (s));
555 ps->set_property ("number", scm_from_int (++i));
558 && to_boolean (last->get_property ("is-title"))
559 && !scm_is_number (ps->get_property ("penalty")))
560 ps->set_property ("penalty", scm_from_int (10000));
563 if (scm_is_pair (scm_cdr (s)))
565 SCM perm = ps->get_property ("page-break-permission");
566 Prob *next = unsmob_prob (scm_cadr (s));
568 next->set_property ("penalty", scm_from_int (10001));
569 else if (perm == ly_symbol2scm ("force"))
570 next->set_property ("penalty", scm_from_int (-10001));
581 if (SCM_BOOL_F != pages_)
585 if (scm_is_pair (bookparts_))
587 for (SCM p = scm_reverse (bookparts_); scm_is_pair (p); p = scm_cdr (p))
588 if (Paper_book *pbookpart = unsmob_paper_book (scm_car (p)))
589 pages_ = scm_append_x (scm_list_2 (pages_, pbookpart->pages ()));
593 SCM proc = paper_->c_variable ("page-breaking-wrapper");
594 pages_ = scm_apply_0 (proc, scm_list_1 (self_scm ()));
596 /* set systems_ from the pages */
597 if (systems_ == SCM_BOOL_F)
600 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
602 Prob *page = unsmob_prob (scm_car (p));
603 SCM systems = page->get_property ("lines");
604 systems_ = scm_append (scm_list_2 (systems_, systems));
612 Paper_book::performances () const
614 return scm_reverse (performances_);