main
Matthew Butterick 6 years ago
parent 317d7bbc14
commit 6750376291

@ -6,6 +6,7 @@
racket/format racket/format
racket/contract racket/contract
racket/generator racket/generator
racket/match
racket/list racket/list
sugar/unstable/class sugar/unstable/class
sugar/unstable/js sugar/unstable/js
@ -29,7 +30,7 @@
(current-auto-first-page (hash-ref options 'autoFirstPage #t)) (current-auto-first-page (hash-ref options 'autoFirstPage #t))
(current-doc-offset 0) (current-doc-offset 0)
(field [doc-byte-strings (open-output-bytes)] (field [doc-portal (open-output-bytes)]
[_pageBuffer null] [_pageBuffer null]
[_offsets (mhasheqv)] ; The PDF object store [_offsets (mhasheqv)] ; The PDF object store
[_ended #f] [_ended #f]
@ -69,7 +70,7 @@
(define bstr (if (not (bytes? x)) (define bstr (if (not (bytes? x))
(string->bytes/latin-1 (string-append x "\n")) (string->bytes/latin-1 (string-append x "\n"))
x)) x))
(write-bytes bstr doc-byte-strings) (write-bytes bstr doc-portal)
(current-doc-offset (+ (current-doc-offset) (bytes-length bstr)))) (current-doc-offset (+ (current-doc-offset) (bytes-length bstr))))
(define/public (addPage [options-arg options]) (define/public (addPage [options-arg options])
@ -79,7 +80,7 @@
;; create a page object ;; create a page object
(set! page (make-object PDFPage this options-arg)) (set! page (make-object PDFPage this options-arg))
(push-field! _pageBuffer this page) (set! _pageBuffer (cons page _pageBuffer))
;; in Kids, store page dictionaries in correct order ;; in Kids, store page dictionaries in correct order
;; this determines order in document ;; this determines order in document
@ -104,8 +105,8 @@
(send page write data) (send page write data)
this) this)
(define/public (_refEnd ref) (define/public (_refEnd aref)
(hash-set! _offsets (· ref id) (· ref offset))) (hash-set! _offsets (· aref id) (· aref offset)))
(define/public (pipe port) (define/public (pipe port)
(set! output-port port)) (set! output-port port))
@ -125,11 +126,10 @@
(· _root end) (· _root end)
(· _root payload Pages end) (· _root payload Pages end)
;; generate xref
(define xref-offset (current-doc-offset)) (define xref-offset (current-doc-offset))
(define sorted-offset-records (sort (hash->list _offsets) < #:key car)) ; sort by refid (match-define (list this-idxs this-offsets)
(define this-offsets (map cdr sorted-offset-records)) (match (sort (hash->list _offsets) < #:key car) ; sort by refid
(define this-idxs (map car sorted-offset-records)) [(list (cons idxs offsets) ...) (list idxs offsets)]))
(write "xref") (write "xref")
(write (format "0 ~a" (add1 (length this-offsets)))) (write (format "0 ~a" (add1 (length this-offsets))))
(write "0000000000 65535 f ") (write "0000000000 65535 f ")
@ -156,7 +156,7 @@
;; in node you (@push null) which signals to the stream ;; in node you (@push null) which signals to the stream
;; to copy to its output port ;; to copy to its output port
;; here we'll do it manually ;; here we'll do it manually
(write-bytes (get-output-bytes doc-byte-strings) output-port) (write-bytes (get-output-bytes doc-portal) output-port)
(close-output-port output-port)) (close-output-port output-port))
; if no 'info key, nothing will be copied from (hash) ; if no 'info key, nothing will be copied from (hash)

Loading…
Cancel
Save