1 ;;;; framework-ps.scm -- structure for PostScript output
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2006 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 (define-module (scm framework-ps))
9 ;;; this is still too big a mess.
11 (use-modules (ice-9 regex)
22 (define framework-ps-module (current-module))
24 ;;(define pdebug stderr)
25 (define (pdebug . rest) #f)
27 (define-public (ps-font-command font)
28 (let* ((name (ly:font-file-name font))
29 (magnify (ly:font-magnification font)))
33 (string-regexp-substitute "[ /%]" "_" name)
34 "m" (string-encode-integer (inexact->exact (round (* 1000 magnify)))))))
36 (define (tex-font? fontname)
38 (equal? (substring fontname 0 2) "cm")
39 (equal? (substring fontname 0 2) "ec")))
41 (define (define-fonts paper)
42 (define font-list (ly:paper-fonts paper))
43 (define (define-font command fontname scaling)
45 "/" command " { /" fontname " " (ly:number->string scaling) " output-scale div selectfont } bind def\n"))
47 (define (standard-tex-font? x)
48 (or (equal? (substring x 0 2) "ms")
49 (equal? (substring x 0 2) "cm")))
51 (define (font-load-command font)
52 (let* ((specced-font-name (ly:font-name font))
53 (fontname (if specced-font-name
55 (ly:font-file-name font)))
56 (command (ps-font-command font))
58 ;; FIXME -- see (ps-font-command )
59 (plain (ps-font-command font))
60 (designsize (ly:font-design-size font))
61 (magnification (* (ly:font-magnification font)))
62 (ops (ly:output-def-lookup paper 'output-scale))
63 (scaling (* ops magnification designsize)))
65 ;; Bluesky pfbs have UPCASE names (sigh.)
66 ;; FIXME - don't support Bluesky?
67 (if (standard-tex-font? fontname)
68 (set! fontname (string-upcase fontname)))
69 (if (equal? fontname "unknown")
70 (display (list font fontname)))
71 (define-font plain fontname scaling)))
74 (map (lambda (x) (font-load-command x))
75 (filter (lambda (x) (not (ly:pango-font? x)))
78 ;; FIXME: duplicated in other output backends
79 ;; FIXME: silly interface name
80 (define (output-variables layout)
81 ;; FIXME: duplicates output-layout's scope-entry->string, mostly
82 (define (value->string val)
84 ((string? val) (string-append "(" val ")"))
85 ((symbol? val) (symbol->string val))
86 ((number? val) (number->string val))
89 (define (output-entry ps-key ly-key)
92 (value->string (ly:output-def-lookup layout ly-key)) " def\n"))
96 (number->string (/ (ly:bp 1)))
97 " def %% millimeter\n"
98 (output-entry "staff-line-thickness" 'line-thickness)
99 (output-entry "line-width" 'line-width)
100 (output-entry "paper-size" 'papersizename)
101 (output-entry "staff-height" 'staff-height) ;junkme.
103 (number->string (ly:output-def-lookup layout 'output-scale))
105 (output-entry "page-height" 'paper-height)
106 (output-entry "page-width" 'paper-width)))
108 (define (dump-page outputter page page-number page-count landscape?)
109 (ly:outputter-dump-string
112 (format "%%Page: ~a ~a\n" page-number page-number)
115 "page-width output-scale lily-output-units mul mul 0 translate 90 rotate\n"
119 "true setstrokeadjust\n"
120 "gsave 0 paper-height translate "
121 "set-ps-scale-to-lily-scale "
123 (ly:outputter-dump-stencil outputter page)
124 (ly:outputter-dump-string outputter "stroke grestore \nshowpage\n"))
126 (define (supplies-or-needs paper load-fonts?)
127 (define (extract-names font)
128 (if (ly:pango-font? font)
129 (map car (ly:pango-font-physical-fonts font))
130 (list (ly:font-name font))))
132 (let* ((fonts (ly:paper-fonts paper))
133 (names (apply append (map extract-names fonts))))
139 "%%DocumentSuppliedResources: font ~a\n"
140 "%%DocumentNeededResources: font ~a\n")
142 (uniq-list (sort names string<?))))))
144 (define (eps-header paper bbox load-fonts?)
145 (string-append "%!PS-Adobe-2.0 EPSF-2.0\n"
146 "%%Creator: LilyPond "
150 (string-join (map ly:number->string bbox) " ") "\n"
152 (if (eq? (ly:output-def-lookup paper 'landscape) #t)
155 (supplies-or-needs paper load-fonts?)
158 (define (ps-document-media paper)
160 (ly:output-def-lookup paper 'output-scale)
161 (ly:output-def-lookup paper 'paper-width)) (ly:bp 1)))
163 (ly:output-def-lookup paper 'paper-height)
164 (ly:output-def-lookup paper 'output-scale))
166 (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t)))
167 (format "%%DocumentMedia: ~a ~$ ~$ ~a ~a ~a\n"
168 (ly:output-def-lookup paper 'papersizename)
177 (define (file-header paper page-count load-fonts?)
178 (string-append "%!PS-Adobe-3.0\n"
179 "%%Creator: LilyPond "
183 "%%Pages: " (number->string page-count) "\n"
184 "%%PageOrder: Ascend\n"
186 (if (eq? (ly:output-def-lookup paper 'landscape) #t)
189 (ps-document-media paper)
190 (supplies-or-needs paper load-fonts?)
193 (define (procset file-name)
195 "%%BeginResource: procset (~a) 1 0
199 file-name (cached-file-contents file-name)))
201 (define (embed-document file-name)
202 (format "%%BeginDocument: ~a
206 file-name (cached-file-contents file-name)))
208 (define (setup-variables paper)
212 (output-variables paper)
215 (define (cff-font? font)
217 ((cff-string (ly:otf-font-table-data font "CFF ")))
218 (> (string-length cff-string) 0)))
220 (define-public (ps-embed-cff body font-set-name version)
223 (format "/~a ~s StartData " font-set-name (string-length body))
228 "%%BeginResource: font ~a
229 %!PS-Adobe-3.0 Resource-FontSet
230 %%DocumentNeededResources: ProcSet (FontSetInit)
231 %%Title: (FontSet/~a)
234 %%IncludeResource: ProcSet (FontSetInit)
235 %%BeginResource: FontSet (~a)
236 /FontSetInit /ProcSet findresource begin
237 %%BeginData: ~s Binary Bytes
239 font-set-name font-set-name version font-set-name
240 (string-length binary-data)))
251 (define (write-preamble paper load-fonts? port)
253 (define (load-font-via-GS font-name-filename)
254 (define (ps-load-file name)
257 "(~a) (r) file .loadfont\n"
258 "% can't find font file: ~a\n")
261 (let* ((font (car font-name-filename))
262 (name (cadr font-name-filename))
263 (file-name (caddr font-name-filename))
264 (bare-file-name (ly:find-file file-name)))
269 (if (mac-font? bare-file-name)
270 (handle-mac-font name bare-file-name)
272 ((string-match "^([eE]mmentaler|[Aa]ybabtu)" file-name)
273 (ps-load-file (ly:find-file
274 (format "~a.otf" file-name))))
275 ((string? bare-file-name)
276 (ps-load-file file-name))
278 (ly:warning (_ "can't embed ~S=~S") name file-name)
283 (define (dir-join a b)
286 (string-append a "/" b)))
288 (define (dir-listing dir-name)
289 (define (dir-helper dir lst)
290 (let ((e (readdir dir)))
291 (if (eof-object? e) lst (dir-helper dir (cons e lst)))))
292 (reverse (dir-helper (opendir dir-name) '())))
294 (define (handle-mac-font name filename)
301 (mkdir dir-name #o700)
302 (set! status (ly:system
303 (format "cd ~a && fondu -force '~a'" dir-name filename)))
305 (set! files (dir-listing dir-name))
310 ((full-name (dir-join dir-name f)))
313 (equal? 'regular (stat:type (stat full-name)))
314 (equal? name (ly:ttf-ps-name full-name)))
317 (font-file-as-ps-string name full-name)))
319 (if (or (equal? "." f)
322 (delete-file full-name))))
330 (set! embed "% failed \n")
331 (ly:warning (_ "can't extract file matching ~a from ~a") name filename)))
334 (define (font-file-as-ps-string name file-name)
336 ((downcase-file-name (string-downcase file-name)))
339 ((and file-name (string-match "\\.pfa" downcase-file-name))
340 (embed-document file-name))
341 ((and file-name (string-match "\\.pfb" downcase-file-name))
342 (ly:pfb->pfa file-name))
343 ((and file-name (string-match "\\.ttf" downcase-file-name))
344 (ly:ttf->pfa file-name))
345 ((and file-name (string-match "\\.otf" downcase-file-name))
346 (ps-embed-cff (ly:otf->cff file-name) name 0))
348 (ly:warning (_ "don't know how to embed ~S=~S") name file-name)
351 (define (mac-font? bare-file-name)
353 (eq? PLATFORM 'darwin)
356 (string-match "\\.dfont" bare-file-name)
357 (= (stat:size (stat bare-file-name)) 0))))
359 (define (load-font font-name-filename)
360 (let* ((font (car font-name-filename))
361 (name (cadr font-name-filename))
362 (file-name (caddr font-name-filename))
363 (bare-file-name (ly:find-file file-name)))
369 ((mac-font? bare-file-name)
370 (handle-mac-font name bare-file-name))
372 ((and font (cff-font? font))
373 (ps-embed-cff (ly:otf-font-table-data font "CFF ")
377 (bare-file-name (font-file-as-ps-string name bare-file-name))
379 (ly:warning (_ "don't know how to embed font ~s ~s ~s")
380 name file-name font))))))
383 (define (load-fonts paper)
384 (let* ((fonts (ly:paper-fonts paper))
389 ((string? (ly:font-file-name font))
393 (ly:font-file-name font))))
394 ((ly:pango-font? font)
396 (lambda (name-psname-pair)
398 (car name-psname-pair)
399 (cdr name-psname-pair)))
400 (ly:pango-font-physical-fonts font)))
403 (ly:font-sub-fonts font))))
408 (sort (apply append all-font-names)
409 (lambda (x y) (string<? (cadr x) (cadr y))))))
412 (font-loader (if (ly:get-option 'gs-font-load)
416 (pfas (map font-loader font-names)))
419 (display "%%BeginProlog\n" port)
423 (format port "\n%%BeginFont: ~a\n" (car f))
424 (display (cdr f) port)
425 (display "\n%%EndFont\n" port))
428 (display (setup-variables paper) port)
430 ;; adobe note 5002: should initialize variables before loading routines.
431 (display (procset "music-drawing-routines.ps") port)
432 (display (procset "lilyponddefs.ps") port)
434 (if (not (ly:get-option 'point-and-click))
435 (display "/mark_URI { pop pop pop pop pop } bind def\n" port))
437 (display "%%EndProlog\n" port)
439 (display "%%BeginSetup\ninit-lilypond-parameters\n%%EndSetup\n\n" port))
441 (define-public (output-framework basename book scopes fields)
442 (let* ((filename (format "~a.ps" basename))
443 (outputter (ly:make-paper-outputter
444 ;; FIXME: better wrap open/open-file,
445 ;; content-mangling is always bad.
446 ;; MINGW hack: need to have "b"inary for embedding CFFs
447 (open-file filename "wb")
449 (paper (ly:paper-book-paper book))
450 (systems (ly:paper-book-systems book))
451 (page-stencils (map page-stencil (ly:paper-book-pages book)))
453 (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t))
454 (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
455 (page-count (length page-stencils))
456 (port (ly:outputter-port outputter)))
458 (if (ly:get-option 'dump-signatures)
459 (write-system-signatures basename (ly:paper-book-systems book) 1))
461 (output-scopes scopes fields basename)
462 (display (file-header paper page-count #t) port)
465 ;; don't do BeginDefaults PageMedia: A4
466 ;; not necessary and wrong
469 (write-preamble paper #t port)
473 (set! page-number (1+ page-number))
474 (dump-page outputter page page-number page-count landscape?))
477 (display "%%Trailer\n%%EOF\n" port)
478 (ly:outputter-close outputter)
479 (postprocess-output book framework-ps-module filename
480 (ly:output-formats))))
482 (define-public (dump-stencil-as-EPS paper dump-me filename load-fonts?)
483 (define (to-bp-box mmbox)
484 (let* ((scale (ly:output-def-lookup paper 'output-scale))
487 (if (or (nan? x) (inf? x))
490 (round (/ (* x scale) (ly:bp 1)))))) mmbox)))
494 (max (1+ (car box)) (caddr box))
495 (max (1+ (cadr box)) (cadddr box))
498 (let* ((outputter (ly:make-paper-outputter
499 ;; FIXME: better wrap open/open-file,
500 ;; content-mangling is always bad.
501 ;; MINGW hack: need to have "b"inary for embedding CFFs
502 (open-file (format "~a.eps" filename) "wb")
506 (port (ly:outputter-port outputter))
507 (xext (ly:stencil-extent dump-me X))
508 (yext (ly:stencil-extent dump-me Y))
512 (if (or (nan? x) (inf? x)
514 (equal? (format #f "~S" x) "+#.#")
515 (equal? (format #f "~S" x) "-#.#"))
518 ;; the left-overshoot is to make sure that
519 ;; bar numbers stick out of margin uniformly.
521 (list (min left-overshoot (car xext))
522 (car yext) (cdr xext) (cdr yext))))
523 (rounded-bbox (to-bp-box bbox))
524 (port (ly:outputter-port outputter))
525 (header (eps-header paper rounded-bbox load-fonts?)))
527 (display header port)
528 (write-preamble paper load-fonts? port)
529 (display "gsave set-ps-scale-to-lily-scale \n" port)
530 (ly:outputter-dump-stencil outputter dump-me)
531 (display "stroke grestore\n%%Trailer\n%%EOF\n" port)
532 (ly:outputter-close outputter)))
534 (define-public (output-preview-framework basename book scopes fields)
535 (let* ((paper (ly:paper-book-paper book))
536 (systems (ly:paper-book-systems book))
537 (scale (ly:output-def-lookup paper 'output-scale))
538 (to-dump-systems '()))
542 (not (ly:get-option 'preview-include-book-title))
544 (ly:prob-property (car systems) 'is-book-title #f))
546 (set! systems (cdr systems)))
551 (paper-system-title? sys)
552 (not (pair? to-dump-systems))
553 (paper-system-title? (car to-dump-systems)))
554 (set! to-dump-systems (cons sys to-dump-systems))))
559 (stack-stencils Y DOWN 0.0
560 (map paper-system-stencil (reverse to-dump-systems)))
561 (format "~a.preview" basename)
564 (postprocess-output book framework-ps-module
565 (format "~a.preview.eps" basename)
566 (cons "png" (ly:output-formats)))))
569 (define-public (output-preview-framework basename book scopes fields)
571 (let* ((paper (ly:paper-book-paper book))
572 (systems (ly:paper-book-systems book))
573 (scale (ly:output-def-lookup paper 'output-scale))
574 (titles (take-while paper-system-title? systems))
575 (non-title (find (lambda (x)
576 (not (paper-system-title? x))) systems))
578 (stack-stencils Y DOWN 0.0
579 (map paper-system-stencil
580 (append titles (list non-title))))))
581 (output-scopes scopes fields basename)
582 (dump-stencil-as-EPS paper dump-me
583 (format "~a.preview" basename)
586 (postprocess-output book framework-ps-module
587 (format "~a.preview.eps" basename)
588 (ly:output-formats)))))
589 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
591 (define-public (convert-to-pdf book name)
592 (let* ((defs (ly:paper-book-paper book))
593 (landscape (ly:output-def-lookup defs 'landscape))
594 (output-scale (ly:output-def-lookup defs 'output-scale))
595 (convert (lambda (x) (* x output-scale (/ (ly:bp 1)))))
597 (paper-width (convert (ly:output-def-lookup defs 'paper-width)))
598 (paper-height (convert (ly:output-def-lookup defs 'paper-height)))
600 (w (if landscape paper-height paper-width))
601 (h (if landscape paper-width paper-height))
604 (if (equal? (basename name ".ps") "-")
605 (ly:warning (_ "can't convert <stdout> to ~S" "PDF"))
606 (postscript->pdf w h name))))
608 (define-public (convert-to-png book name)
609 (let* ((defs (ly:paper-book-paper book))
610 (defs-resolution (ly:output-def-lookup defs 'pngresolution))
611 (resolution (if (number? defs-resolution)
613 (ly:get-option 'resolution)))
614 (paper-width (ly:output-def-lookup defs 'paper-width))
615 (paper-height (ly:output-def-lookup defs 'paper-height))
616 (output-scale (ly:output-def-lookup defs 'output-scale)))
618 (postscript->png resolution
619 (* paper-width output-scale (/ (ly:bp 1)))
620 (* paper-height output-scale (/ (ly:bp 1)))
623 (define-public (convert-to-dvi book name)
624 (ly:warning (_ "can't generate ~S using the postscript back-end") "DVI"))
626 (define-public (convert-to-tex book name)
627 (ly:warning (_ "can't generate ~S using the postscript back-end") "TeX"))
629 (define-public (convert-to-ps book name)
632 (define-public (output-classic-framework basename book scopes fields)
634 (ly:error (_ "\nThe PostScript backend does not support the 'classic'
635 framework. Use the EPS backend instead,
637 lilypond -b eps <file>
639 or remove the lilypond-book specific settings from the input.