main
Matthew Butterick 5 years ago
parent 0cb5b04856
commit 8c1320a2d0

@ -28,7 +28,7 @@
(unless (equal? (· options Subtype) "Link")
(hash-ref! options 'C
(λ ()
(send this _normalizeColor (or (· options color) '(0 0 0))))))
(send this normalize-color (or (· options color) '(0 0 0))))))
(hash-remove! options 'color)
(when (string? (· options Dest)) (hash-update! options 'Dest String))

@ -12,7 +12,7 @@
(provide color-mixin)
(define (_normalizeColor color)
(define (normalize-color color)
;; parses color string into list of values
(let loop ([color color])
(cond
@ -44,57 +44,57 @@
(field [_opacityRegistry (mhash)]
[_opacityCount 0]
[_gradCount 0]
[_fillColor #f])
[_fill-color #f])
(define/public (_setColor color-in stroke)
(define color (_normalizeColor color-in))
(define/public (set-color color-in stroke)
(define color (normalize-color color-in))
(define op (if stroke "SCN" "scn"))
(cond
[(not color)]
#;[(is-a? color PDFGradient)
(_setColorSpace "Pattern" stroke)
(set-color-space "Pattern" stroke)
(send color apply op)
#t] ; todo
[else
(define color-space (case (length color)
[(4) "DeviceCMYK"]
[(3) "DeviceRGB"]
[else (raise-argument-error '_setColor "color of length 3 or 4" color)]))
(_setColorSpace color-space stroke)
[else (raise-argument-error 'set-color "color of length 3 or 4" color)]))
(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) (number num #:round #false)) color) " ") op))
#t]))
(define/public (_setColorSpace space stroke)
(define/public (set-color-space space stroke)
(define op (if stroke "CS" "cs"))
(send this addContent (format "/~a ~a" space op)))
(define/public (fillColor color [opacity 1])
(unless (_normalizeColor color)
(raise-argument-error 'fillColor "valid color string" color))
(when (_setColor color #f) (fillOpacity opacity))
(define/public (fill-color color [opacity 1])
(unless (normalize-color color)
(raise-argument-error 'fill-color "valid color string" color))
(when (set-color color #f) (fill-opacity opacity))
;; save this for text wrapper, which needs to reset
;; the fill color on new pages
(set! _fillColor (list color opacity))
(set! _fill-color (list color opacity))
this)
(define/public (strokeColor color [opacity 1])
(unless (_normalizeColor color)
(raise-argument-error 'strokeColor "valid color string" color))
(when (_setColor color #t) (strokeOpacity opacity))
(define/public (stroke-color color [opacity 1])
(unless (normalize-color color)
(raise-argument-error 'stroke-color "valid color string" color))
(when (set-color color #t) (stroke-opacity opacity))
this)
(define/public (fillOpacity opacity)
(_doOpacity opacity #f)
(define/public (fill-opacity opacity)
(do-opacity opacity #f)
this)
(define/public (strokeOpacity opacity)
(_doOpacity #f opacity)
(define/public (stroke-opacity opacity)
(do-opacity #f opacity)
this)
(define/public (_doOpacity [fill-arg #f] [stroke-arg #f])
(define/public (do-opacity [fill-arg #f] [stroke-arg #f])
(define fill-opacity (and fill-arg (bounded 0 fill-arg 1)))
(define stroke-opacity (and stroke-arg (bounded 0 stroke-arg 1)))
(when (or fill-opacity stroke-opacity)
@ -264,14 +264,12 @@
"yellow" '(255 255 0)
"yellowgreen" '(154 205 50)))
(module+ test
(require rackunit)
(define c (new (color-mixin)))
(check-equal? (_normalizeColor "#6699Cc") '(0.4 0.6 0.8))
(check-false (_normalizeColor "#88aaCCC"))
(check-equal? (_normalizeColor "#69C") '(0.4 0.6 0.8))
(check-equal? (_normalizeColor "#69c") '(0.4 0.6 0.8))
(check-false (_normalizeColor "#8aCC"))
(check-equal? (_normalizeColor "aqua") '(0 1 1)))
(check-equal? (normalize-color "#6699Cc") '(0.4 0.6 0.8))
(check-false (normalize-color "#88aaCCC"))
(check-equal? (normalize-color "#69C") '(0.4 0.6 0.8))
(check-equal? (normalize-color "#69c") '(0.4 0.6 0.8))
(check-false (normalize-color "#8aCC"))
(check-equal? (normalize-color "aqua") '(0 1 1)))

@ -157,8 +157,8 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(when (or (· options underline) (· options strike))
(send this save)
(unless (· options stroke)
(define fillColorArgs (· this _fillColor))
(send this strokeColor . fillColorArgs))
(define fill-colorArgs (· this _fill-color))
(send this stroke-color . fill-colorArgs))
(define lineWidth (if (< (· this _fontSize) 10)
0.5
(floor (/ (· this _fontSize) 10))))

@ -186,19 +186,19 @@
(define/contract (fill this [color #f] #:rule [rule #f])
(() ((or/c color-string? #f) #:rule (or/c string? #f)) . ->*m . object?)
(when color (send this fillColor color)) ;; fillColor method is from color mixin
(when color (send this fill-color color)) ;; fill-color method is from color mixin
(send this addContent (format "f~a" (_windingRule rule))))
(define/contract (stroke this [color #f])
(() ((or/c color-string? #f)) . ->*m . object?)
(when color (send this strokeColor color))
(when color (send this stroke-color color))
(send this addContent "S"))
(define/contract (fillAndStroke this [fill #f] [stroke fill] #:rule [rule #f])
(() ((or/c color-string? #f) (or/c color-string? #f) #:rule (or/c string? #f)) . ->*m . object?)
(when fill (send* this [fillColor fill] [strokeColor stroke]))
(when fill (send* this [fill-color fill] [stroke-color stroke]))
(send this addContent (format "B~a" (_windingRule rule))))

@ -3,7 +3,7 @@
(define (proc doc)
(send* doc
[fillColor "blue"]
[fill-color "blue"]
[font "Helvetica" 30]
[translate 50 50]
[text "Here is a link!" 100 100 (hash

@ -34,7 +34,7 @@
[translate 400 0]
[circle 100 50 50]
[lineWidth 3]
[fillOpacity 0.8]
[fill-opacity 0.8]
[fillAndStroke "red" "#900"]
[restore])

@ -84,7 +84,7 @@ for part in tiger
# Add some text with annotations
doc.add-page()
.fillColor("blue")
.fill-color("blue")
.text('Here is a link!', 100, 100, { link: 'http://google.com/', underline: true })
doc.end()
|#
Loading…
Cancel
Save