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

75 lines
2.1 KiB
Racket

#lang racket/base
(require
"helper.rkt"
"param.rkt"
racket/class
racket/contract
racket/list
sugar/unstable/class
sugar/unstable/js
sugar/unstable/dict
sugar/unstable/port
"object.rkt"
"zlib.rkt")
7 years ago
(provide PDFReference)
7 years ago
(define-subclass object% (PDFReference document id [payload (mhash)])
6 years ago
(field [ref-byte-strings empty]
[offset #f])
(as-methods
write
end
toString))
7 years ago
7 years ago
(define/contract (write this x)
6 years ago
((or/c string? bytes? input-port?) . ->m . void?)
6 years ago
(push-field! ref-byte-strings this
(let loop ([x x])
(cond
6 years ago
[(bytes? x) x]
[(input-port? x) (loop (port->bytes x))]
[else (bytes-append (newBuffer x) #"\n")]))))
7 years ago
6 years ago
(define got-ref-byte-strings? pair?)
7 years ago
7 years ago
(define/contract (end this [chunk #f])
6 years ago
(() ((or/c string? bytes? input-port?)) . ->*m . void?)
(when chunk (send this write chunk))
7 years ago
#;(report* 'end! (· this id))
7 years ago
(define bstrs-to-write
6 years ago
(let ([current-bstrs (reverse (· this ref-byte-strings))])
(if (and (compress-streams?)
(not (hash-ref (· this payload) 'Filter #f))
6 years ago
(got-ref-byte-strings? current-bstrs))
7 years ago
(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
6 years ago
(when (got-ref-byte-strings? bstrs-to-write)
7 years ago
(hash-set! (· this payload) 'Length (apply + (map buffer-length bstrs-to-write))))
7 years ago
(define this-doc (· this document))
6 years ago
(set-field! offset this (current-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)))
6 years ago
(when (got-ref-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"))
7 years ago
#;(report (· this id))
7 years ago
(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)))