1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2004--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
18 (define-module (scm framework-ps))
20 ;;; this is still too big a mess.
22 (use-modules (ice-9 string-fun)
31 (define format ergonomic-simple-format)
33 (define framework-ps-module (current-module))
35 ;;(define pdebug stderr)
36 (define (pdebug . rest) #f)
38 (define-public (ps-font-command font)
39 (let* ((name (ly:font-file-name font))
40 (magnify (ly:font-magnification font)))
49 "m" (string-encode-integer (inexact->exact (round (* 1000 magnify)))))))
51 (define (define-fonts paper)
52 (define font-list (ly:paper-fonts paper))
54 (define (define-font command fontname scaling)
58 " " (ly:number->string scaling) " output-scale div selectfont }"
61 (define (font-load-command font)
62 (let* ((specced-font-name (ly:font-name font))
63 (fontname (if specced-font-name
65 (ly:font-file-name font)))
66 (command (ps-font-command font))
68 ;; FIXME -- see (ps-font-command)
69 (plain (ps-font-command font))
70 (designsize (ly:font-design-size font))
71 (magnification (* (ly:font-magnification font)))
72 (ops (ly:output-def-lookup paper 'output-scale))
73 (scaling (* ops magnification designsize)))
74 (if (equal? fontname "unknown")
75 (display (list font fontname)))
76 (define-font plain fontname scaling)))
79 (map (lambda (x) (font-load-command x))
80 (filter (lambda (x) (not (ly:pango-font? x)))
83 ;; FIXME: duplicated in other output backends
84 ;; FIXME: silly interface name
85 (define (output-variables layout)
86 ;; FIXME: duplicates output-layout's scope-entry->string, mostly
87 (define (value->string val)
89 ((string? val) (string-append "(" val ")"))
90 ((symbol? val) (symbol->string val))
91 ((number? val) (number->string val))
94 (define (output-entry ps-key ly-key)
97 (value->string (ly:output-def-lookup layout ly-key)) " def\n"))
100 "/lily-output-units "
101 (number->string (/ (ly:bp 1))) " def %% millimeter\n"
102 (output-entry "staff-line-thickness" 'line-thickness)
103 (output-entry "line-width" 'line-width)
104 (output-entry "paper-size" 'papersizename)
105 (output-entry "staff-height" 'staff-height) ;junkme.
107 (number->string (ly:output-def-lookup layout 'output-scale)) " def\n"
108 (output-entry "page-height" 'paper-height)
109 (output-entry "page-width" 'paper-width)))
111 (define (dump-page outputter page page-number page-count landscape?)
112 (ly:outputter-dump-string
115 (format "%%Page: ~a ~a\n" page-number page-number)
118 "page-width output-scale lily-output-units mul mul 0 translate 90 rotate\n"
122 "true setstrokeadjust\n"
123 "gsave 0 paper-height translate set-ps-scale-to-lily-scale\n"))
124 (ly:outputter-dump-stencil outputter page)
125 (ly:outputter-dump-string outputter "stroke grestore\nshowpage\n"))
127 (define (supplies-or-needs paper load-fonts?)
128 (define (extract-names font)
129 (if (ly:pango-font? font)
130 (map car (ly:pango-font-physical-fonts font))
131 (list (ly:font-name font))))
133 (let* ((fonts (ly:paper-fonts paper))
134 (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 " (lilypond-version) "\n"
148 (string-join (map ly:number->string bbox) " ") "\n"
150 (if (eq? (ly:output-def-lookup paper 'landscape) #t)
153 (supplies-or-needs paper load-fonts?)
156 (define (ps-document-media paper)
158 (ly:output-def-lookup paper 'output-scale)
159 (ly:output-def-lookup paper 'paper-width)) (ly:bp 1)))
161 (ly:output-def-lookup paper 'paper-height)
162 (ly:output-def-lookup paper 'output-scale))
164 (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t)))
165 (ly:format "%%DocumentMedia: ~a ~2f ~2f ~a ~a ~a\n"
166 (ly:output-def-lookup paper 'papersizename)
174 (define (file-header paper page-count load-fonts?)
175 (string-append "%!PS-Adobe-3.0\n"
176 "%%Creator: LilyPond " (lilypond-version) "\n"
177 "%%Pages: " (number->string page-count) "\n"
178 "%%PageOrder: Ascend\n"
180 (if (eq? (ly:output-def-lookup paper 'landscape) #t)
183 (ps-document-media paper)
184 (supplies-or-needs paper load-fonts?)
187 (define (procset file-name)
189 "%%BeginResource: procset (~a) 1 0
193 file-name (cached-file-contents file-name)))
195 (define (embed-document file-name)
196 (format "%%BeginDocument: ~a
200 file-name (cached-file-contents file-name)))
202 (define (setup-variables paper)
206 (output-variables paper)))
208 (define (cff-font? font)
209 (let* ((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))
219 "%%BeginResource: font ~a
220 %!PS-Adobe-3.0 Resource-FontSet
221 %%DocumentNeededResources: ProcSet (FontSetInit)
222 %%Title: (FontSet/~a)
225 %%IncludeResource: ProcSet (FontSetInit)
226 %%BeginResource: FontSet (~a)
227 /FontSetInit /ProcSet findresource begin
228 %%BeginData: ~s Binary Bytes
230 font-set-name font-set-name version font-set-name
231 (string-length binary-data)))
235 (string-append header
239 (define (write-preamble paper load-fonts? port)
240 (define (internal-font? file-name)
241 (or (string-startswith file-name "Emmentaler")
242 (string-startswith file-name "emmentaler")
243 (string-startswith file-name "aybabtu")
244 (string-startswith file-name "Aybabtu")))
246 (define (load-font-via-GS font-name-filename)
247 (define (ps-load-file file-name)
248 (if (string? file-name)
249 (if (string-contains file-name (ly:get-option 'datadir))
251 (set! file-name (ly:string-substitute (ly:get-option 'datadir)
254 "lilypond-datadir (~a) concatstrings (r) file .loadfont\n"
256 (format "(~a) (r) file .loadfont\n" file-name))
257 (format "% cannot find font file: ~a\n" file-name)))
259 (let* ((font (car font-name-filename))
260 (name (cadr font-name-filename))
261 (file-name (caddr font-name-filename))
262 (bare-file-name (ly:find-file file-name)))
264 (if (mac-font? bare-file-name)
265 (handle-mac-font name bare-file-name)
267 ((internal-font? file-name)
268 (ps-load-file (ly:find-file
269 (format "~a.otf" file-name))))
270 ((string? bare-file-name)
271 (ps-load-file file-name))
273 (ly:warning (_ "cannot embed ~S=~S") name file-name)
276 (define (dir-join a b)
279 (string-append a "/" b)))
281 (define (dir-listing dir-name)
282 (define (dir-helper dir lst)
283 (let ((e (readdir dir)))
286 (dir-helper dir (cons e lst)))))
287 (reverse (dir-helper (opendir dir-name) '())))
289 (define (handle-mac-font name filename)
290 (let* ((dir-name (tmpnam))
294 (mkdir dir-name #o700)
295 (set! status (ly:system
296 (format "cd ~a && fondu -force '~a'" dir-name filename)))
297 (set! files (dir-listing dir-name))
300 (let* ((full-name (dir-join dir-name f)))
302 (equal? 'regular (stat:type (stat full-name)))
303 (equal? name (ly:ttf-ps-name full-name)))
304 (set! embed (font-file-as-ps-string name full-name 0)))
305 (if (or (equal? "." f)
308 (delete-file full-name))))
313 (set! embed "% failed\n")
314 (ly:warning (_ "cannot extract file matching ~a from ~a")
318 (define (font-file-as-ps-string name file-name font-index)
319 (let* ((downcase-file-name (string-downcase file-name)))
321 ((and file-name (string-endswith downcase-file-name ".pfa"))
322 (embed-document file-name))
323 ((and file-name (string-endswith downcase-file-name ".pfb"))
324 (ly:pfb->pfa file-name))
325 ((and file-name (string-endswith downcase-file-name ".ttf"))
326 (ly:ttf->pfa file-name))
327 ((and file-name (string-endswith downcase-file-name ".ttc"))
328 (ly:ttf->pfa file-name font-index))
329 ((and file-name (string-endswith downcase-file-name ".otf"))
330 (ps-embed-cff (ly:otf->cff file-name) name 0))
332 (ly:warning (_ "do not know how to embed ~S=~S") name file-name)
335 (define (mac-font? bare-file-name)
336 (and (eq? PLATFORM 'darwin)
338 (or (string-endswith bare-file-name ".dfont")
339 (= (stat:size (stat bare-file-name)) 0))))
341 (define (load-font font-psname-filename-fontindex)
342 (let* ((font (list-ref font-psname-filename-fontindex 0))
343 (name (list-ref font-psname-filename-fontindex 1))
344 (file-name (list-ref font-psname-filename-fontindex 2))
345 (font-index (list-ref font-psname-filename-fontindex 3))
346 (bare-file-name (ly:find-file file-name)))
348 (cond ((mac-font? bare-file-name)
349 (handle-mac-font name bare-file-name))
350 ((and font (cff-font? font))
351 (ps-embed-cff (ly:otf-font-table-data font "CFF ")
354 (bare-file-name (font-file-as-ps-string
355 name bare-file-name font-index))
357 (ly:warning (_ "do not know how to embed font ~s ~s ~s")
358 name file-name font))))))
360 (define (load-fonts paper)
361 (let* ((fonts (ly:paper-fonts paper))
363 ;; todo - doc format of list.
367 (cond ((string? (ly:font-file-name font))
370 (ly:font-file-name font)
372 ((ly:pango-font? font)
373 (map (lambda (psname-filename-fontindex)
375 (list-ref psname-filename-fontindex 0)
376 (list-ref psname-filename-fontindex 1)
377 (list-ref psname-filename-fontindex 2)))
378 (ly:pango-font-physical-fonts font)))
380 (ly:font-sub-fonts font))))
382 (font-names (uniq-list
383 (sort (apply append all-font-names)
384 (lambda (x y) (string<? (cadr x) (cadr y))))))
386 ;; slightly spaghetti-ish: deciding what to load where
390 (cond ((ly:get-option 'gs-load-fonts)
391 (load-font-via-GS name))
392 ((ly:get-option 'gs-load-lily-fonts)
393 (if (or (string-contains (caddr name)
394 (ly:get-option 'datadir))
395 (internal-font? (caddr name)))
396 (load-font-via-GS name)
400 (pfas (map font-loader font-names)))
403 (display "%%BeginProlog\n" port)
406 "/lilypond-datadir where {pop} {userdict /lilypond-datadir (~a) put } ifelse"
407 (ly:get-option 'datadir))
409 (for-each (lambda (f)
410 (format port "\n%%BeginFont: ~a\n" (car f))
411 (display (cdr f) port)
412 (display "%%EndFont\n" port))
414 (display (setup-variables paper) port)
416 ;; adobe note 5002: should initialize variables before loading routines.
417 (display (procset "music-drawing-routines.ps") port)
418 (display (procset "lilyponddefs.ps") port)
419 (display "%%EndProlog\n" port)
420 (display "%%BeginSetup\ninit-lilypond-parameters\n%%EndSetup\n\n" port))
422 (define-public (output-framework basename book scopes fields)
423 (let* ((filename (format "~a.ps" basename))
424 (outputter (ly:make-paper-outputter
425 ;; FIXME: better wrap open/open-file,
426 ;; content-mangling is always bad.
427 ;; MINGW hack: need to have "b"inary for embedding CFFs
428 (open-file filename "wb")
430 (paper (ly:paper-book-paper book))
431 (systems (ly:paper-book-systems book))
432 (page-stencils (map page-stencil (ly:paper-book-pages book)))
433 (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t))
434 (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
435 (page-count (length page-stencils))
436 (port (ly:outputter-port outputter)))
437 (if (ly:get-option 'clip-systems)
438 (clip-system-EPSes basename book))
439 (if (ly:get-option 'dump-signatures)
440 (write-system-signatures basename (ly:paper-book-systems book) 1))
441 (output-scopes scopes fields basename)
442 (display (file-header paper page-count #t) port)
443 ;; don't do BeginDefaults PageMedia: A4
444 ;; not necessary and wrong
445 (write-preamble paper #t port)
448 (set! page-number (1+ page-number))
449 (dump-page outputter page page-number page-count landscape?))
451 (display "%%Trailer\n%%EOF\n" port)
452 (ly:outputter-close outputter)
453 (postprocess-output book framework-ps-module filename
454 (ly:output-formats))))
456 (define-public (dump-stencil-as-EPS paper dump-me filename
458 (let* ((xext (ly:stencil-extent dump-me X))
459 (yext (ly:stencil-extent dump-me Y))
460 (padding (ly:get-option 'eps-box-padding))
461 (left-overshoot (if (number? padding)
462 (* -1 padding (ly:output-def-lookup paper 'mm))
467 (if (or (nan? x) (inf? x)
469 (equal? (format #f "~S" x) "+#.#")
470 (equal? (format #f "~S" x) "-#.#"))
473 ;; the left-overshoot is to make sure that
474 ;; bar numbers stick out of margin uniformly.
477 (if (number? left-overshoot)
478 (min left-overshoot (car xext))
480 (car yext) (cdr xext) (cdr yext)))))
481 (dump-stencil-as-EPS-with-bbox paper dump-me filename load-fonts bbox)))
483 (define-public (dump-stencil-as-EPS-with-bbox paper dump-me filename
486 "Create an EPS file from stencil DUMP-ME to FILENAME. BBOX has
487 format (left-x, lower-y, right x, up-y). If LOAD-FONTS set, include
489 (define (to-rounded-bp-box box)
490 "Convert box to 1/72 inch with rounding to enlarge the box."
491 (let* ((scale (ly:output-def-lookup paper 'output-scale))
492 (strip-non-number (lambda (x)
497 (directed-round (lambda (x rounder)
499 (rounder (/ (* (strip-non-number x) scale)
501 (list (directed-round (car box) floor)
502 (directed-round (cadr box) floor)
503 (directed-round (max (1+ (car box)) (caddr box)) ceiling)
504 (directed-round (max (1+ (cadr box)) (cadddr box)) ceiling))))
506 (let* ((outputter (ly:make-paper-outputter
507 ;; FIXME: better wrap open/open-file,
508 ;; content-mangling is always bad.
509 ;; MINGW hack: need to have "b"inary for embedding CFFs
510 (open-file (format "~a.eps" filename) "wb")
512 (port (ly:outputter-port outputter))
513 (rounded-bbox (to-rounded-bp-box bbox))
514 (port (ly:outputter-port outputter))
515 (header (eps-header paper rounded-bbox load-fonts)))
516 (display header port)
517 (write-preamble paper load-fonts port)
518 (display "gsave set-ps-scale-to-lily-scale\n" port)
519 (ly:outputter-dump-stencil outputter dump-me)
520 (display "stroke grestore\n%%Trailer\n%%EOF\n" port)
521 (ly:outputter-close outputter)))
523 (define (clip-systems-to-region basename paper systems region do-pdf do-png)
524 (let* ((extents-system-pairs
525 (filtered-map (lambda (paper-system)
526 (let* ((x-ext (system-clipped-x-extent
527 (paper-system-system-grob paper-system)
530 (cons x-ext paper-system)
535 (lambda (ext-system-pair)
536 (let* ((xext (car ext-system-pair))
537 (paper-system (cdr ext-system-pair))
538 (yext (paper-system-extent paper-system Y))
539 (bbox (list (car xext) (car yext)
540 (cdr xext) (cdr yext)))
541 (filename (if (< 0 count)
542 (format "~a-~a" basename count)
544 (set! count (1+ count))
545 (dump-stencil-as-EPS-with-bbox paper
546 (paper-system-stencil paper-system)
548 (ly:get-option 'include-eps-fonts)
551 (postscript->pdf 0 0 (format "~a.eps" filename)))
553 (postscript->png (ly:get-option 'resolution) 0 0
554 (format "~a.eps" filename)))))
555 extents-system-pairs)))
557 (define-public (clip-system-EPSes basename paper-book)
559 (member "pdf" (ly:output-formats)))
561 (member "png" (ly:output-formats)))
563 (define (clip-score-systems basename systems)
564 (let* ((layout (ly:grob-layout (paper-system-system-grob (car systems))))
565 (regions (ly:output-def-lookup layout 'clip-regions)))
568 (clip-systems-to-region
569 (format "~a-from-~a-to-~a-clip"
571 (rhythmic-location->file-string (car region))
572 (rhythmic-location->file-string (cdr region)))
573 layout systems region
577 ;; partition in system lists sharing their layout blocks
578 (let* ((systems (ly:paper-book-systems paper-book))
580 (score-system-list '()))
582 (lambda (system last-system)
583 (if (not (and last-system
584 (equal? (paper-system-layout last-system)
585 (paper-system-layout system))))
586 (set! score-system-list (cons '() score-system-list)))
587 (if (paper-system-layout system)
588 (set-car! score-system-list (cons system (car score-system-list))))
593 (for-each (lambda (system-list)
594 ;; filter out headers and top-level markup
595 (if (pair? system-list)
598 (format "~a-~a" basename count)
603 (define-public (output-preview-framework basename book scopes fields)
604 (let* ((paper (ly:paper-book-paper book))
605 (systems (ly:paper-book-systems book))
606 (scale (ly:output-def-lookup paper 'output-scale))
607 (to-dump-systems '()))
609 (if (and (not (ly:get-option 'include-book-title-preview))
611 (ly:prob-property (car systems) 'is-book-title #f))
612 (set! systems (cdr systems)))
615 (if (or (paper-system-title? sys)
616 (not (pair? to-dump-systems))
617 (paper-system-title? (car to-dump-systems)))
618 (set! to-dump-systems (cons sys to-dump-systems))))
620 (dump-stencil-as-EPS paper
621 (stack-stencils Y DOWN 0.0
622 (map paper-system-stencil
623 (reverse to-dump-systems)))
624 (format "~a.preview" basename)
626 (postprocess-output book framework-ps-module
627 (format "~a.preview.eps" basename)
628 (cons "png" (ly:output-formats)))))
631 (define-public (output-preview-framework basename book scopes fields)
632 (let* ((paper (ly:paper-book-paper book))
633 (systems (ly:paper-book-systems book))
634 (scale (ly:output-def-lookup paper 'output-scale))
635 (titles (take-while paper-system-title? systems))
636 (non-title (find (lambda (x)
637 (not (paper-system-title? x))) systems))
639 (stack-stencils Y DOWN 0.0
640 (map paper-system-stencil
641 (append titles (list non-title))))))
642 (output-scopes scopes fields basename)
643 (dump-stencil-as-EPS paper dump-me
644 (format "~a.preview" basename)
646 (postprocess-output book framework-ps-module
647 (format "~a.preview.eps" basename)
648 (ly:output-formats)))))
650 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
652 (define-public (convert-to-pdf book name)
653 (let* ((defs (ly:paper-book-paper book))
654 (landscape (ly:output-def-lookup defs 'landscape))
655 (output-scale (ly:output-def-lookup defs 'output-scale))
657 (* x output-scale (/ (ly:bp 1)))))
658 (paper-width (convert (ly:output-def-lookup defs 'paper-width)))
659 (paper-height (convert (ly:output-def-lookup defs 'paper-height)))
660 (w (if landscape paper-height paper-width))
661 (h (if landscape paper-width paper-height)))
662 (if (equal? (basename name ".ps") "-")
663 (ly:warning (_ "cannot convert <stdout> to ~S" "PDF"))
664 (postscript->pdf w h name))))
666 (define-public (convert-to-png book name)
667 (let* ((defs (ly:paper-book-paper book))
668 (defs-resolution (ly:output-def-lookup defs 'pngresolution))
669 (resolution (if (number? defs-resolution)
671 (ly:get-option 'resolution)))
672 (paper-width (ly:output-def-lookup defs 'paper-width))
673 (paper-height (ly:output-def-lookup defs 'paper-height))
674 (output-scale (ly:output-def-lookup defs 'output-scale)))
675 (postscript->png resolution
676 (* paper-width output-scale (/ (ly:bp 1)))
677 (* paper-height output-scale (/ (ly:bp 1)))
680 (define-public (convert-to-ps book name)
683 (define-public (output-classic-framework basename book scopes fields)
684 (ly:error (_ "\nThe PostScript backend does not support the
685 system-by-system output. For that, use the EPS backend instead,
687 lilypond -dbackend=eps FILE
689 If have cut & pasted a lilypond fragment from a webpage, be sure
690 to only remove anything before
692 %% ****************************************************************
693 %% Start cut-&-pastable-section
694 %% ****************************************************************