main
Matthew Butterick 6 years ago
parent 5068b7e344
commit fd58f7a545

@ -60,12 +60,12 @@
(set-color-space color-space stroke)
;; 181126 don't round, to be consistent with pdfkit behavior
(send this addContent (format "~a ~a" (string-join (map (λ (num) (numberizer num #:round #false)) color) " ") op))
(send this add-content (format "~a ~a" (string-join (map (λ (num) (numberizer num #:round #false)) color) " ") op))
#true]))
(define/public (set-color-space space stroke)
(define op (if stroke "CS" "cs"))
(send this addContent (format "/~a ~a" space op)))
(send this add-content (format "/~a ~a" space op)))
(define/public (fill-color color [opacity 1])
(unless (normalize-color color)
@ -110,7 +110,7 @@
(set! @opacity-count (add1 @opacity-count))
(list ref-dict (format "Gs~a" @opacity-count)))))
(hash-set! (send (send this page) ext_gstates) name dictionary)
(send this addContent (format "/~a gs" name))))))
(send this add-content (format "/~a gs" name))))))
(define named-colors
(hash "aliceblue" '(240 248 255)

@ -27,7 +27,7 @@
(super-new)
(field [@pages null])
(define/public (page) (first @pages))
(define/public (addContent data)
(define/public (add-content data)
(send (first @pages) write data)
this))))))))
(set-current-ref-id! 1)

@ -88,7 +88,7 @@
(when (= @y y) (set! y (+ y h)))
(send this save)
(send this transform w 0 0 (- h) x (+ y h))
(send this addContent (format "/~a Do" (get-field label image)))
(send this add-content (format "/~a Do" (get-field label image)))
(send this restore)
this)

@ -31,7 +31,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
@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
(inherit add-content) ; from base
(define/public (move-down [lines 1] #:factor [factor 1])
(set! @y (+ @y (* factor (@current-line-height #t) (+ lines @line-gap))))
@ -108,15 +108,15 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(hash-ref! (send (first @pages) fonts) current-font-id
(λ () (send @current-font make-font-ref)))
(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
(add-content "BT") ; begin the text object
(add-content (format "1 0 0 1 ~a ~a Tm" (numberizer x) (numberizer y))) ; text position
(add-content (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)))
(addContent (format "~a Tr" mode))))
(add-content (format "~a Tr" mode))))
(when (not (zero? character-spacing))
(addContent (format "~a Tc" character-spacing)))
(add-content (format "~a Tc" character-spacing)))
;; Add the actual text
;; 180321: the first call to this operation is very slow from Quad
@ -141,7 +141,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(define (flush idx)
(add-segment idx)
(when (positive? (length commands))
(addContent (format "[~a] TJ" (string-join (reverse commands) " ")))
(add-content (format "[~a] TJ" (string-join (reverse commands) " ")))
(set! commands empty)))
(for/fold ([had-offset #f] [x x])
@ -152,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)
(addContent ; Move the text position and flush just the current character
(add-content ; 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)))))
@ -161,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
(addContent (format "1 0 0 1 ~a ~a Tm" (numberizer x) (numberizer y))))
(add-content (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)))
@ -169,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))
(addContent "ET") ; end the text object
(add-content "ET") ; end the text object
(restore)))) ; restore flipped coordinate system

@ -18,12 +18,12 @@
(super-new)
(field [@ctm default-ctm-value]
[@ctm-stack null])
(inherit addContent) ; from base
(inherit add-content) ; from base
(inherit stroke-color fill-color) ; from color
(define/public (save)
(set! @ctm-stack (cons @ctm @ctm-stack))
(addContent "q"))
(add-content "q"))
(define/public (restore)
(set! @ctm (if (pair? @ctm-stack)
@ -31,55 +31,55 @@
(car @ctm-stack)
(set! @ctm-stack (cdr @ctm-stack)))
default-ctm-value))
(addContent "Q"))
(add-content "Q"))
(define/public (close-path)
(addContent "h"))
(add-content "h"))
(define/public (line-cap [c #f])
(define cap-styles (hasheq 'butt 0 'round 1 'square 2))
(addContent
(add-content
(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))
(addContent
(add-content
(format "~a j" (if (symbol? j)
(hash-ref cap-styles j)
""))))
(define/public (line-width w)
(addContent (format "~a w" (number w))))
(add-content (format "~a w" (number w))))
(define/public (dash length [options (mhash)])
(cond
[(list? length)
(addContent
(add-content
(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))
(addContent (format "[~a ~a] ~a d" (number length) (number space) (number phase)))]
(add-content (format "[~a ~a] ~a d" (number length) (number space) (number phase)))]
[else this]))
(define/public (move-to x y)
(addContent (format "~a ~a m" x y)))
(add-content (format "~a ~a m" x y)))
(define/public (line-to x y)
(addContent (format "~a ~a l" x y)))
(add-content (format "~a ~a l" x y)))
(define/public (bezier-curve-to cp1x cp1y cp2x cp2y x y)
(addContent (format "~a c" (string-join (map number (list cp1x cp1y cp2x cp2y x y)) " "))))
(add-content (format "~a c" (string-join (map number (list cp1x cp1y cp2x cp2y x y)) " "))))
(define/public (quadratic-curve-to cpx cpy x y)
(addContent (format "~a v" (string-join (map number (list cpx cpy x y)) " "))))
(add-content (format "~a v" (string-join (map number (list cpx cpy x y)) " "))))
(define/public (rect x y w h)
(addContent (format "~a re" (string-join (map number (list x y w h)) " "))))
(add-content (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
@ -123,27 +123,27 @@
(define/public (fill [color #f] #:rule [rule #f])
(when color (fill-color color)) ;; fill-color method is from color mixin
(addContent (format "f~a" (_windingRule rule))))
(add-content (format "f~a" (_windingRule rule))))
(define/public (stroke [color #f])
(when color (stroke-color color))
(addContent "S"))
(add-content "S"))
(define/public (fill-and-stroke [fill #f] [stroke fill] #:rule [rule #f])
(when fill (fill-color fill) (stroke-color stroke))
(addContent (format "B~a" (_windingRule rule))))
(add-content (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])
(addContent (string-append "W" (_windingRule rule) " n")))
(add-content (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))
(addContent (make-transform-string new-ctm)))
(add-content (make-transform-string new-ctm)))
(define/public (shear x y)
(transform 1 y x 1 0 0))

Loading…
Cancel
Save