2 paper-book.cc -- implement Paper_book
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2009 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "paper-book.hh"
12 #include "international.hh"
14 #include "output-def.hh"
15 #include "paper-column.hh"
16 #include "paper-score.hh"
17 #include "paper-system.hh"
18 #include "text-interface.hh"
20 #include "program-option.hh"
21 #include "page-marker.hh"
23 #include "ly-smobs.icc"
25 Paper_book::Paper_book ()
32 performances_
= SCM_EOL
;
33 systems_
= SCM_BOOL_F
;
40 Paper_book::~Paper_book ()
44 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book
);
45 IMPLEMENT_SMOBS (Paper_book
);
46 IMPLEMENT_TYPE_P (Paper_book
, "ly:paper-book?");
49 Paper_book::mark_smob (SCM smob
)
51 Paper_book
*b
= (Paper_book
*) SCM_CELL_WORD_1 (smob
);
53 scm_gc_mark (b
->paper_
->self_scm ());
55 scm_gc_mark (b
->parent_
->self_scm ());
56 scm_gc_mark (b
->header_
);
57 scm_gc_mark (b
->header_0_
);
58 scm_gc_mark (b
->pages_
);
59 scm_gc_mark (b
->performances_
);
60 scm_gc_mark (b
->scores_
);
61 scm_gc_mark (b
->bookparts_
);
66 Paper_book::print_smob (SCM smob
, SCM port
, scm_print_state
*)
68 Paper_book
*b
= (Paper_book
*) SCM_CELL_WORD_1 (smob
);
70 scm_puts ("#<Paper_book>", port
);
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 int *first_page_number
,
116 int *first_performance_number
)
119 if (scm_is_pair (performances_
))
121 SCM proc
= ly_lily_module_constant ("write-performances-midis");
126 scm_long2num (*first_performance_number
));
127 *first_performance_number
+= scm_ilength (performances_
);
130 if (scm_is_pair (bookparts_
))
132 for (SCM p
= bookparts_
; scm_is_pair (p
); p
= scm_cdr (p
))
133 if (Paper_book
*pbookpart
= unsmob_paper_book (scm_car (p
)))
135 bool is_last_part
= (is_last
&& !scm_is_pair (scm_cdr (p
)));
136 page_nb
+= pbookpart
->output_aux (output_channel
,
139 first_performance_number
);
144 if (scores_
== SCM_EOL
)
146 paper_
->set_variable (ly_symbol2scm ("first-page-number"),
147 scm_long2num (*first_page_number
));
148 paper_
->set_variable (ly_symbol2scm ("is-last-bookpart"),
149 ly_bool2scm (is_last
));
150 /* Generate all stencils to trigger font loads. */
151 page_nb
= scm_ilength (pages ());
152 *first_page_number
+= page_nb
;
158 Paper_book::output (SCM output_channel
)
160 int first_page_number
161 = robust_scm2int (paper_
->c_variable ("first-page-number"), 1);
162 int first_performance_number
= 0;
164 /* FIXME: We need a line-width for ps output (framework-ps.scm:92).
165 If we don't have any, we take the paper-width unless we know
166 better which line-width to choose (e.g. if there are \bookparts
167 with different line-widths) and why we need it at all.
170 if (paper_
->c_variable ("line-width") == SCM_UNDEFINED
)
171 paper_
->set_variable (ly_symbol2scm ("line-width"),
172 paper_
->c_variable ("paper-width"));
174 if (!output_aux (output_channel
,
177 &first_performance_number
))
180 SCM scopes
= SCM_EOL
;
181 if (ly_is_module (header_
))
182 scopes
= scm_cons (header_
, scopes
);
184 string mod_nm
= "scm framework-" + get_output_backend_name ();
186 SCM mod
= scm_c_resolve_module (mod_nm
.c_str ());
188 if (get_program_option ("print-pages"))
190 SCM framework
= ly_module_lookup (mod
,
191 ly_symbol2scm ("output-framework"));
193 if (framework
!= SCM_BOOL_F
)
195 SCM func
= scm_variable_ref (framework
);
196 scm_apply_0 (func
, scm_list_n (output_channel
,
203 warning (_f ("program option -dprint-pages not supported by backend `%s'",
204 get_output_backend_name ()));
207 if (get_program_option ("preview"))
210 = ly_module_lookup (mod
, ly_symbol2scm ("output-preview-framework"));
212 if (framework
!= SCM_BOOL_F
)
214 SCM func
= scm_variable_ref (framework
);
215 scm_apply_0 (func
, scm_list_n (output_channel
,
222 warning (_f ("program option -dpreview not supported by backend `%s'",
223 get_output_backend_name ()));
228 Paper_book::classic_output_aux (SCM output
,
229 int *first_performance_number
)
231 if (scm_is_pair (performances_
))
233 SCM proc
= ly_lily_module_constant ("write-performances-midis");
237 scm_long2num (*first_performance_number
));
238 *first_performance_number
+= scm_ilength (performances_
);
241 /* Generate all stencils to trigger font loads. */
246 Paper_book::classic_output (SCM output
)
248 int first_performance_number
= 0;
249 classic_output_aux (output
, &first_performance_number
);
251 SCM scopes
= SCM_EOL
;
252 if (ly_is_module (header_
))
253 scopes
= scm_cons (header_
, scopes
);
255 if (ly_is_module (header_0_
))
256 scopes
= scm_cons (header_0_
, scopes
);
258 string format
= get_output_backend_name ();
259 string mod_nm
= "scm framework-" + format
;
261 SCM mod
= scm_c_resolve_module (mod_nm
.c_str ());
262 SCM func
= scm_c_module_lookup (mod
, "output-classic-framework");
264 func
= scm_variable_ref (func
);
265 scm_apply_0 (func
, scm_list_n (output
,
271 progress_indication ("\n");
274 /* TODO: resurrect more complex user-tweaks for titling? */
276 Paper_book::book_title ()
278 SCM title_func
= paper_
->lookup_variable (ly_symbol2scm ("book-title"));
281 SCM scopes
= SCM_EOL
;
282 if (ly_is_module (header_
))
283 scopes
= scm_cons (header_
, scopes
);
286 if (ly_is_procedure (title_func
))
287 tit
= scm_call_2 (title_func
,
291 if (unsmob_stencil (tit
))
292 title
= *unsmob_stencil (tit
);
294 if (!title
.is_empty ())
295 title
.align_to (Y_AXIS
, UP
);
301 Paper_book::score_title (SCM header
)
303 SCM title_func
= paper_
->lookup_variable (ly_symbol2scm ("score-title"));
307 SCM scopes
= SCM_EOL
;
308 if (ly_is_module (header_
))
309 scopes
= scm_cons (header_
, scopes
);
311 if (ly_is_module (header
))
312 scopes
= scm_cons (header
, scopes
);
315 if (ly_is_procedure (title_func
))
316 tit
= scm_call_2 (title_func
,
320 if (unsmob_stencil (tit
))
321 title
= *unsmob_stencil (tit
);
323 if (!title
.is_empty ())
324 title
.align_to (Y_AXIS
, UP
);
330 set_page_permission (SCM sys
, SCM symbol
, SCM permission
)
332 if (Paper_score
*ps
= dynamic_cast<Paper_score
*> (unsmob_music_output (sys
)))
334 vector
<Grob
*> cols
= ps
->get_columns ();
337 Paper_column
*col
= dynamic_cast<Paper_column
*> (cols
.back ());
338 col
->set_property (symbol
, permission
);
339 col
->find_prebroken_piece (LEFT
)->set_property (symbol
, permission
);
342 else if (Prob
*pb
= unsmob_prob (sys
))
343 pb
->set_property (symbol
, permission
);
346 /* read the breakbefore property of a score block and set up the preceding
347 system-spec to honour it. That is, SYS should be the system spec that
348 immediately precedes the score (from which HEADER is taken)
349 in the get_system_specs () list */
351 set_system_penalty (SCM sys
, SCM header
)
353 if (ly_is_module (header
))
355 SCM force
= ly_module_lookup (header
, ly_symbol2scm ("breakbefore"));
356 if (SCM_VARIABLEP (force
)
357 && scm_is_bool (SCM_VARIABLE_REF (force
)))
359 if (to_boolean (SCM_VARIABLE_REF (force
)))
361 set_page_permission (sys
, ly_symbol2scm ("page-break-permission"),
362 ly_symbol2scm ("force"));
363 set_page_permission (sys
, ly_symbol2scm ("line-break-permission"),
364 ly_symbol2scm ("force"));
367 set_page_permission (sys
, ly_symbol2scm ("page-break-permission"),
374 set_labels (SCM sys
, SCM labels
)
376 if (Paper_score
*ps
= dynamic_cast<Paper_score
*> (unsmob_music_output (sys
)))
378 vector
<Grob
*> cols
= ps
->get_columns ();
381 Paper_column
*col
= dynamic_cast<Paper_column
*> (cols
[0]);
382 col
->set_property ("labels",
383 scm_append_x (scm_list_2 (col
->get_property ("labels"),
385 Paper_column
*col_right
386 = dynamic_cast<Paper_column
*> (col
->find_prebroken_piece (RIGHT
));
387 col_right
->set_property ("labels",
388 scm_append_x (scm_list_2 (col_right
->get_property ("labels"),
392 else if (Prob
*pb
= unsmob_prob (sys
))
393 pb
->set_property ("labels",
394 scm_append_x (scm_list_2 (pb
->get_property ("labels"),
399 Paper_book::get_score_title (SCM header
)
401 Stencil title
= score_title (header
);
402 if (title
.is_empty ())
403 title
= score_title (header_
);
404 if (!title
.is_empty ())
407 TODO: this should come from the \layout {} block, which should
408 override settings from \paper {}
411 = paper_
->lookup_variable (ly_symbol2scm ("score-title-properties"));
412 Prob
*ps
= make_paper_system (props
);
413 paper_system_set_stencil (ps
, title
);
415 return ps
->self_scm ();
423 Paper_book::get_system_specs ()
425 SCM system_specs
= SCM_EOL
;
427 Stencil title
= book_title ();
428 if (!title
.is_empty ())
431 = paper_
->lookup_variable (ly_symbol2scm ("book-title-properties"));
432 Prob
*ps
= make_paper_system (props
);
433 paper_system_set_stencil (ps
, title
);
435 system_specs
= scm_cons (ps
->self_scm (), system_specs
);
440 = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
441 paper_
->self_scm ());
443 SCM interpret_markup_list
= ly_lily_module_constant ("interpret-markup-list");
444 SCM header
= SCM_EOL
;
445 SCM labels
= SCM_EOL
;
446 for (SCM s
= scm_reverse (scores_
); scm_is_pair (s
); s
= scm_cdr (s
))
448 if (ly_is_module (scm_car (s
)))
450 header
= scm_car (s
);
451 if (header_0_
== SCM_EOL
)
454 else if (Page_marker
*page_marker
= unsmob_page_marker (scm_car (s
)))
456 /* page markers are used to set page breaking/turning permission,
457 or to place bookmarking labels */
458 if (scm_is_symbol (page_marker
->permission_symbol ()))
460 /* set previous element page break or turn permission */
461 if (scm_is_pair (system_specs
))
462 set_page_permission (scm_car (system_specs
),
463 page_marker
->permission_symbol (),
464 page_marker
->permission_value ());
466 if (scm_is_symbol (page_marker
->label ()))
468 /* The next element label is to be set */
469 labels
= scm_cons (page_marker
->label (), labels
);
472 else if (Music_output
*mop
= unsmob_music_output (scm_car (s
)))
474 if (Paper_score
*pscore
= dynamic_cast<Paper_score
*> (mop
))
476 SCM title
= get_score_title (header
);
478 if (scm_is_pair (system_specs
))
479 set_system_penalty (scm_car (system_specs
), header
);
481 if (unsmob_prob (title
))
483 system_specs
= scm_cons (title
, system_specs
);
484 unsmob_prob (title
)->unprotect ();
488 system_specs
= scm_cons (pscore
->self_scm (), system_specs
);
489 if (scm_is_pair (labels
))
491 set_labels (scm_car (system_specs
), labels
);
502 else if (Text_interface::is_markup_list (scm_car (s
)))
504 SCM texts
= scm_call_3 (interpret_markup_list
,
508 for (SCM list
= texts
; scm_is_pair (list
) ; list
= scm_cdr (list
))
510 SCM t
= scm_car (list
);
512 Prob
*ps
= make_paper_system (SCM_EOL
);
513 ps
->set_property ("page-break-permission",
514 ly_symbol2scm ("allow"));
515 ps
->set_property ("page-turn-permission",
516 ly_symbol2scm ("allow"));
518 paper_system_set_stencil (ps
, *unsmob_stencil (t
));
519 ps
->set_property ("is-title", SCM_BOOL_T
);
521 /* For each markup other than the first, place it as closely as
522 possible to the previous markup and don't allow stretching. */
523 ps
->set_property ("tight-spacing", SCM_BOOL_T
);
525 system_specs
= scm_cons (ps
->self_scm (), system_specs
);
528 if (scm_is_pair (labels
))
530 set_labels (scm_car (system_specs
), labels
);
533 // FIXME: figure out penalty.
534 //set_system_penalty (ps, scores_[i].header_);
541 system_specs
= scm_reverse_x (system_specs
, SCM_EOL
);
546 Paper_book::systems ()
548 if (systems_
!= SCM_BOOL_F
)
552 if (scm_is_pair (bookparts_
))
554 for (SCM p
= bookparts_
; scm_is_pair (p
); p
= scm_cdr (p
))
555 if (Paper_book
*pbookpart
= unsmob_paper_book (scm_car (p
)))
556 systems_
= scm_append_x (scm_list_2 (systems_
,
557 pbookpart
->systems ()));
561 SCM specs
= get_system_specs ();
562 for (SCM s
= specs
; scm_is_pair (s
); s
= scm_cdr (s
))
564 if (Paper_score
*pscore
565 = dynamic_cast<Paper_score
*> (unsmob_music_output (scm_car (s
))))
568 = scm_vector_to_list (pscore
->get_paper_systems ());
570 system_list
= scm_reverse (system_list
);
571 systems_
= scm_append (scm_list_2 (system_list
, systems_
));
575 systems_
= scm_cons (scm_car (s
), systems_
);
578 systems_
= scm_reverse (systems_
);
580 /* backwards compatibility for the old page breaker */
583 for (SCM s
= systems_
; scm_is_pair (s
); s
= scm_cdr (s
))
585 Prob
*ps
= unsmob_prob (scm_car (s
));
586 ps
->set_property ("number", scm_from_int (++i
));
589 && to_boolean (last
->get_property ("is-title"))
590 && !scm_is_number (ps
->get_property ("penalty")))
591 ps
->set_property ("penalty", scm_from_int (10000));
594 if (scm_is_pair (scm_cdr (s
)))
596 SCM perm
= ps
->get_property ("page-break-permission");
597 Prob
*next
= unsmob_prob (scm_cadr (s
));
599 next
->set_property ("penalty", scm_from_int (10001));
600 else if (perm
== ly_symbol2scm ("force"))
601 next
->set_property ("penalty", scm_from_int (-10001));
612 if (SCM_BOOL_F
!= pages_
)
616 if (scm_is_pair (bookparts_
))
618 for (SCM p
= bookparts_
; scm_is_pair (p
); p
= scm_cdr (p
))
619 if (Paper_book
*pbookpart
= unsmob_paper_book (scm_car (p
)))
620 pages_
= scm_append_x (scm_list_2 (pages_
, pbookpart
->pages ()));
622 else if (scm_is_pair (scores_
))
624 SCM page_breaking
= paper_
->c_variable ("page-breaking");
625 pages_
= scm_apply_0 (page_breaking
, scm_list_1 (self_scm ()));
626 SCM post_process
= paper_
->c_variable ("page-post-process");
627 if (ly_is_procedure (post_process
))
628 scm_apply_2 (post_process
, paper_
->self_scm (), pages_
, SCM_EOL
);
630 /* set systems_ from the pages */
631 if (systems_
== SCM_BOOL_F
)
634 for (SCM p
= pages_
; scm_is_pair (p
); p
= scm_cdr (p
))
636 Prob
*page
= unsmob_prob (scm_car (p
));
637 SCM systems
= page
->get_property ("lines");
638 systems_
= scm_append (scm_list_2 (systems_
, systems
));
646 Paper_book::performances () const
648 return scm_reverse (performances_
);