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->paper_->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 ());
177 SCM func = scm_c_module_lookup (mod, "output-framework");
179 func = scm_variable_ref (func);
180 scm_apply_0 (func, scm_list_n (out->self_scm (),
184 scm_makfrom0str (outname.to_str0 ()),
187 scm_gc_unprotect_object (out->self_scm ());
188 post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
193 String file_name = outname + ".preview." + format;
194 Paper_outputter *out = get_paper_outputter (file_name, format);
196 SCM func = scm_c_module_lookup (mod, "output-preview-framework");
197 func = scm_variable_ref (func);
198 scm_apply_0 (func, scm_list_n (out->self_scm (),
202 scm_makfrom0str (outname.to_str0 ()),
206 scm_gc_unprotect_object (out->self_scm ());
208 post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
211 progress_indication ("\n");
215 Paper_book::classic_output (String outname)
217 /* Generate all stencils to trigger font loads. */
221 SCM scopes = SCM_EOL;
222 if (ly_c_module_p (header_))
223 scopes = scm_cons (header_, scopes);
225 if (ly_c_module_p (score_systems_[0].header_))
226 scopes = scm_cons (score_systems_[0].header_, scopes);
229 Array<String> output_formats = split_string (output_format_global, ',');
231 for (int i = 0; i < output_formats.size (); i++)
233 String format = output_formats[i];
234 String mod_nm = "scm framework-" + format;
236 SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
237 SCM func = scm_c_module_lookup (mod, "output-classic-framework");
239 func = scm_variable_ref (func);
241 Paper_outputter *out = get_paper_outputter (outname + "." + format,
244 scm_apply_0 (func, scm_list_n (out->self_scm (), self_scm (), scopes,
246 scm_makfrom0str (outname.to_str0 ()),
249 scm_gc_unprotect_object (out->self_scm ());
250 progress_indication ("\n");
254 LY_DEFINE (ly_paper_book_pages, "ly:paper-book-pages",
256 "Return pages in book PB.")
258 return unsmob_paper_book(pb)->pages ();
261 LY_DEFINE (ly_paper_book_scopes, "ly:paper-book-scopes",
263 "Return pages in layout book @var{book}.")
265 Paper_book *pb = unsmob_paper_book(book);
266 SCM_ASSERT_TYPE(pb, book, SCM_ARG1, __FUNCTION__, "Paper_book");
268 SCM scopes = SCM_EOL;
269 if (ly_c_module_p (pb->header_))
270 scopes = scm_cons (pb->header_, scopes);
275 LY_DEFINE (ly_paper_book_systems, "ly:paper-book-systems",
277 "Return systems in book PB.")
279 return unsmob_paper_book (pb)->systems ();
282 LY_DEFINE (ly_paper_book_paper, "ly:paper-book-paper",
284 "Return pages in book PB.")
286 return unsmob_paper_book (pb)->paper_->self_scm ();
289 /* TODO: resurrect more complex user-tweaks for titling? */
291 Paper_book::book_title ()
293 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
296 SCM scopes = SCM_EOL;
297 if (ly_c_module_p (header_))
298 scopes = scm_cons (header_, scopes);
302 if (ly_c_procedure_p (title_func))
303 tit = scm_call_2 (title_func,
307 if (unsmob_stencil (tit))
308 title = *unsmob_stencil (tit);
310 if (!title.is_empty ())
311 title.align_to (Y_AXIS, UP);
317 Paper_book::score_title (int i)
319 SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
324 SCM scopes = SCM_EOL;
325 if (ly_c_module_p (header_))
326 scopes = scm_cons (header_, scopes);
328 if (ly_c_module_p (score_systems_[i].header_))
329 scopes = scm_cons (score_systems_[i].header_, scopes);
333 if (ly_c_procedure_p (title_func))
334 tit = scm_call_2 (title_func,
338 if (unsmob_stencil (tit))
339 title = *unsmob_stencil (tit);
341 if (!title.is_empty ())
342 title.align_to (Y_AXIS, UP);
348 Paper_book::systems ()
350 if (systems_ != SCM_BOOL_F)
354 Stencil title = book_title ();
356 if (!title.is_empty ())
358 Paper_system *ps = new Paper_system (title, true);
359 systems_ = scm_cons (ps->self_scm (), systems_);
360 scm_gc_unprotect_object (ps->self_scm ());
363 int score_count = score_systems_.size ();
364 for (int i = 0; i < score_count; i++)
366 Stencil title = score_title (i);
367 if (!title.is_empty ())
369 Paper_system *ps = new Paper_system (title, true);
370 systems_ = scm_cons (ps->self_scm (), systems_);
371 scm_gc_unprotect_object (ps->self_scm ());
374 if (scm_vector_p (score_systems_[i].systems_) == SCM_BOOL_T)
377 SCM system_list = scm_vector_to_list (score_systems_[i].systems_);
379 system_list = scm_reverse (system_list);
380 systems_ = scm_append (scm_list_2 (system_list, systems_));
384 systems_ = scm_reverse (systems_);
387 Paper_system *last = 0;
388 for (SCM s = systems_; s != SCM_EOL; s = scm_cdr (s))
390 Paper_system *ps = unsmob_paper_system (scm_car (s));
393 if (last && last->is_title ())
395 ps->penalty_ = 10000;
405 if (SCM_BOOL_F != pages_)
409 SCM proc = paper_->c_variable ("page-breaking");
410 pages_ = scm_apply_0 (proc, scm_list_2 (systems (), self_scm ()));
415 /****************************************************************/
417 Score_systems::Score_systems ()
424 Score_systems::gc_mark ()
426 scm_gc_mark (systems_);
427 scm_gc_mark (header_);