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 ("write-performances-midis");
96 scm_call_2 (proc, performances (), 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 if (scm_is_pair (performances_))
141 SCM proc = ly_lily_module_constant ("write-performances-midis");
143 scm_call_2 (proc, performances (), output);
146 /* Generate all stencils to trigger font loads. */
149 SCM scopes = SCM_EOL;
150 if (ly_is_module (header_))
151 scopes = scm_cons (header_, scopes);
153 if (ly_is_module (header_0_))
154 scopes = scm_cons (header_0_, scopes);
156 string format = output_backend_global;
157 string mod_nm = "scm framework-" + format;
159 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
160 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
162 func = scm_variable_ref (func);
163 scm_apply_0 (func, scm_list_n (output,
169 progress_indication ("\n");
172 /* TODO: resurrect more complex user-tweaks for titling? */
174 Paper_book::book_title ()
176 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
179 SCM scopes = SCM_EOL;
180 if (ly_is_module (header_))
181 scopes = scm_cons (header_, scopes);
184 if (ly_is_procedure (title_func))
185 tit = scm_call_2 (title_func,
189 if (unsmob_stencil (tit))
190 title = *unsmob_stencil (tit);
192 if (!title.is_empty ())
193 title.align_to (Y_AXIS, UP);
199 Paper_book::score_title (SCM header)
201 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
205 SCM scopes = SCM_EOL;
206 if (ly_is_module (header_))
207 scopes = scm_cons (header_, scopes);
209 if (ly_is_module (header))
210 scopes = scm_cons (header, scopes);
213 if (ly_is_procedure (title_func))
214 tit = scm_call_2 (title_func,
218 if (unsmob_stencil (tit))
219 title = *unsmob_stencil (tit);
221 if (!title.is_empty ())
222 title.align_to (Y_AXIS, UP);
227 /* read the breakbefore property of a score block and set up the preceding
228 system-spec to honour it. That is, SYM should be the system spec that
229 immediately precedes the score (from which HEADER is taken)
230 in the get_system_specs() list */
232 set_system_penalty (SCM sys, SCM header)
234 if (ly_is_module (header))
236 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
237 if (SCM_VARIABLEP (force)
238 && scm_is_bool (SCM_VARIABLE_REF (force)))
240 bool b = to_boolean (SCM_VARIABLE_REF (force));
241 SCM sym = b ? ly_symbol2scm ("force") : SCM_EOL;
243 if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
245 vector<Grob*> cols = ps->get_columns ();
248 Paper_column *col = dynamic_cast<Paper_column*> (cols.back ());
249 col->set_property ("page-break-permission", sym);
250 col->find_prebroken_piece (LEFT)->set_property ("page-break-permission", sym);
253 else if (Prob *pb = unsmob_prob (sys))
254 pb->set_property ("page-break-permission", sym);
261 Paper_book::get_score_title (SCM header)
263 Stencil title = score_title (header);
264 if (title.is_empty ())
265 title = score_title (header_);
266 if (!title.is_empty ())
269 TODO: this should come from the \layout {} block, which should
270 override settings from \paper {}
272 SCM props = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
273 Prob *ps = make_paper_system (props);
274 paper_system_set_stencil (ps, title);
276 return ps->self_scm();
284 Paper_book::get_system_specs ()
286 SCM system_specs = SCM_EOL;
288 Stencil title = book_title ();
289 if (!title.is_empty ())
291 SCM props = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
292 Prob *ps = make_paper_system (props);
293 paper_system_set_stencil (ps, title);
295 system_specs = scm_cons (ps->self_scm (), system_specs);
300 = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
301 paper_->self_scm ());
303 SCM header = SCM_EOL;
304 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
306 if (ly_is_module (scm_car (s)))
308 header = scm_car (s);
309 if (header_0_ == SCM_EOL)
312 else if (Music_output *mop = unsmob_music_output (scm_car (s)))
314 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
316 SCM title = get_score_title (header);
318 if (scm_is_pair (system_specs))
319 set_system_penalty (scm_car (system_specs), header);
321 if (unsmob_prob (title))
323 system_specs = scm_cons (title, system_specs);
324 unsmob_prob (title)->unprotect ();
328 system_specs = scm_cons (pscore->self_scm (), system_specs);
337 else if (Text_interface::is_markup (scm_car (s)))
339 SCM t = Text_interface::interpret_markup (paper_->self_scm (),
344 Prob *ps = make_paper_system (SCM_EOL);
345 paper_system_set_stencil (ps, *unsmob_stencil (t));
346 ps->set_property ("is-title", SCM_BOOL_T);
347 system_specs = scm_cons (ps->self_scm (), system_specs);
350 // FIXME: figure out penalty.
351 //set_system_penalty (ps, scores_[i].header_);
357 system_specs = scm_reverse_x (system_specs, SCM_EOL);
362 Paper_book::systems ()
364 if (systems_ != SCM_BOOL_F)
368 SCM specs = get_system_specs ();
369 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
371 if (Paper_score *pscore = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
373 SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
374 system_list = scm_reverse (system_list);
375 systems_ = scm_append (scm_list_2 (system_list, systems_));
379 systems_ = scm_cons (scm_car (s), systems_);
383 systems_ = scm_reverse (systems_);
385 /* backwards compatibility for the old page breaker */
388 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
390 Prob *ps = unsmob_prob (scm_car (s));
391 ps->set_property ("number", scm_from_int (++i));
394 && to_boolean (last->get_property ("is-title"))
395 && !scm_is_number (ps->get_property ("penalty")))
396 ps->set_property ("penalty", scm_from_int (10000));
399 if (scm_is_pair (scm_cdr (s)))
401 SCM perm = ps->get_property ("page-break-permission");
402 Prob *next = unsmob_prob (scm_cadr (s));
404 next->set_property ("penalty", scm_from_int (10001));
405 else if (perm == ly_symbol2scm ("force"))
406 next->set_property ("penalty", scm_from_int (-10001));
416 if (SCM_BOOL_F != pages_)
420 SCM proc = paper_->c_variable ("page-breaking-wrapper");
421 pages_ = scm_apply_0 (proc, scm_list_1 (self_scm ()));
423 /* set systems_ from the pages */
424 if (systems_ == SCM_BOOL_F)
427 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
429 Prob *page = unsmob_prob (scm_car (p));
430 SCM systems = page->get_property ("lines");
432 systems_ = scm_append (scm_list_2 (systems_, systems));
440 Paper_book::performances () const
442 return scm_reverse (performances_);