]> git.donarmstrong.com Git - lilypond.git/blob - scm/framework-ps.scm
Run grand-replace (issue 3765)
[lilypond.git] / scm / framework-ps.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2004--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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/>.
17
18 (define-module (scm framework-ps))
19
20 ;;; this is still too big a mess.
21
22 (use-modules (ice-9 string-fun)
23              (guile)
24              (scm page)
25              (scm paper-system)
26              (srfi srfi-1)
27              (srfi srfi-13)
28              (scm clip-region)
29              (lily))
30
31 (define format ergonomic-simple-format)
32
33 (define framework-ps-module (current-module))
34
35 ;;(define pdebug stderr)
36 (define (pdebug . rest) #f)
37
38 (define-public (ps-font-command font)
39   (let* ((name (ly:font-file-name font))
40          (magnify (ly:font-magnification font)))
41     (string-append
42      "magfont"
43      (ly:string-substitute
44       " " "_"
45       (ly:string-substitute
46        "/" "_"
47        (ly:string-substitute
48         "%" "_" name)))
49      "m" (string-encode-integer (inexact->exact (round (* 1000 magnify)))))))
50
51 (define (ps-define-pango-pf pango-pf font-name scaling)
52   "")
53
54 (define (ps-define-font font font-name scaling)
55   (string-append
56    "/" (ps-font-command font)
57    " { /" font-name
58    " " (ly:number->string scaling) " output-scale div selectfont }"
59    " bind def\n"))
60
61 ;; FIXME: duplicated in other output backends
62 ;; FIXME: silly interface name
63 (define (output-variables layout)
64   ;; FIXME: duplicates output-layout's scope-entry->string, mostly
65   (define (value->string val)
66     (cond
67      ((string? val) (string-append "(" val ")"))
68      ((symbol? val) (symbol->string val))
69      ((number? val) (number->string val))
70      (else "")))
71
72   (define (output-entry ps-key ly-key)
73     (string-append
74      "/" ps-key " "
75      (value->string (ly:output-def-lookup layout ly-key)) " def\n"))
76
77   (string-append
78    "/lily-output-units "
79    (number->string (/ (ly:bp 1))) " def %% millimeter\n"
80    (output-entry "staff-line-thickness" 'line-thickness)
81    (output-entry "line-width" 'line-width)
82    (output-entry "paper-size" 'papersizename)
83    (output-entry "staff-height" 'staff-height)  ;junkme.
84    "/output-scale "
85    (number->string (ly:output-def-lookup layout 'output-scale)) " def\n"
86    (output-entry "page-height" 'paper-height)
87    (output-entry "page-width" 'paper-width)
88    (if (ly:get-option 'strokeadjust) "true setstrokeadjust\n" "")
89    ))
90
91 (define (dump-page outputter page page-number page-count landscape?)
92   (ly:outputter-dump-string
93    outputter
94    (string-append
95     (format #f "%%Page: ~a ~a\n" page-number page-number)
96     "%%BeginPageSetup\n"
97     (if landscape?
98         "page-width output-scale lily-output-units mul mul 0 translate 90 rotate\n"
99         "")
100     "%%EndPageSetup\n"
101     "\n"
102     "gsave 0 paper-height translate set-ps-scale-to-lily-scale\n"))
103   (ly:outputter-dump-stencil outputter page)
104   (ly:outputter-dump-string outputter "stroke grestore\nshowpage\n"))
105
106 (define (supplies-or-needs paper load-fonts?)
107   (define (extract-names font)
108     (if (ly:pango-font? font)
109         (map car (ly:pango-font-physical-fonts font))
110         (list (ly:font-name font))))
111
112   (let* ((fonts (ly:paper-fonts paper))
113          (names (append-map extract-names fonts)))
114     (string-concatenate
115      (map (lambda (f)
116             (format #f
117                     (if load-fonts?
118                         "%%DocumentSuppliedResources: font ~a\n"
119                         "%%DocumentNeededResources: font ~a\n")
120                     f))
121           (uniq-list (sort names string<?))))))
122
123 (define (eps-header paper bbox load-fonts?)
124   (string-append "%!PS-Adobe-2.0 EPSF-2.0\n"
125                  "%%Creator: LilyPond " (lilypond-version) "\n"
126                  "%%BoundingBox: "
127                  (string-join (map ly:number->string bbox) " ") "\n"
128                  "%%Orientation: "
129                  (if (eq? (ly:output-def-lookup paper 'landscape) #t)
130                      "Landscape\n"
131                      "Portrait\n")
132                  (supplies-or-needs paper load-fonts?)
133                  "%%EndComments\n"))
134
135 (define (ps-document-media paper)
136   (let* ((w (/ (*
137                 (ly:output-def-lookup paper 'output-scale)
138                 (ly:output-def-lookup paper 'paper-width)) (ly:bp 1)))
139          (h (/ (*
140                 (ly:output-def-lookup paper 'paper-height)
141                 (ly:output-def-lookup paper 'output-scale))
142                (ly:bp 1)))
143          (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t)))
144     (ly:format "%%DocumentMedia: ~a ~2f ~2f ~a ~a ~a\n"
145                (ly:output-def-lookup paper 'papersizename)
146                (if landscape? h w)
147                (if landscape? w h)
148                80   ;; weight
149                "()" ;; color
150                "()" ;; type
151                )))
152
153 (define (file-header paper page-count load-fonts?)
154   (string-append "%!PS-Adobe-3.0\n"
155                  "%%Creator: LilyPond " (lilypond-version) "\n"
156                  "%%Pages: " (number->string page-count) "\n"
157                  "%%PageOrder: Ascend\n"
158                  "%%Orientation: "
159                  (if (eq? (ly:output-def-lookup paper 'landscape) #t)
160                      "Landscape\n"
161                      "Portrait\n")
162                  (ps-document-media paper)
163                  (supplies-or-needs paper load-fonts?)
164                  "%%EndComments\n"))
165
166 (define (procset file-name)
167   (format #f
168           "%%BeginResource: procset (~a) 1 0
169 ~a
170 %%EndResource
171 "
172           file-name (cached-file-contents file-name)))
173
174 (define (embed-document file-name)
175   (format #f "%%BeginDocument: ~a
176 ~a
177 %%EndDocument
178 "
179           file-name (cached-file-contents file-name)))
180
181 (define (setup-variables paper)
182   (string-append
183    "\n"
184    (define-fonts paper ps-define-font ps-define-pango-pf)
185    (output-variables paper)))
186
187 (define (cff-font? font)
188   (let* ((cff-string (ly:otf-font-table-data font "CFF ")))
189     (> (string-length cff-string) 0)))
190
191 (define-public (ps-embed-cff body font-set-name version)
192   (let* ((binary-data
193           (string-append
194            (format #f "/~a ~s StartData " font-set-name (string-length body))
195            body))
196          (header
197           (format #f
198                   "%%BeginResource: font ~a
199 %!PS-Adobe-3.0 Resource-FontSet
200 %%DocumentNeededResources: ProcSet (FontSetInit)
201 %%Title: (FontSet/~a)
202 %%Version: ~s
203 %%EndComments
204 %%IncludeResource: ProcSet (FontSetInit)
205 %%BeginResource: FontSet (~a)
206 /FontSetInit /ProcSet findresource begin
207 %%BeginData: ~s Binary Bytes
208 "
209                   font-set-name font-set-name version font-set-name
210                   (string-length binary-data)))
211          (footer "\n%%EndData
212 %%EndResource
213 %%EndResource\n"))
214     (string-append header
215                    binary-data
216                    footer)))
217
218 (define (write-preamble paper load-fonts? port)
219   (define (internal-font? file-name)
220     (or (string-startswith file-name "Emmentaler")
221         (string-startswith file-name "emmentaler")
222         ))
223
224   (define (load-font-via-GS font-name-filename)
225     (define (ps-load-file file-name)
226       (if (string? file-name)
227           (if (string-contains file-name (ly:get-option 'datadir))
228               (begin
229                 (set! file-name (ly:string-substitute (ly:get-option 'datadir)
230                                                       "" file-name))
231                 (format #f
232                         "lilypond-datadir (~a) concatstrings (r) file .loadfont\n"
233                         file-name))
234               (format #f "(~a) (r) file .loadfont\n" file-name))
235           (format #f "% cannot find font file: ~a\n" file-name)))
236
237     (let* ((font (car font-name-filename))
238            (name (cadr font-name-filename))
239            (file-name (caddr font-name-filename))
240            (bare-file-name (ly:find-file file-name)))
241       (cons name
242             (if (mac-font? bare-file-name)
243                 (handle-mac-font name bare-file-name)
244                 (cond
245                  ((internal-font? file-name)
246                   (ps-load-file (ly:find-file
247                                  (format #f "~a.otf" file-name))))
248                  ((string? bare-file-name)
249                   (ps-load-file file-name))
250                  (else
251                   (ly:warning (_ "cannot embed ~S=~S") name file-name)
252                   ""))))))
253
254   (define (dir-join a b)
255     (if (equal? a "")
256         b
257         (string-append a "/" b)))
258
259   (define (dir-listing dir-name)
260     (define (dir-helper dir lst)
261       (let ((e (readdir dir)))
262         (if (eof-object? e)
263             lst
264             (dir-helper dir (cons e lst)))))
265     (reverse (dir-helper (opendir dir-name) '())))
266
267   (define (handle-mac-font name file-name)
268     (let* ((dir-name (tmpnam))
269            (files '())
270            (status 0)
271            (embed #f)
272            (cwd (getcwd)))
273       (mkdir dir-name #o700)
274       (chdir dir-name)
275       (set! status (ly:system (list "fondu" "-force" file-name)))
276       (chdir cwd)
277       (set! files (dir-listing dir-name))
278       (for-each
279        (lambda (f)
280          (let* ((full-name (dir-join dir-name f)))
281            (if (and (not embed)
282                     (equal? 'regular (stat:type (stat full-name)))
283                     (equal? name (ly:ttf-ps-name full-name)))
284                (set! embed (font-file-as-ps-string name full-name 0)))
285            (if (or (equal? "." f)
286                    (equal? ".." f))
287                #t
288                (delete-file full-name))))
289        files)
290       (rmdir dir-name)
291       (if (not embed)
292           (begin
293             (set! embed "% failed\n")
294             (ly:warning (_ "cannot extract file matching ~a from ~a")
295                         name file-name)))
296       embed))
297
298   (define (font-file-as-ps-string name file-name font-index)
299     (let* ((downcase-file-name (string-downcase file-name)))
300       (cond
301        ((and file-name (string-endswith downcase-file-name ".pfa"))
302         (embed-document file-name))
303        ((and file-name (string-endswith downcase-file-name ".pfb"))
304         (ly:pfb->pfa file-name))
305        ((and file-name (string-endswith downcase-file-name ".ttf"))
306         (ly:ttf->pfa file-name))
307        ((and file-name (string-endswith downcase-file-name ".ttc"))
308         (ly:ttf->pfa file-name font-index))
309        ((and file-name (string-endswith downcase-file-name ".otf"))
310         (ps-embed-cff (ly:otf->cff file-name) name 0))
311        (else
312         (ly:warning (_ "do not know how to embed ~S=~S") name file-name)
313         ""))))
314
315   (define (mac-font? bare-file-name)
316     (and (eq? PLATFORM 'darwin)
317          bare-file-name
318          (or (string-endswith bare-file-name ".dfont")
319              (= (stat:size (stat bare-file-name)) 0))))
320
321   (define (load-font font-psname-filename-fontindex)
322     (let* ((font (list-ref font-psname-filename-fontindex 0))
323            (name (list-ref font-psname-filename-fontindex 1))
324            (file-name (list-ref font-psname-filename-fontindex 2))
325            (font-index (list-ref font-psname-filename-fontindex 3))
326            (bare-file-name (ly:find-file file-name)))
327       (cons name
328             (cond ((mac-font? bare-file-name)
329                    (handle-mac-font name bare-file-name))
330                   ((and font (cff-font? font))
331                    (ps-embed-cff (ly:otf-font-table-data font "CFF ")
332                                  name
333                                  0))
334                   (bare-file-name (font-file-as-ps-string
335                                    name bare-file-name font-index))
336                   (else
337                    (ly:warning (_ "do not know how to embed font ~s ~s ~s")
338                                name file-name font))))))
339
340   (define (load-fonts paper)
341     (let* ((fonts (ly:paper-fonts paper))
342
343            ;; todo - doc format of list.
344            (all-font-names
345             (map
346              (lambda (font)
347                (cond ((string? (ly:font-file-name font))
348                       (list (list font
349                                   (ly:font-name font)
350                                   (ly:font-file-name font)
351                                   #f)))
352                      ((ly:pango-font? font)
353                       (map (lambda (psname-filename-fontindex)
354                              (list #f
355                                    (list-ref psname-filename-fontindex 0)
356                                    (list-ref psname-filename-fontindex 1)
357                                    (list-ref psname-filename-fontindex 2)))
358                            (ly:pango-font-physical-fonts font)))
359                      (else
360                       (ly:font-sub-fonts font))))
361              fonts))
362            (font-names (uniq-list
363                         (sort (concatenate all-font-names)
364                               (lambda (x y) (string<? (cadr x) (cadr y))))))
365
366            ;; slightly spaghetti-ish: deciding what to load where
367            ;; is smeared out.
368            (font-loader
369             (lambda (name)
370               (cond ((ly:get-option 'gs-load-fonts)
371                      (load-font-via-GS name))
372                     ((ly:get-option 'gs-load-lily-fonts)
373                      (if (or (string-contains (caddr name)
374                                               (ly:get-option 'datadir))
375                              (internal-font? (caddr name)))
376                          (load-font-via-GS name)
377                          (load-font name)))
378                     (else
379                      (load-font name)))))
380            (pfas (map font-loader font-names)))
381       pfas))
382
383
384   (display "%%BeginProlog\n" port)
385   (format
386    port
387    "/lilypond-datadir where {pop} {userdict /lilypond-datadir (~a) put } ifelse"
388    (ly:get-option 'datadir))
389   (if load-fonts?
390       (for-each (lambda (f)
391                   (format port "\n%%BeginFont: ~a\n" (car f))
392                   (display (cdr f) port)
393                   (display "%%EndFont\n" port))
394                 (load-fonts paper)))
395   (display (setup-variables paper) port)
396
397   ;; adobe note 5002: should initialize variables before loading routines.
398   (display (procset "music-drawing-routines.ps") port)
399   (display (procset "lilyponddefs.ps") port)
400   (display "%%EndProlog\n" port)
401   (display "%%BeginSetup\ninit-lilypond-parameters\n%%EndSetup\n\n" port))
402
403 (define (ps-quote str)
404   (fold
405    (lambda (replacement-list result)
406      (string-join
407       (string-split
408        result
409        (car replacement-list))
410       (cadr replacement-list)))
411    str
412    '((#\\ "\\\\") (#\( "\\(") (#\) "\\)"))))
413
414 ;;; Create DOCINFO pdfmark containing metadata
415 ;;; header fields with pdf prefix override those without the prefix
416 (define (handle-metadata header port)
417   (define (metadata-encode val)
418     ;; First, call ly:encode-string-for-pdf to encode the string (latin1 or
419     ;; utf-16be), then escape all parentheses and backslashes
420     ;; FIXME guile-2.0: use (string->utf16 str 'big) instead
421
422     (ps-quote (ly:encode-string-for-pdf val)))
423   (define (metadata-lookup-output overridevar fallbackvar field)
424     (let* ((overrideval (ly:modules-lookup (list header) overridevar))
425            (fallbackval (ly:modules-lookup (list header) fallbackvar))
426            (val (if overrideval overrideval fallbackval)))
427       (if val
428           (format port "/~a (~a)\n" field (metadata-encode (markup->string val (list header)))))))
429   (display "[ " port)
430   (metadata-lookup-output 'pdfcomposer 'composer "Author")
431   (format port "/Creator (LilyPond ~a)\n" (lilypond-version))
432   (metadata-lookup-output 'pdftitle 'title "Title")
433   (metadata-lookup-output 'pdfsubject 'subject "Subject")
434   (metadata-lookup-output 'pdfkeywords 'keywords "Keywords")
435   (metadata-lookup-output 'pdfmodDate 'modDate "ModDate")
436   (metadata-lookup-output 'pdfsubtitle 'subtitle "Subtitle")
437   (metadata-lookup-output 'pdfcomposer 'composer "Composer")
438   (metadata-lookup-output 'pdfarranger 'arranger "Arranger")
439   (metadata-lookup-output 'pdfpoet 'poet "Poet")
440   (metadata-lookup-output 'pdfcopyright 'copyright "Copyright")
441   (display "/DOCINFO pdfmark\n\n" port))
442
443
444 (define-public (output-framework basename book scopes fields)
445   (let* ((filename (format #f "~a.ps" basename))
446          (outputter (ly:make-paper-outputter
447                      ;; FIXME: better wrap open/open-file,
448                      ;; content-mangling is always bad.
449                      ;; MINGW hack: need to have "b"inary for embedding CFFs
450                      (open-file filename "wb")
451                      'ps))
452          (paper (ly:paper-book-paper book))
453          (header (ly:paper-book-header book))
454          (systems (ly:paper-book-systems book))
455          (page-stencils (map page-stencil (ly:paper-book-pages book)))
456          (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t))
457          (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
458          (page-count (length page-stencils))
459          (port (ly:outputter-port outputter)))
460     (if (ly:get-option 'clip-systems)
461         (clip-system-EPSes basename book))
462     (if (ly:get-option 'dump-signatures)
463         (write-system-signatures basename (ly:paper-book-systems book) 1))
464     (output-scopes scopes fields basename)
465     (display (file-header paper page-count #t) port)
466     ;; don't do BeginDefaults PageMedia: A4
467     ;; not necessary and wrong
468     (write-preamble paper #t port)
469     (if (module? header)
470         (handle-metadata header port))
471     (for-each
472      (lambda (page)
473        (set! page-number (1+ page-number))
474        (dump-page outputter page page-number page-count landscape?))
475      page-stencils)
476     (display "%%Trailer\n%%EOF\n" port)
477     (ly:outputter-close outputter)
478     (postprocess-output book framework-ps-module filename
479                         (ly:output-formats))))
480
481 (define-public (dump-stencil-as-EPS paper dump-me filename
482                                     load-fonts)
483   (let* ((xext (ly:stencil-extent dump-me X))
484          (yext (ly:stencil-extent dump-me Y))
485          (padding (ly:get-option 'eps-box-padding))
486          (left-overshoot (if (number? padding)
487                              (* -1 padding (ly:output-def-lookup paper 'mm))
488                              #f))
489          (bbox
490           (map
491            (lambda (x)
492              (if (or (nan? x) (inf? x)
493                      ;; FIXME: huh?
494                      (equal? (format #f "~S" x) "+#.#")
495                      (equal? (format #f "~S" x) "-#.#"))
496                  0.0 x))
497
498            ;; the left-overshoot is to make sure that
499            ;; bar numbers stick out of margin uniformly.
500            ;;
501            (list
502             (if (number? left-overshoot)
503                 (min left-overshoot (car xext))
504                 (car xext))
505             (car yext) (cdr xext) (cdr yext)))))
506     (dump-stencil-as-EPS-with-bbox paper dump-me filename load-fonts bbox)))
507
508 (define-public (dump-stencil-as-EPS-with-bbox paper dump-me filename
509                                               load-fonts
510                                               bbox)
511   "Create an EPS file from stencil @var{dump-me} to @var{filename}.
512 @var{bbox} has format @code{(left-x, lower-y, right-x, upper-y)}.  If
513 @var{load-fonts} set, include fonts inline."
514   (define (to-rounded-bp-box box)
515     "Convert box to 1/72 inch with rounding to enlarge the box."
516     (let* ((scale (ly:output-def-lookup paper 'output-scale))
517            (strip-non-number (lambda (x)
518                                (if (or (nan? x)
519                                        (inf? x))
520                                    0.0
521                                    x)))
522            (directed-round (lambda (x rounder)
523                              (inexact->exact
524                               (rounder (/ (* (strip-non-number x) scale)
525                                           (ly:bp 1)))))))
526       (list (directed-round (car box) floor)
527             (directed-round (cadr box) floor)
528             (directed-round (max (1+ (car box)) (caddr box)) ceiling)
529             (directed-round (max (1+ (cadr box)) (cadddr box)) ceiling))))
530
531   (let* ((outputter (ly:make-paper-outputter
532                      ;; FIXME: better wrap open/open-file,
533                      ;; content-mangling is always bad.
534                      ;; MINGW hack: need to have "b"inary for embedding CFFs
535                      (open-file (format #f "~a.eps" filename) "wb")
536                      'ps))
537          (port (ly:outputter-port outputter))
538          (rounded-bbox (to-rounded-bp-box bbox))
539          (port (ly:outputter-port outputter))
540          (header (eps-header paper rounded-bbox load-fonts)))
541     (display header port)
542     (write-preamble paper load-fonts port)
543     (display "/mark_page_link { pop pop pop pop pop } bind def\n" port)
544     (display "gsave set-ps-scale-to-lily-scale\n" port)
545     (ly:outputter-dump-stencil outputter dump-me)
546     (display "stroke grestore\n%%Trailer\n%%EOF\n" port)
547     (ly:outputter-close outputter)))
548
549 (define (clip-systems-to-region basename paper systems region do-pdf do-png)
550   (let* ((extents-system-pairs
551           (filtered-map (lambda (paper-system)
552                           (let* ((x-ext (system-clipped-x-extent
553                                          (paper-system-system-grob paper-system)
554                                          region)))
555                             (if x-ext
556                                 (cons x-ext paper-system)
557                                 #f)))
558                         systems))
559          (count 0))
560     (for-each
561      (lambda (ext-system-pair)
562        (let* ((xext (car ext-system-pair))
563               (paper-system (cdr ext-system-pair))
564               (yext (paper-system-extent paper-system Y))
565               (bbox (list (car xext) (car yext)
566                           (cdr xext) (cdr yext)))
567               (filename (if (< 0 count)
568                             (format #f "~a-~a" basename count)
569                             basename)))
570          (set! count (1+ count))
571          (dump-stencil-as-EPS-with-bbox paper
572                                         (paper-system-stencil paper-system)
573                                         filename
574                                         (ly:get-option 'include-eps-fonts)
575                                         bbox)
576          (if do-pdf
577              (postscript->pdf 0 0 (format #f "~a.eps" filename)))
578          (if do-png
579              (postscript->png (ly:get-option 'resolution) 0 0
580                               (format #f "~a.eps" filename)))))
581      extents-system-pairs)))
582
583 (define-public (clip-system-EPSes basename paper-book)
584   (define do-pdf
585     (member "pdf" (ly:output-formats)))
586   (define do-png
587     (member "png" (ly:output-formats)))
588
589   (define (clip-score-systems basename systems)
590     (let* ((layout (ly:grob-layout (paper-system-system-grob (car systems))))
591            (regions (ly:output-def-lookup layout 'clip-regions)))
592       (for-each
593        (lambda (region)
594          (clip-systems-to-region
595           (format #f "~a-from-~a-to-~a-clip"
596                   basename
597                   (rhythmic-location->file-string (car region))
598                   (rhythmic-location->file-string (cdr region)))
599           layout systems region
600           do-pdf do-png))
601        regions)))
602
603   ;; partition in system lists sharing their layout blocks
604   (let* ((systems (ly:paper-book-systems paper-book))
605          (count 0)
606          (score-system-list '()))
607     (fold
608      (lambda (system last-system)
609        (if (not (and last-system
610                      (equal? (paper-system-layout last-system)
611                              (paper-system-layout system))))
612            (set! score-system-list (cons '() score-system-list)))
613        (if (paper-system-layout system)
614            (set-car! score-system-list (cons system (car score-system-list))))
615        ;; pass value.
616        system)
617      #f
618      systems)
619     (for-each (lambda (system-list)
620                 ;; filter out headers and top-level markup
621                 (if (pair? system-list)
622                     (clip-score-systems
623                      (if (> count 0)
624                          (format #f "~a-~a" basename count)
625                          basename)
626                      system-list)))
627               score-system-list)))
628
629 (define-public (output-preview-framework basename book scopes fields)
630   (let* ((paper (ly:paper-book-paper book))
631          (systems (relevant-book-systems book))
632          (to-dump-systems (relevant-dump-systems systems)))
633     (dump-stencil-as-EPS paper
634                          (stack-stencils Y DOWN 0.0
635                                          (map paper-system-stencil
636                                               (reverse to-dump-systems)))
637                          (format #f "~a.preview" basename)
638                          #t)
639     (postprocess-output book framework-ps-module
640                         (format #f "~a.preview.eps" basename)
641                         (cons "png" (ly:output-formats)))))
642
643 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
644
645 (define (output-width-height defs)
646   (let* ((landscape (ly:output-def-lookup defs 'landscape))
647          (output-scale (ly:output-def-lookup defs 'output-scale))
648          (convert (lambda (x)
649                     (* x output-scale (/ (ly:bp 1)))))
650          (paper-width (convert (ly:output-def-lookup defs 'paper-width)))
651          (paper-height (convert (ly:output-def-lookup defs 'paper-height)))
652          (w (if landscape paper-height paper-width))
653          (h (if landscape paper-width paper-height)))
654     (cons w h)))
655
656 (define (output-resolution defs)
657   (let ((defs-resolution (ly:output-def-lookup defs 'pngresolution)))
658     (if (number? defs-resolution)
659         defs-resolution
660         (ly:get-option 'resolution))))
661
662 (define (output-filename name)
663   (if (equal? (basename name ".ps") "-")
664       (string-append "./" name)
665       name))
666
667 (define-public (convert-to-pdf book name)
668   (let* ((defs (ly:paper-book-paper book))
669          (width-height (output-width-height defs))
670          (width (car width-height))
671          (height (cdr width-height))
672          (filename (output-filename name)))
673     (postscript->pdf width height filename)))
674
675 (define-public (convert-to-png book name)
676   (let* ((defs (ly:paper-book-paper book))
677          (resolution (output-resolution defs))
678          (width-height (output-width-height defs))
679          (width (car width-height))
680          (height (cdr width-height))
681          (filename (output-filename name)))
682     (postscript->png resolution width height filename)))
683
684 (define-public (convert-to-ps book name)
685   #t)
686
687 (define-public (output-classic-framework basename book scopes fields)
688   (ly:error (_ "\nThe PostScript backend does not support the
689 system-by-system output.  For that, use the EPS backend instead,
690
691   lilypond -dbackend=eps FILE
692
693 If have cut & pasted a lilypond fragment from a webpage, be sure
694 to only remove anything before
695
696   %% ****************************************************************
697   %% Start cut-&-pastable-section
698   %% ****************************************************************
699 ")))