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 ".otf"))
345 (ps-embed-cff (ly:otf->cff file-name) name 0))
347 (ly:warning (_ "do not know how to embed ~S=~S") name file-name)
350 (define (mac-font? bare-file-name)
352 (eq? PLATFORM 'darwin)
355 (string-endswith bare-file-name ".dfont")
356 (= (stat:size (stat bare-file-name)) 0))))
358 (define (load-font font-name-filename)
359 (let* ((font (car font-name-filename))
360 (name (cadr font-name-filename))
361 (file-name (caddr font-name-filename))
362 (bare-file-name (ly:find-file file-name)))
368 ((mac-font? bare-file-name)
369 (handle-mac-font name bare-file-name))
371 ((and font (cff-font? font))
372 (ps-embed-cff (ly:otf-font-table-data font "CFF ")
376 (bare-file-name (font-file-as-ps-string name bare-file-name))
378 (ly:warning (_ "do not know how to embed font ~s ~s ~s")
379 name file-name font))))))
382 (define (load-fonts paper)
383 (let* ((fonts (ly:paper-fonts paper))
385 ;; todo - doc format of list.
390 ((string? (ly:font-file-name font))
394 (ly:font-file-name font))))
395 ((ly:pango-font? font)
397 (lambda (name-psname-pair)
399 (car name-psname-pair)
400 (cdr name-psname-pair)))
401 (ly:pango-font-physical-fonts font)))
404 (ly:font-sub-fonts font))))
409 (sort (apply append all-font-names)
410 (lambda (x y) (string<? (cadr x) (cadr y))))))
412 ;; slightly spaghetti-ish: deciding what to load where
414 (font-loader (lambda (name)
416 ((ly:get-option 'gs-load-fonts)
417 (load-font-via-GS name))
418 ((ly:get-option 'gs-load-lily-fonts)
420 (string-contains (caddr name) (ly:get-option 'datadir))
421 (internal-font? (caddr name)))
423 (load-font-via-GS name)
425 (else (load-font name)))))
427 (pfas (map font-loader font-names)))
430 (display "%%BeginProlog\n" port)
433 "/lilypond-datadir where {pop} {userdict /lilypond-datadir (~a) put } ifelse"
434 (ly:get-option 'datadir))
439 (format port "\n%%BeginFont: ~a\n" (car f))
440 (display (cdr f) port)
441 (display "\n%%EndFont\n" port))
444 (display (setup-variables paper) port)
446 ;; adobe note 5002: should initialize variables before loading routines.
447 (display (procset "music-drawing-routines.ps") port)
448 (display (procset "lilyponddefs.ps") port)
450 (display "%%EndProlog\n" port)
452 (display "%%BeginSetup\ninit-lilypond-parameters\n%%EndSetup\n\n" port))
454 (define-public (output-framework basename book scopes fields)
455 (let* ((filename (format "~a.ps" basename))
456 (outputter (ly:make-paper-outputter
457 ;; FIXME: better wrap open/open-file,
458 ;; content-mangling is always bad.
459 ;; MINGW hack: need to have "b"inary for embedding CFFs
460 (open-file filename "wb")
462 (paper (ly:paper-book-paper book))
463 (systems (ly:paper-book-systems book))
464 (page-stencils (map page-stencil (ly:paper-book-pages book)))
466 (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t))
467 (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
468 (page-count (length page-stencils))
469 (port (ly:outputter-port outputter)))
472 (if (ly:get-option 'clip-systems)
473 (clip-system-EPSes basename book))
475 (if (ly:get-option 'dump-signatures)
476 (write-system-signatures basename (ly:paper-book-systems book) 1))
478 (output-scopes scopes fields basename)
479 (display (file-header paper page-count #t) port)
481 ;; don't do BeginDefaults PageMedia: A4
482 ;; not necessary and wrong
484 (write-preamble paper #t port)
488 (set! page-number (1+ page-number))
489 (dump-page outputter page page-number page-count landscape?))
492 (display "%%Trailer\n%%EOF\n" port)
493 (ly:outputter-close outputter)
494 (postprocess-output book framework-ps-module filename
495 (ly:output-formats))))
497 (define-public (dump-stencil-as-EPS paper dump-me filename
501 ((xext (ly:stencil-extent dump-me X))
502 (yext (ly:stencil-extent dump-me Y))
503 (padding (ly:get-option 'eps-box-padding))
504 (left-overshoot (if (number? padding)
505 (* -1 padding (ly:output-def-lookup paper 'mm))
510 (if (or (nan? x) (inf? x)
512 (equal? (format #f "~S" x) "+#.#")
513 (equal? (format #f "~S" x) "-#.#"))
516 ;; the left-overshoot is to make sure that
517 ;; bar numbers stick out of margin uniformly.
521 (if (number? left-overshoot)
522 (min left-overshoot (car xext))
524 (car yext) (cdr xext) (cdr yext)))))
526 (dump-stencil-as-EPS-with-bbox paper dump-me filename load-fonts bbox)
530 (define-public (dump-stencil-as-EPS-with-bbox paper dump-me filename
533 "Create an EPS file from stencil DUMP-ME to FILENAME. BBOX has format
534 (left-x, lower-y, right x, up-y). If LOAD-FONTS set, include fonts inline."
536 (define (to-rounded-bp-box box)
537 "Convert box to 1/72 inch with rounding to enlarge the box."
538 (let* ((scale (ly:output-def-lookup paper 'output-scale))
539 (strip-non-number (lambda (x)
540 (if (or (nan? x) (inf? x)) 0.0 x)))
541 (directed-round (lambda (x rounder)
543 (rounder (/ (* (strip-non-number x) scale)
545 (list (directed-round (car box) floor)
546 (directed-round (cadr box) floor)
547 (directed-round (max (1+ (car box)) (caddr box)) ceiling)
548 (directed-round (max (1+ (cadr box)) (cadddr box)) ceiling)
551 (let* ((outputter (ly:make-paper-outputter
552 ;; FIXME: better wrap open/open-file,
553 ;; content-mangling is always bad.
554 ;; MINGW hack: need to have "b"inary for embedding CFFs
555 (open-file (format "~a.eps" filename) "wb")
558 (port (ly:outputter-port outputter))
559 (rounded-bbox (to-rounded-bp-box bbox))
560 (port (ly:outputter-port outputter))
561 (header (eps-header paper rounded-bbox load-fonts)))
563 (display header port)
564 (write-preamble paper load-fonts port)
565 (display "gsave set-ps-scale-to-lily-scale\n" port)
566 (ly:outputter-dump-stencil outputter dump-me)
567 (display "stroke grestore\n%%Trailer\n%%EOF\n" port)
568 (ly:outputter-close outputter)))
572 (define (clip-systems-to-region
573 basename paper systems region
577 ((extents-system-pairs
579 (lambda (paper-system)
581 ((x-ext (system-clipped-x-extent
582 (paper-system-system-grob paper-system)
586 (cons x-ext paper-system)
593 (lambda (ext-system-pair)
595 ((xext (car ext-system-pair))
596 (paper-system (cdr ext-system-pair))
597 (yext (paper-system-extent paper-system Y))
598 (bbox (list (car xext) (car yext)
599 (cdr xext) (cdr yext)))
600 (filename (if (< 0 count)
601 (format "~a-~a" basename count)
604 (set! count (1+ count))
605 (dump-stencil-as-EPS-with-bbox
607 (paper-system-stencil paper-system)
609 (ly:get-option 'include-eps-fonts)
613 (postscript->pdf 0 0 (format "~a.eps" filename)))
616 extents-system-pairs)
620 (define-public (clip-system-EPSes basename paper-book)
621 (define do-pdf (member "pdf" (ly:output-formats)))
623 (define (clip-score-systems basename systems)
625 ((layout (ly:grob-layout (paper-system-system-grob (car systems))))
626 (regions (ly:output-def-lookup layout 'clip-regions)))
630 (clip-systems-to-region
631 (format "~a-from-~a-to-~a-clip"
633 (rhythmic-location->file-string (car region))
634 (rhythmic-location->file-string (cdr region)))
635 layout systems region
641 ;; partition in system lists sharing their layout blocks
643 ((systems (ly:paper-book-systems paper-book))
645 (score-system-list '()))
648 (lambda (system last-system)
651 (if (not (and last-system
652 (equal? (paper-system-layout last-system)
653 (paper-system-layout system))))
654 (set! score-system-list (cons '() score-system-list)))
656 (if (paper-system-layout system)
657 (set-car! score-system-list (cons system (car score-system-list))))
666 (lambda (system-list)
669 (format "~a-~a" basename count)
676 (define-public (output-preview-framework basename book scopes fields)
677 (let* ((paper (ly:paper-book-paper book))
678 (systems (ly:paper-book-systems book))
679 (scale (ly:output-def-lookup paper 'output-scale))
680 (to-dump-systems '()))
684 (not (ly:get-option 'include-book-title-preview))
686 (ly:prob-property (car systems) 'is-book-title #f))
688 (set! systems (cdr systems)))
693 (paper-system-title? sys)
694 (not (pair? to-dump-systems))
695 (paper-system-title? (car to-dump-systems)))
696 (set! to-dump-systems (cons sys to-dump-systems))))
701 (stack-stencils Y DOWN 0.0
702 (map paper-system-stencil (reverse to-dump-systems)))
703 (format "~a.preview" basename)
706 (postprocess-output book framework-ps-module
707 (format "~a.preview.eps" basename)
708 (cons "png" (ly:output-formats)))))
711 (define-public (output-preview-framework basename book scopes fields)
713 (let* ((paper (ly:paper-book-paper book))
714 (systems (ly:paper-book-systems book))
715 (scale (ly:output-def-lookup paper 'output-scale))
716 (titles (take-while paper-system-title? systems))
717 (non-title (find (lambda (x)
718 (not (paper-system-title? x))) systems))
720 (stack-stencils Y DOWN 0.0
721 (map paper-system-stencil
722 (append titles (list non-title))))))
723 (output-scopes scopes fields basename)
724 (dump-stencil-as-EPS paper dump-me
725 (format "~a.preview" basename)
728 (postprocess-output book framework-ps-module
729 (format "~a.preview.eps" basename)
730 (ly:output-formats)))))
732 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
734 (define-public (convert-to-pdf book name)
735 (let* ((defs (ly:paper-book-paper book))
736 (landscape (ly:output-def-lookup defs 'landscape))
737 (output-scale (ly:output-def-lookup defs 'output-scale))
738 (convert (lambda (x) (* x output-scale (/ (ly:bp 1)))))
740 (paper-width (convert (ly:output-def-lookup defs 'paper-width)))
741 (paper-height (convert (ly:output-def-lookup defs 'paper-height)))
743 (w (if landscape paper-height paper-width))
744 (h (if landscape paper-width paper-height))
747 (if (equal? (basename name ".ps") "-")
748 (ly:warning (_ "cannot convert <stdout> to ~S" "PDF"))
749 (postscript->pdf w h name))))
751 (define-public (convert-to-png book name)
752 (let* ((defs (ly:paper-book-paper book))
753 (defs-resolution (ly:output-def-lookup defs 'pngresolution))
754 (resolution (if (number? defs-resolution)
756 (ly:get-option 'resolution)))
757 (paper-width (ly:output-def-lookup defs 'paper-width))
758 (paper-height (ly:output-def-lookup defs 'paper-height))
759 (output-scale (ly:output-def-lookup defs 'output-scale)))
761 (postscript->png resolution
762 (* paper-width output-scale (/ (ly:bp 1)))
763 (* paper-height output-scale (/ (ly:bp 1)))
766 (define-public (convert-to-ps book name)
769 (define-public (output-classic-framework basename book scopes fields)
771 (ly:error (_ "\nThe PostScript backend does not support the system-by-system
772 output. For that, use the EPS backend instead,
774 lilypond -dbackend=eps FILE
776 If have cut & pasted a lilypond fragment from a webpage, be sure
777 to only remove anything before
779 %% ****************************************************************
780 %% Start cut-&-pastable-section
781 %% ****************************************************************