X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=scm%2Fframework-ps.scm;h=d6bf44eff2bde58008ce6ad70a2a64501e3f576e;hb=5b4b0d6e9a197e8f9eb085b7c2ad78b8be3e5cfc;hp=a2d7fa822a8e70ac1fcee21d0889df5aa5c70a32;hpb=e58f03944cce661e4732af3e63aeb60edae97846;p=lilypond.git diff --git a/scm/framework-ps.scm b/scm/framework-ps.scm index a2d7fa822a..d6bf44eff2 100644 --- a/scm/framework-ps.scm +++ b/scm/framework-ps.scm @@ -2,22 +2,22 @@ ;;;; ;;;; source file of the GNU LilyPond music typesetter ;;;; -;;;; (c) 2004--2006 Han-Wen Nienhuys +;;;; (c) 2004--2008 Han-Wen Nienhuys (define-module (scm framework-ps)) ;;; this is still too big a mess. -(use-modules (ice-9 regex) - (ice-9 string-fun) - (ice-9 format) +(use-modules (ice-9 string-fun) (guile) (scm page) (scm paper-system) (srfi srfi-1) (srfi srfi-13) + (scm clip-region) (lily)) +(define format ergonomic-simple-format) (define framework-ps-module (current-module)) @@ -30,7 +30,12 @@ (string-append "magfont" - (string-regexp-substitute "[ /%]" "_" name) + (ly:string-substitute + " " "_" + (ly:string-substitute + "/" "_" + (ly:string-substitute + "%" "_" name))) "m" (string-encode-integer (inexact->exact (round (* 1000 magnify))))))) (define (tex-font? fontname) @@ -44,10 +49,6 @@ (string-append "/" command " { /" fontname " " (ly:number->string scaling) " output-scale div selectfont } bind def\n")) - (define (standard-tex-font? x) - (or (equal? (substring x 0 2) "ms") - (equal? (substring x 0 2) "cm"))) - (define (font-load-command font) (let* ((specced-font-name (ly:font-name font)) (fontname (if specced-font-name @@ -62,10 +63,6 @@ (ops (ly:output-def-lookup paper 'output-scale)) (scaling (* ops magnification designsize))) - ;; Bluesky pfbs have UPCASE names (sigh.) - ;; FIXME - don't support Bluesky? - (if (standard-tex-font? fontname) - (set! fontname (string-upcase fontname))) (if (equal? fontname "unknown") (display (list font fontname))) (define-font plain fontname scaling))) @@ -115,12 +112,13 @@ "page-width output-scale lily-output-units mul mul 0 translate 90 rotate\n" "") "%%EndPageSetup\n" - + + "true setstrokeadjust\n" "gsave 0 paper-height translate " "set-ps-scale-to-lily-scale " "\n")) (ly:outputter-dump-stencil outputter page) - (ly:outputter-dump-string outputter "stroke grestore \nshowpage\n")) + (ly:outputter-dump-string outputter "stroke grestore\nshowpage\n")) (define (supplies-or-needs paper load-fonts?) (define (extract-names font) @@ -163,13 +161,13 @@ (ly:output-def-lookup paper 'output-scale)) (ly:bp 1))) (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t))) - (format "%%DocumentMedia: ~a ~$ ~$ ~a ~a ~a\n" - (ly:output-def-lookup paper 'papersizename) - (if landscape? h w) - (if landscape? w h) - 80 ;; weight - "()" ;; color - "()" ;; type + (ly:format "%%DocumentMedia: ~a ~2f ~2f ~a ~a ~a\n" + (ly:output-def-lookup paper 'papersizename) + (if landscape? h w) + (if landscape? w h) + 80 ;; weight + "()" ;; color + "()" ;; type ))) @@ -248,14 +246,21 @@ (define (write-preamble paper load-fonts? port) - - (define (load-font-via-GS font-name-filename) - (define (ps-load-file name) - (format - (if (string? name) - "(~a) (r) file .loadfont\n" - "% can't find font file: ~a\n") - name)) + (define (internal-font? file-name) + (or (string-startswith file-name "Emmentaler") + (string-startswith file-name "emmentaler") + (string-startswith file-name "aybabtu") + (string-startswith file-name "Aybabtu"))) + (define (load-font-via-GS font-name-filename) + (define (ps-load-file file-name) + (if (string? file-name) + (if (string-contains file-name (ly:get-option 'datadir)) + (begin + (set! file-name (ly:string-substitute (ly:get-option 'datadir) "" file-name)) + (format "lilypond-datadir (~a) concatstrings (r) file .loadfont" file-name)) + + (format "(~a) (r) file .loadfont\n" file-name)) + (format "% cannot find font file: ~a\n" file-name))) (let* ((font (car font-name-filename)) (name (cadr font-name-filename)) @@ -264,15 +269,20 @@ (cons name - (cond - ((string-match "^([eE]mmentaler|[Aa]ybabtu)" file-name) - (ps-load-file (ly:find-file - (format "~a.otf" file-name)))) - ((string? bare-file-name) - (ps-load-file file-name)) - (else - (ly:warning (_ "can't embed ~S=~S") name file-name) - ""))))) + + (if (mac-font? bare-file-name) + (handle-mac-font name bare-file-name) + (cond + ((internal-font? file-name) + (ps-load-file (ly:find-file + (format "~a.otf" file-name)))) + ((string? bare-file-name) + (ps-load-file file-name)) + (else + (ly:warning (_ "cannot embed ~S=~S") name file-name) + ""))) + + ))) (define (dir-join a b) (if (equal? a "") @@ -321,8 +331,8 @@ (if (not embed) (begin - (set! embed "% failed \n") - (ly:warning (_ "can't extract file matching ~a from ~a") name filename))) + (set! embed "% failed\n") + (ly:warning (_ "cannot extract file matching ~a from ~a") name filename))) embed)) (define (font-file-as-ps-string name file-name) @@ -330,18 +340,26 @@ ((downcase-file-name (string-downcase file-name))) (cond - ((and file-name (string-match "\\.pfa" downcase-file-name)) + ((and file-name (string-endswith downcase-file-name ".pfa")) (embed-document file-name)) - ((and file-name (string-match "\\.pfb" downcase-file-name)) + ((and file-name (string-endswith downcase-file-name ".pfb")) (ly:pfb->pfa file-name)) - ((and file-name (string-match "\\.ttf" downcase-file-name)) + ((and file-name (string-endswith downcase-file-name ".ttf")) (ly:ttf->pfa file-name)) - ((and file-name (string-match "\\.otf" downcase-file-name)) + ((and file-name (string-endswith downcase-file-name ".otf")) (ps-embed-cff (ly:otf->cff file-name) name 0)) (else - (ly:warning (_ "don't know how to embed ~S=~S") name file-name) + (ly:warning (_ "do not know how to embed ~S=~S") name file-name) "")))) - + + (define (mac-font? bare-file-name) + (and + (eq? PLATFORM 'darwin) + bare-file-name + (or + (string-endswith bare-file-name ".dfont") + (= (stat:size (stat bare-file-name)) 0)))) + (define (load-font font-name-filename) (let* ((font (car font-name-filename)) (name (cadr font-name-filename)) @@ -352,14 +370,7 @@ name (cond - ((and - (eq? PLATFORM 'darwin) - bare-file-name (string-match "\\.dfont" bare-file-name)) - (handle-mac-font name bare-file-name)) - - ((and - (eq? PLATFORM 'darwin) - bare-file-name (= (stat:size (stat bare-file-name)) 0)) + ((mac-font? bare-file-name) (handle-mac-font name bare-file-name)) ((and font (cff-font? font)) @@ -369,12 +380,14 @@ (bare-file-name (font-file-as-ps-string name bare-file-name)) (else - (ly:warning (_ "don't know how to embed font ~s ~s ~s") + (ly:warning (_ "do not know how to embed font ~s ~s ~s") name file-name font)))))) (define (load-fonts paper) (let* ((fonts (ly:paper-fonts paper)) + + ;; todo - doc format of list. (all-font-names (map (lambda (font) @@ -401,15 +414,30 @@ (sort (apply append all-font-names) (lambda (x y) (stringexact - (round (/ (* x scale) (ly:bp 1)))))) mmbox))) - - (list (car box) - (cadr box) - (max (1+ (car box)) (caddr box)) - (max (1+ (cadr box)) (cadddr box)) + (strip-non-number (lambda (x) + (if (or (nan? x) (inf? x)) 0.0 x))) + (directed-round (lambda (x rounder) + (inexact->exact + (rounder (/ (* (strip-non-number x) scale) + (ly:bp 1))))))) + (list (directed-round (car box) floor) + (directed-round (cadr box) floor) + (directed-round (max (1+ (car box)) (caddr box)) ceiling) + (directed-round (max (1+ (cadr box)) (cadddr box)) ceiling) ))) (let* ((outputter (ly:make-paper-outputter @@ -484,37 +558,126 @@ ;; content-mangling is always bad. ;; MINGW hack: need to have "b"inary for embedding CFFs (open-file (format "~a.eps" filename) "wb") - "ps")) + 'ps)) - (left-overshoot -3) (port (ly:outputter-port outputter)) - (xext (ly:stencil-extent dump-me X)) - (yext (ly:stencil-extent dump-me Y)) - (bbox - (map - (lambda (x) - (if (or (nan? x) (inf? x) - ;; FIXME: huh? - (equal? (format #f "~S" x) "+#.#") - (equal? (format #f "~S" x) "-#.#")) - 0.0 x)) - - ;; the left-overshoot is to make sure that - ;; bar numbers stick out of margin uniformly. - ;; - (list (min left-overshoot (car xext)) - (car yext) (cdr xext) (cdr yext)))) - (rounded-bbox (to-bp-box bbox)) + (rounded-bbox (to-rounded-bp-box bbox)) (port (ly:outputter-port outputter)) - (header (eps-header paper rounded-bbox load-fonts?))) + (header (eps-header paper rounded-bbox load-fonts))) (display header port) - (write-preamble paper load-fonts? port) - (display "gsave set-ps-scale-to-lily-scale \n" port) + (write-preamble paper load-fonts port) + (display "gsave set-ps-scale-to-lily-scale\n" port) (ly:outputter-dump-stencil outputter dump-me) (display "stroke grestore\n%%Trailer\n%%EOF\n" port) (ly:outputter-close outputter))) + + +(define (clip-systems-to-region + basename paper systems region + do-pdf) + + (let* + ((extents-system-pairs + (filtered-map + (lambda (paper-system) + (let* + ((x-ext (system-clipped-x-extent + (paper-system-system-grob paper-system) + region))) + + (if x-ext + (cons x-ext paper-system) + #f))) + + systems)) + (count 0)) + + (for-each + (lambda (ext-system-pair) + (let* + ((xext (car ext-system-pair)) + (paper-system (cdr ext-system-pair)) + (yext (paper-system-extent paper-system Y)) + (bbox (list (car xext) (car yext) + (cdr xext) (cdr yext))) + (filename (if (< 0 count) + (format "~a-~a" basename count) + basename))) + + (set! count (1+ count)) + (dump-stencil-as-EPS-with-bbox + paper + (paper-system-stencil paper-system) + filename + (ly:get-option 'include-eps-fonts) + bbox) + + (if do-pdf + (postscript->pdf 0 0 (format "~a.eps" filename))) + )) + + extents-system-pairs) + )) + + +(define-public (clip-system-EPSes basename paper-book) + (define do-pdf (member "pdf" (ly:output-formats))) + + (define (clip-score-systems basename systems) + (let* + ((layout (ly:grob-layout (paper-system-system-grob (car systems)))) + (regions (ly:output-def-lookup layout 'clip-regions))) + + (for-each + (lambda (region) + (clip-systems-to-region + (format "~a-from-~a-to-~a-clip" + basename + (rhythmic-location->file-string (car region)) + (rhythmic-location->file-string (cdr region))) + layout systems region + do-pdf)) + + regions))) + + + ;; partition in system lists sharing their layout blocks + (let* + ((systems (ly:paper-book-systems paper-book)) + (count 0) + (score-system-list '())) + + (fold + (lambda (system last-system) + + + (if (not (and last-system + (equal? (paper-system-layout last-system) + (paper-system-layout system)))) + (set! score-system-list (cons '() score-system-list))) + + (if (paper-system-layout system) + (set-car! score-system-list (cons system (car score-system-list)))) + + ;; pass value. + system) + + #f + systems) + + (for-each + (lambda (system-list) + (clip-score-systems + (if (> count 0) + (format "~a-~a" basename count) + basename) + system-list)) + + score-system-list))) + + (define-public (output-preview-framework basename book scopes fields) (let* ((paper (ly:paper-book-paper book)) (systems (ly:paper-book-systems book)) @@ -523,7 +686,7 @@ ;; skip booktitles. (if (and - (not (ly:get-option 'preview-include-book-title)) + (not (ly:get-option 'include-book-title-preview)) (pair? systems) (ly:prob-property (car systems) 'is-book-title #f)) @@ -570,16 +733,25 @@ (postprocess-output book framework-ps-module (format "~a.preview.eps" basename) (ly:output-formats))))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-public (convert-to-pdf book name) (let* ((defs (ly:paper-book-paper book)) - (papersizename (ly:output-def-lookup defs 'papersizename))) + (landscape (ly:output-def-lookup defs 'landscape)) + (output-scale (ly:output-def-lookup defs 'output-scale)) + (convert (lambda (x) (* x output-scale (/ (ly:bp 1))))) + + (paper-width (convert (ly:output-def-lookup defs 'paper-width))) + (paper-height (convert (ly:output-def-lookup defs 'paper-height))) + + (w (if landscape paper-height paper-width)) + (h (if landscape paper-width paper-height)) + ) (if (equal? (basename name ".ps") "-") - (ly:warning (_ "can't convert to ~S" "PDF")) - (postscript->pdf (if (string? papersizename) papersizename "a4") - name)))) + (ly:warning (_ "cannot convert to ~S" "PDF")) + (postscript->pdf w h name)))) (define-public (convert-to-png book name) (let* ((defs (ly:paper-book-paper book)) @@ -587,27 +759,35 @@ (resolution (if (number? defs-resolution) defs-resolution (ly:get-option 'resolution))) - (papersizename (ly:output-def-lookup defs 'papersizename))) + (paper-width (ly:output-def-lookup defs 'paper-width)) + (paper-height (ly:output-def-lookup defs 'paper-height)) + (output-scale (ly:output-def-lookup defs 'output-scale))) (postscript->png resolution - (if (string? papersizename) papersizename "a4") + (* paper-width output-scale (/ (ly:bp 1))) + (* paper-height output-scale (/ (ly:bp 1))) name))) (define-public (convert-to-dvi book name) - (ly:warning (_ "can't generate ~S using the postscript back-end") "DVI")) + (ly:warning (_ "cannot generate ~S using the postscript back-end") "DVI")) (define-public (convert-to-tex book name) - (ly:warning (_ "can't generate ~S using the postscript back-end") "TeX")) + (ly:warning (_ "cannot generate ~S using the postscript back-end") "TeX")) (define-public (convert-to-ps book name) #t) (define-public (output-classic-framework basename book scopes fields) - (ly:error (_ "\nThe PostScript backend does not support the 'classic' -framework. Use the EPS backend instead, + (ly:error (_ "\nThe PostScript backend does not support the system-by-system +output. For that, use the EPS backend instead, + + lilypond -dbackend=eps FILE - lilypond -b eps +If have cut & pasted a lilypond fragment from a webpage, be sure +to only remove anything before -or remove the lilypond-book specific settings from the input. + %% **************************************************************** + %% Start cut-&-pastable-section + %% **************************************************************** ")))