2 paper-book.cc -- implement Paper_book
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2006 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"
20 #include "ly-smobs.icc"
22 Paper_book::Paper_book ()
28 performances_ = SCM_EOL;
29 systems_ = SCM_BOOL_F;
35 Paper_book::~Paper_book ()
39 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
40 IMPLEMENT_SMOBS (Paper_book);
41 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?");
44 Paper_book::mark_smob (SCM smob)
46 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
48 scm_gc_mark (b->paper_->self_scm ());
49 scm_gc_mark (b->header_);
50 scm_gc_mark (b->header_0_);
51 scm_gc_mark (b->pages_);
52 scm_gc_mark (b->performances_);
53 scm_gc_mark (b->scores_);
58 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
60 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
62 scm_puts ("#<Paper_book>", port);
70 for (vsize i = dump_header_fieldnames_global.size (); i--;)
72 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
78 Paper_book::add_score (SCM s)
80 scores_ = scm_cons (s, scores_);
84 Paper_book::add_performance (SCM s)
86 performances_ = scm_cons (s, performances_);
90 Paper_book::output (SCM output_channel)
92 if (scm_is_pair (performances_))
94 SCM proc = ly_lily_module_constant ("paper-book-write-midis");
96 scm_call_2 (proc, self_scm (), output_channel);
99 if (scores_ == SCM_EOL)
102 /* Generate all stencils to trigger font loads. */
105 SCM scopes = SCM_EOL;
106 if (ly_is_module (header_))
107 scopes = scm_cons (header_, scopes);
109 string mod_nm = "scm framework-" + output_backend_global;
111 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
114 SCM func = scm_c_module_lookup (mod, "output-framework");
116 func = scm_variable_ref (func);
117 scm_apply_0 (func, scm_list_n (output_channel,
126 SCM func = scm_c_module_lookup (mod, "output-preview-framework");
127 func = scm_variable_ref (func);
128 scm_apply_0 (func, scm_list_n (output_channel,
137 Paper_book::classic_output (SCM output)
139 /* Generate all stencils to trigger font loads. */
142 SCM scopes = SCM_EOL;
143 if (ly_is_module (header_))
144 scopes = scm_cons (header_, scopes);
146 if (ly_is_module (header_0_))
147 scopes = scm_cons (header_0_, scopes);
149 string format = output_backend_global;
150 string mod_nm = "scm framework-" + format;
152 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
153 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
155 func = scm_variable_ref (func);
156 scm_apply_0 (func, scm_list_n (output,
162 progress_indication ("\n");
165 /* TODO: resurrect more complex user-tweaks for titling? */
167 Paper_book::book_title ()
169 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
172 SCM scopes = SCM_EOL;
173 if (ly_is_module (header_))
174 scopes = scm_cons (header_, scopes);
177 if (ly_is_procedure (title_func))
178 tit = scm_call_2 (title_func,
182 if (unsmob_stencil (tit))
183 title = *unsmob_stencil (tit);
185 if (!title.is_empty ())
186 title.align_to (Y_AXIS, UP);
192 Paper_book::score_title (SCM header)
194 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
198 SCM scopes = SCM_EOL;
199 if (ly_is_module (header_))
200 scopes = scm_cons (header_, scopes);
202 if (ly_is_module (header))
203 scopes = scm_cons (header, scopes);
206 if (ly_is_procedure (title_func))
207 tit = scm_call_2 (title_func,
211 if (unsmob_stencil (tit))
212 title = *unsmob_stencil (tit);
214 if (!title.is_empty ())
215 title.align_to (Y_AXIS, UP);
220 /* read the breakbefore property of a score block and set up the preceding
221 system-spec to honour it. That is, SYM should be the system spec that
222 immediately precedes the score (from which HEADER is taken)
223 in the get_system_specs() list */
225 set_system_penalty (SCM sys, SCM header)
227 if (ly_is_module (header))
229 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
230 if (SCM_VARIABLEP (force)
231 && scm_is_bool (SCM_VARIABLE_REF (force)))
233 bool b = to_boolean (SCM_VARIABLE_REF (force));
234 SCM sym = b ? ly_symbol2scm ("force") : SCM_EOL;
236 if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
238 vector<Grob*> cols = ps->get_columns ();
241 Paper_column *col = dynamic_cast<Paper_column*> (cols.back ());
242 col->set_property ("page-break-permission", sym);
243 col->find_prebroken_piece (LEFT)->set_property ("page-break-permission", sym);
246 else if (Prob *pb = unsmob_prob (sys))
247 pb->set_property ("page-break-permission", sym);
254 Paper_book::get_score_title (SCM header)
256 Stencil title = score_title (header);
257 if (title.is_empty ())
258 title = score_title (header_);
259 if (!title.is_empty ())
262 TODO: this should come from the \layout {} block, which should
263 override settings from \paper {}
265 SCM props = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
266 Prob *ps = make_paper_system (props);
267 paper_system_set_stencil (ps, title);
269 return ps->self_scm();
277 Paper_book::get_system_specs ()
279 SCM system_specs = SCM_EOL;
281 Stencil title = book_title ();
282 if (!title.is_empty ())
284 SCM props = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
285 Prob *ps = make_paper_system (props);
286 paper_system_set_stencil (ps, title);
288 system_specs = scm_cons (ps->self_scm (), system_specs);
293 = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
294 paper_->self_scm ());
296 SCM header = SCM_EOL;
297 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
299 if (ly_is_module (scm_car (s)))
301 header = scm_car (s);
302 if (header_0_ == SCM_EOL)
305 else if (Music_output *mop = unsmob_music_output (scm_car (s)))
307 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
309 SCM title = get_score_title (header);
311 if (scm_is_pair (system_specs))
312 set_system_penalty (scm_car (system_specs), header);
314 if (unsmob_prob (title))
316 system_specs = scm_cons (title, system_specs);
317 unsmob_prob (title)->unprotect ();
321 system_specs = scm_cons (pscore->self_scm (), system_specs);
330 else if (Text_interface::is_markup (scm_car (s)))
332 SCM t = Text_interface::interpret_markup (paper_->self_scm (),
337 Prob *ps = make_paper_system (SCM_EOL);
338 paper_system_set_stencil (ps, *unsmob_stencil (t));
339 ps->set_property ("is-title", SCM_BOOL_T);
340 system_specs = scm_cons (ps->self_scm (), system_specs);
343 // FIXME: figure out penalty.
344 //set_system_penalty (ps, scores_[i].header_);
350 system_specs = scm_reverse_x (system_specs, SCM_EOL);
355 Paper_book::systems ()
357 if (systems_ != SCM_BOOL_F)
361 SCM specs = get_system_specs ();
362 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
364 if (Paper_score *pscore = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
366 SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
367 system_list = scm_reverse (system_list);
368 systems_ = scm_append (scm_list_2 (system_list, systems_));
372 systems_ = scm_cons (scm_car (s), systems_);
376 systems_ = scm_reverse (systems_);
378 /* backwards compatibility for the old page breaker */
381 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
383 Prob *ps = unsmob_prob (scm_car (s));
384 ps->set_property ("number", scm_from_int (++i));
387 && to_boolean (last->get_property ("is-title"))
388 && !scm_is_number (ps->get_property ("penalty")))
389 ps->set_property ("penalty", scm_from_int (10000));
392 if (scm_is_pair (scm_cdr (s)))
394 SCM perm = ps->get_property ("page-break-permission");
395 Prob *next = unsmob_prob (scm_cadr (s));
397 next->set_property ("penalty", scm_from_int (10001));
398 else if (perm == ly_symbol2scm ("force"))
399 next->set_property ("penalty", scm_from_int (-10001));
409 if (SCM_BOOL_F != pages_)
413 SCM proc = paper_->c_variable ("page-breaking-wrapper");
414 pages_ = scm_apply_0 (proc, scm_list_1 (self_scm ()));
416 /* set systems_ from the pages */
417 if (systems_ == SCM_BOOL_F)
420 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
422 Prob *page = unsmob_prob (scm_car (p));
423 SCM systems = page->get_property ("lines");
425 systems_ = scm_append (scm_list_2 (systems_, systems));
433 Paper_book::performances () const
435 return scm_reverse (performances_);