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-score.hh"
15 #include "paper-system.hh"
16 #include "text-interface.hh"
19 #include "ly-smobs.icc"
21 Paper_book::Paper_book ()
27 performances_ = SCM_EOL;
28 systems_ = SCM_BOOL_F;
34 Paper_book::~Paper_book ()
38 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
39 IMPLEMENT_SMOBS (Paper_book);
40 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?");
43 Paper_book::mark_smob (SCM smob)
45 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
47 scm_gc_mark (b->paper_->self_scm ());
48 scm_gc_mark (b->header_);
49 scm_gc_mark (b->header_0_);
50 scm_gc_mark (b->pages_);
51 scm_gc_mark (b->performances_);
52 scm_gc_mark (b->scores_);
57 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
59 Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
61 scm_puts ("#<Paper_book>", port);
69 for (vsize i = dump_header_fieldnames_global.size (); i--;)
71 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
77 Paper_book::add_score (SCM s)
79 scores_ = scm_cons (s, scores_);
83 Paper_book::add_performance (SCM s)
85 performances_ = scm_cons (s, performances_);
89 Paper_book::output (SCM output_channel)
91 if (scm_is_pair (performances_))
93 SCM proc = ly_lily_module_constant ("paper-book-write-midis");
95 scm_call_2 (proc, self_scm (), output_channel);
98 if (scores_ == SCM_EOL)
101 /* Generate all stencils to trigger font loads. */
104 SCM scopes = SCM_EOL;
105 if (ly_is_module (header_))
106 scopes = scm_cons (header_, scopes);
108 string mod_nm = "scm framework-" + output_backend_global;
110 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
113 SCM func = scm_c_module_lookup (mod, "output-framework");
115 func = scm_variable_ref (func);
116 scm_apply_0 (func, scm_list_n (output_channel,
125 SCM func = scm_c_module_lookup (mod, "output-preview-framework");
126 func = scm_variable_ref (func);
127 scm_apply_0 (func, scm_list_n (output_channel,
136 Paper_book::classic_output (SCM output)
138 /* Generate all stencils to trigger font loads. */
141 SCM scopes = SCM_EOL;
142 if (ly_is_module (header_))
143 scopes = scm_cons (header_, scopes);
145 if (ly_is_module (header_0_))
146 scopes = scm_cons (header_0_, scopes);
148 string format = output_backend_global;
149 string mod_nm = "scm framework-" + format;
151 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
152 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
154 func = scm_variable_ref (func);
155 scm_apply_0 (func, scm_list_n (output,
161 progress_indication ("\n");
164 /* TODO: resurrect more complex user-tweaks for titling? */
166 Paper_book::book_title ()
168 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
171 SCM scopes = SCM_EOL;
172 if (ly_is_module (header_))
173 scopes = scm_cons (header_, scopes);
176 if (ly_is_procedure (title_func))
177 tit = scm_call_2 (title_func,
181 if (unsmob_stencil (tit))
182 title = *unsmob_stencil (tit);
184 if (!title.is_empty ())
185 title.align_to (Y_AXIS, UP);
191 Paper_book::score_title (SCM header)
193 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
197 SCM scopes = SCM_EOL;
198 if (ly_is_module (header_))
199 scopes = scm_cons (header_, scopes);
201 if (ly_is_module (header))
202 scopes = scm_cons (header, scopes);
205 if (ly_is_procedure (title_func))
206 tit = scm_call_2 (title_func,
210 if (unsmob_stencil (tit))
211 title = *unsmob_stencil (tit);
213 if (!title.is_empty ())
214 title.align_to (Y_AXIS, UP);
219 /* read the breakbefore property of a score block and set up the preceding
220 system-spec to honour it. That is, SYM should be the system spec that
221 immediately precedes the score (from which HEADER is taken)
222 in the get_system_specs() list */
224 set_system_penalty (SCM sys, SCM header)
226 if (ly_is_module (header))
228 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
229 if (SCM_VARIABLEP (force)
230 && scm_is_bool (SCM_VARIABLE_REF (force)))
232 bool b = to_boolean (SCM_VARIABLE_REF (force));
233 SCM sym = b ? ly_symbol2scm ("force") : SCM_EOL;
235 if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
237 vector<Grob*> cols = ps->get_columns ();
239 cols.back ()->set_property ("page-break-permission", sym);
241 else if (Prob *pb = unsmob_prob (sys))
242 pb->set_property ("page-break-permission", sym);
249 Paper_book::get_score_title (SCM header)
251 Stencil title = score_title (header);
252 if (title.is_empty ())
253 title = score_title (header_);
254 if (!title.is_empty ())
257 TODO: this should come from the \layout {} block, which should
258 override settings from \paper {}
260 SCM props = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
261 Prob *ps = make_paper_system (props);
262 paper_system_set_stencil (ps, title);
264 return ps->self_scm();
272 Paper_book::get_system_specs ()
274 SCM system_specs = SCM_EOL;
276 Stencil title = book_title ();
277 if (!title.is_empty ())
279 SCM props = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
280 Prob *ps = make_paper_system (props);
281 paper_system_set_stencil (ps, title);
283 system_specs = scm_cons (ps->self_scm (), system_specs);
288 = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
289 paper_->self_scm ());
291 SCM header = SCM_EOL;
292 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
294 if (ly_is_module (scm_car (s)))
296 header = scm_car (s);
297 if (header_0_ == SCM_EOL)
300 else if (Music_output *mop = unsmob_music_output (scm_car (s)))
302 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
304 SCM title = get_score_title (header);
306 if (scm_is_pair (system_specs))
307 set_system_penalty (scm_car (system_specs), header);
309 if (unsmob_prob (title))
311 system_specs = scm_cons (title, system_specs);
312 unsmob_prob (title)->unprotect ();
316 system_specs = scm_cons (pscore->self_scm (), system_specs);
325 else if (Text_interface::is_markup (scm_car (s)))
327 SCM t = Text_interface::interpret_markup (paper_->self_scm (),
332 Prob *ps = make_paper_system (SCM_EOL);
333 paper_system_set_stencil (ps, *unsmob_stencil (t));
334 ps->set_property ("is-title", SCM_BOOL_T);
335 system_specs = scm_cons (ps->self_scm (), system_specs);
338 // FIXME: figure out penalty.
339 //set_system_penalty (ps, scores_[i].header_);
345 system_specs = scm_reverse_x (system_specs, SCM_EOL);
350 Paper_book::systems ()
352 if (systems_ != SCM_BOOL_F)
356 SCM specs = get_system_specs ();
357 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
359 if (Paper_score *pscore = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
361 SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
362 system_list = scm_reverse (system_list);
363 systems_ = scm_append (scm_list_2 (system_list, systems_));
367 systems_ = scm_cons (scm_car (s), systems_);
371 systems_ = scm_reverse (systems_);
375 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
377 Prob *ps = unsmob_prob (scm_car (s));
378 ps->set_property ("number", scm_from_int (++i));
381 && to_boolean (last->get_property ("is-title"))
382 && !scm_is_number (ps->get_property ("penalty")))
383 ps->set_property ("penalty", scm_from_int (10000));
393 if (SCM_BOOL_F != pages_)
397 SCM proc = paper_->c_variable ("page-breaking");
398 pages_ = scm_apply_0 (proc, scm_list_1(self_scm ()));
400 /* set systems_ from the pages */
401 if (systems_ == SCM_BOOL_F)
404 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
406 Prob *page = unsmob_prob (scm_car (p));
407 SCM systems = page->get_property ("lines");
409 systems_ = scm_append (scm_list_2 (systems_, systems));
417 Paper_book::performances () const
419 return scm_reverse (performances_);