2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2015 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"
33 #include "ly-module.hh"
34 #include "lily-imports.hh"
37 Paper_book::Paper_book ()
44 performances_ = SCM_EOL;
45 systems_ = SCM_BOOL_F;
52 Paper_book::~Paper_book ()
56 const char Paper_book::type_p_name_[] = "ly:paper-book?";
59 Paper_book::mark_smob () const
62 scm_gc_mark (paper_->self_scm ());
64 scm_gc_mark (parent_->self_scm ());
65 scm_gc_mark (header_);
66 scm_gc_mark (header_0_);
68 scm_gc_mark (performances_);
69 scm_gc_mark (scores_);
70 scm_gc_mark (bookparts_);
75 Paper_book::top_paper ()
77 Output_def *paper = paper_;
78 while (paper->parent_)
79 paper = paper->parent_;
87 for (vsize i = dump_header_fieldnames_global.size (); i--;)
89 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
95 Paper_book::add_score (SCM s)
97 scores_ = scm_cons (s, scores_);
101 Paper_book::add_bookpart (SCM p)
103 bookparts_ = scm_cons (p, bookparts_);
107 Paper_book::add_performance (SCM s)
109 performances_ = scm_cons (s, performances_);
113 Paper_book::output_aux (SCM output_channel,
115 long *first_page_number,
116 long *first_performance_number)
119 if (scm_is_pair (performances_))
121 Lily::write_performances_midis (performances (),
123 scm_from_long (*first_performance_number));
124 *first_performance_number += scm_ilength (performances_);
127 if (scm_is_pair (bookparts_))
129 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
130 if (Paper_book *pbookpart = unsmob<Paper_book> (scm_car (p)))
132 bool is_last_part = (is_last && !scm_is_pair (scm_cdr (p)));
133 page_nb += pbookpart->output_aux (output_channel,
136 first_performance_number);
141 if (scm_is_null (scores_))
143 paper_->set_variable (ly_symbol2scm ("first-page-number"),
144 scm_from_long (*first_page_number));
145 paper_->set_variable (ly_symbol2scm ("is-last-bookpart"),
146 ly_bool2scm (is_last));
147 /* Generate all stencils to trigger font loads. */
148 page_nb = scm_ilength (pages ());
149 *first_page_number += page_nb;
155 Paper_book::output (SCM output_channel)
157 long first_page_number
158 = robust_scm2int (paper_->c_variable ("first-page-number"), 1);
159 long first_performance_number = 0;
161 /* FIXME: We need a line-width for ps output (framework-ps.scm:92).
162 If we don't have any, we take the paper-width unless we know
163 better which line-width to choose (e.g. if there are \bookparts
164 with different line-widths) and why we need it at all.
167 if (SCM_UNBNDP (paper_->c_variable ("line-width")))
168 paper_->set_variable (ly_symbol2scm ("line-width"),
169 paper_->c_variable ("paper-width"));
171 if (!output_aux (output_channel,
174 &first_performance_number))
177 SCM scopes = SCM_EOL;
178 if (ly_is_module (header_))
179 scopes = scm_cons (header_, scopes);
181 string mod_nm = "scm framework-" + get_output_backend_name ();
183 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
185 if (get_program_option ("print-pages"))
187 SCM framework = ly_module_lookup (mod,
188 ly_symbol2scm ("output-framework"));
190 if (scm_is_true (framework))
192 SCM func = scm_variable_ref (framework);
200 warning (_f ("program option -dprint-pages not supported by backend `%s'",
201 get_output_backend_name ()));
204 if (get_program_option ("preview"))
207 = ly_module_lookup (mod, ly_symbol2scm ("output-preview-framework"));
209 if (scm_is_true (framework))
211 SCM func = scm_variable_ref (framework);
219 warning (_f ("program option -dpreview not supported by backend `%s'",
220 get_output_backend_name ()));
225 Paper_book::classic_output_aux (SCM output,
226 long *first_performance_number)
228 if (scm_is_pair (performances_))
230 Lily::write_performances_midis (performances (),
232 scm_from_long (*first_performance_number));
233 *first_performance_number += scm_ilength (performances_);
236 /* Generate all stencils to trigger font loads. */
241 Paper_book::classic_output (SCM output)
243 long first_performance_number = 0;
244 classic_output_aux (output, &first_performance_number);
246 SCM scopes = SCM_EOL;
247 if (ly_is_module (header_))
248 scopes = scm_cons (header_, scopes);
250 if (ly_is_module (header_0_))
251 scopes = scm_cons (header_0_, scopes);
253 string format = get_output_backend_name ();
254 string mod_nm = "scm framework-" + format;
256 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
257 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
259 func = scm_variable_ref (func);
265 progress_indication ("\n");
268 /* TODO: resurrect more complex user-tweaks for titling? */
270 Paper_book::book_title ()
272 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
275 SCM scopes = SCM_EOL;
276 if (ly_is_module (header_))
277 scopes = scm_cons (header_, scopes);
280 if (ly_is_procedure (title_func))
281 tit = scm_call_2 (title_func,
285 if (unsmob<Stencil> (tit))
286 title = *unsmob<Stencil> (tit);
288 if (!title.is_empty ())
289 title.align_to (Y_AXIS, UP);
295 Paper_book::score_title (SCM header)
297 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
301 SCM scopes = SCM_EOL;
302 if (ly_is_module (header_))
303 scopes = scm_cons (header_, scopes);
305 if (ly_is_module (header))
306 scopes = scm_cons (header, scopes);
309 if (ly_is_procedure (title_func))
310 tit = scm_call_2 (title_func,
314 if (unsmob<Stencil> (tit))
315 title = *unsmob<Stencil> (tit);
317 if (!title.is_empty ())
318 title.align_to (Y_AXIS, UP);
324 set_page_permission (SCM sys, SCM symbol, SCM permission)
326 if (Paper_score *ps = unsmob<Paper_score> (sys))
328 vector<Grob *> cols = ps->get_columns ();
331 Paper_column *col = dynamic_cast<Paper_column *> (cols.back ());
332 col->set_property (symbol, permission);
333 col->find_prebroken_piece (LEFT)->set_property (symbol, permission);
336 else if (Prob *pb = unsmob<Prob> (sys))
337 pb->set_property (symbol, permission);
340 /* read the breakbefore property of a score block and set up the preceding
341 system-spec to honour it. That is, SYS should be the system spec that
342 immediately precedes the score (from which HEADER is taken)
343 in the get_system_specs () list */
345 set_system_penalty (SCM sys, SCM header)
347 if (ly_is_module (header))
349 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
350 if (SCM_VARIABLEP (force)
351 && scm_is_bool (SCM_VARIABLE_REF (force)))
353 if (to_boolean (SCM_VARIABLE_REF (force)))
355 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
356 ly_symbol2scm ("force"));
357 set_page_permission (sys, ly_symbol2scm ("line-break-permission"),
358 ly_symbol2scm ("force"));
361 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
368 set_labels (SCM sys, SCM labels)
370 if (Paper_score *ps = unsmob<Paper_score> (sys))
372 vector<Grob *> cols = ps->get_columns ();
375 Paper_column *col = dynamic_cast<Paper_column *> (cols[0]);
376 col->set_property ("labels",
377 scm_append_x (scm_list_2 (col->get_property ("labels"),
379 Paper_column *col_right
380 = dynamic_cast<Paper_column *> (col->find_prebroken_piece (RIGHT));
381 col_right->set_property ("labels",
382 scm_append_x (scm_list_2 (col_right->get_property ("labels"),
386 else if (Prob *pb = unsmob<Prob> (sys))
387 pb->set_property ("labels",
388 scm_append_x (scm_list_2 (pb->get_property ("labels"),
393 Paper_book::get_score_title (SCM header)
395 Stencil title = score_title (header);
396 if (title.is_empty ())
397 title = score_title (header_);
398 if (!title.is_empty ())
401 TODO: this should come from the \layout {} block, which should
402 override settings from \paper {}
405 = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
406 Prob *ps = make_paper_system (props);
407 paper_system_set_stencil (ps, title);
409 return ps->self_scm ();
416 Paper_book::get_system_specs ()
418 SCM system_specs = SCM_EOL;
420 Stencil title = book_title ();
421 if (!title.is_empty ())
424 = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
425 Prob *ps = make_paper_system (props);
426 paper_system_set_stencil (ps, title);
428 system_specs = scm_cons (ps->self_scm (), system_specs);
433 = Lily::layout_extract_page_properties (paper_->self_scm ());
435 SCM header = SCM_EOL;
436 SCM labels = SCM_EOL;
437 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
439 if (ly_is_module (scm_car (s)))
441 header = scm_car (s);
442 if (scm_is_null (header_0_))
445 else if (Page_marker *page_marker = unsmob<Page_marker> (scm_car (s)))
447 /* page markers are used to set page breaking/turning permission,
448 or to place bookmarking labels */
449 if (scm_is_symbol (page_marker->permission_symbol ()))
451 /* set previous element page break or turn permission */
452 if (scm_is_pair (system_specs))
453 set_page_permission (scm_car (system_specs),
454 page_marker->permission_symbol (),
455 page_marker->permission_value ());
457 if (scm_is_symbol (page_marker->label ()))
459 /* The next element label is to be set */
460 labels = scm_cons (page_marker->label (), labels);
463 else if (Music_output *mop = unsmob<Music_output> (scm_car (s)))
465 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
467 SCM title = get_score_title (header);
469 if (scm_is_pair (system_specs))
470 set_system_penalty (scm_car (system_specs), header);
472 if (unsmob<Prob> (title))
474 system_specs = scm_cons (title, system_specs);
475 unsmob<Prob> (title)->unprotect ();
479 system_specs = scm_cons (pscore->self_scm (), system_specs);
480 if (scm_is_pair (labels))
482 set_labels (scm_car (system_specs), labels);
493 else if (Text_interface::is_markup_list (scm_car (s)))
495 SCM texts = Lily::interpret_markup_list (paper_->self_scm (),
500 for (SCM list = texts; scm_is_pair (list); list = scm_cdr (list))
502 SCM t = scm_car (list);
504 Prob *ps = make_paper_system (SCM_EOL);
505 ps->set_property ("page-break-permission",
506 ly_symbol2scm ("allow"));
507 ps->set_property ("page-turn-permission",
508 ly_symbol2scm ("allow"));
509 ps->set_property ("last-markup-line", SCM_BOOL_F);
510 ps->set_property ("first-markup-line", SCM_BOOL_F);
512 paper_system_set_stencil (ps, *unsmob<Stencil> (t));
514 SCM footnotes = get_footnotes (unsmob<Stencil> (t)->expr ());
515 ps->set_property ("footnotes", footnotes);
516 ps->set_property ("is-title", SCM_BOOL_T);
521 // last line so far, in a multi-line paragraph
523 //Place closely to previous line, no stretching.
524 ps->set_property ("tight-spacing", SCM_BOOL_T);
526 system_specs = scm_cons (ps->self_scm (), system_specs);
529 if (scm_is_pair (labels))
531 set_labels (scm_car (system_specs), labels);
534 // FIXME: figure out penalty.
535 //set_system_penalty (ps, scores_[i].header_);
537 /* Set properties to avoid widowed/orphaned lines.
538 Single-line markup_lists are excluded, but in future
539 we may want to add the case of a very short, single line. */
542 last->set_property ("last-markup-line", SCM_BOOL_T);
543 first->set_property ("first-markup-line", SCM_BOOL_T);
550 system_specs = scm_reverse_x (system_specs, SCM_EOL);
555 Paper_book::systems ()
557 if (scm_is_true (systems_))
561 if (scm_is_pair (bookparts_))
563 SCM system_list = SCM_EOL;
564 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
565 if (Paper_book *pbookpart = unsmob<Paper_book> (scm_car (p)))
566 system_list = scm_cons (pbookpart->systems (), system_list);
567 systems_ = scm_append (scm_reverse_x (system_list, SCM_EOL));
571 SCM specs = get_system_specs ();
572 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
574 if (Paper_score * pscore
575 = unsmob<Paper_score> (scm_car (s)))
578 = scm_vector_to_list (pscore->get_paper_systems ());
580 systems_ = scm_reverse_x (system_list, systems_);
584 systems_ = scm_cons (scm_car (s), systems_);
587 systems_ = scm_reverse_x (systems_, SCM_EOL);
589 /* backwards compatibility for the old page breaker */
592 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
594 Prob *ps = unsmob<Prob> (scm_car (s));
595 ps->set_property ("number", scm_from_int (++i));
598 && to_boolean (last->get_property ("is-title"))
599 && !scm_is_number (ps->get_property ("penalty")))
600 ps->set_property ("penalty", scm_from_int (10000));
603 if (scm_is_pair (scm_cdr (s)))
605 SCM perm = ps->get_property ("page-break-permission");
606 Prob *next = unsmob<Prob> (scm_cadr (s));
607 if (scm_is_null (perm))
608 next->set_property ("penalty", scm_from_int (10001));
609 else if (scm_is_eq (perm, ly_symbol2scm ("force")))
610 next->set_property ("penalty", scm_from_int (-10001));
621 if (scm_is_true (pages_))
625 if (scm_is_pair (bookparts_))
627 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
628 if (Paper_book *pbookpart = unsmob<Paper_book> (scm_car (p)))
629 pages_ = scm_cons (pbookpart->pages (), pages_);
630 pages_ = scm_append (scm_reverse_x (pages_, SCM_EOL));
632 else if (scm_is_pair (scores_))
634 SCM page_breaking = paper_->c_variable ("page-breaking");
635 pages_ = scm_call_1 (page_breaking, self_scm ());
637 // Create all the page stencils.
638 SCM page_module = scm_c_resolve_module ("scm page");
639 SCM page_stencil = scm_c_module_lookup (page_module, "page-stencil");
640 page_stencil = scm_variable_ref (page_stencil);
641 for (SCM pages = pages_; scm_is_pair (pages); pages = scm_cdr (pages))
642 scm_call_1 (page_stencil, scm_car (pages));
644 // Perform any user-supplied post-processing.
645 SCM post_process = paper_->c_variable ("page-post-process");
646 if (ly_is_procedure (post_process))
647 scm_call_2 (post_process, paper_->self_scm (), pages_);
649 /* set systems_ from the pages */
650 if (scm_is_false (systems_))
653 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
655 Prob *page = unsmob<Prob> (scm_car (p));
656 SCM systems = page->get_property ("lines");
657 systems_ = scm_cons (systems, systems_);
659 systems_ = scm_append (scm_reverse_x (systems_, SCM_EOL));
666 Paper_book::performances () const
668 return scm_reverse (performances_);