2 paper-book.cc -- implement Paper_book
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2007 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "paper-book.hh"
13 #include "output-def.hh"
14 #include "paper-column.hh"
15 #include "paper-score.hh"
16 #include "paper-system.hh"
17 #include "text-interface.hh"
19 #include "program-option.hh"
20 #include "page-marker.hh"
22 #include "ly-smobs.icc"
24 Paper_book::Paper_book ()
30 performances_ = SCM_EOL;
31 systems_ = SCM_BOOL_F;
37 Paper_book::~Paper_book ()
41 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
42 IMPLEMENT_SMOBS (Paper_book);
43 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?");
46 Paper_book::mark_smob (SCM smob)
48 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
50 scm_gc_mark (b->paper_->self_scm ());
51 scm_gc_mark (b->header_);
52 scm_gc_mark (b->header_0_);
53 scm_gc_mark (b->pages_);
54 scm_gc_mark (b->performances_);
55 scm_gc_mark (b->scores_);
60 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
62 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
64 scm_puts ("#<Paper_book>", port);
72 for (vsize i = dump_header_fieldnames_global.size (); i--;)
74 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
80 Paper_book::add_score (SCM s)
82 scores_ = scm_cons (s, scores_);
86 Paper_book::add_performance (SCM s)
88 performances_ = scm_cons (s, performances_);
92 Paper_book::output (SCM output_channel)
94 if (scm_is_pair (performances_))
96 SCM proc = ly_lily_module_constant ("write-performances-midis");
98 scm_call_2 (proc, performances (), output_channel);
101 if (scores_ == SCM_EOL)
104 /* Generate all stencils to trigger font loads. */
107 SCM scopes = SCM_EOL;
108 if (ly_is_module (header_))
109 scopes = scm_cons (header_, scopes);
111 string mod_nm = "scm framework-" + get_output_backend_name ();
113 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
115 if (get_program_option ("print-pages"))
117 SCM func = scm_c_module_lookup (mod, "output-framework");
119 func = scm_variable_ref (func);
120 scm_apply_0 (func, scm_list_n (output_channel,
127 if (get_program_option ("preview"))
129 SCM func = scm_c_module_lookup (mod, "output-preview-framework");
130 func = scm_variable_ref (func);
131 scm_apply_0 (func, scm_list_n (output_channel,
140 Paper_book::classic_output (SCM output)
142 if (scm_is_pair (performances_))
144 SCM proc = ly_lily_module_constant ("write-performances-midis");
146 scm_call_2 (proc, performances (), output);
149 /* Generate all stencils to trigger font loads. */
152 SCM scopes = SCM_EOL;
153 if (ly_is_module (header_))
154 scopes = scm_cons (header_, scopes);
156 if (ly_is_module (header_0_))
157 scopes = scm_cons (header_0_, scopes);
159 string format = get_output_backend_name ();
160 string mod_nm = "scm framework-" + format;
162 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
163 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
165 func = scm_variable_ref (func);
166 scm_apply_0 (func, scm_list_n (output,
172 progress_indication ("\n");
175 /* TODO: resurrect more complex user-tweaks for titling? */
177 Paper_book::book_title ()
179 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
182 SCM scopes = SCM_EOL;
183 if (ly_is_module (header_))
184 scopes = scm_cons (header_, scopes);
187 if (ly_is_procedure (title_func))
188 tit = scm_call_2 (title_func,
192 if (unsmob_stencil (tit))
193 title = *unsmob_stencil (tit);
195 if (!title.is_empty ())
196 title.align_to (Y_AXIS, UP);
202 Paper_book::score_title (SCM header)
204 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
208 SCM scopes = SCM_EOL;
209 if (ly_is_module (header_))
210 scopes = scm_cons (header_, scopes);
212 if (ly_is_module (header))
213 scopes = scm_cons (header, scopes);
216 if (ly_is_procedure (title_func))
217 tit = scm_call_2 (title_func,
221 if (unsmob_stencil (tit))
222 title = *unsmob_stencil (tit);
224 if (!title.is_empty ())
225 title.align_to (Y_AXIS, UP);
231 set_page_permission (SCM sys, SCM symbol, SCM permission)
233 if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
235 vector<Grob*> cols = ps->get_columns ();
238 Paper_column *col = dynamic_cast<Paper_column*> (cols.back ());
239 col->set_property (symbol, permission);
240 col->find_prebroken_piece (LEFT)->set_property (symbol, permission);
243 else if (Prob *pb = unsmob_prob (sys))
244 pb->set_property (symbol, permission);
247 /* read the breakbefore property of a score block and set up the preceding
248 system-spec to honour it. That is, SYS should be the system spec that
249 immediately precedes the score (from which HEADER is taken)
250 in the get_system_specs () list */
252 set_system_penalty (SCM sys, SCM header)
254 if (ly_is_module (header))
256 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
257 if (SCM_VARIABLEP (force)
258 && scm_is_bool (SCM_VARIABLE_REF (force)))
260 if (to_boolean (SCM_VARIABLE_REF (force)))
262 set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
263 ly_symbol2scm ("force"));
264 set_page_permission (sys, ly_symbol2scm ("line-break-permission"),
265 ly_symbol2scm ("force"));
268 set_page_permission (sys, ly_symbol2scm ("page-break-permission"), SCM_EOL);
274 set_label (SCM sys, SCM label)
276 if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
278 vector<Grob*> cols = ps->get_columns ();
281 Paper_column *col = dynamic_cast<Paper_column*> (cols[0]);
282 col->set_property ("labels", scm_cons (label, col->get_property ("labels")));
283 Paper_column *col_right = dynamic_cast<Paper_column*> (col->find_prebroken_piece (RIGHT));
284 col_right->set_property ("labels", scm_cons (label, col_right->get_property ("labels")));
287 else if (Prob *pb = unsmob_prob (sys))
288 pb->set_property ("labels", scm_cons (label, pb->get_property ("labels")));
292 Paper_book::get_score_title (SCM header)
294 Stencil title = score_title (header);
295 if (title.is_empty ())
296 title = score_title (header_);
297 if (!title.is_empty ())
300 TODO: this should come from the \layout {} block, which should
301 override settings from \paper {}
303 SCM props = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
304 Prob *ps = make_paper_system (props);
305 paper_system_set_stencil (ps, title);
307 return ps->self_scm ();
315 Paper_book::get_system_specs ()
317 SCM system_specs = SCM_EOL;
319 Stencil title = book_title ();
320 if (!title.is_empty ())
322 SCM props = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
323 Prob *ps = make_paper_system (props);
324 paper_system_set_stencil (ps, title);
326 system_specs = scm_cons (ps->self_scm (), system_specs);
331 = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
332 paper_->self_scm ());
334 SCM interpret_markup_list = ly_lily_module_constant ("interpret-markup-list");
335 SCM header = SCM_EOL;
336 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
338 if (ly_is_module (scm_car (s)))
340 header = scm_car (s);
341 if (header_0_ == SCM_EOL)
344 else if (Page_marker *page_marker = unsmob_page_marker (scm_car (s)))
346 /* page markers are used to set page breaking/turning permission,
347 or to place bookmarking labels */
348 if (scm_is_symbol (page_marker->permission_symbol ()))
350 /* set previous element page break or turn permission */
351 if (scm_is_pair (system_specs))
352 set_page_permission (scm_car (system_specs),
353 page_marker->permission_symbol (),
354 page_marker->permission_value ());
356 if (scm_is_symbol (page_marker->label ()))
358 /* set previous element label */
359 if (scm_is_pair (system_specs))
360 set_label (scm_car (system_specs), page_marker->label ());
363 else if (Music_output *mop = unsmob_music_output (scm_car (s)))
365 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
367 SCM title = get_score_title (header);
369 if (scm_is_pair (system_specs))
370 set_system_penalty (scm_car (system_specs), header);
372 if (unsmob_prob (title))
374 system_specs = scm_cons (title, system_specs);
375 unsmob_prob (title)->unprotect ();
379 system_specs = scm_cons (pscore->self_scm (), system_specs);
388 else if (Text_interface::is_markup_list (scm_car (s)))
390 SCM texts = scm_call_3 (interpret_markup_list,
394 for (SCM list = texts ; scm_is_pair (list) ; list = scm_cdr (list))
396 SCM t = scm_car (list);
398 Prob *ps = make_paper_system (SCM_EOL);
399 ps->set_property ("page-break-permission", ly_symbol2scm ("allow"));
400 ps->set_property ("page-turn-permission", ly_symbol2scm ("allow"));
402 paper_system_set_stencil (ps, *unsmob_stencil (t));
403 ps->set_property ("is-title", SCM_BOOL_T);
404 if (scm_is_pair (scm_cdr (list)))
406 /* If an other markup is following, set this markup
407 * next padding and next space to 0, so that baseline-skip
408 * only should be taken into account for lines vertical
410 ps->set_property ("next-padding", scm_double2num (0.0));
411 ps->set_property ("next-space", scm_double2num (0.0));
413 system_specs = scm_cons (ps->self_scm (), system_specs);
416 // FIXME: figure out penalty.
417 //set_system_penalty (ps, scores_[i].header_);
424 system_specs = scm_reverse_x (system_specs, SCM_EOL);
429 Paper_book::systems ()
431 if (systems_ != SCM_BOOL_F)
435 SCM specs = get_system_specs ();
436 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
438 if (Paper_score *pscore = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
440 SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
441 system_list = scm_reverse (system_list);
442 systems_ = scm_append (scm_list_2 (system_list, systems_));
446 systems_ = scm_cons (scm_car (s), systems_);
450 systems_ = scm_reverse (systems_);
452 /* backwards compatibility for the old page breaker */
455 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
457 Prob *ps = unsmob_prob (scm_car (s));
458 ps->set_property ("number", scm_from_int (++i));
461 && to_boolean (last->get_property ("is-title"))
462 && !scm_is_number (ps->get_property ("penalty")))
463 ps->set_property ("penalty", scm_from_int (10000));
466 if (scm_is_pair (scm_cdr (s)))
468 SCM perm = ps->get_property ("page-break-permission");
469 Prob *next = unsmob_prob (scm_cadr (s));
471 next->set_property ("penalty", scm_from_int (10001));
472 else if (perm == ly_symbol2scm ("force"))
473 next->set_property ("penalty", scm_from_int (-10001));
483 if (SCM_BOOL_F != pages_)
487 SCM proc = paper_->c_variable ("page-breaking-wrapper");
488 pages_ = scm_apply_0 (proc, scm_list_1 (self_scm ()));
490 /* set systems_ from the pages */
491 if (systems_ == SCM_BOOL_F)
494 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
496 Prob *page = unsmob_prob (scm_car (p));
497 SCM systems = page->get_property ("lines");
499 systems_ = scm_append (scm_list_2 (systems_, systems));
507 Paper_book::performances () const
509 return scm_reverse (performances_);