From 5068b7e34455e4fa05c2bd310f76677366951bc7 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 25 Dec 2018 09:59:29 -0800 Subject: [PATCH] inherits --- pitfall/pitfall/document.rkt | 29 ++++++++++++--------- pitfall/pitfall/text.rkt | 50 +++++++++++++++++++----------------- pitfall/pitfall/vector.rkt | 44 ++++++++++++++++--------------- 3 files changed, 67 insertions(+), 56 deletions(-) diff --git a/pitfall/pitfall/document.rkt b/pitfall/pitfall/document.rkt index 9d1b4e48..6038aa53 100644 --- a/pitfall/pitfall/document.rkt +++ b/pitfall/pitfall/document.rkt @@ -18,16 +18,26 @@ (provide PDFDocument) (define PDFDocument - (class (annotation-mixin (image-mixin (text-mixin (fonts-mixin (vector-mixin (color-mixin object%)))))) + (class (annotation-mixin + (image-mixin + (text-mixin + (fonts-mixin + (vector-mixin + (color-mixin (class object% + (super-new) + (field [@pages null]) + (define/public (page) (first @pages)) + (define/public (addContent data) + (send (first @pages) write data) + this)))))))) (set-current-ref-id! 1) (register-ref-listener (λ (ref) (send this store-ref ref))) (super-new) - (init-field [(@options options) (mhasheq)]) - (field [@pages null] - [@refs null] + (init-field [(@options options) (mhasheq)]) + (field [@refs null] [@root (make-ref (mhasheq 'Type "Catalog" - 'Pages (make-ref (mhasheq 'Type "Pages"))))] + 'Pages (make-ref (mhasheq 'Type "Pages"))))] ;; initialize the metadata [@info (mhasheq 'Producer "PITFALL" 'Creator "PITFALL" @@ -36,7 +46,8 @@ ;; initialize mixins (inherit-field @ctm) ; from vector mixin (inherit-field @font-families) (inherit font) ; from font mixin - (inherit-field [@x x] [@y y]) + (inherit-field [@x x] [@y y]) ; from text + (inherit-field @pages) (inherit page) ; from base ;; initialize params (current-compress-streams? (hash-ref @options 'compress #t)) @@ -51,8 +62,6 @@ (define/public (store-ref ref) (set! @refs (cons ref @refs))) - (define/public (page) (first @pages)) - (define/public (add-page [options-arg @options]) ;; create a page object (define page-parent (dict-ref @root 'Pages)) @@ -67,10 +76,6 @@ (send this transform 1 0 0 -1 0 (get-field height (page))) this) - (define/public (addContent data) - (send (page) write data) - this) - (define/public (end) (write-bytes-out (format "%PDF-~a" (current-pdf-version))) (write-bytes-out "%ÿÿÿÿ") diff --git a/pitfall/pitfall/text.rkt b/pitfall/pitfall/text.rkt index 1077e526..18bb7c0b 100644 --- a/pitfall/pitfall/text.rkt +++ b/pitfall/pitfall/text.rkt @@ -27,8 +27,11 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee (inherit-field [@current-font current-font] [@current-font-size current-font-size] - [@current-fill-color current-fill-color]) + [@current-fill-color current-fill-color] + @pages) (inherit [@current-line-height current-line-height]) + (inherit save line-width move-to line-to stroke stroke-color transform restore) ; from vector + (inherit addContent) ; from base (define/public (move-down [lines 1] #:factor [factor 1]) (set! @y (+ @y (* factor (@current-line-height #t) (+ lines @line-gap)))) @@ -73,46 +76,47 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee ;; create underline or strikethrough line (when (or (hash-ref options 'underline #f) (hash-ref options 'strike #f)) - (send this save) + (save) (unless (hash-ref options 'stroke #f) (define fill-color-args @current-fill-color) (send this stroke-color . fill-color-args)) - (define line-width (if (< @current-font-size 10) + (define width (if (< @current-font-size 10) 0.5 (floor (/ @current-font-size 10)))) - (send this line-width line-width) + (line-width width) (define d (if (hash-ref options 'underline) 1 2)) (define line-y (+ y-in (/ (@current-line-height) d))) (when (hash-ref options 'underline) - (set! line-y (+ line-y (- line-width)))) + (set! line-y (+ line-y (- width)))) - (send this move-to x line-y) - (send this line-to (+ x (force rendered-width)) line-y) - (send this stroke) - (send this restore)) + (move-to x line-y) + (line-to (+ x (force rendered-width)) line-y) + (stroke) + (restore)) ;; flip coordinate system - (send this save) - (send this transform 1 0 0 -1 0 (get-field height (send this page))) - (define y (- (get-field height (send this page)) + (save) + (define page-height (get-field height (first @pages))) + (transform 1 0 0 -1 0 page-height) + (define y (- page-height y-in (* (/ (get-field ascender @current-font) 1000) @current-font-size))) ;; add current font to page if necessary (define current-font-id (get-field id @current-font)) - (hash-ref! (send (send this page) fonts) current-font-id + (hash-ref! (send (first @pages) fonts) current-font-id (λ () (send @current-font make-font-ref))) - (send this addContent "BT") ; begin the text object - (send this addContent (format "1 0 0 1 ~a ~a Tm" (numberizer x) (numberizer y))) ; text position - (send this addContent (format "/~a ~a Tf" (get-field id @current-font) + (addContent "BT") ; begin the text object + (addContent (format "1 0 0 1 ~a ~a Tm" (numberizer x) (numberizer y))) ; text position + (addContent (format "/~a ~a Tf" current-font-id (numberizer @current-font-size))) ; font and font size (let ([mode (+ (if (hash-ref options 'fill #f) 1 0) (if (hash-ref options 'stroke #f) 1 0))]) (when (and mode (not (zero? mode))) - (send this addContent (format "~a Tr" mode)))) + (addContent (format "~a Tr" mode)))) (when (not (zero? character-spacing)) - (send this addContent (format "~a Tc" character-spacing))) + (addContent (format "~a Tc" character-spacing))) ;; Add the actual text ;; 180321: the first call to this operation is very slow from Quad @@ -137,7 +141,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee (define (flush idx) (add-segment idx) (when (positive? (length commands)) - (send this addContent (format "[~a] TJ" (string-join (reverse commands) " "))) + (addContent (format "[~a] TJ" (string-join (reverse commands) " "))) (set! commands empty))) (for/fold ([had-offset #f] [x x]) @@ -148,7 +152,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee ;; so we can move the text position. [(or (not (zero? (glyph-position-x-offset posn))) (not (zero? (glyph-position-y-offset posn)))) (flush idx) - (send this addContent ; Move the text position and flush just the current character + (addContent ; Move the text position and flush just the current character (format "1 0 0 1 ~a ~a Tm" (numberizer (+ x (* (glyph-position-x-offset posn) scale))) (numberizer (+ y (* (glyph-position-y-offset posn) scale))))) @@ -157,7 +161,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee [else ;; If the last character had an offset, reset the text position (when had-offset - (send this addContent (format "1 0 0 1 ~a ~a Tm" (numberizer x) (numberizer y)))) + (addContent (format "1 0 0 1 ~a ~a Tm" (numberizer x) (numberizer y)))) ;; Group segments that don't have any advance adjustments (unless (zero? (- (glyph-position-x-advance posn) (glyph-position-advance-width posn))) (add-segment (add1 idx))) @@ -165,5 +169,5 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee (values having-offset (+ x (* (glyph-position-x-advance posn) scale)))) (flush (vector-length positions)) - (send this addContent "ET") ; end the text object - (send this restore)))) ; restore flipped coordinate system + (addContent "ET") ; end the text object + (restore)))) ; restore flipped coordinate system diff --git a/pitfall/pitfall/vector.rkt b/pitfall/pitfall/vector.rkt index ab8d6360..e3e8635d 100644 --- a/pitfall/pitfall/vector.rkt +++ b/pitfall/pitfall/vector.rkt @@ -18,10 +18,12 @@ (super-new) (field [@ctm default-ctm-value] [@ctm-stack null]) + (inherit addContent) ; from base + (inherit stroke-color fill-color) ; from color (define/public (save) (set! @ctm-stack (cons @ctm @ctm-stack)) - (send this addContent "q")) + (addContent "q")) (define/public (restore) (set! @ctm (if (pair? @ctm-stack) @@ -29,55 +31,55 @@ (car @ctm-stack) (set! @ctm-stack (cdr @ctm-stack))) default-ctm-value)) - (send this addContent "Q")) + (addContent "Q")) (define/public (close-path) - (send this addContent "h")) + (addContent "h")) (define/public (line-cap [c #f]) (define cap-styles (hasheq 'butt 0 'round 1 'square 2)) - (send this addContent + (addContent (format "~a J" (if (symbol? c) (hash-ref cap-styles c) "")))) (define/public (line-join [j #f]) (define cap-styles (hasheq 'miter 0 'round 1 'bevel 2)) - (send this addContent + (addContent (format "~a j" (if (symbol? j) (hash-ref cap-styles j) "")))) (define/public (line-width w) - (send this addContent (format "~a w" (number w)))) + (addContent (format "~a w" (number w)))) (define/public (dash length [options (mhash)]) (cond [(list? length) - (send this addContent + (addContent (format "[~a] ~a d" (string-join (map number length) " ") (hash-ref options 'phase 0)))] [length (define space (hash-ref options 'space length)) (define phase (hash-ref options 'phase 0)) - (send this addContent (format "[~a ~a] ~a d" (number length) (number space) (number phase)))] + (addContent (format "[~a ~a] ~a d" (number length) (number space) (number phase)))] [else this])) (define/public (move-to x y) - (send this addContent (format "~a ~a m" x y))) + (addContent (format "~a ~a m" x y))) (define/public (line-to x y) - (send this addContent (format "~a ~a l" x y))) + (addContent (format "~a ~a l" x y))) (define/public (bezier-curve-to cp1x cp1y cp2x cp2y x y) - (send this addContent (format "~a c" (string-join (map number (list cp1x cp1y cp2x cp2y x y)) " ")))) + (addContent (format "~a c" (string-join (map number (list cp1x cp1y cp2x cp2y x y)) " ")))) (define/public (quadratic-curve-to cpx cpy x y) - (send this addContent (format "~a v" (string-join (map number (list cpx cpy x y)) " ")))) + (addContent (format "~a v" (string-join (map number (list cpx cpy x y)) " ")))) (define/public (rect x y w h) - (send this addContent (format "~a re" (string-join (map number (list x y w h)) " ")))) + (addContent (format "~a re" (string-join (map number (list x y w h)) " ")))) (define/public (ellipse x y r1 [r2 r1]) ;; based on http://stackoverflow.com/questions/2172798/how-to-draw-an-oval-in-html5-canvas/2173084#2173084 @@ -120,28 +122,28 @@ (if (and (string? rule) (regexp-match #rx"^even-?odd$" rule)) "*" "")) (define/public (fill [color #f] #:rule [rule #f]) - (when color (send this fill-color color)) ;; fill-color method is from color mixin - (send this addContent (format "f~a" (_windingRule rule)))) + (when color (fill-color color)) ;; fill-color method is from color mixin + (addContent (format "f~a" (_windingRule rule)))) (define/public (stroke [color #f]) - (when color (send this stroke-color color)) - (send this addContent "S")) + (when color (stroke-color color)) + (addContent "S")) (define/public (fill-and-stroke [fill #f] [stroke fill] #:rule [rule #f]) - (when fill (send* this [fill-color fill] [stroke-color stroke])) - (send this addContent (format "B~a" (_windingRule rule)))) + (when fill (fill-color fill) (stroke-color stroke)) + (addContent (format "B~a" (_windingRule rule)))) (define tm/c (list/c number? number? number? number? number? number?)) (define/public (make-transform-string ctm) (format "~a cm" (string-join (map number ctm) " "))) (define/public (clip [rule #f]) - (send this addContent (string-append "W" (_windingRule rule) " n"))) + (addContent (string-append "W" (_windingRule rule) " n"))) (define/public (transform scaleX shearY shearX scaleY mdx mdy) (define new-ctm (list scaleX shearY shearX scaleY mdx mdy)) (set! @ctm (combine-transforms (· this @ctm) new-ctm)) - (send this addContent (make-transform-string new-ctm))) + (addContent (make-transform-string new-ctm))) (define/public (shear x y) (transform 1 y x 1 0 0))