2 paper-book.cc -- implement Paper_book
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "ly-module.hh"
11 #include "output-def.hh"
12 #include "paper-book.hh"
13 #include "paper-outputter.hh"
14 #include "paper-score.hh"
15 #include "paper-system.hh"
19 #include "ly-smobs.icc"
21 Paper_book::Paper_book ()
24 systems_ = SCM_BOOL_F;
31 Paper_book::~Paper_book ()
35 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
36 IMPLEMENT_SMOBS (Paper_book)
37 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
40 Paper_book::mark_smob (SCM smob)
42 Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
43 for (int i = 0; i < b->score_systems_.size (); i++)
44 b->score_systems_[i].gc_mark ();
47 scm_gc_mark (b->bookpaper_->self_scm ());
48 scm_gc_mark (b->header_);
49 scm_gc_mark (b->pages_);
54 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
56 Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
58 scm_puts ("#<", port);
59 scm_puts (classname (b), port);
61 //scm_puts (b->, port);
67 split_string (String s, char c)
76 s = s.nomid_string (0, 1);
83 rv.push (s.cut_string (0, i));
84 s = s.nomid_string (0, i);
94 for (int i = dump_header_fieldnames_global.size (); i--; )
96 = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
101 LY_DEFINE (ly_output_formats, "ly:output-formats",
103 "Formats passed to --format as a list of strings, "
104 "used for the output.")
106 Array<String> output_formats = split_string (output_format_global, ',');
109 int output_formats_count = output_formats.size ();
110 for (int i = 0; i < output_formats_count; i ++)
111 lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst);
117 Paper_book::post_processing (SCM module,
123 char const *func_name_;
125 {make_tex, "convert-to-tex"},
126 {make_dvi, "convert-to-dvi"},
127 {make_ps, "convert-to-ps"},
128 {make_pdf, "convert-to-pdf"},
129 {make_png, "convert-to-png"},
133 for (int i= 0; settings[i].func_name_; i++)
135 if (settings[i].do_it_)
137 SCM func = scm_c_module_lookup (module, settings[i].func_name_);
138 if (scm_variable_p (func) == SCM_BOOL_T)
140 func = scm_variable_ref (func);
141 if (ly_c_procedure_p (func))
142 scm_call_2 (func, self_scm(), file_name);
149 Paper_book::output (String outname)
151 if (!score_systems_.size ())
154 /* Generate all stencils to trigger font loads. */
157 SCM formats = ly_output_formats ();
158 for (SCM s = formats; scm_is_pair (s); s = scm_cdr (s))
160 String format = ly_scm2string (scm_car (s));
161 String file_name = outname;
163 if (file_name != "-")
164 file_name += "." + format;
166 Paper_outputter *out = get_paper_outputter (file_name, format);
168 SCM scopes = SCM_EOL;
169 if (ly_c_module_p (header_))
170 scopes = scm_cons (header_, scopes);
172 String mod_nm = "scm framework-" + format;
174 SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
175 SCM func = scm_c_module_lookup (mod, "output-framework");
177 func = scm_variable_ref (func);
178 scm_apply_0 (func, scm_list_n (out->self_scm (),
182 scm_makfrom0str (outname.to_str0 ()),
185 scm_gc_unprotect_object (out->self_scm ());
187 post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
191 String file_name = outname + ".preview." + format;
192 Paper_outputter *out = get_paper_outputter (file_name, format);
194 SCM func = scm_c_module_lookup (mod, "output-preview-framework");
195 func = scm_variable_ref (func);
196 scm_apply_0 (func, scm_list_n (out->self_scm (),
200 scm_makfrom0str (outname.to_str0 ()),
204 scm_gc_unprotect_object (out->self_scm ());
206 post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
209 progress_indication ("\n");
214 Paper_book::classic_output (String outname)
216 /* Generate all stencils to trigger font loads. */
220 SCM scopes = SCM_EOL;
221 if (ly_c_module_p (header_))
222 scopes = scm_cons (header_, scopes);
224 if (ly_c_module_p (score_systems_[0].header_))
225 scopes = scm_cons (score_systems_[0].header_, scopes);
228 Array<String> output_formats = split_string (output_format_global, ',');
230 for (int i = 0; i < output_formats.size (); i++)
232 String format = output_formats[i];
233 String mod_nm = "scm framework-" + format;
235 SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
236 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
238 func = scm_variable_ref (func);
240 Paper_outputter *out = get_paper_outputter (outname + "." + format,
243 scm_apply_0 (func, scm_list_n (out->self_scm (), self_scm (), scopes,
245 scm_makfrom0str (outname.to_str0 ()),
248 scm_gc_unprotect_object (out->self_scm ());
249 progress_indication ("\n");
253 LY_DEFINE (ly_paper_book_pages, "ly:paper-book-pages",
255 "Return pages in book PB.")
257 return unsmob_paper_book(pb)->pages ();
260 LY_DEFINE (ly_paper_book_scopes, "ly:paper-book-scopes",
262 "Return pages in paper book @var{book}.")
264 Paper_book *pb = unsmob_paper_book(book);
265 SCM_ASSERT_TYPE(pb, book, SCM_ARG1, __FUNCTION__, "Paper_book");
267 SCM scopes = SCM_EOL;
268 if (ly_c_module_p (pb->header_))
269 scopes = scm_cons (pb->header_, scopes);
274 LY_DEFINE (ly_paper_book_systems, "ly:paper-book-systems",
276 "Return systems in book PB.")
278 return unsmob_paper_book (pb)->systems ();
281 LY_DEFINE (ly_paper_book_book_paper, "ly:paper-book-book-paper",
283 "Return pages in book PB.")
285 return unsmob_paper_book (pb)->bookpaper_->self_scm ();
288 /* TODO: resurrect more complex user-tweaks for titling? */
290 Paper_book::book_title ()
292 SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
295 SCM scopes = SCM_EOL;
296 if (ly_c_module_p (header_))
297 scopes = scm_cons (header_, scopes);
301 if (ly_c_procedure_p (title_func))
302 tit = scm_call_2 (title_func,
303 bookpaper_->self_scm (),
306 if (unsmob_stencil (tit))
307 title = *unsmob_stencil (tit);
309 if (!title.is_empty ())
310 title.align_to (Y_AXIS, UP);
316 Paper_book::score_title (int i)
318 SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
323 SCM scopes = SCM_EOL;
324 if (ly_c_module_p (header_))
325 scopes = scm_cons (header_, scopes);
327 if (ly_c_module_p (score_systems_[i].header_))
328 scopes = scm_cons (score_systems_[i].header_, scopes);
332 if (ly_c_procedure_p (title_func))
333 tit = scm_call_2 (title_func,
334 bookpaper_->self_scm (),
337 if (unsmob_stencil (tit))
338 title = *unsmob_stencil (tit);
340 if (!title.is_empty ())
341 title.align_to (Y_AXIS, UP);
347 Paper_book::systems ()
349 if (systems_ != SCM_BOOL_F)
353 Stencil title = book_title ();
355 if (!title.is_empty ())
357 Paper_system *ps = new Paper_system (title, true);
358 systems_ = scm_cons (ps->self_scm (), systems_);
359 scm_gc_unprotect_object (ps->self_scm ());
362 int score_count = score_systems_.size ();
363 for (int i = 0; i < score_count; i++)
365 Stencil title = score_title (i);
366 if (!title.is_empty ())
368 Paper_system *ps = new Paper_system (title, true);
369 systems_ = scm_cons (ps->self_scm (), systems_);
370 scm_gc_unprotect_object (ps->self_scm ());
373 if (scm_vector_p (score_systems_[i].systems_) == SCM_BOOL_T)
376 SCM system_list = scm_vector_to_list (score_systems_[i].systems_);
378 system_list = scm_reverse (system_list);
379 systems_ = scm_append (scm_list_2 (system_list, systems_));
383 systems_ = scm_reverse (systems_);
386 Paper_system *last = 0;
387 for (SCM s = systems_; s != SCM_EOL; s = scm_cdr (s))
389 Paper_system *ps = unsmob_paper_system (scm_car (s));
392 if (last && last->is_title ())
394 ps->penalty_ = 10000;
404 if (SCM_BOOL_F != pages_)
408 SCM proc = bookpaper_->c_variable ("page-breaking");
409 pages_ = scm_apply_0 (proc, scm_list_2 (systems (), self_scm ()));
414 /****************************************************************/
416 Score_systems::Score_systems ()
423 Score_systems::gc_mark ()
425 scm_gc_mark (systems_);
426 scm_gc_mark (header_);