From: Han-Wen Nienhuys Date: Tue, 4 Jan 2005 23:28:41 +0000 (+0000) Subject: * scm/lily.scm (completize-formats): new function X-Git-Tag: release/2.5.14~315 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=a0c5f43a69fc72f878d7b3f635030f67f1a535db;p=lilypond.git * scm/lily.scm (completize-formats): new function (postprocess-output): new function * lily/paper-book.cc (classic_output): change calling convention. Give basename as first argument. remove Paper_book::post_processing(). * lily/lily-guile.cc (LY_DEFINE): ly:output-backend, new function. --- diff --git a/ChangeLog b/ChangeLog index 4d7e7aebef..2c46f45224 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,19 @@ +2005-01-05 Han-Wen Nienhuys + + * scm/lily.scm (completize-formats): new function + (postprocess-output): new function + + * lily/paper-book.cc (classic_output): change calling + convention. Give basename as first argument. + remove Paper_book::post_processing(). + + * lily/lily-guile.cc (LY_DEFINE): ly:output-backend, new function. + 2005-01-04 Han-Wen Nienhuys + * lily/paper-outputter.cc (LY_DEFINE): new function + ly:outputter-close. + * lily/main.cc: change --format,-f to --backend,-b * lily/include/main.hh: rename format to backend. diff --git a/Documentation/user/invoking.itely b/Documentation/user/invoking.itely index 1fdeee0ff3..adfd8ca366 100644 --- a/Documentation/user/invoking.itely +++ b/Documentation/user/invoking.itely @@ -55,8 +55,11 @@ sequentially. The function @code{ly:set-option} allows access to some internal variables. Use @code{-e '(ly:option-usage)'} for more information. - @item -f,--format=@var{format} +which formats should be written. Choices are @code{svg}, @code{ps}, +@code{pdf}, @code{png}, @code{tex}, @code{dvi}. + +@item -b,--backend=@var{format} the output format to use for the back-end. Choices are @table @code @item tex diff --git a/input/simple.ly b/input/simple.ly index 0663d906cf..835aaed41a 100644 --- a/input/simple.ly +++ b/input/simple.ly @@ -1,6 +1,6 @@ %% A simple piece in LilyPond, a scale. \relative { - c' d e f g a b c + c d e f g a b c } %% Optional helper for automatic updating by convert-ly. May be omitted. diff --git a/lily/include/main.hh b/lily/include/main.hh index 3ff399afc5..902cccc0c2 100644 --- a/lily/include/main.hh +++ b/lily/include/main.hh @@ -43,6 +43,8 @@ extern bool make_dvi; extern bool make_ps; extern bool make_pdf; extern bool make_tex; +extern String output_format_global; + extern bool make_preview; extern bool make_pages; diff --git a/lily/include/misc.hh b/lily/include/misc.hh index 754379d477..72da2c90e9 100644 --- a/lily/include/misc.hh +++ b/lily/include/misc.hh @@ -1,3 +1,12 @@ +/* + misc.hh -- declare miscellaneous functions. + + source file of the GNU LilyPond music typesetter + + (c) 2005 Han-Wen Nienhuys + +*/ + #ifndef MISC_HH #define MISC_HH @@ -19,7 +28,7 @@ sign (int i) else return 0; } - +Array split_string (String s, char c); inline Real linear_interpolate (Real x, Real x1, Real x2, Real y1, Real y2) @@ -28,7 +37,5 @@ linear_interpolate (Real x, Real x1, Real x2, Real y1, Real y2) (x - x1) / (x2 - x1) * y2 ; } - - #endif diff --git a/lily/lily-guile.cc b/lily/lily-guile.cc index fa0ad5f8e0..524d8e9178 100644 --- a/lily/lily-guile.cc +++ b/lily/lily-guile.cc @@ -36,6 +36,7 @@ inline int my_isnan (Real r) { return isnan (r); } #include "pitch.hh" #include "dimensions.hh" #include "source-file.hh" +#include "misc.hh" // #define TEST_GC @@ -853,3 +854,28 @@ LY_DEFINE (ly_gettext, "ly:gettext", return scm_makfrom0str (gettext (scm_i_string_chars (string))); } + + + +LY_DEFINE (ly_output_backend, "ly:output-backend", + 0, 0, 0, (), + "Return name of output backend.") +{ + return scm_makfrom0str (output_backend_global.to_str0 ()); +} + + +LY_DEFINE (ly_output_formats, "ly:output-formats", + 0, 0, 0, (), + "Formats passed to --format as a list of strings, " + "used for the output.") +{ + Array output_formats = split_string (output_format_global, ','); + + SCM lst = SCM_EOL; + int output_formats_count = output_formats.size (); + for (int i = 0; i < output_formats_count; i ++) + lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst); + + return lst; +} diff --git a/lily/main.cc b/lily/main.cc index ba9c0bc950..b2a55d1ae8 100644 --- a/lily/main.cc +++ b/lily/main.cc @@ -47,6 +47,7 @@ bool no_layout_global_b = false; One of tex, ps, scm, as. */ String output_backend_global = "ps"; +String output_format_global = "pdf"; /* Current output name. */ String output_name_global; @@ -62,13 +63,9 @@ bool verbose_global_b = false; */ String init_scheme_code_string = "(begin #t "; -bool make_pdf = false; -bool make_dvi = false; -bool make_ps = false; -bool make_png = false; bool make_preview = false; bool make_pages = true; -bool make_tex = false; + /* * Miscellaneous global stuff. @@ -128,6 +125,7 @@ static Long_option_init options_static[] = /* Bug in option parser: --output =foe is taken as an abbreviation for --output-format. */ {_i ("EXT"), "backend", 'b', _i ("select back-end to use")}, + {_i ("EXTs"), "format", 'f', _i ("list of formats to dump")}, {0, "help", 'h', _i ("print this help")}, {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")}, {_i ("DIR"), "include", 'I', _i ("add DIR to search path")}, @@ -251,44 +249,6 @@ prepend_load_path (String dir) scm_c_eval_string (s.to_str0 ()); } -static void -determine_output_options () -{ - bool found_gnome = (output_backend_global == "gnome"); - bool found_svg = (output_backend_global == "svg"); - bool found_tex = (output_backend_global == "tex"); - - - if (make_pdf || make_png) - { - make_ps = true; - } - if (make_ps && found_tex) - { - make_dvi = true; - } - if (make_dvi && found_tex) - { - make_tex = true; - } - if (!found_gnome - && !found_svg - && !(make_dvi - || make_tex - || make_ps - || make_png - || make_pdf)) - { - make_pdf = true; - make_ps = true; - if (found_tex) - { - make_dvi = true; - make_tex = true; - } - } -} - void init_global_tweak_registry (); static void @@ -310,7 +270,6 @@ main_with_guile (void *, int, char **) call_constructors (); init_global_tweak_registry (); init_freetype (); - determine_output_options (); all_fonts_global = new All_font_metrics (global_path.to_string ()); init_scheme_code_string += ")"; @@ -409,15 +368,14 @@ parse_argv (int argc, char **argv) warranty (); exit (0); break; - case 'f': - if (option_parser->optional_argument_str0_ == "help") - { - printf (_ ("This option is for developers only.").to_str0 ()); - printf (_ ("Read the sources for more information.").to_str0 ()); - exit (0); - } + case 'b': output_backend_global = option_parser->optional_argument_str0_; break; + + case 'f': + output_format_global = option_parser->optional_argument_str0_; + break; + case 'H': dump_header_fieldnames_global .push (option_parser->optional_argument_str0_); diff --git a/lily/misc.cc b/lily/misc.cc index 649f72c900..8a1a3e94ca 100644 --- a/lily/misc.cc +++ b/lily/misc.cc @@ -7,9 +7,11 @@ Jan Nieuwenhuizen */ +#include + #include "misc.hh" +#include "string.hh" -#include /* Return the 2-log, rounded down @@ -35,4 +37,26 @@ log_2 (double x) return log (x) /log (2.0); } - +Array +split_string (String s, char c) +{ + Array rv; + while (s.length ()) + { + int i = s.index (c); + + if (i == 0) + { + s = s.nomid_string (0, 1); + continue; + } + + if (i < 0) + i = s.length () ; + + rv.push (s.cut_string (0, i)); + s = s.nomid_string (0, i); + } + + return rv; +} diff --git a/lily/paper-book.cc b/lily/paper-book.cc index 353d28572c..e6d63aa0cb 100644 --- a/lily/paper-book.cc +++ b/lily/paper-book.cc @@ -63,30 +63,6 @@ Paper_book::print_smob (SCM smob, SCM port, scm_print_state*) return 1; } -Array -split_string (String s, char c) -{ - Array rv; - while (s.length ()) - { - int i = s.index (c); - - if (i == 0) - { - s = s.nomid_string (0, 1); - continue; - } - - if (i < 0) - i = s.length () ; - - rv.push (s.cut_string (0, i)); - s = s.nomid_string (0, i); - } - - return rv; -} - SCM dump_fields () { @@ -98,38 +74,6 @@ dump_fields () return fields; } -void -Paper_book::post_processing (SCM module, - SCM file_name) -{ - struct - { - bool do_it_; - char const *func_name_; - } settings[] = { - {make_tex, "convert-to-tex"}, - {make_dvi, "convert-to-dvi"}, - {make_ps, "convert-to-ps"}, - {make_pdf, "convert-to-pdf"}, - {make_png, "convert-to-png"}, - {0, 0}, - }; - - for (int i = 0; settings[i].func_name_; i++) - { - if (settings[i].do_it_) - { - SCM func = scm_c_module_lookup (module, settings[i].func_name_); - if (scm_variable_p (func) == SCM_BOOL_T) - { - func = scm_variable_ref (func); - if (ly_c_procedure_p (func)) - scm_call_2 (func, self_scm (), file_name); - } - } - } -} - void Paper_book::output (String outname) { @@ -139,19 +83,11 @@ Paper_book::output (String outname) /* Generate all stencils to trigger font loads. */ pages (); - String format = output_backend_global; - String file_name = outname; - - if (file_name != "-") - file_name += "." + format; - - Paper_outputter *out = get_paper_outputter (file_name, format); - SCM scopes = SCM_EOL; if (ly_c_module_p (header_)) scopes = scm_cons (header_, scopes); - String mod_nm = "scm framework-" + format; + String mod_nm = "scm framework-" + output_backend_global; SCM mod = scm_c_resolve_module (mod_nm.to_str0 ()); if (make_pages) @@ -159,35 +95,22 @@ Paper_book::output (String outname) SCM func = scm_c_module_lookup (mod, "output-framework"); func = scm_variable_ref (func); - scm_apply_0 (func, scm_list_n (out->self_scm (), + scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()), self_scm (), scopes, dump_fields (), - scm_makfrom0str (outname.to_str0 ()), SCM_UNDEFINED)); - out->close (); - scm_gc_unprotect_object (out->self_scm ()); - post_processing (mod, scm_makfrom0str (file_name.to_str0 ())); } if (make_preview) { - String file_name = outname + ".preview." + format; - Paper_outputter *out = get_paper_outputter (file_name, format); - SCM func = scm_c_module_lookup (mod, "output-preview-framework"); func = scm_variable_ref (func); - scm_apply_0 (func, scm_list_n (out->self_scm (), + scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()), self_scm (), scopes, dump_fields (), - scm_makfrom0str (outname.to_str0 ()), SCM_UNDEFINED)); - - out->close (); - scm_gc_unprotect_object (out->self_scm ()); - - post_processing (mod, scm_makfrom0str (file_name.to_str0 ())); } progress_indication ("\n"); } @@ -215,15 +138,12 @@ Paper_book::classic_output (String outname) func = scm_variable_ref (func); - Paper_outputter *out = get_paper_outputter (outname + "." + format, - format); - - scm_apply_0 (func, scm_list_n (out->self_scm (), self_scm (), scopes, + scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()), + self_scm (), + scopes, dump_fields (), - scm_makfrom0str (outname.to_str0 ()), SCM_UNDEFINED)); - scm_gc_unprotect_object (out->self_scm ()); progress_indication ("\n"); } diff --git a/lily/paper-outputter.cc b/lily/paper-outputter.cc index 72135b9138..74b2a5b2d0 100644 --- a/lily/paper-outputter.cc +++ b/lily/paper-outputter.cc @@ -107,13 +107,28 @@ Paper_outputter::output_stencil (Stencil stil) (void*) this, Offset (0,0)); } -Paper_outputter * -get_paper_outputter (String outname, String f) +LY_DEFINE (ly_make_paper_outputter, "ly:make-paper-outputter", + 2, 0, 0, (SCM outname, SCM format), + "Create an outputter that evaluates within " + "@code{output-}@var{format}, writing to file @var{outname}.") { + SCM_ASSERT_TYPE(scm_is_string (outname), outname, SCM_ARG1, __FUNCTION__, + "String"); + SCM_ASSERT_TYPE(scm_is_string (format), format, SCM_ARG2, __FUNCTION__, + "String"); + + String outname_str = ly_scm2string (outname); + String f = ly_scm2string (format); + progress_indication (_f ("Layout output to `%s'...", - outname == "-" ? String ("") : outname)); + outname_str == "-" + ? String ("") + : outname_str)); progress_indication ("\n"); - return new Paper_outputter (outname, f); + Paper_outputter *po = new Paper_outputter (outname_str, f); + + scm_gc_unprotect_object (po->self_scm ()); + return po->self_scm (); } /* FIXME: why is output_* wrapper called dump? */ @@ -140,6 +155,18 @@ LY_DEFINE (ly_outputter_dump_string, "ly:outputter-dump-string", return po->dump_string (str); } + +LY_DEFINE (ly_outputter_close, "ly:outputter-close", + 1, 0, 0, (SCM outputter), + "Close port of @var{outputter}.") +{ + Paper_outputter *po = unsmob_outputter (outputter); + SCM_ASSERT_TYPE (po, outputter, SCM_ARG1, __FUNCTION__, "Paper_outputter"); + + po->close (); + return SCM_UNSPECIFIED; +} + void Paper_outputter::close () { diff --git a/scm/framework-ps.scm b/scm/framework-ps.scm index b34175b76e..5daf3e8343 100644 --- a/scm/framework-ps.scm +++ b/scm/framework-ps.scm @@ -14,6 +14,8 @@ (srfi srfi-13) (lily)) +(define framework-ps-module (current-module)) + (define (stderr string . rest) (apply format (cons (current-error-port) (cons string rest))) (force-output (current-error-port))) @@ -217,13 +219,19 @@ (load-fonts paper) (define-fonts paper))) -(define-public (output-framework outputter book scopes fields basename) - (let* ((paper (ly:paper-book-paper book)) + + + +(define-public (output-framework basename book scopes fields ) + (let* ((filename (format "~a.ps" basename)) + (outputter (ly:make-paper-outputter filename + (ly:output-backend))) + (paper (ly:paper-book-paper book)) (pages (ly:paper-book-pages book)) (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t)) (page-number (1- (ly:output-def-lookup paper 'firstpagenumber))) (page-count (length pages))) - + (for-each (lambda (x) (ly:outputter-dump-string outputter x)) @@ -237,10 +245,16 @@ (dump-page outputter page page-number page-count landscape?)) pages) - (ly:outputter-dump-string outputter "%%Trailer\n%%EOF\n"))) - -(define-public (output-preview-framework outputter book scopes fields basename) - (let* ((paper (ly:paper-book-paper book)) + (ly:outputter-dump-string outputter "%%Trailer\n%%EOF\n") + (ly:outputter-close outputter) + (postprocess-output book framework-ps-module filename (ly:output-formats)) +)) + +(define-public (output-preview-framework basename book scopes fields ) + (let* ((filename (format "~a.ps" basename)) + (outputter (ly:make-paper-outputter filename + (ly:output-backend))) + (paper (ly:paper-book-paper book)) (systems (ly:paper-book-systems book)) (scale (ly:output-def-lookup paper 'outputscale )) (titles (take-while ly:paper-system-title? systems)) @@ -279,7 +293,37 @@ "\n")) (ly:outputter-dump-stencil outputter dump-me) - (ly:outputter-dump-string outputter "} stop-system\n%%Trailer\n%%EOF\n"))) + (ly:outputter-dump-string outputter "} stop-system\n%%Trailer\n%%EOF\n") + (ly:outputter-close outputter) + (postprocess-output book framework-ps-module filename + (ly:output-formats)) +)) + + +(define-public (output-classic-framework + basename book scopes fields) + (let* ((paper (ly:paper-book-paper book)) + (lines (ly:paper-book-systems book)) + (last-line (car (last-pair lines)))) + (for-each + (lambda (x) + (ly:outputter-dump-string outputter x)) + (list + ;;FIXME + (header paper (length lines) #f) + "\\def\\lilypondclassic{1}%\n" + (output-scopes scopes fields basename) + (define-fonts paper) + (header-end))) + + (for-each + (lambda (line) (dump-line outputter line (eq? line last-line))) lines) + (ly:outputter-dump-string outputter "\\lilypondend\n") + (ly:outputter-close outputter) + (postprocess-output book framework-ps-module filename (ly:output-formats)) + )) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (convert-to-pdf book name) (let* ((defs (ly:paper-book-paper book)) diff --git a/scm/framework-tex.scm b/scm/framework-tex.scm index cc1b4eaa3b..4a2a0b6193 100644 --- a/scm/framework-tex.scm +++ b/scm/framework-tex.scm @@ -16,6 +16,17 @@ (srfi srfi-13) (lily)) +(define (output-formats) + (define formats (ly:output-formats)) + (set! formats (completize-formats formats)) + (if (member "ps" formats) + (set! formats (cons "dvi" formats))) + (if (member "dvi" formats) + (set! formats (cons "tex" formats))) + + formats) + +(define framework-tex-module (current-module)) (define-public (sanitize-tex-string s) (if (ly:get-option 'safe) (regexp-substitute/global @@ -207,8 +218,10 @@ "}%\n\\vfill\n" "}%\n\\vfill\n\\lilypondpagebreak\n"))) -(define-public (output-framework outputter book scopes fields basename ) - (let* ((paper (ly:paper-book-paper book)) +(define-public (output-framework basename book scopes fields) + (let* ((filename (format "~a.tex" basename)) + (outputter (ly:make-paper-outputter filename "tex")) + (paper (ly:paper-book-paper book)) (pages (ly:paper-book-pages book)) (last-page (car (last-pair pages))) (with-extents @@ -225,7 +238,10 @@ (lambda (page) (dump-page outputter page (eq? last-page page) with-extents)) pages) - (ly:outputter-dump-string outputter "\\lilypondend\n"))) + (ly:outputter-dump-string outputter "\\lilypondend\n") + (ly:outputter-close outputter) + (postprocess-output book framework-tex-module filename + (output-formats)))) (define (dump-line putter line last?) (ly:outputter-dump-string @@ -244,8 +260,10 @@ "}\\interscoreline\n"))) (define-public (output-classic-framework - outputter book scopes fields basename) - (let* ((paper (ly:paper-book-paper book)) + basename book scopes fields) + (let* ((filename (format "~a.tex" basename)) + (outputter (ly:make-paper-outputter filename "tex")) + (paper (ly:paper-book-paper book)) (lines (ly:paper-book-systems book)) (last-line (car (last-pair lines)))) (for-each @@ -261,11 +279,18 @@ (for-each (lambda (line) (dump-line outputter line (eq? line last-line))) lines) - (ly:outputter-dump-string outputter "\\lilypondend\n"))) + (ly:outputter-dump-string outputter "\\lilypondend\n") + (ly:outputter-close outputter) + (postprocess-output book framework-tex-module filename + (output-formats)) + )) (define-public (output-preview-framework - outputter book scopes fields basename ) - (let* ((paper (ly:paper-book-paper book)) + basename book scopes fields) + (let* ((filename (format "~a.tex" basename)) + (outputter (ly:make-paper-outputter filename + "tex")) + (paper (ly:paper-book-paper book)) (lines (ly:paper-book-systems book)) (first-notes-index (list-index (lambda (s) (not (ly:paper-system-title? s))) @@ -287,7 +312,12 @@ (lambda (lst) (dump-line outputter lst (not (ly:paper-system-title? lst)))) (take lines (1+ first-notes-index))) - (ly:outputter-dump-string outputter "\\lilypondend\n"))) + (ly:outputter-dump-string outputter "\\lilypondend\n") + (ly:outputter-close outputter) + (postprocess-output book framework-tex-module filename + (output-formats)) + +)) (define-public (convert-to-pdf book name) (let* ((defs (ly:paper-book-paper book)) diff --git a/scm/lily.scm b/scm/lily.scm index c5fda582e2..4f621724d5 100644 --- a/scm/lily.scm +++ b/scm/lily.scm @@ -269,6 +269,7 @@ predicates. Print a message at LOCATION if any predicate failed." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; backend helpers. (define-public (ly:system command) (let* ((status 0) @@ -321,6 +322,23 @@ predicates. Print a message at LOCATION if any predicate failed." name))) (ly:system cmd))) +(define-public (postprocess-output paper-book module filename formats) + (for-each (lambda (f) + ((eval (string->symbol (string-append "convert-to-" f)) + module) + paper-book filename)) + + formats)) + +(define-public (completize-formats formats) + (if (member "png" formats) + (set! formats (cons "ps" formats))) + (if (member "pdf" formats) + (set! formats (cons "ps" formats))) + + formats) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + (define-public (lilypond-main files) "Entry point for LilyPond." (let* ((failed '())