main
Matthew Butterick 6 years ago
parent 5cfdbb7ce5
commit 2c9ac3b6dc

@ -24,13 +24,11 @@
(define mixed% (annotation-mixin (image-mixin (text-mixin (fonts-mixin (color-mixin (vector-mixin object%)))))))
(define pdf-version 1.3)
(define-subclass mixed% (PDFDocument [options (mhash)])
(compress-streams? (hash-ref options 'compress #t))
(current-doc-offset 0)
(field [byte-strings empty]
(field [doc-byte-strings empty]
[_pageBuffer null]
[_offsets (mhash)] ; The PDF object store
[_ended #f]
@ -73,7 +71,7 @@
(hash-set! info key val))
;; Write the header
(write this (format "%PDF-~a" pdf-version)) ; PDF version
(write this (format "%PDF-~a" (current-pdf-version))) ; PDF version
(write this (string-append "%" (list->string (map integer->char (make-list 4 #xFF))))) ; 4 binary chars, as recommended by the spec
;; Add the first page
@ -128,7 +126,7 @@
(define bstr (if (not (isBuffer? x))
(newBuffer (string-append x "\n"))
x))
(push-field! byte-strings this bstr)
(push-field! doc-byte-strings this bstr)
(current-doc-offset (+ (current-doc-offset) (buffer-length bstr))))
@ -212,7 +210,7 @@
;; here we'll do it manually
(define this-output-port (· this output-port))
(copy-port (open-input-bytes
(apply bytes-append (reverse (· this byte-strings)))) this-output-port)
(apply bytes-append (reverse (· this doc-byte-strings)))) this-output-port)
(close-output-port this-output-port))

@ -2,4 +2,6 @@
(provide (all-defined-out))
(define test-mode (make-parameter #f))
(define compress-streams? (make-parameter #f))
(define current-pdf-version (make-parameter 1.3))
(define current-doc-offset (make-parameter 'doc-offset-not-initialized))

@ -14,7 +14,7 @@
(provide PDFReference)
(define-subclass object% (PDFReference document id [payload (mhash)])
(field [byte-strings empty]
(field [ref-byte-strings empty]
[offset #f])
(as-methods
@ -25,14 +25,14 @@
(define/contract (write this x)
((or/c string? isBuffer? input-port?) . ->m . void?)
(push-field! byte-strings this
(push-field! ref-byte-strings this
(let loop ([x x])
(cond
[(isBuffer? x) x]
[(input-port? x) (loop (port->bytes x))]
[else (bytes-append (newBuffer x) #"\n")]))))
(define got-byte-strings? pair?)
(define got-ref-byte-strings? pair?)
(define/contract (end this [chunk #f])
(() ((or/c string? isBuffer? input-port?)) . ->*m . void?)
@ -40,16 +40,16 @@
#;(report* 'end! (· this id))
(define bstrs-to-write
(let ([current-bstrs (reverse (· this byte-strings))])
(let ([current-bstrs (reverse (· this ref-byte-strings))])
(if (and (compress-streams?)
(not (hash-ref (· this payload) 'Filter #f))
(got-byte-strings? current-bstrs))
(got-ref-byte-strings? current-bstrs))
(let ([deflated-chunk (deflate (apply bytes-append current-bstrs))])
(hash-set! (· this payload) 'Filter "FlateDecode")
(list deflated-chunk))
current-bstrs)))
(when (got-byte-strings? bstrs-to-write)
(when (got-ref-byte-strings? bstrs-to-write)
(hash-set! (· this payload) 'Length (apply + (map buffer-length bstrs-to-write))))
(define this-doc (· this document))
@ -58,7 +58,7 @@
(with-method ([doc_write (this-doc write)])
(doc_write (format "~a 0 obj" (· this id)))
(doc_write (convert (· this payload)))
(when (got-byte-strings? bstrs-to-write)
(when (got-ref-byte-strings? bstrs-to-write)
(doc_write "stream")
(for ([bstr (in-list bstrs-to-write)])
(doc_write bstr))

Loading…
Cancel
Save