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"
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::top_paper ()
75 Output_def *paper = paper_;
76 while (paper->parent_)
77 paper = paper->parent_;
85 for (vsize i = dump_header_fieldnames_global.size (); i--;)
87 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
93 Paper_book::add_score (SCM s)
95 scores_ = scm_cons (s, scores_);
99 Paper_book::add_bookpart (SCM p)
101 bookparts_ = scm_cons (p, bookparts_);
105 Paper_book::add_performance (SCM s)
107 performances_ = scm_cons (s, performances_);
111 Paper_book::output_aux (SCM output_channel,
113 long *first_page_number,
114 long *first_performance_number)
117 if (scm_is_pair (performances_))
119 SCM proc = ly_lily_module_constant ("write-performances-midis");
124 scm_from_long (*first_performance_number));
125 *first_performance_number += scm_ilength (performances_);
128 if (scm_is_pair (bookparts_))
130 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
131 if (Paper_book *pbookpart = Paper_book::unsmob (scm_car (p)))
133 bool is_last_part = (is_last && !scm_is_pair (scm_cdr (p)));
134 page_nb += pbookpart->output_aux (output_channel,
137 first_performance_number);
142 if (scores_ == SCM_EOL)
144 paper_->set_variable (ly_symbol2scm ("first-page-number"),
145 scm_from_long (*first_page_number));
146 paper_->set_variable (ly_symbol2scm ("is-last-bookpart"),
147 ly_bool2scm (is_last));
148 /* Generate all stencils to trigger font loads. */
149 page_nb = scm_ilength (pages ());
150 *first_page_number += page_nb;
156 Paper_book::output (SCM output_channel)
158 long first_page_number
159 = robust_scm2int (paper_->c_variable ("first-page-number"), 1);
160 long first_performance_number = 0;
162 /* FIXME: We need a line-width for ps output (framework-ps.scm:92).
163 If we don't have any, we take the paper-width unless we know
164 better which line-width to choose (e.g. if there are \bookparts
165 with different line-widths) and why we need it at all.
168 if (paper_->c_variable ("line-width") == SCM_UNDEFINED)
169 paper_->set_variable (ly_symbol2scm ("line-width"),
170 paper_->c_variable ("paper-width"));
172 if (!output_aux (output_channel,
175 &first_performance_number))
178 SCM scopes = SCM_EOL;
179 if (ly_is_module (header_))
180 scopes = scm_cons (header_, scopes);
182 string mod_nm = "scm framework-" + get_output_backend_name ();
184 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
186 if (get_program_option ("print-pages"))
188 SCM framework = ly_module_lookup (mod,
189 ly_symbol2scm ("output-framework"));
191 if (framework != SCM_BOOL_F)
193 SCM func = scm_variable_ref (framework);
201 warning (_f ("program option -dprint-pages not supported by backend `%s'",
202 get_output_backend_name ()));
205 if (get_program_option ("preview"))
208 = ly_module_lookup (mod, ly_symbol2scm ("output-preview-framework"));
210 if (framework != SCM_BOOL_F)
212 SCM func = scm_variable_ref (framework);
220 warning (_f ("program option -dpreview not supported by backend `%s'",
221 get_output_backend_name ()));
226 Paper_book::classic_output_aux (SCM output,
227 long *first_performance_number)
229 if (scm_is_pair (performances_))
231 SCM proc = ly_lily_module_constant ("write-performances-midis");
235 scm_from_long (*first_performance_number));
236 *first_performance_number += scm_ilength (performances_);
239 /* Generate all stencils to trigger font loads. */
244 Paper_book::classic_output (SCM output)
246 long first_performance_number = 0;
247 classic_output_aux (output, &first_performance_number);
249 SCM scopes = SCM_EOL;
250 if (ly_is_module (header_))
251 scopes = scm_cons (header_, scopes);
253 if (ly_is_module (header_0_))
254 scopes = scm_cons (header_0_, scopes);
256 string format = get_output_backend_name ();
257 string mod_nm = "scm framework-" + format;
259 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
260 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
262 func = scm_variable_ref (func);
268 progress_indication ("\n");
271 /* TODO: resurrect more complex user-tweaks for titling? */
273 Paper_book::book_title ()
275 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
278 SCM scopes = SCM_EOL;
279 if (ly_is_module (header_))
280 scopes = scm_cons (header_, scopes);
283 if (ly_is_procedure (title_func))
284 tit = scm_call_2 (title_func,
288 if (Stencil::is_smob (tit))
289 title = *Stencil::unsmob (tit);
291 if (!title.is_empty ())
292 title.align_to (Y_AXIS, UP);
298 Paper_book::score_title (SCM header)
300 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
304 SCM scopes = SCM_EOL;
305 if (ly_is_module (header_))
306 scopes = scm_cons (header_, scopes);
308 if (ly_is_module (header))
309 scopes = scm_cons (header, scopes);
312 if (ly_is_procedure (title_func))
313 tit = scm_call_2 (title_func,
317 if (Stencil::is_smob (tit))
318 title = *Stencil::unsmob (tit);
320 if (!title.is_empty ())
321 title.align_to (Y_AXIS, UP);
327 set_page_permission (SCM sys, SCM symbol, SCM permission)
329 if (Paper_score *ps = dynamic_cast<Paper_score *> (Music_output::unsmob (sys)))
331 vector<Grob *> cols = ps->get_columns ();
334 Paper_column *col = dynamic_cast<Paper_column *> (cols.back ());
335 col->set_property (symbol, permission);
336 col->find_prebroken_piece (LEFT)->set_property (symbol, permission);
339 else if (Prob *pb = Prob::unsmob (sys))
340 pb->set_property (symbol, permission);
343 /* read the breakbefore property of a score block and set up the preceding
344 system-spec to honour it. That is, SYS should be the system spec that
345 immediately precedes the score (from which HEADER is taken)
346 in the get_system_specs () list */
348 set_system_penalty (SCM sys, SCM header)
350 if (ly_is_module (header))
352 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
353 if (SCM_VARIABLEP (force)
354 && scm_is_bool (SCM_VARIABLE_REF (force)))
356 if (to_boolean (SCM_VARIABLE_REF (force)))
358 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
359 ly_symbol2scm ("force"));
360 set_page_permission (sys, ly_symbol2scm ("line-break-permission"),
361 ly_symbol2scm ("force"));
364 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
371 set_labels (SCM sys, SCM labels)
373 if (Paper_score *ps = dynamic_cast<Paper_score *> (Music_output::unsmob (sys)))
375 vector<Grob *> cols = ps->get_columns ();
378 Paper_column *col = dynamic_cast<Paper_column *> (cols[0]);
379 col->set_property ("labels",
380 scm_append_x (scm_list_2 (col->get_property ("labels"),
382 Paper_column *col_right
383 = dynamic_cast<Paper_column *> (col->find_prebroken_piece (RIGHT));
384 col_right->set_property ("labels",
385 scm_append_x (scm_list_2 (col_right->get_property ("labels"),
389 else if (Prob *pb = Prob::unsmob (sys))
390 pb->set_property ("labels",
391 scm_append_x (scm_list_2 (pb->get_property ("labels"),
396 Paper_book::get_score_title (SCM header)
398 Stencil title = score_title (header);
399 if (title.is_empty ())
400 title = score_title (header_);
401 if (!title.is_empty ())
404 TODO: this should come from the \layout {} block, which should
405 override settings from \paper {}
408 = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
409 Prob *ps = make_paper_system (props);
410 paper_system_set_stencil (ps, title);
412 return ps->self_scm ();
419 Paper_book::get_system_specs ()
421 SCM system_specs = SCM_EOL;
423 Stencil title = book_title ();
424 if (!title.is_empty ())
427 = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
428 Prob *ps = make_paper_system (props);
429 paper_system_set_stencil (ps, title);
431 system_specs = scm_cons (ps->self_scm (), system_specs);
436 = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
437 paper_->self_scm ());
439 SCM interpret_markup_list = ly_lily_module_constant ("interpret-markup-list");
440 SCM header = SCM_EOL;
441 SCM labels = SCM_EOL;
442 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
444 if (ly_is_module (scm_car (s)))
446 header = scm_car (s);
447 if (header_0_ == SCM_EOL)
450 else if (Page_marker *page_marker = Page_marker::unsmob (scm_car (s)))
452 /* page markers are used to set page breaking/turning permission,
453 or to place bookmarking labels */
454 if (scm_is_symbol (page_marker->permission_symbol ()))
456 /* set previous element page break or turn permission */
457 if (scm_is_pair (system_specs))
458 set_page_permission (scm_car (system_specs),
459 page_marker->permission_symbol (),
460 page_marker->permission_value ());
462 if (scm_is_symbol (page_marker->label ()))
464 /* The next element label is to be set */
465 labels = scm_cons (page_marker->label (), labels);
468 else if (Music_output *mop = Music_output::unsmob (scm_car (s)))
470 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
472 SCM title = get_score_title (header);
474 if (scm_is_pair (system_specs))
475 set_system_penalty (scm_car (system_specs), header);
477 if (Prob::is_smob (title))
479 system_specs = scm_cons (title, system_specs);
480 Prob::unsmob (title)->unprotect ();
484 system_specs = scm_cons (pscore->self_scm (), system_specs);
485 if (scm_is_pair (labels))
487 set_labels (scm_car (system_specs), labels);
498 else if (Text_interface::is_markup_list (scm_car (s)))
500 SCM texts = scm_call_3 (interpret_markup_list,
506 for (SCM list = texts; scm_is_pair (list); list = scm_cdr (list))
508 SCM t = scm_car (list);
510 Prob *ps = make_paper_system (SCM_EOL);
511 ps->set_property ("page-break-permission",
512 ly_symbol2scm ("allow"));
513 ps->set_property ("page-turn-permission",
514 ly_symbol2scm ("allow"));
515 ps->set_property ("last-markup-line", SCM_BOOL_F);
516 ps->set_property ("first-markup-line", SCM_BOOL_F);
518 paper_system_set_stencil (ps, *Stencil::unsmob (t));
520 SCM footnotes = get_footnotes (Stencil::unsmob (t)->expr ());
521 ps->set_property ("footnotes", footnotes);
522 ps->set_property ("is-title", SCM_BOOL_T);
527 // last line so far, in a multi-line paragraph
529 //Place closely to previous line, no stretching.
530 ps->set_property ("tight-spacing", SCM_BOOL_T);
532 system_specs = scm_cons (ps->self_scm (), system_specs);
535 if (scm_is_pair (labels))
537 set_labels (scm_car (system_specs), labels);
540 // FIXME: figure out penalty.
541 //set_system_penalty (ps, scores_[i].header_);
543 /* Set properties to avoid widowed/orphaned lines.
544 Single-line markup_lists are excluded, but in future
545 we may want to add the case of a very short, single line. */
548 last->set_property ("last-markup-line", SCM_BOOL_T);
549 first->set_property ("first-markup-line", SCM_BOOL_T);
556 system_specs = scm_reverse_x (system_specs, SCM_EOL);
561 Paper_book::systems ()
563 if (systems_ != SCM_BOOL_F)
567 if (scm_is_pair (bookparts_))
569 SCM system_list = SCM_EOL;
570 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
571 if (Paper_book *pbookpart = Paper_book::unsmob (scm_car (p)))
572 system_list = scm_cons (pbookpart->systems (), system_list);
573 systems_ = scm_append (scm_reverse_x (system_list, SCM_EOL));
577 SCM specs = get_system_specs ();
578 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
580 if (Paper_score * pscore
581 = dynamic_cast<Paper_score *> (Music_output::unsmob (scm_car (s))))
584 = scm_vector_to_list (pscore->get_paper_systems ());
586 systems_ = scm_reverse_x (system_list, systems_);
590 systems_ = scm_cons (scm_car (s), systems_);
593 systems_ = scm_reverse_x (systems_, SCM_EOL);
595 /* backwards compatibility for the old page breaker */
598 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
600 Prob *ps = Prob::unsmob (scm_car (s));
601 ps->set_property ("number", scm_from_int (++i));
604 && to_boolean (last->get_property ("is-title"))
605 && !scm_is_number (ps->get_property ("penalty")))
606 ps->set_property ("penalty", scm_from_int (10000));
609 if (scm_is_pair (scm_cdr (s)))
611 SCM perm = ps->get_property ("page-break-permission");
612 Prob *next = Prob::unsmob (scm_cadr (s));
614 next->set_property ("penalty", scm_from_int (10001));
615 else if (perm == ly_symbol2scm ("force"))
616 next->set_property ("penalty", scm_from_int (-10001));
627 if (SCM_BOOL_F != pages_)
631 if (scm_is_pair (bookparts_))
633 for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
634 if (Paper_book *pbookpart = Paper_book::unsmob (scm_car (p)))
635 pages_ = scm_cons (pbookpart->pages (), pages_);
636 pages_ = scm_append (scm_reverse_x (pages_, SCM_EOL));
638 else if (scm_is_pair (scores_))
640 SCM page_breaking = paper_->c_variable ("page-breaking");
641 pages_ = scm_call_1 (page_breaking, self_scm ());
643 // Create all the page stencils.
644 SCM page_module = scm_c_resolve_module ("scm page");
645 SCM page_stencil = scm_c_module_lookup (page_module, "page-stencil");
646 page_stencil = scm_variable_ref (page_stencil);
647 for (SCM pages = pages_; scm_is_pair (pages); pages = scm_cdr (pages))
648 scm_call_1 (page_stencil, scm_car (pages));
650 // Perform any user-supplied post-processing.
651 SCM post_process = paper_->c_variable ("page-post-process");
652 if (ly_is_procedure (post_process))
653 scm_call_2 (post_process, paper_->self_scm (), pages_);
655 /* set systems_ from the pages */
656 if (systems_ == SCM_BOOL_F)
659 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
661 Prob *page = Prob::unsmob (scm_car (p));
662 SCM systems = page->get_property ("lines");
663 systems_ = scm_cons (systems, systems_);
665 systems_ = scm_append (scm_reverse_x (systems_, SCM_EOL));
672 Paper_book::performances () const
674 return scm_reverse (performances_);