1 ;;;; framework-ps.scm -- structure for PostScript output
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 (define-module (scm framework-ps))
9 ;;; this is still too big a mess.
11 (use-modules (ice-9 string-fun)
20 (define format ergonomic-simple-format)
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)))
39 "m" (string-encode-integer (inexact->exact (round (* 1000 magnify)))))))
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 (font-load-command font)
48 (let* ((specced-font-name (ly:font-name font))
49 (fontname (if specced-font-name
51 (ly:font-file-name font)))
52 (command (ps-font-command font))
54 ;; FIXME -- see (ps-font-command )
55 (plain (ps-font-command font))
56 (designsize (ly:font-design-size font))
57 (magnification (* (ly:font-magnification font)))
58 (ops (ly:output-def-lookup paper 'output-scale))
59 (scaling (* ops magnification designsize)))
61 (if (equal? fontname "unknown")
62 (display (list font fontname)))
63 (define-font plain fontname scaling)))
66 (map (lambda (x) (font-load-command x))
67 (filter (lambda (x) (not (ly:pango-font? x)))
70 ;; FIXME: duplicated in other output backends
71 ;; FIXME: silly interface name
72 (define (output-variables layout)
73 ;; FIXME: duplicates output-layout's scope-entry->string, mostly
74 (define (value->string val)
76 ((string? val) (string-append "(" val ")"))
77 ((symbol? val) (symbol->string val))
78 ((number? val) (number->string val))
81 (define (output-entry ps-key ly-key)
84 (value->string (ly:output-def-lookup layout ly-key)) " def\n"))
88 (number->string (/ (ly:bp 1)))
89 " def %% millimeter\n"
90 (output-entry "staff-line-thickness" 'line-thickness)
91 (output-entry "line-width" 'line-width)
92 (output-entry "paper-size" 'papersizename)
93 (output-entry "staff-height" 'staff-height) ;junkme.
95 (number->string (ly:output-def-lookup layout 'output-scale))
97 (output-entry "page-height" 'paper-height)
98 (output-entry "page-width" 'paper-width)))
100 (define (dump-page outputter page page-number page-count landscape?)
101 (ly:outputter-dump-string
104 (format "%%Page: ~a ~a\n" page-number page-number)
107 "page-width output-scale lily-output-units mul mul 0 translate 90 rotate\n"
111 "true setstrokeadjust\n"
112 "gsave 0 paper-height translate "
113 "set-ps-scale-to-lily-scale "
115 (ly:outputter-dump-stencil outputter page)
116 (ly:outputter-dump-string outputter "stroke grestore\nshowpage\n"))
118 (define (supplies-or-needs paper load-fonts?)
119 (define (extract-names font)
120 (if (ly:pango-font? font)
121 (map car (ly:pango-font-physical-fonts font))
122 (list (ly:font-name font))))
124 (let* ((fonts (ly:paper-fonts paper))
125 (names (apply append (map extract-names fonts))))
131 "%%DocumentSuppliedResources: font ~a\n"
132 "%%DocumentNeededResources: font ~a\n")
134 (uniq-list (sort names string<?))))))
136 (define (eps-header paper bbox load-fonts?)
137 (string-append "%!PS-Adobe-2.0 EPSF-2.0\n"
138 "%%Creator: LilyPond "
142 (string-join (map ly:number->string bbox) " ") "\n"
144 (if (eq? (ly:output-def-lookup paper 'landscape) #t)
147 (supplies-or-needs paper load-fonts?)
150 (define (ps-document-media paper)
152 (ly:output-def-lookup paper 'output-scale)
153 (ly:output-def-lookup paper 'paper-width)) (ly:bp 1)))
155 (ly:output-def-lookup paper 'paper-height)
156 (ly:output-def-lookup paper 'output-scale))
158 (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t)))
159 (ly:format "%%DocumentMedia: ~a ~2f ~2f ~a ~a ~a\n"
160 (ly:output-def-lookup paper 'papersizename)
169 (define (file-header paper page-count load-fonts?)
170 (string-append "%!PS-Adobe-3.0\n"
171 "%%Creator: LilyPond "
175 "%%Pages: " (number->string page-count) "\n"
176 "%%PageOrder: Ascend\n"
178 (if (eq? (ly:output-def-lookup paper 'landscape) #t)
181 (ps-document-media paper)
182 (supplies-or-needs paper load-fonts?)
185 (define (procset file-name)
187 "%%BeginResource: procset (~a) 1 0
191 file-name (cached-file-contents file-name)))
193 (define (embed-document file-name)
194 (format "%%BeginDocument: ~a
198 file-name (cached-file-contents file-name)))
200 (define (setup-variables paper)
204 (output-variables paper)
207 (define (cff-font? font)
209 ((cff-string (ly:otf-font-table-data font "CFF ")))
210 (> (string-length cff-string) 0)))
212 (define-public (ps-embed-cff body font-set-name version)
215 (format "/~a ~s StartData " font-set-name (string-length body))
220 "%%BeginResource: font ~a
221 %!PS-Adobe-3.0 Resource-FontSet
222 %%DocumentNeededResources: ProcSet (FontSetInit)
223 %%Title: (FontSet/~a)
226 %%IncludeResource: ProcSet (FontSetInit)
227 %%BeginResource: FontSet (~a)
228 /FontSetInit /ProcSet findresource begin
229 %%BeginData: ~s Binary Bytes
231 font-set-name font-set-name version font-set-name
232 (string-length binary-data)))
243 (define (write-preamble paper load-fonts? port)
244 (define (internal-font? file-name)
245 (or (string-startswith file-name "Emmentaler")
246 (string-startswith file-name "emmentaler")
247 (string-startswith file-name "aybabtu")
248 (string-startswith file-name "Aybabtu")))
249 (define (load-font-via-GS font-name-filename)
250 (define (ps-load-file file-name)
251 (if (string? file-name)
252 (if (string-contains file-name (ly:get-option 'datadir))
254 (set! file-name (ly:string-substitute (ly:get-option 'datadir) "" file-name))
255 (format "lilypond-datadir (~a) concatstrings (r) file .loadfont" file-name))
257 (format "(~a) (r) file .loadfont\n" file-name))
258 (format "% cannot find font file: ~a\n" file-name)))
260 (let* ((font (car font-name-filename))
261 (name (cadr font-name-filename))
262 (file-name (caddr font-name-filename))
263 (bare-file-name (ly:find-file file-name)))
268 (if (mac-font? bare-file-name)
269 (handle-mac-font name bare-file-name)
271 ((internal-font? file-name)
272 (ps-load-file (ly:find-file
273 (format "~a.otf" file-name))))
274 ((string? bare-file-name)
275 (ps-load-file file-name))
277 (ly:warning (_ "cannot embed ~S=~S") name file-name)
282 (define (dir-join a b)
285 (string-append a "/" b)))
287 (define (dir-listing dir-name)
288 (define (dir-helper dir lst)
289 (let ((e (readdir dir)))
290 (if (eof-object? e) lst (dir-helper dir (cons e lst)))))
291 (reverse (dir-helper (opendir dir-name) '())))
293 (define (handle-mac-font name filename)
300 (mkdir dir-name #o700)
301 (set! status (ly:system
302 (format "cd ~a && fondu -force '~a'" dir-name filename)))
304 (set! files (dir-listing dir-name))
309 ((full-name (dir-join dir-name f)))
312 (equal? 'regular (stat:type (stat full-name)))
313 (equal? name (ly:ttf-ps-name full-name)))
316 (font-file-as-ps-string name full-name)))
318 (if (or (equal? "." f)
321 (delete-file full-name))))
329 (set! embed "% failed\n")
330 (ly:warning (_ "cannot extract file matching ~a from ~a") name filename)))
333 (define (font-file-as-ps-string name file-name)
335 ((downcase-file-name (string-downcase file-name)))
338 ((and file-name (string-endswith downcase-file-name ".pfa"))
339 (embed-document file-name))
340 ((and file-name (string-endswith downcase-file-name ".pfb"))
341 (ly:pfb->pfa file-name))
342 ((and file-name (string-endswith downcase-file-name ".ttf"))
343 (ly:ttf->pfa file-name))
344 ((and file-name (string-endswith downcase-file-name ".ttc"))
345 (ly:ttf->pfa file-name))
346 ((and file-name (string-endswith downcase-file-name ".otf"))
347 (ps-embed-cff (ly:otf->cff file-name) name 0))
349 (ly:warning (_ "do not know how to embed ~S=~S") name file-name)
352 (define (mac-font? bare-file-name)
354 (eq? PLATFORM 'darwin)
357 (string-endswith bare-file-name ".dfont")
358 (= (stat:size (stat bare-file-name)) 0))))
360 (define (load-font font-name-filename)
361 (let* ((font (car font-name-filename))
362 (name (cadr font-name-filename))
363 (file-name (caddr font-name-filename))
364 (bare-file-name (ly:find-file file-name)))
370 ((mac-font? bare-file-name)
371 (handle-mac-font name bare-file-name))
373 ((and font (cff-font? font))
374 (ps-embed-cff (ly:otf-font-table-data font "CFF ")
378 (bare-file-name (font-file-as-ps-string name bare-file-name))
380 (ly:warning (_ "do not know how to embed font ~s ~s ~s")
381 name file-name font))))))
384 (define (load-fonts paper)
385 (let* ((fonts (ly:paper-fonts paper))
387 ;; todo - doc format of list.
392 ((string? (ly:font-file-name font))
396 (ly:font-file-name font))))
397 ((ly:pango-font? font)
399 (lambda (name-psname-pair)
401 (car name-psname-pair)
402 (cdr name-psname-pair)))
403 (ly:pango-font-physical-fonts font)))
406 (ly:font-sub-fonts font))))
411 (sort (apply append all-font-names)
412 (lambda (x y) (string<? (cadr x) (cadr y))))))
414 ;; slightly spaghetti-ish: deciding what to load where
416 (font-loader (lambda (name)
418 ((ly:get-option 'gs-load-fonts)
419 (load-font-via-GS name))
420 ((ly:get-option 'gs-load-lily-fonts)
422 (string-contains (caddr name) (ly:get-option 'datadir))
423 (internal-font? (caddr name)))
425 (load-font-via-GS name)
427 (else (load-font name)))))
429 (pfas (map font-loader font-names)))
432 (display "%%BeginProlog\n" port)
435 "/lilypond-datadir where {pop} {userdict /lilypond-datadir (~a) put } ifelse"
436 (ly:get-option 'datadir))
441 (format port "\n%%BeginFont: ~a\n" (car f))
442 (display (cdr f) port)
443 (display "\n%%EndFont\n" port))
446 (display (setup-variables paper) port)
448 ;; adobe note 5002: should initialize variables before loading routines.
449 (display (procset "music-drawing-routines.ps") port)
450 (display (procset "lilyponddefs.ps") port)
452 (display "%%EndProlog\n" port)
454 (display "%%BeginSetup\ninit-lilypond-parameters\n%%EndSetup\n\n" port))
456 (define-public (output-framework basename book scopes fields)
457 (let* ((filename (format "~a.ps" basename))
458 (outputter (ly:make-paper-outputter
459 ;; FIXME: better wrap open/open-file,
460 ;; content-mangling is always bad.
461 ;; MINGW hack: need to have "b"inary for embedding CFFs
462 (open-file filename "wb")
464 (paper (ly:paper-book-paper book))
465 (systems (ly:paper-book-systems book))
466 (page-stencils (map page-stencil (ly:paper-book-pages book)))
468 (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t))
469 (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
470 (page-count (length page-stencils))
471 (port (ly:outputter-port outputter)))
474 (if (ly:get-option 'clip-systems)
475 (clip-system-EPSes basename book))
477 (if (ly:get-option 'dump-signatures)
478 (write-system-signatures basename (ly:paper-book-systems book) 1))
480 (output-scopes scopes fields basename)
481 (display (file-header paper page-count #t) port)
483 ;; don't do BeginDefaults PageMedia: A4
484 ;; not necessary and wrong
486 (write-preamble paper #t port)
490 (set! page-number (1+ page-number))
491 (dump-page outputter page page-number page-count landscape?))
494 (display "%%Trailer\n%%EOF\n" port)
495 (ly:outputter-close outputter)
496 (postprocess-output book framework-ps-module filename
497 (ly:output-formats))))
499 (define-public (dump-stencil-as-EPS paper dump-me filename
503 ((xext (ly:stencil-extent dump-me X))
504 (yext (ly:stencil-extent dump-me Y))
505 (padding (ly:get-option 'eps-box-padding))
506 (left-overshoot (if (number? padding)
507 (* -1 padding (ly:output-def-lookup paper 'mm))
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.
523 (if (number? left-overshoot)
524 (min left-overshoot (car xext))
526 (car yext) (cdr xext) (cdr yext)))))
528 (dump-stencil-as-EPS-with-bbox paper dump-me filename load-fonts bbox)
532 (define-public (dump-stencil-as-EPS-with-bbox paper dump-me filename
535 "Create an EPS file from stencil DUMP-ME to FILENAME. BBOX has format
536 (left-x, lower-y, right x, up-y). If LOAD-FONTS set, include fonts inline."
538 (define (to-rounded-bp-box box)
539 "Convert box to 1/72 inch with rounding to enlarge the box."
540 (let* ((scale (ly:output-def-lookup paper 'output-scale))
541 (strip-non-number (lambda (x)
542 (if (or (nan? x) (inf? x)) 0.0 x)))
543 (directed-round (lambda (x rounder)
545 (rounder (/ (* (strip-non-number x) scale)
547 (list (directed-round (car box) floor)
548 (directed-round (cadr box) floor)
549 (directed-round (max (1+ (car box)) (caddr box)) ceiling)
550 (directed-round (max (1+ (cadr box)) (cadddr box)) ceiling)
553 (let* ((outputter (ly:make-paper-outputter
554 ;; FIXME: better wrap open/open-file,
555 ;; content-mangling is always bad.
556 ;; MINGW hack: need to have "b"inary for embedding CFFs
557 (open-file (format "~a.eps" filename) "wb")
560 (port (ly:outputter-port outputter))
561 (rounded-bbox (to-rounded-bp-box bbox))
562 (port (ly:outputter-port outputter))
563 (header (eps-header paper rounded-bbox load-fonts)))
565 (display header port)
566 (write-preamble paper load-fonts port)
567 (display "gsave set-ps-scale-to-lily-scale\n" port)
568 (ly:outputter-dump-stencil outputter dump-me)
569 (display "stroke grestore\n%%Trailer\n%%EOF\n" port)
570 (ly:outputter-close outputter)))
574 (define (clip-systems-to-region
575 basename paper systems region
579 ((extents-system-pairs
581 (lambda (paper-system)
583 ((x-ext (system-clipped-x-extent
584 (paper-system-system-grob paper-system)
588 (cons x-ext paper-system)
595 (lambda (ext-system-pair)
597 ((xext (car ext-system-pair))
598 (paper-system (cdr ext-system-pair))
599 (yext (paper-system-extent paper-system Y))
600 (bbox (list (car xext) (car yext)
601 (cdr xext) (cdr yext)))
602 (filename (if (< 0 count)
603 (format "~a-~a" basename count)
606 (set! count (1+ count))
607 (dump-stencil-as-EPS-with-bbox
609 (paper-system-stencil paper-system)
611 (ly:get-option 'include-eps-fonts)
615 (postscript->pdf 0 0 (format "~a.eps" filename)))
618 extents-system-pairs)
622 (define-public (clip-system-EPSes basename paper-book)
623 (define do-pdf (member "pdf" (ly:output-formats)))
625 (define (clip-score-systems basename systems)
627 ((layout (ly:grob-layout (paper-system-system-grob (car systems))))
628 (regions (ly:output-def-lookup layout 'clip-regions)))
632 (clip-systems-to-region
633 (format "~a-from-~a-to-~a-clip"
635 (rhythmic-location->file-string (car region))
636 (rhythmic-location->file-string (cdr region)))
637 layout systems region
643 ;; partition in system lists sharing their layout blocks
645 ((systems (ly:paper-book-systems paper-book))
647 (score-system-list '()))
650 (lambda (system last-system)
653 (if (not (and last-system
654 (equal? (paper-system-layout last-system)
655 (paper-system-layout system))))
656 (set! score-system-list (cons '() score-system-list)))
658 (if (paper-system-layout system)
659 (set-car! score-system-list (cons system (car score-system-list))))
668 (lambda (system-list)
671 (format "~a-~a" basename count)
678 (define-public (output-preview-framework basename book scopes fields)
679 (let* ((paper (ly:paper-book-paper book))
680 (systems (ly:paper-book-systems book))
681 (scale (ly:output-def-lookup paper 'output-scale))
682 (to-dump-systems '()))
686 (not (ly:get-option 'include-book-title-preview))
688 (ly:prob-property (car systems) 'is-book-title #f))
690 (set! systems (cdr systems)))
695 (paper-system-title? sys)
696 (not (pair? to-dump-systems))
697 (paper-system-title? (car to-dump-systems)))
698 (set! to-dump-systems (cons sys to-dump-systems))))
703 (stack-stencils Y DOWN 0.0
704 (map paper-system-stencil (reverse to-dump-systems)))
705 (format "~a.preview" basename)
708 (postprocess-output book framework-ps-module
709 (format "~a.preview.eps" basename)
710 (cons "png" (ly:output-formats)))))
713 (define-public (output-preview-framework basename book scopes fields)
715 (let* ((paper (ly:paper-book-paper book))
716 (systems (ly:paper-book-systems book))
717 (scale (ly:output-def-lookup paper 'output-scale))
718 (titles (take-while paper-system-title? systems))
719 (non-title (find (lambda (x)
720 (not (paper-system-title? x))) systems))
722 (stack-stencils Y DOWN 0.0
723 (map paper-system-stencil
724 (append titles (list non-title))))))
725 (output-scopes scopes fields basename)
726 (dump-stencil-as-EPS paper dump-me
727 (format "~a.preview" basename)
730 (postprocess-output book framework-ps-module
731 (format "~a.preview.eps" basename)
732 (ly:output-formats)))))
734 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
736 (define-public (convert-to-pdf book name)
737 (let* ((defs (ly:paper-book-paper book))
738 (landscape (ly:output-def-lookup defs 'landscape))
739 (output-scale (ly:output-def-lookup defs 'output-scale))
740 (convert (lambda (x) (* x output-scale (/ (ly:bp 1)))))
742 (paper-width (convert (ly:output-def-lookup defs 'paper-width)))
743 (paper-height (convert (ly:output-def-lookup defs 'paper-height)))
745 (w (if landscape paper-height paper-width))
746 (h (if landscape paper-width paper-height))
749 (if (equal? (basename name ".ps") "-")
750 (ly:warning (_ "cannot convert <stdout> to ~S" "PDF"))
751 (postscript->pdf w h name))))
753 (define-public (convert-to-png book name)
754 (let* ((defs (ly:paper-book-paper book))
755 (defs-resolution (ly:output-def-lookup defs 'pngresolution))
756 (resolution (if (number? defs-resolution)
758 (ly:get-option 'resolution)))
759 (paper-width (ly:output-def-lookup defs 'paper-width))
760 (paper-height (ly:output-def-lookup defs 'paper-height))
761 (output-scale (ly:output-def-lookup defs 'output-scale)))
763 (postscript->png resolution
764 (* paper-width output-scale (/ (ly:bp 1)))
765 (* paper-height output-scale (/ (ly:bp 1)))
768 (define-public (convert-to-ps book name)
771 (define-public (output-classic-framework basename book scopes fields)
773 (ly:error (_ "\nThe PostScript backend does not support the system-by-system
774 output. For that, use the EPS backend instead,
776 lilypond -dbackend=eps FILE
778 If have cut & pasted a lilypond fragment from a webpage, be sure
779 to only remove anything before
781 %% ****************************************************************
782 %% Start cut-&-pastable-section
783 %% ****************************************************************