You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/pitfall/pitfall/reference.rkt

59 lines
1.7 KiB
Racket

#lang pitfall/racket
(require "object.rkt" "zlib.rkt")
7 years ago
(provide PDFReference)
7 years ago
(define-subclass object% (PDFReference document id [payload (mhash)])
(super-new)
7 years ago
(field [byte-strings empty]
[offset #f])
(as-methods
write
end
toString))
7 years ago
7 years ago
(define/contract (write this x)
((or/c string? isBuffer?) . ->m . void?)
7 years ago
(push-field! byte-strings this (if (isBuffer? x)
x
(bytes-append (newBuffer x) #"\n"))))
7 years ago
7 years ago
(define got-byte-strings? pair?)
7 years ago
7 years ago
(define/contract (end this)
(->m void?)
7 years ago
7 years ago
(define bstrs-to-write
(let ([current-bstrs (reverse (· this byte-strings))])
7 years ago
(if (and (compress-streams?)
7 years ago
(not (hash-ref (· this payload) 'Filter #f))
7 years ago
(got-byte-strings? current-bstrs))
(let ([deflated-chunk (deflate (apply bytes-append current-bstrs))])
7 years ago
(hash-set! (· this payload) 'Filter "FlateDecode")
7 years ago
(list deflated-chunk))
7 years ago
current-bstrs)))
7 years ago
7 years ago
(when (got-byte-strings? bstrs-to-write)
(hash-set! (· this payload) 'Length (apply + (map buffer-length bstrs-to-write))))
7 years ago
(define this-doc (· this document))
7 years ago
(set-field! offset this (· this-doc _offset))
7 years ago
7 years ago
(with-method ([doc_write (this-doc write)])
7 years ago
(doc_write (format "~a 0 obj" (· this id)))
7 years ago
(doc_write (convert (· this payload)))
7 years ago
(when (got-byte-strings? bstrs-to-write)
7 years ago
(doc_write "stream")
7 years ago
(for ([bstr (in-list bstrs-to-write)])
(doc_write bstr))
7 years ago
(doc_write "\nendstream"))
(doc_write "endobj"))
(send this-doc _refEnd this))
7 years ago
7 years ago
7 years ago
(define/contract (toString this)
(->m string?)
7 years ago
(format "~a 0 R" (· this id)))