From b0731d4af2b5d725c87f65f74db59f14907cdc11 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 8 Jun 2019 09:47:21 -0700 Subject: [PATCH] keyword args for `image` --- pitfall/pitfall/image.rkt | 32 +++++++++++++++++++++----------- pitfall/ptest/test5.rkt | 2 +- pitfall/ptest/test7.rkt | 2 +- pitfall/ptest/test8.rkt | 2 +- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/pitfall/pitfall/image.rkt b/pitfall/pitfall/image.rkt index 354e6ea8..41d7e765 100644 --- a/pitfall/pitfall/image.rkt +++ b/pitfall/pitfall/image.rkt @@ -21,9 +21,19 @@ [else (raise-argument-error 'open-pdf-image "valid image format" src)])) (img-constructor data label)) - (define (image doc src [x-in #f] [y-in #f] [options (mhasheq)]) - (define x (or x-in (hash-ref options 'x #f) (pdf-x doc))) - (define y (or y-in (hash-ref options 'y #f) (pdf-y doc))) + (define (image doc src [x-in #f] [y-in #f] + #:x [x-kwarg #f] + #:y [y-kwarg #f] + #:width [width #f] + #:height [height #f] + #:scale [scale #f] + #:fit [fit #f] + #:cover [cover #f] + #:align [align #f] + #:valign [valign #f] + ) + (define x (or x-in x-kwarg (pdf-x doc))) + (define y (or y-in y-kwarg (pdf-y doc))) (define image (cond [(and (string? src) (hash-ref (pdf-image-registry doc) src #f))] @@ -35,8 +45,8 @@ (define image-width ($img-width image)) (define image-height ($img-height image)) - (define options-width (hash-ref options 'width #f)) - (define options-height (hash-ref options 'height #f)) + (define options-width width) + (define options-height height) (define w (or options-width image-width)) (define h (or options-height image-height)) (define wp #f) @@ -55,11 +65,11 @@ (set! hp (/ h image-width)) (set! w (* image-width hp)) (set! h (* image-height hp))] - [(hash-ref options 'scale #f) + [scale => (λ (scale-val) (set! w (* image-width scale-val)) (set! h (* image-height scale-val)))] - [(hash-ref options 'fit #f) + [fit => (λ (fit-val) (match-define (list bw bh) fit-val) (set! bp (/ bw bh)) @@ -71,7 +81,7 @@ [else (set! w (* bh ip)) (set! h bh)]))] - [(hash-ref options 'cover #f) + [cover => (λ (cover-val) (match-define (list bw bh) cover-val) (set! bp (/ bw bh)) @@ -84,11 +94,11 @@ (set! w bw) (set! h (/ bw ip))]))]) - (when (or (hash-ref options 'fit #f) (hash-ref options 'cover #f)) - (case (hash-ref options 'align #f) + (when (or fit cover) + (case align [("center") (set! x (+ x (/ bw 2) (- (/ w 2))))] [("right") (set! x (+ x bw - w))]) - (case (hash-ref options 'valign #f) + (case valign [("center") (set! y (+ y (/ bh 2) (- (/ h 2))))] [("bottom") (set! y (+ y bh - h))])) diff --git a/pitfall/ptest/test5.rkt b/pitfall/ptest/test5.rkt index 53c44613..2b5416fe 100644 --- a/pitfall/ptest/test5.rkt +++ b/pitfall/ptest/test5.rkt @@ -7,7 +7,7 @@ [font doc "Times-Italic"] [font-size doc 25] [text doc "Some fantastic text!" 100 100] - [image doc death 100 160 (hash 'width 412)]) + [image doc death 100 160 #:width 412]) (define-runtime-path this "test5rkt.pdf") (make-doc this #f proc) diff --git a/pitfall/ptest/test7.rkt b/pitfall/ptest/test7.rkt index 4be2ef30..dc27e371 100644 --- a/pitfall/ptest/test7.rkt +++ b/pitfall/ptest/test7.rkt @@ -7,7 +7,7 @@ [font doc "Times-Italic"] [font-size doc 25] [text doc "Here comes a JPEG!" 100 100] - [image doc test-jpeg 100 160 (hash 'width 412)]) + [image doc test-jpeg 100 160 #:width 412]) (define-runtime-path this "test7rkt.pdf") (make-doc this #f proc) diff --git a/pitfall/ptest/test8.rkt b/pitfall/ptest/test8.rkt index cf1180ea..2f670472 100644 --- a/pitfall/ptest/test8.rkt +++ b/pitfall/ptest/test8.rkt @@ -7,7 +7,7 @@ [font doc "Helvetica-Bold"] [font-size doc 25] [text doc "Another fantastic pic" 100 100] - [image doc pic 100 160 (hash 'width 412)]) + [image doc pic 100 160 #:width 412]) ; node's zlib.deflate makes smaller files, for some reason ; so don't compare file sizes in this case