have the pict datatype support conversion to PNG, EPS, or PDF bytes

This commit is contained in:
Matthew Flatt 2010-11-26 11:50:07 -07:00
parent 7cb15899ae
commit 1b56d84155
6 changed files with 41 additions and 3 deletions

View File

@ -23,6 +23,7 @@ should be considered standard:
@item{@scheme['gif-bytes] --- a byte string containing a GIF image encoding}
@item{@scheme['png-bytes] --- a byte string containing a PNG image encoding}
@item{@scheme['ps-bytes] --- a byte string containing a PostScript document}
@item{@scheme['eps-bytes] --- a byte string containing an Encapsulated PostScript document}
@item{@scheme['pdf-bytes] --- a byte string containing a PDF document}
]

View File

@ -65,6 +65,10 @@ picts. The functions @racket[pict-width], @racket[pict-height],
@racket[pict-descent], and @racket[pict-ascent] extract bounding-box
information from a pict.
A pict is a convertible datatype through the @racket[file/convertible]
protocol. Supported conversions include @racket['png-bytes],
@racket['eps-bytes], and @racket['pdf-bytes].
@defstruct[pict ([draw any/c]
[width real?]

View File

@ -123,7 +123,8 @@
(provide texpict-common-setup^)
(define-signature texpict-common-setup^
(connect
~connect))
~connect
convert-pict))
(provide texpict-internal^)
(define-signature texpict-internal^

View File

@ -2,7 +2,8 @@
(require racket/draw
racket/class
racket/list)
racket/list
file/convertible)
(require "common-sig.ss")
@ -20,7 +21,9 @@
children ; list of child records
panbox ; panorama box, computed on demand
last) ; a descendent for the bottom-right
#:mutable)
#:mutable
#:property prop:convertible (lambda (v mode default)
(convert-pict v mode default)))
(define-struct child (pict dx dy sx sy))
(define-struct bbox (x1 y1 x2 y2 ay dy))

View File

@ -442,3 +442,30 @@
(define (draw-pict p dc dx dy)
((make-pict-drawer p) dc dx dy))
(define (convert-pict p format default)
(case format
[(png-bytes)
(let* ([bm (make-bitmap (max 1 (pict-width p)) (max 1 (pict-height p)))]
[dc (make-object bitmap-dc% bm)])
(draw-pict p dc 0 0)
(send dc set-bitmap #f)
(let ([s (open-output-bytes)])
(send bm save-file s 'png)
(get-output-bytes s)))]
[(eps-bytes pdf-bytes)
(let ([s (open-output-bytes)])
(let ([dc (new (if (eq? format 'eps-bytes) post-script-dc% pdf-dc%)
[interactive #f]
[as-eps #t]
[output s])])
(send dc start-doc "pict")
(send dc start-page)
(draw-pict p dc 0 0)
(send dc end-page)
(send dc end-doc))
(get-output-bytes s))]
[else default]))

View File

@ -466,3 +466,5 @@
[else (error 'pict->string "bad tag: ~s" tag)])))))
(define pict->commands pict->command-list)
(define (convert-pict p v d) d)