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 ()
60 scm_gc_mark (paper_->self_scm ());
62 scm_gc_mark (parent_->self_scm ());
63 scm_gc_mark (header_);
64 scm_gc_mark (header_0_);
66 scm_gc_mark (performances_);
67 scm_gc_mark (scores_);
68 scm_gc_mark (bookparts_);
73 Paper_book::print_smob (SCM smob, SCM port, scm_print_state *)
75 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
77 scm_puts ("#<Paper_book>", port);
82 Paper_book::top_paper ()
84 Output_def *paper = paper_;
85 while (paper->parent_)
86 paper = paper->parent_;
94 for (vsize i = dump_header_fieldnames_global.size (); i--;)
96 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
102 Paper_book::add_score (SCM s)
104 scores_ = scm_cons (s, scores_);
108 Paper_book::add_bookpart (SCM p)
110 bookparts_ = scm_cons (p, bookparts_);
114 Paper_book::add_performance (SCM s)
116 performances_ = scm_cons (s, performances_);
120 Paper_book::output_aux (SCM output_channel,
122 long *first_page_number,
123 long *first_performance_number)
126 if (scm_is_pair (performances_))
128 SCM proc = ly_lily_module_constant ("write-performances-midis");
133 scm_from_long (*first_performance_number));
134 *first_performance_number += scm_ilength (performances_);
137 if (scm_is_pair (bookparts_))
139 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
140 if (Paper_book *pbookpart = Paper_book::unsmob (scm_car (p)))
142 bool is_last_part = (is_last && !scm_is_pair (scm_cdr (p)));
143 page_nb += pbookpart->output_aux (output_channel,
146 first_performance_number);
151 if (scores_ == SCM_EOL)
153 paper_->set_variable (ly_symbol2scm ("first-page-number"),
154 scm_from_long (*first_page_number));
155 paper_->set_variable (ly_symbol2scm ("is-last-bookpart"),
156 ly_bool2scm (is_last));
157 /* Generate all stencils to trigger font loads. */
158 page_nb = scm_ilength (pages ());
159 *first_page_number += page_nb;
165 Paper_book::output (SCM output_channel)
167 long first_page_number
168 = robust_scm2int (paper_->c_variable ("first-page-number"), 1);
169 long first_performance_number = 0;
171 /* FIXME: We need a line-width for ps output (framework-ps.scm:92).
172 If we don't have any, we take the paper-width unless we know
173 better which line-width to choose (e.g. if there are \bookparts
174 with different line-widths) and why we need it at all.
177 if (paper_->c_variable ("line-width") == SCM_UNDEFINED)
178 paper_->set_variable (ly_symbol2scm ("line-width"),
179 paper_->c_variable ("paper-width"));
181 if (!output_aux (output_channel,
184 &first_performance_number))
187 SCM scopes = SCM_EOL;
188 if (ly_is_module (header_))
189 scopes = scm_cons (header_, scopes);
191 string mod_nm = "scm framework-" + get_output_backend_name ();
193 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
195 if (get_program_option ("print-pages"))
197 SCM framework = ly_module_lookup (mod,
198 ly_symbol2scm ("output-framework"));
200 if (framework != SCM_BOOL_F)
202 SCM func = scm_variable_ref (framework);
210 warning (_f ("program option -dprint-pages not supported by backend `%s'",
211 get_output_backend_name ()));
214 if (get_program_option ("preview"))
217 = ly_module_lookup (mod, ly_symbol2scm ("output-preview-framework"));
219 if (framework != SCM_BOOL_F)
221 SCM func = scm_variable_ref (framework);
229 warning (_f ("program option -dpreview not supported by backend `%s'",
230 get_output_backend_name ()));
235 Paper_book::classic_output_aux (SCM output,
236 long *first_performance_number)
238 if (scm_is_pair (performances_))
240 SCM proc = ly_lily_module_constant ("write-performances-midis");
244 scm_from_long (*first_performance_number));
245 *first_performance_number += scm_ilength (performances_);
248 /* Generate all stencils to trigger font loads. */
253 Paper_book::classic_output (SCM output)
255 long first_performance_number = 0;
256 classic_output_aux (output, &first_performance_number);
258 SCM scopes = SCM_EOL;
259 if (ly_is_module (header_))
260 scopes = scm_cons (header_, scopes);
262 if (ly_is_module (header_0_))
263 scopes = scm_cons (header_0_, scopes);
265 string format = get_output_backend_name ();
266 string mod_nm = "scm framework-" + format;
268 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
269 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
271 func = scm_variable_ref (func);
277 progress_indication ("\n");
280 /* TODO: resurrect more complex user-tweaks for titling? */
282 Paper_book::book_title ()
284 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
287 SCM scopes = SCM_EOL;
288 if (ly_is_module (header_))
289 scopes = scm_cons (header_, scopes);
292 if (ly_is_procedure (title_func))
293 tit = scm_call_2 (title_func,
297 if (Stencil::unsmob (tit))
298 title = *Stencil::unsmob (tit);
300 if (!title.is_empty ())
301 title.align_to (Y_AXIS, UP);
307 Paper_book::score_title (SCM header)
309 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
313 SCM scopes = SCM_EOL;
314 if (ly_is_module (header_))
315 scopes = scm_cons (header_, scopes);
317 if (ly_is_module (header))
318 scopes = scm_cons (header, scopes);
321 if (ly_is_procedure (title_func))
322 tit = scm_call_2 (title_func,
326 if (Stencil::unsmob (tit))
327 title = *Stencil::unsmob (tit);
329 if (!title.is_empty ())
330 title.align_to (Y_AXIS, UP);
336 set_page_permission (SCM sys, SCM symbol, SCM permission)
338 if (Paper_score *ps = dynamic_cast<Paper_score *> (Music_output::unsmob (sys)))
340 vector<Grob *> cols = ps->get_columns ();
343 Paper_column *col = dynamic_cast<Paper_column *> (cols.back ());
344 col->set_property (symbol, permission);
345 col->find_prebroken_piece (LEFT)->set_property (symbol, permission);
348 else if (Prob *pb = Prob::unsmob (sys))
349 pb->set_property (symbol, permission);
352 /* read the breakbefore property of a score block and set up the preceding
353 system-spec to honour it. That is, SYS should be the system spec that
354 immediately precedes the score (from which HEADER is taken)
355 in the get_system_specs () list */
357 set_system_penalty (SCM sys, SCM header)
359 if (ly_is_module (header))
361 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
362 if (SCM_VARIABLEP (force)
363 && scm_is_bool (SCM_VARIABLE_REF (force)))
365 if (to_boolean (SCM_VARIABLE_REF (force)))
367 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
368 ly_symbol2scm ("force"));
369 set_page_permission (sys, ly_symbol2scm ("line-break-permission"),
370 ly_symbol2scm ("force"));
373 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
380 set_labels (SCM sys, SCM labels)
382 if (Paper_score *ps = dynamic_cast<Paper_score *> (Music_output::unsmob (sys)))
384 vector<Grob *> cols = ps->get_columns ();
387 Paper_column *col = dynamic_cast<Paper_column *> (cols[0]);
388 col->set_property ("labels",
389 scm_append_x (scm_list_2 (col->get_property ("labels"),
391 Paper_column *col_right
392 = dynamic_cast<Paper_column *> (col->find_prebroken_piece (RIGHT));
393 col_right->set_property ("labels",
394 scm_append_x (scm_list_2 (col_right->get_property ("labels"),
398 else if (Prob *pb = Prob::unsmob (sys))
399 pb->set_property ("labels",
400 scm_append_x (scm_list_2 (pb->get_property ("labels"),
405 Paper_book::get_score_title (SCM header)
407 Stencil title = score_title (header);
408 if (title.is_empty ())
409 title = score_title (header_);
410 if (!title.is_empty ())
413 TODO: this should come from the \layout {} block, which should
414 override settings from \paper {}
417 = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
418 Prob *ps = make_paper_system (props);
419 paper_system_set_stencil (ps, title);
421 return ps->self_scm ();
428 Paper_book::get_system_specs ()
430 SCM system_specs = SCM_EOL;
432 Stencil title = book_title ();
433 if (!title.is_empty ())
436 = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
437 Prob *ps = make_paper_system (props);
438 paper_system_set_stencil (ps, title);
440 system_specs = scm_cons (ps->self_scm (), system_specs);
445 = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
446 paper_->self_scm ());
448 SCM interpret_markup_list = ly_lily_module_constant ("interpret-markup-list");
449 SCM header = SCM_EOL;
450 SCM labels = SCM_EOL;
451 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
453 if (ly_is_module (scm_car (s)))
455 header = scm_car (s);
456 if (header_0_ == SCM_EOL)
459 else if (Page_marker *page_marker = Page_marker::unsmob (scm_car (s)))
461 /* page markers are used to set page breaking/turning permission,
462 or to place bookmarking labels */
463 if (scm_is_symbol (page_marker->permission_symbol ()))
465 /* set previous element page break or turn permission */
466 if (scm_is_pair (system_specs))
467 set_page_permission (scm_car (system_specs),
468 page_marker->permission_symbol (),
469 page_marker->permission_value ());
471 if (scm_is_symbol (page_marker->label ()))
473 /* The next element label is to be set */
474 labels = scm_cons (page_marker->label (), labels);
477 else if (Music_output *mop = Music_output::unsmob (scm_car (s)))
479 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
481 SCM title = get_score_title (header);
483 if (scm_is_pair (system_specs))
484 set_system_penalty (scm_car (system_specs), header);
486 if (Prob::unsmob (title))
488 system_specs = scm_cons (title, system_specs);
489 Prob::unsmob (title)->unprotect ();
493 system_specs = scm_cons (pscore->self_scm (), system_specs);
494 if (scm_is_pair (labels))
496 set_labels (scm_car (system_specs), labels);
507 else if (Text_interface::is_markup_list (scm_car (s)))
509 SCM texts = scm_call_3 (interpret_markup_list,
515 for (SCM list = texts; scm_is_pair (list); list = scm_cdr (list))
517 SCM t = scm_car (list);
519 Prob *ps = make_paper_system (SCM_EOL);
520 ps->set_property ("page-break-permission",
521 ly_symbol2scm ("allow"));
522 ps->set_property ("page-turn-permission",
523 ly_symbol2scm ("allow"));
524 ps->set_property ("last-markup-line", SCM_BOOL_F);
525 ps->set_property ("first-markup-line", SCM_BOOL_F);
527 paper_system_set_stencil (ps, *Stencil::unsmob (t));
529 SCM footnotes = get_footnotes (Stencil::unsmob (t)->expr ());
530 ps->set_property ("footnotes", footnotes);
531 ps->set_property ("is-title", SCM_BOOL_T);
536 // last line so far, in a multi-line paragraph
538 //Place closely to previous line, no stretching.
539 ps->set_property ("tight-spacing", SCM_BOOL_T);
541 system_specs = scm_cons (ps->self_scm (), system_specs);
544 if (scm_is_pair (labels))
546 set_labels (scm_car (system_specs), labels);
549 // FIXME: figure out penalty.
550 //set_system_penalty (ps, scores_[i].header_);
552 /* Set properties to avoid widowed/orphaned lines.
553 Single-line markup_lists are excluded, but in future
554 we may want to add the case of a very short, single line. */
557 last->set_property ("last-markup-line", SCM_BOOL_T);
558 first->set_property ("first-markup-line", SCM_BOOL_T);
565 system_specs = scm_reverse_x (system_specs, SCM_EOL);
570 Paper_book::systems ()
572 if (systems_ != SCM_BOOL_F)
576 if (scm_is_pair (bookparts_))
578 SCM system_list = SCM_EOL;
579 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
580 if (Paper_book *pbookpart = Paper_book::unsmob (scm_car (p)))
581 system_list = scm_cons (pbookpart->systems (), system_list);
582 systems_ = scm_append (scm_reverse_x (system_list, SCM_EOL));
586 SCM specs = get_system_specs ();
587 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
589 if (Paper_score * pscore
590 = dynamic_cast<Paper_score *> (Music_output::unsmob (scm_car (s))))
593 = scm_vector_to_list (pscore->get_paper_systems ());
595 systems_ = scm_reverse_x (system_list, systems_);
599 systems_ = scm_cons (scm_car (s), systems_);
602 systems_ = scm_reverse_x (systems_, SCM_EOL);
604 /* backwards compatibility for the old page breaker */
607 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
609 Prob *ps = Prob::unsmob (scm_car (s));
610 ps->set_property ("number", scm_from_int (++i));
613 && to_boolean (last->get_property ("is-title"))
614 && !scm_is_number (ps->get_property ("penalty")))
615 ps->set_property ("penalty", scm_from_int (10000));
618 if (scm_is_pair (scm_cdr (s)))
620 SCM perm = ps->get_property ("page-break-permission");
621 Prob *next = Prob::unsmob (scm_cadr (s));
623 next->set_property ("penalty", scm_from_int (10001));
624 else if (perm == ly_symbol2scm ("force"))
625 next->set_property ("penalty", scm_from_int (-10001));
636 if (SCM_BOOL_F != pages_)
640 if (scm_is_pair (bookparts_))
642 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
643 if (Paper_book *pbookpart = Paper_book::unsmob (scm_car (p)))
644 pages_ = scm_cons (pbookpart->pages (), pages_);
645 pages_ = scm_append (scm_reverse_x (pages_, SCM_EOL));
647 else if (scm_is_pair (scores_))
649 SCM page_breaking = paper_->c_variable ("page-breaking");
650 pages_ = scm_call_1 (page_breaking, self_scm ());
652 // Create all the page stencils.
653 SCM page_module = scm_c_resolve_module ("scm page");
654 SCM page_stencil = scm_c_module_lookup (page_module, "page-stencil");
655 page_stencil = scm_variable_ref (page_stencil);
656 for (SCM pages = pages_; scm_is_pair (pages); pages = scm_cdr (pages))
657 scm_call_1 (page_stencil, scm_car (pages));
659 // Perform any user-supplied post-processing.
660 SCM post_process = paper_->c_variable ("page-post-process");
661 if (ly_is_procedure (post_process))
662 scm_call_2 (post_process, paper_->self_scm (), pages_);
664 /* set systems_ from the pages */
665 if (systems_ == SCM_BOOL_F)
668 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
670 Prob *page = Prob::unsmob (scm_car (p));
671 SCM systems = page->get_property ("lines");
672 systems_ = scm_cons (systems, systems_);
674 systems_ = scm_append (scm_reverse_x (systems_, SCM_EOL));
681 Paper_book::performances () const
683 return scm_reverse (performances_);