1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2004--2010 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")
245 (define (load-font-via-GS font-name-filename)
246 (define (ps-load-file file-name)
247 (if (string? file-name)
248 (if (string-contains file-name (ly:get-option 'datadir))
250 (set! file-name (ly:string-substitute (ly:get-option 'datadir)
253 "lilypond-datadir (~a) concatstrings (r) file .loadfont\n"
255 (format "(~a) (r) file .loadfont\n" file-name))
256 (format "% cannot find font file: ~a\n" file-name)))
258 (let* ((font (car font-name-filename))
259 (name (cadr font-name-filename))
260 (file-name (caddr font-name-filename))
261 (bare-file-name (ly:find-file file-name)))
263 (if (mac-font? bare-file-name)
264 (handle-mac-font name bare-file-name)
266 ((internal-font? file-name)
267 (ps-load-file (ly:find-file
268 (format "~a.otf" file-name))))
269 ((string? bare-file-name)
270 (ps-load-file file-name))
272 (ly:warning (_ "cannot embed ~S=~S") name file-name)
275 (define (dir-join a b)
278 (string-append a "/" b)))
280 (define (dir-listing dir-name)
281 (define (dir-helper dir lst)
282 (let ((e (readdir dir)))
285 (dir-helper dir (cons e lst)))))
286 (reverse (dir-helper (opendir dir-name) '())))
288 (define (handle-mac-font name filename)
289 (let* ((dir-name (tmpnam))
293 (mkdir dir-name #o700)
294 (set! status (ly:system
295 (format "cd ~a && fondu -force '~a'" dir-name filename)))
296 (set! files (dir-listing dir-name))
299 (let* ((full-name (dir-join dir-name f)))
301 (equal? 'regular (stat:type (stat full-name)))
302 (equal? name (ly:ttf-ps-name full-name)))
303 (set! embed (font-file-as-ps-string name full-name 0)))
304 (if (or (equal? "." f)
307 (delete-file full-name))))
312 (set! embed "% failed\n")
313 (ly:warning (_ "cannot extract file matching ~a from ~a")
317 (define (font-file-as-ps-string name file-name font-index)
318 (let* ((downcase-file-name (string-downcase file-name)))
320 ((and file-name (string-endswith downcase-file-name ".pfa"))
321 (embed-document file-name))
322 ((and file-name (string-endswith downcase-file-name ".pfb"))
323 (ly:pfb->pfa file-name))
324 ((and file-name (string-endswith downcase-file-name ".ttf"))
325 (ly:ttf->pfa file-name))
326 ((and file-name (string-endswith downcase-file-name ".ttc"))
327 (ly:ttf->pfa file-name font-index))
328 ((and file-name (string-endswith downcase-file-name ".otf"))
329 (ps-embed-cff (ly:otf->cff file-name) name 0))
331 (ly:warning (_ "do not know how to embed ~S=~S") name file-name)
334 (define (mac-font? bare-file-name)
335 (and (eq? PLATFORM 'darwin)
337 (or (string-endswith bare-file-name ".dfont")
338 (= (stat:size (stat bare-file-name)) 0))))
340 (define (load-font font-psname-filename-fontindex)
341 (let* ((font (list-ref font-psname-filename-fontindex 0))
342 (name (list-ref font-psname-filename-fontindex 1))
343 (file-name (list-ref font-psname-filename-fontindex 2))
344 (font-index (list-ref font-psname-filename-fontindex 3))
345 (bare-file-name (ly:find-file file-name)))
347 (cond ((mac-font? bare-file-name)
348 (handle-mac-font name bare-file-name))
349 ((and font (cff-font? font))
350 (ps-embed-cff (ly:otf-font-table-data font "CFF ")
353 (bare-file-name (font-file-as-ps-string
354 name bare-file-name font-index))
356 (ly:warning (_ "do not know how to embed font ~s ~s ~s")
357 name file-name font))))))
359 (define (load-fonts paper)
360 (let* ((fonts (ly:paper-fonts paper))
362 ;; todo - doc format of list.
366 (cond ((string? (ly:font-file-name font))
369 (ly:font-file-name font)
371 ((ly:pango-font? font)
372 (map (lambda (psname-filename-fontindex)
374 (list-ref psname-filename-fontindex 0)
375 (list-ref psname-filename-fontindex 1)
376 (list-ref psname-filename-fontindex 2)))
377 (ly:pango-font-physical-fonts font)))
379 (ly:font-sub-fonts font))))
381 (font-names (uniq-list
382 (sort (apply append all-font-names)
383 (lambda (x y) (string<? (cadr x) (cadr y))))))
385 ;; slightly spaghetti-ish: deciding what to load where
389 (cond ((ly:get-option 'gs-load-fonts)
390 (load-font-via-GS name))
391 ((ly:get-option 'gs-load-lily-fonts)
392 (if (or (string-contains (caddr name)
393 (ly:get-option 'datadir))
394 (internal-font? (caddr name)))
395 (load-font-via-GS name)
399 (pfas (map font-loader font-names)))
402 (display "%%BeginProlog\n" port)
405 "/lilypond-datadir where {pop} {userdict /lilypond-datadir (~a) put } ifelse"
406 (ly:get-option 'datadir))
408 (for-each (lambda (f)
409 (format port "\n%%BeginFont: ~a\n" (car f))
410 (display (cdr f) port)
411 (display "%%EndFont\n" port))
413 (display (setup-variables paper) port)
415 ;; adobe note 5002: should initialize variables before loading routines.
416 (display (procset "music-drawing-routines.ps") port)
417 (display (procset "lilyponddefs.ps") port)
418 (display "%%EndProlog\n" port)
419 (display "%%BeginSetup\ninit-lilypond-parameters\n%%EndSetup\n\n" port))
421 (define-public (output-framework basename book scopes fields)
422 (let* ((filename (format "~a.ps" basename))
423 (outputter (ly:make-paper-outputter
424 ;; FIXME: better wrap open/open-file,
425 ;; content-mangling is always bad.
426 ;; MINGW hack: need to have "b"inary for embedding CFFs
427 (open-file filename "wb")
429 (paper (ly:paper-book-paper book))
430 (systems (ly:paper-book-systems book))
431 (page-stencils (map page-stencil (ly:paper-book-pages book)))
432 (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t))
433 (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
434 (page-count (length page-stencils))
435 (port (ly:outputter-port outputter)))
436 (if (ly:get-option 'clip-systems)
437 (clip-system-EPSes basename book))
438 (if (ly:get-option 'dump-signatures)
439 (write-system-signatures basename (ly:paper-book-systems book) 1))
440 (output-scopes scopes fields basename)
441 (display (file-header paper page-count #t) port)
442 ;; don't do BeginDefaults PageMedia: A4
443 ;; not necessary and wrong
444 (write-preamble paper #t port)
447 (set! page-number (1+ page-number))
448 (dump-page outputter page page-number page-count landscape?))
450 (display "%%Trailer\n%%EOF\n" port)
451 (ly:outputter-close outputter)
452 (postprocess-output book framework-ps-module filename
453 (ly:output-formats))))
455 (define-public (dump-stencil-as-EPS paper dump-me filename
457 (let* ((xext (ly:stencil-extent dump-me X))
458 (yext (ly:stencil-extent dump-me Y))
459 (padding (ly:get-option 'eps-box-padding))
460 (left-overshoot (if (number? padding)
461 (* -1 padding (ly:output-def-lookup paper 'mm))
466 (if (or (nan? x) (inf? x)
468 (equal? (format #f "~S" x) "+#.#")
469 (equal? (format #f "~S" x) "-#.#"))
472 ;; the left-overshoot is to make sure that
473 ;; bar numbers stick out of margin uniformly.
476 (if (number? left-overshoot)
477 (min left-overshoot (car xext))
479 (car yext) (cdr xext) (cdr yext)))))
480 (dump-stencil-as-EPS-with-bbox paper dump-me filename load-fonts bbox)))
482 (define-public (dump-stencil-as-EPS-with-bbox paper dump-me filename
485 "Create an EPS file from stencil DUMP-ME to FILENAME. BBOX has
486 format (left-x, lower-y, right x, up-y). If LOAD-FONTS set, include
488 (define (to-rounded-bp-box box)
489 "Convert box to 1/72 inch with rounding to enlarge the box."
490 (let* ((scale (ly:output-def-lookup paper 'output-scale))
491 (strip-non-number (lambda (x)
496 (directed-round (lambda (x rounder)
498 (rounder (/ (* (strip-non-number x) scale)
500 (list (directed-round (car box) floor)
501 (directed-round (cadr box) floor)
502 (directed-round (max (1+ (car box)) (caddr box)) ceiling)
503 (directed-round (max (1+ (cadr box)) (cadddr box)) ceiling))))
505 (let* ((outputter (ly:make-paper-outputter
506 ;; FIXME: better wrap open/open-file,
507 ;; content-mangling is always bad.
508 ;; MINGW hack: need to have "b"inary for embedding CFFs
509 (open-file (format "~a.eps" filename) "wb")
511 (port (ly:outputter-port outputter))
512 (rounded-bbox (to-rounded-bp-box bbox))
513 (port (ly:outputter-port outputter))
514 (header (eps-header paper rounded-bbox load-fonts)))
515 (display header port)
516 (write-preamble paper load-fonts port)
517 (display "gsave set-ps-scale-to-lily-scale\n" port)
518 (ly:outputter-dump-stencil outputter dump-me)
519 (display "stroke grestore\n%%Trailer\n%%EOF\n" port)
520 (ly:outputter-close outputter)))
522 (define (clip-systems-to-region basename paper systems region do-pdf do-png)
523 (let* ((extents-system-pairs
524 (filtered-map (lambda (paper-system)
525 (let* ((x-ext (system-clipped-x-extent
526 (paper-system-system-grob paper-system)
529 (cons x-ext paper-system)
534 (lambda (ext-system-pair)
535 (let* ((xext (car ext-system-pair))
536 (paper-system (cdr ext-system-pair))
537 (yext (paper-system-extent paper-system Y))
538 (bbox (list (car xext) (car yext)
539 (cdr xext) (cdr yext)))
540 (filename (if (< 0 count)
541 (format "~a-~a" basename count)
543 (set! count (1+ count))
544 (dump-stencil-as-EPS-with-bbox paper
545 (paper-system-stencil paper-system)
547 (ly:get-option 'include-eps-fonts)
550 (postscript->pdf 0 0 (format "~a.eps" filename)))
552 (postscript->png (ly:get-option 'resolution) 0 0
553 (format "~a.eps" filename)))))
554 extents-system-pairs)))
556 (define-public (clip-system-EPSes basename paper-book)
558 (member "pdf" (ly:output-formats)))
560 (member "png" (ly:output-formats)))
562 (define (clip-score-systems basename systems)
563 (let* ((layout (ly:grob-layout (paper-system-system-grob (car systems))))
564 (regions (ly:output-def-lookup layout 'clip-regions)))
567 (clip-systems-to-region
568 (format "~a-from-~a-to-~a-clip"
570 (rhythmic-location->file-string (car region))
571 (rhythmic-location->file-string (cdr region)))
572 layout systems region
576 ;; partition in system lists sharing their layout blocks
577 (let* ((systems (ly:paper-book-systems paper-book))
579 (score-system-list '()))
581 (lambda (system last-system)
582 (if (not (and last-system
583 (equal? (paper-system-layout last-system)
584 (paper-system-layout system))))
585 (set! score-system-list (cons '() score-system-list)))
586 (if (paper-system-layout system)
587 (set-car! score-system-list (cons system (car score-system-list))))
592 (for-each (lambda (system-list)
593 ;; filter out headers and top-level markup
594 (if (pair? system-list)
597 (format "~a-~a" basename count)
602 (define-public (output-preview-framework basename book scopes fields)
603 (let* ((paper (ly:paper-book-paper book))
604 (systems (relevant-book-systems book))
605 (to-dump-systems (relevant-dump-systems systems)))
606 (dump-stencil-as-EPS paper
607 (stack-stencils Y DOWN 0.0
608 (map paper-system-stencil
609 (reverse to-dump-systems)))
610 (format "~a.preview" basename)
612 (postprocess-output book framework-ps-module
613 (format "~a.preview.eps" basename)
614 (cons "png" (ly:output-formats)))))
616 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
618 (define-public (convert-to-pdf book name)
619 (let* ((defs (ly:paper-book-paper book))
620 (landscape (ly:output-def-lookup defs 'landscape))
621 (output-scale (ly:output-def-lookup defs 'output-scale))
623 (* x output-scale (/ (ly:bp 1)))))
624 (paper-width (convert (ly:output-def-lookup defs 'paper-width)))
625 (paper-height (convert (ly:output-def-lookup defs 'paper-height)))
626 (w (if landscape paper-height paper-width))
627 (h (if landscape paper-width paper-height)))
628 (if (equal? (basename name ".ps") "-")
629 (set! name (string-append "./" name)))
630 (postscript->pdf w h name)))
632 (define-public (convert-to-png book name)
633 (let* ((defs (ly:paper-book-paper book))
634 (defs-resolution (ly:output-def-lookup defs 'pngresolution))
635 (resolution (if (number? defs-resolution)
637 (ly:get-option 'resolution)))
638 (paper-width (ly:output-def-lookup defs 'paper-width))
639 (paper-height (ly:output-def-lookup defs 'paper-height))
640 (output-scale (ly:output-def-lookup defs 'output-scale)))
641 (if (equal? (basename name ".ps") "-")
642 (set! name (string-append "./" name)))
643 (postscript->png resolution
644 (* paper-width output-scale (/ (ly:bp 1)))
645 (* paper-height output-scale (/ (ly:bp 1)))
648 (define-public (convert-to-ps book name)
651 (define-public (output-classic-framework basename book scopes fields)
652 (ly:error (_ "\nThe PostScript backend does not support the
653 system-by-system output. For that, use the EPS backend instead,
655 lilypond -dbackend=eps FILE
657 If have cut & pasted a lilypond fragment from a webpage, be sure
658 to only remove anything before
660 %% ****************************************************************
661 %% Start cut-&-pastable-section
662 %% ****************************************************************