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

66 lines
1.8 KiB
Racket

#lang pitfall/racket
(require "object.rkt" "zlib.rkt")
7 years ago
(provide PDFReference)
(define-subclass object% (PDFReference document id [data (mhash)])
(super-new)
(field [gen 0]
7 years ago
[compress (and (· document compress) (not (hash-ref data 'Filter #f)))]
[chunks empty]
[offset #f])
(as-methods
write
_write
end
toString))
7 years ago
7 years ago
(define/contract (write this data)
(any/c . ->m . void?)
(send this _write data #f void))
7 years ago
7 years ago
7 years ago
(define/contract (_write this chunk-in encoding callback)
7 years ago
((or/c string? isBuffer?) (or/c string? #f) procedure? . ->m . any/c)
7 years ago
(define chunk (if (isBuffer? chunk-in)
chunk-in
(newBuffer (string-append chunk-in "\n"))))
(push-end-field! chunks this chunk)
7 years ago
(hash-update! (· this data) 'Length (curry + (buffer-length chunk)) 0)
7 years ago
(callback))
7 years ago
7 years ago
7 years ago
(define/contract (end this)
(->m void?)
7 years ago
7 years ago
(define chunks-to-write
(let ([current-chunks (· this chunks)])
(if (and (· this compress) (pair? current-chunks))
(let ([deflated-chunk (deflate (apply bytes-append current-chunks))])
(hash-set*! (· this data)
'Filter "FlateDecode"
'Length (buffer-length deflated-chunk))
(list deflated-chunk))
current-chunks)))
7 years ago
(define this-doc (· this document))
7 years ago
(set-field! offset this (· this-doc _offset))
7 years ago
(with-method ([doc_write (this-doc _write)])
(doc_write (format "~a ~a obj" (· this id) (· this gen)))
(doc_write (convert (· this data)))
(when (pair? chunks-to-write)
(doc_write "stream")
(for ([chunk (in-list chunks-to-write)])
(doc_write chunk))
(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?)
(format "~a ~a R" (· this id) (· this gen)))