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