2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2014 Jan Nieuwenhuizen <janneke@gnu.org>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "paper-book.hh"
23 #include "international.hh"
25 #include "output-def.hh"
26 #include "paper-column.hh"
27 #include "paper-score.hh"
28 #include "paper-system.hh"
29 #include "text-interface.hh"
31 #include "program-option.hh"
32 #include "page-marker.hh"
35 Paper_book::Paper_book ()
42 performances_ = SCM_EOL;
43 systems_ = SCM_BOOL_F;
50 Paper_book::~Paper_book ()
54 const char Paper_book::type_p_name_[] = "ly:paper-book?";
57 Paper_book::mark_smob (SCM smob)
59 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
61 scm_gc_mark (b->paper_->self_scm ());
63 scm_gc_mark (b->parent_->self_scm ());
64 scm_gc_mark (b->header_);
65 scm_gc_mark (b->header_0_);
66 scm_gc_mark (b->pages_);
67 scm_gc_mark (b->performances_);
68 scm_gc_mark (b->scores_);
69 scm_gc_mark (b->bookparts_);
74 Paper_book::print_smob (SCM smob, SCM port, scm_print_state *)
76 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
78 scm_puts ("#<Paper_book>", port);
83 Paper_book::top_paper ()
85 Output_def *paper = paper_;
86 while (paper->parent_)
87 paper = paper->parent_;
95 for (vsize i = dump_header_fieldnames_global.size (); i--;)
97 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
103 Paper_book::add_score (SCM s)
105 scores_ = scm_cons (s, scores_);
109 Paper_book::add_bookpart (SCM p)
111 bookparts_ = scm_cons (p, bookparts_);
115 Paper_book::add_performance (SCM s)
117 performances_ = scm_cons (s, performances_);
121 Paper_book::output_aux (SCM output_channel,
123 long *first_page_number,
124 long *first_performance_number)
127 if (scm_is_pair (performances_))
129 SCM proc = ly_lily_module_constant ("write-performances-midis");
134 scm_from_long (*first_performance_number));
135 *first_performance_number += scm_ilength (performances_);
138 if (scm_is_pair (bookparts_))
140 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
141 if (Paper_book *pbookpart = Paper_book::unsmob (scm_car (p)))
143 bool is_last_part = (is_last && !scm_is_pair (scm_cdr (p)));
144 page_nb += pbookpart->output_aux (output_channel,
147 first_performance_number);
152 if (scores_ == SCM_EOL)
154 paper_->set_variable (ly_symbol2scm ("first-page-number"),
155 scm_from_long (*first_page_number));
156 paper_->set_variable (ly_symbol2scm ("is-last-bookpart"),
157 ly_bool2scm (is_last));
158 /* Generate all stencils to trigger font loads. */
159 page_nb = scm_ilength (pages ());
160 *first_page_number += page_nb;
166 Paper_book::output (SCM output_channel)
168 long first_page_number
169 = robust_scm2int (paper_->c_variable ("first-page-number"), 1);
170 long first_performance_number = 0;
172 /* FIXME: We need a line-width for ps output (framework-ps.scm:92).
173 If we don't have any, we take the paper-width unless we know
174 better which line-width to choose (e.g. if there are \bookparts
175 with different line-widths) and why we need it at all.
178 if (paper_->c_variable ("line-width") == SCM_UNDEFINED)
179 paper_->set_variable (ly_symbol2scm ("line-width"),
180 paper_->c_variable ("paper-width"));
182 if (!output_aux (output_channel,
185 &first_performance_number))
188 SCM scopes = SCM_EOL;
189 if (ly_is_module (header_))
190 scopes = scm_cons (header_, scopes);
192 string mod_nm = "scm framework-" + get_output_backend_name ();
194 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
196 if (get_program_option ("print-pages"))
198 SCM framework = ly_module_lookup (mod,
199 ly_symbol2scm ("output-framework"));
201 if (framework != SCM_BOOL_F)
203 SCM func = scm_variable_ref (framework);
211 warning (_f ("program option -dprint-pages not supported by backend `%s'",
212 get_output_backend_name ()));
215 if (get_program_option ("preview"))
218 = ly_module_lookup (mod, ly_symbol2scm ("output-preview-framework"));
220 if (framework != SCM_BOOL_F)
222 SCM func = scm_variable_ref (framework);
230 warning (_f ("program option -dpreview not supported by backend `%s'",
231 get_output_backend_name ()));
236 Paper_book::classic_output_aux (SCM output,
237 long *first_performance_number)
239 if (scm_is_pair (performances_))
241 SCM proc = ly_lily_module_constant ("write-performances-midis");
245 scm_from_long (*first_performance_number));
246 *first_performance_number += scm_ilength (performances_);
249 /* Generate all stencils to trigger font loads. */
254 Paper_book::classic_output (SCM output)
256 long first_performance_number = 0;
257 classic_output_aux (output, &first_performance_number);
259 SCM scopes = SCM_EOL;
260 if (ly_is_module (header_))
261 scopes = scm_cons (header_, scopes);
263 if (ly_is_module (header_0_))
264 scopes = scm_cons (header_0_, scopes);
266 string format = get_output_backend_name ();
267 string mod_nm = "scm framework-" + format;
269 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
270 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
272 func = scm_variable_ref (func);
278 progress_indication ("\n");
281 /* TODO: resurrect more complex user-tweaks for titling? */
283 Paper_book::book_title ()
285 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
288 SCM scopes = SCM_EOL;
289 if (ly_is_module (header_))
290 scopes = scm_cons (header_, scopes);
293 if (ly_is_procedure (title_func))
294 tit = scm_call_2 (title_func,
298 if (Stencil::unsmob (tit))
299 title = *Stencil::unsmob (tit);
301 if (!title.is_empty ())
302 title.align_to (Y_AXIS, UP);
308 Paper_book::score_title (SCM header)
310 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
314 SCM scopes = SCM_EOL;
315 if (ly_is_module (header_))
316 scopes = scm_cons (header_, scopes);
318 if (ly_is_module (header))
319 scopes = scm_cons (header, scopes);
322 if (ly_is_procedure (title_func))
323 tit = scm_call_2 (title_func,
327 if (Stencil::unsmob (tit))
328 title = *Stencil::unsmob (tit);
330 if (!title.is_empty ())
331 title.align_to (Y_AXIS, UP);
337 set_page_permission (SCM sys, SCM symbol, SCM permission)
339 if (Paper_score *ps = dynamic_cast<Paper_score *> (Music_output::unsmob (sys)))
341 vector<Grob *> cols = ps->get_columns ();
344 Paper_column *col = dynamic_cast<Paper_column *> (cols.back ());
345 col->set_property (symbol, permission);
346 col->find_prebroken_piece (LEFT)->set_property (symbol, permission);
349 else if (Prob *pb = Prob::unsmob (sys))
350 pb->set_property (symbol, permission);
353 /* read the breakbefore property of a score block and set up the preceding
354 system-spec to honour it. That is, SYS should be the system spec that
355 immediately precedes the score (from which HEADER is taken)
356 in the get_system_specs () list */
358 set_system_penalty (SCM sys, SCM header)
360 if (ly_is_module (header))
362 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
363 if (SCM_VARIABLEP (force)
364 && scm_is_bool (SCM_VARIABLE_REF (force)))
366 if (to_boolean (SCM_VARIABLE_REF (force)))
368 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
369 ly_symbol2scm ("force"));
370 set_page_permission (sys, ly_symbol2scm ("line-break-permission"),
371 ly_symbol2scm ("force"));
374 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
381 set_labels (SCM sys, SCM labels)
383 if (Paper_score *ps = dynamic_cast<Paper_score *> (Music_output::unsmob (sys)))
385 vector<Grob *> cols = ps->get_columns ();
388 Paper_column *col = dynamic_cast<Paper_column *> (cols[0]);
389 col->set_property ("labels",
390 scm_append_x (scm_list_2 (col->get_property ("labels"),
392 Paper_column *col_right
393 = dynamic_cast<Paper_column *> (col->find_prebroken_piece (RIGHT));
394 col_right->set_property ("labels",
395 scm_append_x (scm_list_2 (col_right->get_property ("labels"),
399 else if (Prob *pb = Prob::unsmob (sys))
400 pb->set_property ("labels",
401 scm_append_x (scm_list_2 (pb->get_property ("labels"),
406 Paper_book::get_score_title (SCM header)
408 Stencil title = score_title (header);
409 if (title.is_empty ())
410 title = score_title (header_);
411 if (!title.is_empty ())
414 TODO: this should come from the \layout {} block, which should
415 override settings from \paper {}
418 = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
419 Prob *ps = make_paper_system (props);
420 paper_system_set_stencil (ps, title);
422 return ps->self_scm ();
429 Paper_book::get_system_specs ()
431 SCM system_specs = SCM_EOL;
433 Stencil title = book_title ();
434 if (!title.is_empty ())
437 = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
438 Prob *ps = make_paper_system (props);
439 paper_system_set_stencil (ps, title);
441 system_specs = scm_cons (ps->self_scm (), system_specs);
446 = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
447 paper_->self_scm ());
449 SCM interpret_markup_list = ly_lily_module_constant ("interpret-markup-list");
450 SCM header = SCM_EOL;
451 SCM labels = SCM_EOL;
452 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
454 if (ly_is_module (scm_car (s)))
456 header = scm_car (s);
457 if (header_0_ == SCM_EOL)
460 else if (Page_marker *page_marker = Page_marker::unsmob (scm_car (s)))
462 /* page markers are used to set page breaking/turning permission,
463 or to place bookmarking labels */
464 if (scm_is_symbol (page_marker->permission_symbol ()))
466 /* set previous element page break or turn permission */
467 if (scm_is_pair (system_specs))
468 set_page_permission (scm_car (system_specs),
469 page_marker->permission_symbol (),
470 page_marker->permission_value ());
472 if (scm_is_symbol (page_marker->label ()))
474 /* The next element label is to be set */
475 labels = scm_cons (page_marker->label (), labels);
478 else if (Music_output *mop = Music_output::unsmob (scm_car (s)))
480 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
482 SCM title = get_score_title (header);
484 if (scm_is_pair (system_specs))
485 set_system_penalty (scm_car (system_specs), header);
487 if (Prob::unsmob (title))
489 system_specs = scm_cons (title, system_specs);
490 Prob::unsmob (title)->unprotect ();
494 system_specs = scm_cons (pscore->self_scm (), system_specs);
495 if (scm_is_pair (labels))
497 set_labels (scm_car (system_specs), labels);
508 else if (Text_interface::is_markup_list (scm_car (s)))
510 SCM texts = scm_call_3 (interpret_markup_list,
516 for (SCM list = texts; scm_is_pair (list); list = scm_cdr (list))
518 SCM t = scm_car (list);
520 Prob *ps = make_paper_system (SCM_EOL);
521 ps->set_property ("page-break-permission",
522 ly_symbol2scm ("allow"));
523 ps->set_property ("page-turn-permission",
524 ly_symbol2scm ("allow"));
525 ps->set_property ("last-markup-line", SCM_BOOL_F);
526 ps->set_property ("first-markup-line", SCM_BOOL_F);
528 paper_system_set_stencil (ps, *Stencil::unsmob (t));
530 SCM footnotes = get_footnotes (Stencil::unsmob (t)->expr ());
531 ps->set_property ("footnotes", footnotes);
532 ps->set_property ("is-title", SCM_BOOL_T);
537 // last line so far, in a multi-line paragraph
539 //Place closely to previous line, no stretching.
540 ps->set_property ("tight-spacing", SCM_BOOL_T);
542 system_specs = scm_cons (ps->self_scm (), system_specs);
545 if (scm_is_pair (labels))
547 set_labels (scm_car (system_specs), labels);
550 // FIXME: figure out penalty.
551 //set_system_penalty (ps, scores_[i].header_);
553 /* Set properties to avoid widowed/orphaned lines.
554 Single-line markup_lists are excluded, but in future
555 we may want to add the case of a very short, single line. */
558 last->set_property ("last-markup-line", SCM_BOOL_T);
559 first->set_property ("first-markup-line", SCM_BOOL_T);
566 system_specs = scm_reverse_x (system_specs, SCM_EOL);
571 Paper_book::systems ()
573 if (systems_ != SCM_BOOL_F)
577 if (scm_is_pair (bookparts_))
579 SCM system_list = SCM_EOL;
580 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
581 if (Paper_book *pbookpart = Paper_book::unsmob (scm_car (p)))
582 system_list = scm_cons (pbookpart->systems (), system_list);
583 systems_ = scm_append (scm_reverse_x (system_list, SCM_EOL));
587 SCM specs = get_system_specs ();
588 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
590 if (Paper_score * pscore
591 = dynamic_cast<Paper_score *> (Music_output::unsmob (scm_car (s))))
594 = scm_vector_to_list (pscore->get_paper_systems ());
596 systems_ = scm_reverse_x (system_list, systems_);
600 systems_ = scm_cons (scm_car (s), systems_);
603 systems_ = scm_reverse_x (systems_, SCM_EOL);
605 /* backwards compatibility for the old page breaker */
608 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
610 Prob *ps = Prob::unsmob (scm_car (s));
611 ps->set_property ("number", scm_from_int (++i));
614 && to_boolean (last->get_property ("is-title"))
615 && !scm_is_number (ps->get_property ("penalty")))
616 ps->set_property ("penalty", scm_from_int (10000));
619 if (scm_is_pair (scm_cdr (s)))
621 SCM perm = ps->get_property ("page-break-permission");
622 Prob *next = Prob::unsmob (scm_cadr (s));
624 next->set_property ("penalty", scm_from_int (10001));
625 else if (perm == ly_symbol2scm ("force"))
626 next->set_property ("penalty", scm_from_int (-10001));
637 if (SCM_BOOL_F != pages_)
641 if (scm_is_pair (bookparts_))
643 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
644 if (Paper_book *pbookpart = Paper_book::unsmob (scm_car (p)))
645 pages_ = scm_cons (pbookpart->pages (), pages_);
646 pages_ = scm_append (scm_reverse_x (pages_, SCM_EOL));
648 else if (scm_is_pair (scores_))
650 SCM page_breaking = paper_->c_variable ("page-breaking");
651 pages_ = scm_call_1 (page_breaking, self_scm ());
653 // Create all the page stencils.
654 SCM page_module = scm_c_resolve_module ("scm page");
655 SCM page_stencil = scm_c_module_lookup (page_module, "page-stencil");
656 page_stencil = scm_variable_ref (page_stencil);
657 for (SCM pages = pages_; scm_is_pair (pages); pages = scm_cdr (pages))
658 scm_call_1 (page_stencil, scm_car (pages));
660 // Perform any user-supplied post-processing.
661 SCM post_process = paper_->c_variable ("page-post-process");
662 if (ly_is_procedure (post_process))
663 scm_call_2 (post_process, paper_->self_scm (), pages_);
665 /* set systems_ from the pages */
666 if (systems_ == SCM_BOOL_F)
669 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
671 Prob *page = Prob::unsmob (scm_car (p));
672 SCM systems = page->get_property ("lines");
673 systems_ = scm_cons (systems, systems_);
675 systems_ = scm_append (scm_reverse_x (systems_, SCM_EOL));
682 Paper_book::performances () const
684 return scm_reverse (performances_);