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"
39 Paper_book::Paper_book ()
46 performances_ = SCM_EOL;
47 systems_ = SCM_BOOL_F;
54 Paper_book::~Paper_book ()
58 const char Paper_book::type_p_name_[] = "ly:paper-book?";
61 Paper_book::mark_smob () const
64 scm_gc_mark (paper_->self_scm ());
66 scm_gc_mark (parent_->self_scm ());
67 scm_gc_mark (header_);
68 scm_gc_mark (header_0_);
70 scm_gc_mark (performances_);
71 scm_gc_mark (scores_);
72 scm_gc_mark (bookparts_);
77 Paper_book::top_paper ()
79 Output_def *paper = paper_;
80 while (paper->parent_)
81 paper = paper->parent_;
89 for (vsize i = dump_header_fieldnames_global.size (); i--;)
91 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
97 Paper_book::add_score (SCM s)
99 scores_ = scm_cons (s, scores_);
103 Paper_book::add_bookpart (SCM p)
105 bookparts_ = scm_cons (p, bookparts_);
109 Paper_book::add_performance (SCM s)
111 performances_ = scm_cons (s, performances_);
115 Paper_book::output_aux (SCM output_channel,
117 long *first_page_number,
118 long *first_performance_number)
121 if (scm_is_pair (performances_))
123 Lily::write_performances_midis (performances (),
125 scm_from_long (*first_performance_number));
126 *first_performance_number += scm_ilength (performances_);
129 if (scm_is_pair (bookparts_))
131 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
132 if (Paper_book *pbookpart = unsmob<Paper_book> (scm_car (p)))
134 bool is_last_part = (is_last && !scm_is_pair (scm_cdr (p)));
135 page_nb += pbookpart->output_aux (output_channel,
138 first_performance_number);
143 if (scm_is_null (scores_))
145 paper_->set_variable (ly_symbol2scm ("first-page-number"),
146 scm_from_long (*first_page_number));
147 paper_->set_variable (ly_symbol2scm ("is-last-bookpart"),
148 ly_bool2scm (is_last));
149 /* Generate all stencils to trigger font loads. */
150 page_nb = scm_ilength (pages ());
151 *first_page_number += page_nb;
157 Paper_book::output (SCM output_channel)
159 long first_page_number
160 = robust_scm2int (paper_->c_variable ("first-page-number"), 1);
161 long first_performance_number = 0;
163 /* FIXME: We need a line-width for ps output (framework-ps.scm:92).
164 If we don't have any, we take the paper-width unless we know
165 better which line-width to choose (e.g. if there are \bookparts
166 with different line-widths) and why we need it at all.
169 if (SCM_UNBNDP (paper_->c_variable ("line-width")))
170 paper_->set_variable (ly_symbol2scm ("line-width"),
171 paper_->c_variable ("paper-width"));
173 if (!output_aux (output_channel,
176 &first_performance_number))
179 SCM scopes = SCM_EOL;
180 if (ly_is_module (header_))
181 scopes = scm_cons (header_, scopes);
183 string mod_nm = "scm framework-" + get_output_backend_name ();
185 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
187 if (get_program_option ("print-pages"))
189 SCM framework = ly_module_lookup (mod,
190 ly_symbol2scm ("output-framework"));
192 if (scm_is_true (framework))
194 SCM func = scm_variable_ref (framework);
202 warning (_f ("program option -dprint-pages not supported by backend `%s'",
203 get_output_backend_name ()));
206 if (get_program_option ("preview"))
209 = ly_module_lookup (mod, ly_symbol2scm ("output-preview-framework"));
211 if (scm_is_true (framework))
213 SCM func = scm_variable_ref (framework);
221 warning (_f ("program option -dpreview not supported by backend `%s'",
222 get_output_backend_name ()));
227 Paper_book::classic_output_aux (SCM output,
228 long *first_performance_number)
230 if (scm_is_pair (performances_))
232 Lily::write_performances_midis (performances (),
234 scm_from_long (*first_performance_number));
235 *first_performance_number += scm_ilength (performances_);
238 /* Generate all stencils to trigger font loads. */
243 Paper_book::classic_output (SCM output)
245 long first_performance_number = 0;
246 classic_output_aux (output, &first_performance_number);
248 SCM scopes = SCM_EOL;
249 if (ly_is_module (header_))
250 scopes = scm_cons (header_, scopes);
252 if (ly_is_module (header_0_))
253 scopes = scm_cons (header_0_, scopes);
255 string format = get_output_backend_name ();
256 string mod_nm = "scm framework-" + format;
258 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
259 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
261 func = scm_variable_ref (func);
267 progress_indication ("\n");
270 /* TODO: resurrect more complex user-tweaks for titling? */
272 Paper_book::book_title ()
274 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
277 SCM scopes = SCM_EOL;
278 if (ly_is_module (header_))
279 scopes = scm_cons (header_, scopes);
282 if (ly_is_procedure (title_func))
283 tit = scm_call_2 (title_func,
287 if (unsmob<Stencil> (tit))
288 title = *unsmob<Stencil> (tit);
290 if (!title.is_empty ())
291 title.align_to (Y_AXIS, UP);
297 Paper_book::score_title (SCM header)
299 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
303 SCM scopes = SCM_EOL;
304 if (ly_is_module (header_))
305 scopes = scm_cons (header_, scopes);
307 if (ly_is_module (header))
308 scopes = scm_cons (header, scopes);
311 if (ly_is_procedure (title_func))
312 tit = scm_call_2 (title_func,
316 if (unsmob<Stencil> (tit))
317 title = *unsmob<Stencil> (tit);
319 if (!title.is_empty ())
320 title.align_to (Y_AXIS, UP);
326 set_page_permission (SCM sys, SCM symbol, SCM permission)
328 if (Paper_score *ps = unsmob<Paper_score> (sys))
330 vector<Grob *> cols = ps->get_columns ();
333 Paper_column *col = dynamic_cast<Paper_column *> (cols.back ());
334 col->set_property (symbol, permission);
335 col->find_prebroken_piece (LEFT)->set_property (symbol, permission);
338 else if (Prob *pb = unsmob<Prob> (sys))
339 pb->set_property (symbol, permission);
342 /* read the breakbefore property of a score block and set up the preceding
343 system-spec to honour it. That is, SYS should be the system spec that
344 immediately precedes the score (from which HEADER is taken)
345 in the get_system_specs () list */
347 set_system_penalty (SCM sys, SCM header)
349 if (ly_is_module (header))
351 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
352 if (SCM_VARIABLEP (force)
353 && scm_is_bool (SCM_VARIABLE_REF (force)))
355 if (to_boolean (SCM_VARIABLE_REF (force)))
357 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
358 ly_symbol2scm ("force"));
359 set_page_permission (sys, ly_symbol2scm ("line-break-permission"),
360 ly_symbol2scm ("force"));
363 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
370 set_labels (SCM sys, SCM labels)
372 if (Paper_score *ps = unsmob<Paper_score> (sys))
374 vector<Grob *> cols = ps->get_columns ();
377 Paper_column *col = dynamic_cast<Paper_column *> (cols[0]);
378 col->set_property ("labels",
379 scm_append_x (scm_list_2 (col->get_property ("labels"),
381 Paper_column *col_right
382 = dynamic_cast<Paper_column *> (col->find_prebroken_piece (RIGHT));
383 col_right->set_property ("labels",
384 scm_append_x (scm_list_2 (col_right->get_property ("labels"),
388 else if (Prob *pb = unsmob<Prob> (sys))
389 pb->set_property ("labels",
390 scm_append_x (scm_list_2 (pb->get_property ("labels"),
395 Paper_book::get_score_title (SCM header)
397 Stencil title = score_title (header);
398 if (title.is_empty ())
399 title = score_title (header_);
400 if (!title.is_empty ())
403 TODO: this should come from the \layout {} block, which should
404 override settings from \paper {}
407 = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
408 Prob *ps = make_paper_system (props);
409 paper_system_set_stencil (ps, title);
411 return ps->self_scm ();
418 Paper_book::get_system_specs ()
420 SCM system_specs = SCM_EOL;
422 Stencil title = book_title ();
423 if (!title.is_empty ())
426 = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
427 Prob *ps = make_paper_system (props);
428 paper_system_set_stencil (ps, title);
430 system_specs = scm_cons (ps->self_scm (), system_specs);
435 = Lily::layout_extract_page_properties (paper_->self_scm ());
437 SCM header = SCM_EOL;
438 SCM labels = SCM_EOL;
439 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
441 if (ly_is_module (scm_car (s)))
443 header = scm_car (s);
444 if (scm_is_null (header_0_))
447 else if (Page_marker *page_marker = unsmob<Page_marker> (scm_car (s)))
449 /* page markers are used to set page breaking/turning permission,
450 or to place bookmarking labels */
451 if (scm_is_symbol (page_marker->permission_symbol ()))
453 /* set previous element page break or turn permission */
454 if (scm_is_pair (system_specs))
455 set_page_permission (scm_car (system_specs),
456 page_marker->permission_symbol (),
457 page_marker->permission_value ());
459 if (scm_is_symbol (page_marker->label ()))
461 /* The next element label is to be set */
462 labels = scm_cons (page_marker->label (), labels);
465 else if (Music_output *mop = unsmob<Music_output> (scm_car (s)))
467 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
469 SCM title = get_score_title (header);
471 if (scm_is_pair (system_specs))
472 set_system_penalty (scm_car (system_specs), header);
474 if (unsmob<Prob> (title))
476 system_specs = scm_cons (title, system_specs);
477 unsmob<Prob> (title)->unprotect ();
481 system_specs = scm_cons (pscore->self_scm (), system_specs);
482 if (scm_is_pair (labels))
484 set_labels (scm_car (system_specs), labels);
495 else if (Text_interface::is_markup_list (scm_car (s)))
497 SCM texts = Lily::interpret_markup_list (paper_->self_scm (),
502 for (SCM list = texts; scm_is_pair (list); list = scm_cdr (list))
504 SCM t = scm_car (list);
506 Prob *ps = make_paper_system (SCM_EOL);
507 ps->set_property ("page-break-permission",
508 ly_symbol2scm ("allow"));
509 ps->set_property ("page-turn-permission",
510 ly_symbol2scm ("allow"));
511 ps->set_property ("last-markup-line", SCM_BOOL_F);
512 ps->set_property ("first-markup-line", SCM_BOOL_F);
514 paper_system_set_stencil (ps, *unsmob<Stencil> (t));
516 SCM footnotes = get_footnotes (unsmob<Stencil> (t)->expr ());
517 ps->set_property ("footnotes", footnotes);
518 ps->set_property ("is-title", SCM_BOOL_T);
523 // last line so far, in a multi-line paragraph
525 //Place closely to previous line, no stretching.
526 ps->set_property ("tight-spacing", SCM_BOOL_T);
528 system_specs = scm_cons (ps->self_scm (), system_specs);
531 if (scm_is_pair (labels))
533 set_labels (scm_car (system_specs), labels);
536 // FIXME: figure out penalty.
537 //set_system_penalty (ps, scores_[i].header_);
539 /* Set properties to avoid widowed/orphaned lines.
540 Single-line markup_lists are excluded, but in future
541 we may want to add the case of a very short, single line. */
544 last->set_property ("last-markup-line", SCM_BOOL_T);
545 first->set_property ("first-markup-line", SCM_BOOL_T);
552 system_specs = scm_reverse_x (system_specs, SCM_EOL);
557 Paper_book::systems ()
559 if (scm_is_true (systems_))
563 if (scm_is_pair (bookparts_))
565 SCM system_list = SCM_EOL;
566 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
567 if (Paper_book *pbookpart = unsmob<Paper_book> (scm_car (p)))
568 system_list = scm_cons (pbookpart->systems (), system_list);
569 systems_ = scm_append (scm_reverse_x (system_list, SCM_EOL));
573 SCM specs = get_system_specs ();
574 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
576 if (Paper_score * pscore
577 = unsmob<Paper_score> (scm_car (s)))
580 = scm_vector_to_list (pscore->get_paper_systems ());
582 systems_ = scm_reverse_x (system_list, systems_);
586 systems_ = scm_cons (scm_car (s), systems_);
589 systems_ = scm_reverse_x (systems_, SCM_EOL);
591 /* backwards compatibility for the old page breaker */
594 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
596 Prob *ps = unsmob<Prob> (scm_car (s));
597 ps->set_property ("number", scm_from_int (++i));
600 && to_boolean (last->get_property ("is-title"))
601 && !scm_is_number (ps->get_property ("penalty")))
602 ps->set_property ("penalty", scm_from_int (10000));
605 if (scm_is_pair (scm_cdr (s)))
607 SCM perm = ps->get_property ("page-break-permission");
608 Prob *next = unsmob<Prob> (scm_cadr (s));
609 if (scm_is_null (perm))
610 next->set_property ("penalty", scm_from_int (10001));
611 else if (scm_is_eq (perm, ly_symbol2scm ("force")))
612 next->set_property ("penalty", scm_from_int (-10001));
623 if (scm_is_true (pages_))
627 if (scm_is_pair (bookparts_))
629 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
630 if (Paper_book *pbookpart = unsmob<Paper_book> (scm_car (p)))
631 pages_ = scm_cons (pbookpart->pages (), pages_);
632 pages_ = scm_append (scm_reverse_x (pages_, SCM_EOL));
634 else if (scm_is_pair (scores_))
636 SCM page_breaking = paper_->c_variable ("page-breaking");
637 pages_ = scm_call_1 (page_breaking, self_scm ());
639 // Create all the page stencils.
640 SCM page_module = scm_c_resolve_module ("scm page");
641 SCM page_stencil = scm_c_module_lookup (page_module, "page-stencil");
642 page_stencil = scm_variable_ref (page_stencil);
643 for (SCM pages = pages_; scm_is_pair (pages); pages = scm_cdr (pages))
644 scm_call_1 (page_stencil, scm_car (pages));
646 // Perform any user-supplied post-processing.
647 SCM post_process = paper_->c_variable ("page-post-process");
648 if (ly_is_procedure (post_process))
649 scm_call_2 (post_process, paper_->self_scm (), pages_);
651 /* set systems_ from the pages */
652 if (scm_is_false (systems_))
655 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
657 Prob *page = unsmob<Prob> (scm_car (p));
658 SCM systems = page->get_property ("lines");
659 systems_ = scm_cons (systems, systems_);
661 systems_ = scm_append (scm_reverse_x (systems_, SCM_EOL));
668 Paper_book::performances () const
670 return scm_reverse (performances_);