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"
21 #include "ly-smobs.icc"
23 Paper_book::Paper_book ()
29 performances_ = SCM_EOL;
30 systems_ = SCM_BOOL_F;
36 Paper_book::~Paper_book ()
40 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
41 IMPLEMENT_SMOBS (Paper_book);
42 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?");
45 Paper_book::mark_smob (SCM smob)
47 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
49 scm_gc_mark (b->paper_->self_scm ());
50 scm_gc_mark (b->header_);
51 scm_gc_mark (b->header_0_);
52 scm_gc_mark (b->pages_);
53 scm_gc_mark (b->performances_);
54 scm_gc_mark (b->scores_);
59 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
61 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
63 scm_puts ("#<Paper_book>", port);
71 for (vsize i = dump_header_fieldnames_global.size (); i--;)
73 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
79 Paper_book::add_score (SCM s)
81 scores_ = scm_cons (s, scores_);
85 Paper_book::add_performance (SCM s)
87 performances_ = scm_cons (s, performances_);
91 Paper_book::output (SCM output_channel)
93 if (scm_is_pair (performances_))
95 SCM proc = ly_lily_module_constant ("write-performances-midis");
97 scm_call_2 (proc, performances (), output_channel);
100 if (scores_ == SCM_EOL)
103 /* Generate all stencils to trigger font loads. */
106 SCM scopes = SCM_EOL;
107 if (ly_is_module (header_))
108 scopes = scm_cons (header_, scopes);
110 string mod_nm = "scm framework-" + get_output_backend_name ();
112 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
115 SCM func = scm_c_module_lookup (mod, "output-framework");
117 func = scm_variable_ref (func);
118 scm_apply_0 (func, scm_list_n (output_channel,
127 SCM func = scm_c_module_lookup (mod, "output-preview-framework");
128 func = scm_variable_ref (func);
129 scm_apply_0 (func, scm_list_n (output_channel,
138 Paper_book::classic_output (SCM output)
140 if (scm_is_pair (performances_))
142 SCM proc = ly_lily_module_constant ("write-performances-midis");
144 scm_call_2 (proc, performances (), output);
147 /* Generate all stencils to trigger font loads. */
150 SCM scopes = SCM_EOL;
151 if (ly_is_module (header_))
152 scopes = scm_cons (header_, scopes);
154 if (ly_is_module (header_0_))
155 scopes = scm_cons (header_0_, scopes);
157 string format = get_output_backend_name ();
158 string mod_nm = "scm framework-" + format;
160 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
161 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
163 func = scm_variable_ref (func);
164 scm_apply_0 (func, scm_list_n (output,
170 progress_indication ("\n");
173 /* TODO: resurrect more complex user-tweaks for titling? */
175 Paper_book::book_title ()
177 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
180 SCM scopes = SCM_EOL;
181 if (ly_is_module (header_))
182 scopes = scm_cons (header_, scopes);
185 if (ly_is_procedure (title_func))
186 tit = scm_call_2 (title_func,
190 if (unsmob_stencil (tit))
191 title = *unsmob_stencil (tit);
193 if (!title.is_empty ())
194 title.align_to (Y_AXIS, UP);
200 Paper_book::score_title (SCM header)
202 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
206 SCM scopes = SCM_EOL;
207 if (ly_is_module (header_))
208 scopes = scm_cons (header_, scopes);
210 if (ly_is_module (header))
211 scopes = scm_cons (header, scopes);
214 if (ly_is_procedure (title_func))
215 tit = scm_call_2 (title_func,
219 if (unsmob_stencil (tit))
220 title = *unsmob_stencil (tit);
222 if (!title.is_empty ())
223 title.align_to (Y_AXIS, UP);
228 /* read the breakbefore property of a score block and set up the preceding
229 system-spec to honour it. That is, SYM should be the system spec that
230 immediately precedes the score (from which HEADER is taken)
231 in the get_system_specs () list */
233 set_system_penalty (SCM sys, SCM header)
235 if (ly_is_module (header))
237 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
238 if (SCM_VARIABLEP (force)
239 && scm_is_bool (SCM_VARIABLE_REF (force)))
241 bool b = to_boolean (SCM_VARIABLE_REF (force));
242 SCM sym = b ? ly_symbol2scm ("force") : SCM_EOL;
244 if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
246 vector<Grob*> cols = ps->get_columns ();
249 Paper_column *col = dynamic_cast<Paper_column*> (cols.back ());
250 col->set_property ("page-break-permission", sym);
251 col->find_prebroken_piece (LEFT)->set_property ("page-break-permission", sym);
254 else if (Prob *pb = unsmob_prob (sys))
255 pb->set_property ("page-break-permission", sym);
262 Paper_book::get_score_title (SCM header)
264 Stencil title = score_title (header);
265 if (title.is_empty ())
266 title = score_title (header_);
267 if (!title.is_empty ())
270 TODO: this should come from the \layout {} block, which should
271 override settings from \paper {}
273 SCM props = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
274 Prob *ps = make_paper_system (props);
275 paper_system_set_stencil (ps, title);
277 return ps->self_scm ();
285 Paper_book::get_system_specs ()
287 SCM system_specs = SCM_EOL;
289 Stencil title = book_title ();
290 if (!title.is_empty ())
292 SCM props = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
293 Prob *ps = make_paper_system (props);
294 paper_system_set_stencil (ps, title);
296 system_specs = scm_cons (ps->self_scm (), system_specs);
301 = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
302 paper_->self_scm ());
304 SCM header = SCM_EOL;
305 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
307 if (ly_is_module (scm_car (s)))
309 header = scm_car (s);
310 if (header_0_ == SCM_EOL)
313 else if (Music_output *mop = unsmob_music_output (scm_car (s)))
315 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
317 SCM title = get_score_title (header);
319 if (scm_is_pair (system_specs))
320 set_system_penalty (scm_car (system_specs), header);
322 if (unsmob_prob (title))
324 system_specs = scm_cons (title, system_specs);
325 unsmob_prob (title)->unprotect ();
329 system_specs = scm_cons (pscore->self_scm (), system_specs);
338 else if (Text_interface::is_markup (scm_car (s)))
340 SCM t = Text_interface::interpret_markup (paper_->self_scm (),
345 Prob *ps = make_paper_system (SCM_EOL);
346 paper_system_set_stencil (ps, *unsmob_stencil (t));
347 ps->set_property ("is-title", SCM_BOOL_T);
348 system_specs = scm_cons (ps->self_scm (), system_specs);
351 // FIXME: figure out penalty.
352 //set_system_penalty (ps, scores_[i].header_);
358 system_specs = scm_reverse_x (system_specs, SCM_EOL);
363 Paper_book::systems ()
365 if (systems_ != SCM_BOOL_F)
369 SCM specs = get_system_specs ();
370 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
372 if (Paper_score *pscore = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
374 SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
375 system_list = scm_reverse (system_list);
376 systems_ = scm_append (scm_list_2 (system_list, systems_));
380 systems_ = scm_cons (scm_car (s), systems_);
384 systems_ = scm_reverse (systems_);
386 /* backwards compatibility for the old page breaker */
389 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
391 Prob *ps = unsmob_prob (scm_car (s));
392 ps->set_property ("number", scm_from_int (++i));
395 && to_boolean (last->get_property ("is-title"))
396 && !scm_is_number (ps->get_property ("penalty")))
397 ps->set_property ("penalty", scm_from_int (10000));
400 if (scm_is_pair (scm_cdr (s)))
402 SCM perm = ps->get_property ("page-break-permission");
403 Prob *next = unsmob_prob (scm_cadr (s));
405 next->set_property ("penalty", scm_from_int (10001));
406 else if (perm == ly_symbol2scm ("force"))
407 next->set_property ("penalty", scm_from_int (-10001));
417 if (SCM_BOOL_F != pages_)
421 SCM proc = paper_->c_variable ("page-breaking-wrapper");
422 pages_ = scm_apply_0 (proc, scm_list_1 (self_scm ()));
424 /* set systems_ from the pages */
425 if (systems_ == SCM_BOOL_F)
428 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
430 Prob *page = unsmob_prob (scm_car (p));
431 SCM systems = page->get_property ("lines");
433 systems_ = scm_append (scm_list_2 (systems_, systems));
441 Paper_book::performances () const
443 return scm_reverse (performances_);