main
Matthew Butterick 6 years ago
parent 1d9f979e91
commit 34df875d63

@ -24,7 +24,7 @@
(hash-set! options (string->symbol (string-titlecase (symbol->string k))) v)) (hash-set! options (string->symbol (string-titlecase (symbol->string k))) v))
(define annots-ref (make-ref options)) (define annots-ref (make-ref options))
(page-annotations (page doc) annots-ref) (page-annotations (current-page doc) annots-ref)
(ref-end annots-ref) (ref-end annots-ref)
doc) doc)

@ -26,7 +26,7 @@
(ref-end ref-dict) (ref-end ref-dict)
(define opacity-index (add1 (length (hash-keys ($doc-opacity-registry doc))))) (define opacity-index (add1 (length (hash-keys ($doc-opacity-registry doc)))))
(list ref-dict (string->symbol (format "Gs~a" opacity-index)))))) (list ref-dict (string->symbol (format "Gs~a" opacity-index))))))
(hash-set! (page-ext_gstates (page doc)) name dictionary) (hash-set! (page-ext_gstates (current-page doc)) name dictionary)
(add-content doc (format "/~a gs" name)))) (add-content doc (format "/~a gs" name))))
(define (fill-color doc color [opacity 1]) (define (fill-color doc color [opacity 1])

@ -75,12 +75,12 @@
(set-$doc-pages! doc (cons (make-page page-parent options-arg) ($doc-pages doc))) (set-$doc-pages! doc (cons (make-page page-parent options-arg) ($doc-pages doc)))
;; reset x and y coordinates ;; reset x and y coordinates
(set-$doc-x! doc (margin-left ($page-margins (page doc)))) (set-$doc-x! doc (margin-left ($page-margins (current-page doc))))
(set-$doc-y! doc (margin-right ($page-margins (page doc)))) (set-$doc-y! doc (margin-right ($page-margins (current-page doc))))
;; flip PDF coordinate system so that the origin is in ;; flip PDF coordinate system so that the origin is in
;; the top left rather than the bottom left ;; the top left rather than the bottom left
(set-$doc-ctm! doc default-ctm-value) (set-$doc-ctm! doc default-ctm-value)
(transform doc 1 0 0 -1 0 ($page-height (page doc))) (transform doc 1 0 0 -1 0 ($page-height (current-page doc)))
doc) doc)
(define (start-doc doc) (define (start-doc doc)

@ -31,7 +31,7 @@
[else (open-image doc src)])) [else (open-image doc src)]))
(unless ($img-ref image) (($img-embed-proc image) image)) (unless ($img-ref image) (($img-embed-proc image) image))
(hash-ref! (page-xobjects (page doc)) ($img-label image) ($img-ref image)) (hash-ref! (page-xobjects (current-page doc)) ($img-label image) ($img-ref image))
(define image-width ($img-width image)) (define image-width ($img-width image))
(define image-height ($img-height image)) (define image-height ($img-height image))

@ -1,44 +1,41 @@
#lang debug racket/base #lang debug racket/base
(require (require
racket/dict racket/dict
racket/match
"reference.rkt" "reference.rkt"
sugar/unstable/dict sugar/unstable/dict
"core.rkt") "core.rkt")
(provide (all-defined-out)) (provide (all-defined-out))
(define (page doc) (car ($doc-pages doc))) (define (current-page doc) (car ($doc-pages doc)))
(define (add-content doc data) (define (add-content doc data)
(page-write (page doc) data)) (page-write (current-page doc) data))
(struct $page (page-parent options size layout dimensions width height content resources margins dictionary) (struct $page (page-parent options size layout dimensions width height content resources margins dictionary)
#:transparent #:mutable) #:transparent #:mutable)
(define (make-page [page-parent #false] [options (mhash)]) (define (make-page [page-parent #false] [options (mhasheq)])
[define size (hash-ref options 'size "letter")] (define size (hash-ref options 'size "letter"))
[define layout (hash-ref options 'layout "portrait")] (define layout (hash-ref options 'layout "portrait"))
[define dimensions (if (list? size) (define dimensions (if (list? size)
size size
(hash-ref page-sizes (string-upcase size)))] (hash-ref page-sizes (string-upcase size))))
[define width (list-ref dimensions (if (equal? layout "portrait") 0 1))] (match-define (list width height) ((if (equal? layout "portrait") values reverse) dimensions))
[define height (list-ref dimensions (if (equal? layout "portrait") 1 0))] (define content (make-ref))
[define content (make-ref)] (define resources (make-ref (mhash 'ProcSet '(PDF Text ImageB ImageC ImageI))))
[define resources (make-ref (mhash 'ProcSet '(PDF Text ImageB ImageC ImageI)))] (define margins
[define margins (match (hash-ref options 'margin #f)
(let ([margin-value (hash-ref options 'margin #f)]) [(? number? margin-value) (margin margin-value margin-value margin-value margin-value)]
(if (number? margin-value) [_ (hash-ref options 'margins (current-default-margins))]))
(margin margin-value margin-value margin-value margin-value) (define page-dictionary
(hash-ref options 'margins (current-default-margins))))] (make-ref (mhasheq 'Type 'Page
;; The page dictionary 'Parent page-parent
[define dictionary 'MediaBox (list 0 0 width height)
(make-ref 'Contents content
(mhash 'Type 'Page 'Resources resources)))
'Parent page-parent ($page page-parent options size layout dimensions width height content resources margins page-dictionary))
'MediaBox (list 0 0 width height)
'Contents content
'Resources resources))]
($page page-parent options size layout dimensions width height content resources margins dictionary))
(define (page-fonts p) (define (page-fonts p)
(dict-ref! ($page-resources p) 'Font (make-hasheq))) (dict-ref! ($page-resources p) 'Font (make-hasheq)))

@ -59,7 +59,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
;; flip coordinate system ;; flip coordinate system
(save doc) (save doc)
(define page-height ($page-height (page doc))) (define page-height ($page-height (current-page doc)))
(transform doc 1 0 0 -1 0 page-height) (transform doc 1 0 0 -1 0 page-height)
(define y (- page-height (define y (- page-height
y-in y-in
@ -68,7 +68,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
;; add current font to page if necessary ;; add current font to page if necessary
(define current-font-id (get-field id ($doc-current-font doc))) (define current-font-id (get-field id ($doc-current-font doc)))
(hash-ref! (page-fonts (page doc)) current-font-id (λ () (send ($doc-current-font doc) make-font-ref))) (hash-ref! (page-fonts (current-page doc)) current-font-id (λ () (send ($doc-current-font doc) make-font-ref)))
(add-content doc "BT") ; begin the text object (add-content doc "BT") ; begin the text object
(add-content doc (format "1 0 0 1 ~a ~a Tm" (numberizer x) (numberizer y))) ; text position (add-content doc (format "1 0 0 1 ~a ~a Tm" (numberizer x) (numberizer y))) ; text position

Loading…
Cancel
Save