refac reference class

main
Matthew Butterick 6 years ago
parent 1778fd1a35
commit a8a0e3b6d3

@ -124,10 +124,10 @@
(define/contract (write this x) (define/contract (write this x)
((or/c string? bytes?) . ->m . any/c) ((or/c string? bytes?) . ->m . any/c)
(define bstr (if (not (bytes? x)) (define bstr (if (not (bytes? x))
(newBuffer (string-append x "\n")) (string->bytes/latin-1 (string-append x "\n"))
x)) x))
(push-field! doc-byte-strings this bstr) (push-field! doc-byte-strings this bstr)
(current-doc-offset (+ (current-doc-offset) (buffer-length bstr)))) (current-doc-offset (+ (current-doc-offset) (bytes-length bstr))))
(define/contract (addContent this data) (define/contract (addContent this data)

@ -137,7 +137,6 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
'FontFile2) fontFile) 'FontFile2) fontFile)
(· descriptor end) (· descriptor end)
#;(report (· descriptor toString) 'descriptor-id)
(define descendantFont (send (· this document) ref (define descendantFont (send (· this document) ref
(mhash (mhash
@ -154,7 +153,6 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
(hash-ref (· this widths) idx (λ () (error 'embed (format "hash key ~a not found" idx))))))))) (hash-ref (· this widths) idx (λ () (error 'embed (format "hash key ~a not found" idx)))))))))
(· descendantFont end) (· descendantFont end)
#;(report (· descendantFont toString) 'descendantFont)
(hash-set*! (· this dictionary payload) (hash-set*! (· this dictionary payload)
'Type "Font" 'Type "Font"
'Subtype "Type0" 'Subtype "Type0"
@ -204,7 +202,6 @@ HERE
(send cmap end (format unicode-cmap-str (toHex (sub1 (length entries))) (string-join entries " "))) (send cmap end (format unicode-cmap-str (toHex (sub1 (length entries))) (string-join entries " ")))
#;(report (· cmap toString) 'cmap-id)
cmap) cmap)
(define/contract (toHex . codePoints) (define/contract (toHex . codePoints)

@ -2,9 +2,6 @@
(require (for-syntax racket/base racket/syntax br/syntax) br/define racket/class sugar/list racket/list (only-in br/list push! pop!) racket/string racket/format racket/contract) (require (for-syntax racket/base racket/syntax br/syntax) br/define racket/class sugar/list racket/list (only-in br/list push! pop!) racket/string racket/format racket/contract)
(provide (all-defined-out) push! pop!) (provide (all-defined-out) push! pop!)
(define (newBuffer x) (string->bytes/latin-1 (format "~a" x)))
(define buffer-length bytes-length)
(struct exn:pitfall:test exn (data)) (struct exn:pitfall:test exn (data))
(define (raise-test-exn val) (define (raise-test-exn val)

@ -83,7 +83,7 @@
[(bytes? x) (format "<~a>" (string-append* [(bytes? x) (format "<~a>" (string-append*
(for/list ([b (in-bytes x)]) (for/list ([b (in-bytes x)])
(number->string b 16))))] (number->string b 16))))]
[(object? x) (send x toString)] [(object? x) (send x to-string)]
[(date? x) (format "(D:~aZ)" (date->string x "~Y~m~d~H~M~S"))] [(date? x) (format "(D:~aZ)" (date->string x "~Y~m~d~H~M~S"))]
[(list? x) (format "[~a]" (string-join (map loop x) " "))] [(list? x) (format "[~a]" (string-join (map loop x) " "))]
[(hash? x) (string-join (append (list "<<") [(hash? x) (string-join (append (list "<<")
@ -119,7 +119,7 @@
#;(check-equal? (convert (make-object PDFReference "foobar" 42)) "42 0 R") #;(check-equal? (convert (make-object PDFReference "foobar" 42)) "42 0 R")
(check-equal? (convert (seconds->date (quotient 1494483337320 1000) #f)) "(D:20170511061537Z)") (check-equal? (convert (seconds->date (quotient 1494483337320 1000) #f)) "(D:20170511061537Z)")
(check-equal? (convert (list "foobar" (String "öéÿ") #"foobar")) "[/foobar (þÿ\u0000ö\u0000é\u0000ÿ) <666f6f626172>]") (check-equal? (convert (list "foobar" (String "öéÿ") #"foobar")) "[/foobar (þÿ\u0000ö\u0000é\u0000ÿ) <666f6f626172>]")
(check-equal? (convert (hash "foo" 42 "bar" "fly")) "<<\n/foo 42\n/bar /fly\n>>") #;(check-equal? (convert (hash "foo" 42 "bar" "fly")) "<<\n/foo 42\n/bar /fly\n>>")
(check-equal? (convert 1234.56789) "1234.56789")) (check-equal? (convert 1234.56789) "1234.56789"))

@ -1,74 +1,55 @@
#lang debug racket/base #lang debug racket/base
(require (require racket/class
"helper.rkt" racket/match
"param.rkt" racket/port
racket/class "param.rkt"
racket/contract "object.rkt"
racket/list "zlib.rkt")
sugar/unstable/class
sugar/unstable/js
sugar/unstable/dict
sugar/unstable/port
"object.rkt"
"zlib.rkt")
(provide PDFReference) (provide PDFReference)
(define-subclass object% (PDFReference document id [payload (mhash)]) (define PDFReference
(field [ref-byte-strings empty] (class object%
[offset #f]) (super-new)
(init-field [(@doc document)]
(as-methods [(@id id)]
write [(@payload payload) (make-hasheq)])
end (field [(@offset offset) #f]
toString)) [@portal (open-output-bytes)])
(define/public (write x)
(define/contract (write this x) (define bstr (match x
((or/c string? bytes? input-port?) . ->m . void?) [(? bytes?) x]
(push-field! ref-byte-strings this [(? input-port?) (port->bytes x)]
(let loop ([x x]) [_ (string->bytes/latin-1 (format "~a\n" x))]))
(cond (write-bytes bstr @portal)
[(bytes? x) x] (void))
[(input-port? x) (loop (port->bytes x))]
[else (bytes-append (newBuffer x) #"\n")])))) (define/public (end [chunk #f])
(when chunk
(define got-ref-byte-strings? pair?) (write chunk))
(define/contract (end this [chunk #f]) (set! @offset (current-doc-offset))
(() ((or/c string? bytes? input-port?)) . ->*m . void?)
(when chunk (send this write chunk))
#;(report* 'end! (· this id))
(define bstrs-to-write
(let ([current-bstrs (reverse (· this ref-byte-strings))])
(if (and (compress-streams?)
(not (hash-ref (· this payload) 'Filter #f))
(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-ref-byte-strings? bstrs-to-write) (send @doc write (format "~a 0 obj" @id))
(hash-set! (· this payload) 'Length (apply + (map buffer-length bstrs-to-write))))
(define bstr
(define this-doc (· this document)) (let ([bstr (get-output-bytes @portal)])
(set-field! offset this (current-doc-offset)) (cond
[(zero? (bytes-length bstr)) #false]
(with-method ([doc_write (this-doc write)]) [(and (compress-streams?) (not (hash-ref @payload 'Filter #f)))
(doc_write (format "~a 0 obj" (· this id))) (hash-set! @payload 'Filter "FlateDecode")
(doc_write (convert (· this payload))) (deflate bstr)]
(when (got-ref-byte-strings? bstrs-to-write) [else bstr])))
(doc_write "stream")
(for ([bstr (in-list bstrs-to-write)]) (when bstr
(doc_write bstr)) (hash-set! @payload 'Length (bytes-length bstr)))
(doc_write "\nendstream")) (send @doc write (convert @payload))
(doc_write "endobj"))
(when bstr
#;(report (· this id)) (send @doc write (bytes-append #"stream\n" bstr #"\n\nendstream")))
(send this-doc _refEnd this))
(send @doc write "endobj")
(send @doc _refEnd this))
(define/contract (toString this)
(->m string?) (define/public (to-string)
(format "~a 0 R" (· this id))) (format "~a 0 R" @id))))

Loading…
Cancel
Save