From 1c30d5aee359d569b45dca8ba63951bbc4ca6cb0 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 22 Dec 2018 23:08:14 -0800 Subject: [PATCH] stop dragging my doc around --- pitfall/pitfall/core.rkt | 2 -- pitfall/pitfall/document.rkt | 6 +++--- pitfall/pitfall/embedded.rkt | 7 ++----- pitfall/pitfall/font-open.rkt | 8 +++----- pitfall/pitfall/font.rkt | 3 +-- pitfall/pitfall/fonts.rkt | 2 +- pitfall/pitfall/images.rkt | 2 +- pitfall/pitfall/jpeg-structy.rkt | 2 +- pitfall/pitfall/jpeg.rkt | 4 ++-- pitfall/pitfall/page-test.rkt | 2 +- pitfall/pitfall/page.rkt | 2 +- pitfall/pitfall/png.rkt | 6 ++---- pitfall/pitfall/reference.rkt | 10 +++++++--- pitfall/pitfall/standard-font.rkt | 9 +++------ 14 files changed, 28 insertions(+), 37 deletions(-) diff --git a/pitfall/pitfall/core.rkt b/pitfall/pitfall/core.rkt index 64aeeefb..6860abab 100644 --- a/pitfall/pitfall/core.rkt +++ b/pitfall/pitfall/core.rkt @@ -26,8 +26,6 @@ (define current-font (make-parameter #f)) (define current-font-size (make-parameter 12)) -(define current-ref-listeners (make-parameter null)) - ;; helpers (define (numberizer x #:round [round? #true]) diff --git a/pitfall/pitfall/document.rkt b/pitfall/pitfall/document.rkt index 2f54518f..cd4ecfd4 100644 --- a/pitfall/pitfall/document.rkt +++ b/pitfall/pitfall/document.rkt @@ -20,8 +20,8 @@ (define PDFDocument (class (annotation-mixin (image-mixin (text-mixin (fonts-mixin (vector-mixin (color-mixin object%)))))) - (set-current-id! 1) - (current-ref-listeners (cons (λ (ref) (send this log-ref ref)) (current-ref-listeners))) + (set-current-ref-id! 1) + (register-ref-listener (λ (ref) (send this log-ref ref))) (super-new) (init-field [(@options options) (mhasheq)]) @@ -60,7 +60,7 @@ (define/public (add-page [options-arg @options]) ;; create a page object (define page-parent (dict-ref @root 'Pages)) - (set! @pages (cons (make-object PDFPage this page-parent options-arg) @pages)) + (set! @pages (cons (make-object PDFPage page-parent options-arg) @pages)) ;; reset x and y coordinates (set! @x (margin-left (get-field margins (page)))) diff --git a/pitfall/pitfall/embedded.rkt b/pitfall/pitfall/embedded.rkt index eb50c2df..d495c19f 100644 --- a/pitfall/pitfall/embedded.rkt +++ b/pitfall/pitfall/embedded.rkt @@ -34,7 +34,6 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee (define EmbeddedFont (class PDFFont - (init document) (init-field font id) (field [subset (create-subset font)] ;; we make `unicode` and `width` fields integer-keyed hashes not lists @@ -44,16 +43,14 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee ;; always include the width of the missing glyph (gid = 0) [name (font-postscript-name font)] [scale (/ 1000 (font-units-per-em font))]) - (super-new [document document] - [ascender (* (font-ascent font) scale)] + (super-new [ascender (* (font-ascent font) scale)] [descender (* (font-descent font) scale)] [bbox (font-bbox font)] [line-gap (* (font-linegap font) scale)]) (inherit-field [@ascender ascender] [@descender descender] - [@dictionary dictionary] - [@document document]) + [@dictionary dictionary]) (define/override (string-width string size [features #f]) ; #f disables features ; null enables default features ; list adds features diff --git a/pitfall/pitfall/font-open.rkt b/pitfall/pitfall/font-open.rkt index 4697c408..f25c1483 100644 --- a/pitfall/pitfall/font-open.rkt +++ b/pitfall/pitfall/font-open.rkt @@ -1,18 +1,16 @@ #lang racket/base (require racket/class - racket/contract "standard-font.rkt" "font.rkt" fontland "embedded.rkt") (provide PDFFont-open) -(define/contract (PDFFont-open document src family id) - (object? any/c any/c any/c . -> . (is-a?/c PDFFont)) +(define (PDFFont-open src family id) (cond [(and (string? src) (isStandardFont src)) - (make-object StandardFont document src id)] + (make-object StandardFont src id)] [else (define font (cond @@ -20,4 +18,4 @@ [(path? src) (open-font (path->string src))] ;; todo: other font-loading cases [else (raise-argument-error 'PDFFont-open "loadable font thingy" src)])) - (make-object EmbeddedFont document font id)])) + (make-object EmbeddedFont font id)])) diff --git a/pitfall/pitfall/font.rkt b/pitfall/pitfall/font.rkt index 62ed186f..992a44f9 100644 --- a/pitfall/pitfall/font.rkt +++ b/pitfall/pitfall/font.rkt @@ -5,8 +5,7 @@ (define PDFFont (class object% (super-new) - (init-field [(@document document) #f] - [(@ascender ascender) #f] + (init-field [(@ascender ascender) #f] [(@descender descender) #f] [(@line-gap line-gap) #f] [(@bbox bbox) #f]) diff --git a/pitfall/pitfall/fonts.rkt b/pitfall/pitfall/fonts.rkt index 4537a128..3b24ef97 100644 --- a/pitfall/pitfall/fonts.rkt +++ b/pitfall/pitfall/fonts.rkt @@ -39,7 +39,7 @@ [_ ; if not, load the font (set! @font-count (add1 @font-count)) (define id (format "F~a" @font-count)) - (set! @current-font (PDFFont-open this src family id)) + (set! @current-font (PDFFont-open src family id)) ;; check for existing font families with the same name already in the PDF (match (hash-ref @font-families (get-field name @current-font) #f) [(? values font) (set! @current-font font)] diff --git a/pitfall/pitfall/images.rkt b/pitfall/pitfall/images.rkt index 695ab807..19eb2b35 100644 --- a/pitfall/pitfall/images.rkt +++ b/pitfall/pitfall/images.rkt @@ -38,7 +38,7 @@ [(and (object? src) (· src width) (· src height)) src] [else (send this openImage src)])) - (unless (· image obj) (send image embed this)) + (unless (· image obj) (send image embed)) (hash-ref! (· this page xobjects) (· image label) (· image obj)) diff --git a/pitfall/pitfall/jpeg-structy.rkt b/pitfall/pitfall/jpeg-structy.rkt index 7584577f..dd6e21af 100644 --- a/pitfall/pitfall/jpeg-structy.rkt +++ b/pitfall/pitfall/jpeg-structy.rkt @@ -43,7 +43,7 @@ (define obj #f) (JPEG label width height obj data bits channels colorSpace))) -(define (embed this doc-in) +(define (embed this) #;(object? . ->m . void?) (unless (· this obj) diff --git a/pitfall/pitfall/jpeg.rkt b/pitfall/pitfall/jpeg.rkt index c61a2eb3..dbe328cf 100644 --- a/pitfall/pitfall/jpeg.rkt +++ b/pitfall/pitfall/jpeg.rkt @@ -51,8 +51,8 @@ [(port? bytes-or-port) bytes-or-port] [else (current-input-port)])) signed big-endian)) -(define/contract (embed this doc-in) - (object? . ->m . void?) +(define/contract (embed this) + (->m void?) (unless (· this obj) (set-field! obj this diff --git a/pitfall/pitfall/page-test.rkt b/pitfall/pitfall/page-test.rkt index d4390a0c..48e48bf2 100644 --- a/pitfall/pitfall/page-test.rkt +++ b/pitfall/pitfall/page-test.rkt @@ -6,7 +6,7 @@ "reference.rkt" "core.rkt" sugar/unstable/js) -(define p (make-object PDFPage (make-object PDFDocument))) +(define p (make-object PDFPage)) (check-equal? (· p size) "letter") (check-equal? (· p layout) "portrait") (check-equal? (· p margins) (margin 72 72 72 72)) diff --git a/pitfall/pitfall/page.rkt b/pitfall/pitfall/page.rkt index 0faeda37..7d84bdad 100644 --- a/pitfall/pitfall/page.rkt +++ b/pitfall/pitfall/page.rkt @@ -11,7 +11,7 @@ (define PDFPage (class object% (super-new) - (init-field @doc [@page-parent #false] [@options (mhash)]) + (init-field [@page-parent #false] [@options (mhash)]) (field [(@size size) (hash-ref @options 'size "letter")] [(@layout layout) (hash-ref @options 'layout "portrait")] [@dimensions (if (list? @size) diff --git a/pitfall/pitfall/png.rkt b/pitfall/pitfall/png.rkt index a64de528..26b36d18 100644 --- a/pitfall/pitfall/png.rkt +++ b/pitfall/pitfall/png.rkt @@ -20,7 +20,6 @@ [width (· image width)] [height (· image height)] [imgData (· image imgData)] - [document #f] [alphaChannel #f] [obj #f]) @@ -28,10 +27,9 @@ embed split-alpha-channel)) -(define/contract (embed this doc-in) - (object? . ->m . void?) +(define/contract (embed this) + (->m void?) - (set-field! document this doc-in) (unless (· this obj) (set-field! obj this diff --git a/pitfall/pitfall/reference.rkt b/pitfall/pitfall/reference.rkt index ec56ec25..30f59fc6 100644 --- a/pitfall/pitfall/reference.rkt +++ b/pitfall/pitfall/reference.rkt @@ -7,7 +7,7 @@ "core.rkt" "object.rkt" "zlib.rkt") -(provide PDFReference set-current-id! make-ref) +(provide (all-defined-out)) (define dictable<%> (interface* () @@ -20,8 +20,12 @@ (define (dict-set! refobj key val) (send refobj set-key! key val)) (define (dict-update! refobj key updater [failure-result (λ () (error 'update-no-key))]) (send refobj update-key! key updater failure-result)))]))) +(define ref-listeners null) +(define (register-ref-listener proc) + (set! ref-listeners (cons proc ref-listeners))) + (define current-id 0) -(define (set-current-id! val) +(define (set-current-ref-id! val) (set! current-id val)) (define (make-ref [payload (make-hasheq)]) @@ -37,7 +41,7 @@ (field [(@offset offset) #f] [@port (open-output-bytes)]) - (for-each (λ (proc) (proc this)) (current-ref-listeners)) + (for-each (λ (proc) (proc this)) ref-listeners) (define/public (write x) (write-bytes (to-bytes x) @port)) diff --git a/pitfall/pitfall/standard-font.rkt b/pitfall/pitfall/standard-font.rkt index d890f8c7..fd467be1 100644 --- a/pitfall/pitfall/standard-font.rkt +++ b/pitfall/pitfall/standard-font.rkt @@ -12,13 +12,11 @@ (define StandardFont (class PDFFont - (init document) (init-field name id) (field [font (make-object AFMFont ((hash-ref standard-fonts name (λ () (raise-argument-error 'PDFFont "valid font name" name)))))]) - (super-new [document document] - [ascender (get-field ascender font)] + (super-new [ascender (get-field ascender font)] [descender (get-field descender font)] [bbox (get-field bbox font)] [line-gap (get-field line-gap font)]) @@ -27,8 +25,7 @@ [@descender descender] [@line-gap line-gap] [@bbox bbox] - [@dictionary dictionary] - [@document document]) + [@dictionary dictionary]) (define/override (embed) (set-field! payload @dictionary @@ -56,7 +53,7 @@ (* width scale)))) (module+ test - (define stdfont (make-object StandardFont #f "Helvetica" #f))) + (define stdfont (make-object StandardFont "Helvetica" #f))) (define (isStandardFont name) (hash-ref standard-fonts name #f))