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 ());
114 if (get_program_option ("print-pages"))
116 SCM func = scm_c_module_lookup (mod, "output-framework");
118 func = scm_variable_ref (func);
119 scm_apply_0 (func, scm_list_n (output_channel,
126 if (get_program_option ("preview"))
128 SCM func = scm_c_module_lookup (mod, "output-preview-framework");
129 func = scm_variable_ref (func);
130 scm_apply_0 (func, scm_list_n (output_channel,
139 Paper_book::classic_output (SCM output)
141 if (scm_is_pair (performances_))
143 SCM proc = ly_lily_module_constant ("write-performances-midis");
145 scm_call_2 (proc, performances (), output);
148 /* Generate all stencils to trigger font loads. */
151 SCM scopes = SCM_EOL;
152 if (ly_is_module (header_))
153 scopes = scm_cons (header_, scopes);
155 if (ly_is_module (header_0_))
156 scopes = scm_cons (header_0_, scopes);
158 string format = get_output_backend_name ();
159 string mod_nm = "scm framework-" + format;
161 SCM mod = scm_c_resolve_module (mod_nm.c_str ());
162 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
164 func = scm_variable_ref (func);
165 scm_apply_0 (func, scm_list_n (output,
171 progress_indication ("\n");
174 /* TODO: resurrect more complex user-tweaks for titling? */
176 Paper_book::book_title ()
178 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
181 SCM scopes = SCM_EOL;
182 if (ly_is_module (header_))
183 scopes = scm_cons (header_, scopes);
186 if (ly_is_procedure (title_func))
187 tit = scm_call_2 (title_func,
191 if (unsmob_stencil (tit))
192 title = *unsmob_stencil (tit);
194 if (!title.is_empty ())
195 title.align_to (Y_AXIS, UP);
201 Paper_book::score_title (SCM header)
203 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
207 SCM scopes = SCM_EOL;
208 if (ly_is_module (header_))
209 scopes = scm_cons (header_, scopes);
211 if (ly_is_module (header))
212 scopes = scm_cons (header, scopes);
215 if (ly_is_procedure (title_func))
216 tit = scm_call_2 (title_func,
220 if (unsmob_stencil (tit))
221 title = *unsmob_stencil (tit);
223 if (!title.is_empty ())
224 title.align_to (Y_AXIS, UP);
229 /* read the breakbefore property of a score block and set up the preceding
230 system-spec to honour it. That is, SYM should be the system spec that
231 immediately precedes the score (from which HEADER is taken)
232 in the get_system_specs () list */
234 set_system_penalty (SCM sys, SCM header)
236 if (ly_is_module (header))
238 SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
239 if (SCM_VARIABLEP (force)
240 && scm_is_bool (SCM_VARIABLE_REF (force)))
242 bool b = to_boolean (SCM_VARIABLE_REF (force));
243 SCM sym = b ? ly_symbol2scm ("force") : SCM_EOL;
245 if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
247 vector<Grob*> cols = ps->get_columns ();
250 Paper_column *col = dynamic_cast<Paper_column*> (cols.back ());
251 col->set_property ("page-break-permission", sym);
252 col->find_prebroken_piece (LEFT)->set_property ("page-break-permission", sym);
255 else if (Prob *pb = unsmob_prob (sys))
256 pb->set_property ("page-break-permission", sym);
263 Paper_book::get_score_title (SCM header)
265 Stencil title = score_title (header);
266 if (title.is_empty ())
267 title = score_title (header_);
268 if (!title.is_empty ())
271 TODO: this should come from the \layout {} block, which should
272 override settings from \paper {}
274 SCM props = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
275 Prob *ps = make_paper_system (props);
276 paper_system_set_stencil (ps, title);
278 return ps->self_scm ();
286 Paper_book::get_system_specs ()
288 SCM system_specs = SCM_EOL;
290 Stencil title = book_title ();
291 if (!title.is_empty ())
293 SCM props = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
294 Prob *ps = make_paper_system (props);
295 paper_system_set_stencil (ps, title);
297 system_specs = scm_cons (ps->self_scm (), system_specs);
302 = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
303 paper_->self_scm ());
305 SCM header = SCM_EOL;
306 for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
308 if (ly_is_module (scm_car (s)))
310 header = scm_car (s);
311 if (header_0_ == SCM_EOL)
314 else if (Music_output *mop = unsmob_music_output (scm_car (s)))
316 if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
318 SCM title = get_score_title (header);
320 if (scm_is_pair (system_specs))
321 set_system_penalty (scm_car (system_specs), header);
323 if (unsmob_prob (title))
325 system_specs = scm_cons (title, system_specs);
326 unsmob_prob (title)->unprotect ();
330 system_specs = scm_cons (pscore->self_scm (), system_specs);
339 else if (Text_interface::is_markup (scm_car (s)))
341 SCM t = Text_interface::interpret_markup (paper_->self_scm (),
346 Prob *ps = make_paper_system (SCM_EOL);
347 /* we don't have a way of specifying page-{break,turn} on
348 markup blocks, so for now we just set everything turnable
349 and breakable by default */
350 ps->set_property ("page-break-permission", ly_symbol2scm ("allow"));
351 ps->set_property ("page-turn-permission", ly_symbol2scm ("allow"));
353 paper_system_set_stencil (ps, *unsmob_stencil (t));
354 ps->set_property ("is-title", SCM_BOOL_T);
355 system_specs = scm_cons (ps->self_scm (), system_specs);
358 // FIXME: figure out penalty.
359 //set_system_penalty (ps, scores_[i].header_);
365 system_specs = scm_reverse_x (system_specs, SCM_EOL);
370 Paper_book::systems ()
372 if (systems_ != SCM_BOOL_F)
376 SCM specs = get_system_specs ();
377 for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
379 if (Paper_score *pscore = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
381 SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
382 system_list = scm_reverse (system_list);
383 systems_ = scm_append (scm_list_2 (system_list, systems_));
387 systems_ = scm_cons (scm_car (s), systems_);
391 systems_ = scm_reverse (systems_);
393 /* backwards compatibility for the old page breaker */
396 for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
398 Prob *ps = unsmob_prob (scm_car (s));
399 ps->set_property ("number", scm_from_int (++i));
402 && to_boolean (last->get_property ("is-title"))
403 && !scm_is_number (ps->get_property ("penalty")))
404 ps->set_property ("penalty", scm_from_int (10000));
407 if (scm_is_pair (scm_cdr (s)))
409 SCM perm = ps->get_property ("page-break-permission");
410 Prob *next = unsmob_prob (scm_cadr (s));
412 next->set_property ("penalty", scm_from_int (10001));
413 else if (perm == ly_symbol2scm ("force"))
414 next->set_property ("penalty", scm_from_int (-10001));
424 if (SCM_BOOL_F != pages_)
428 SCM proc = paper_->c_variable ("page-breaking-wrapper");
429 pages_ = scm_apply_0 (proc, scm_list_1 (self_scm ()));
431 /* set systems_ from the pages */
432 if (systems_ == SCM_BOOL_F)
435 for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
437 Prob *page = unsmob_prob (scm_car (p));
438 SCM systems = page->get_property ("lines");
440 systems_ = scm_append (scm_list_2 (systems_, systems));
448 Paper_book::performances () const
450 return scm_reverse (performances_);