diff --git a/pitfall/pitfall/reference.rkt b/pitfall/pitfall/reference.rkt index 682ca200..552b54b3 100644 --- a/pitfall/pitfall/reference.rkt +++ b/pitfall/pitfall/reference.rkt @@ -2,10 +2,20 @@ (require "object.rkt" "zlib.rkt") (provide PDFReference) +(define-subclass object% (Chunks) + (super-new) + (define chunks null) + (define/public (append-chunk chunk) + (set! chunks (append chunks (list chunk)))) + ) + +(define-subclass Chunks (CompressedChunks) + (super-new)) + + (define-subclass object% (PDFReference document id [data (mhash)]) (super-new) (field [gen 0] - [i-wanna-be-deflated #f] [compress (and (· document compress) (not (hash-ref data 'Filter #f)))] [chunks empty] [offset #f]) @@ -28,7 +38,6 @@ chunk-in (newBuffer (string-append chunk-in "\n")))) (hash-ref! (· this data) 'Length 0) - (when (· this compress) (set-field! i-wanna-be-deflated this #t)) (push-end-field! chunks this chunk) (hash-update! (· this data) 'Length (curry + (buffer-length chunk))) (callback)) @@ -37,7 +46,7 @@ (define/contract (end this [chunk #f]) (() ((or/c any/c #f)) . ->*m . void?) - (when (· this i-wanna-be-deflated) + (when (and (· this compress) (positive? (length (· this chunks)))) (define deflated-chunk (deflate (apply bytes-append (· this chunks)))) (set-field! chunks this (list deflated-chunk)) (hash-set*! (· this data) @@ -56,7 +65,7 @@ (when (positive? (length this-chunks)) (send this-doc _write "stream") (for ([chunk (in-list this-chunks)]) - (send this-doc _write chunk)) + (send this-doc _write chunk)) (send this-doc _write "\nendstream"))) (send* this-doc