main
Matthew Butterick 5 years ago
parent 8c1320a2d0
commit a66814cfd7

@ -49,10 +49,10 @@
(match-define (list a0 a1 a2 a3 a4 a5)
(append cmd-args (make-list (- 6 (length cmd-args)) #f)))
(case cmd-name
[(M) (send doc moveTo . cmd-args)
[(M) (send doc move-to . cmd-args)
(values a0 a1 #f #f a0 a1)]
[(m) (loop 'M (list (+ cx a0) (+ cy a1)))]
[(C) (send doc bezierCurveTo . cmd-args)
[(C) (send doc bezier-curve-to . cmd-args)
(values a4 a5 a2 a3 sx sy)]
[(c) (loop 'C (list (+ cx a0) (+ cy a1)
(+ cx a2) (+ cy a3)
@ -64,14 +64,14 @@
(values a2 a3 a0 a1 sx sy))]
[(s) (loop 'S (list (+ cx a0) (+ cy a1)
(+ cx a2) (+ cy a3)))]
[(L) (send doc lineTo . cmd-args)
[(L) (send doc line-to . cmd-args)
(values a0 a1 #f #f sx sy)]
[(l) (loop 'L (list (+ cx a0) (+ cy a1)))]
[(H) (loop 'L (list a0 cy))]
[(h) (loop 'L (list (+ cx a0) cy))]
[(V) (loop 'L (list cx a0))]
[(v) (loop 'L (list cx (+ cy a0)))]
[(Q) (send doc quadraticCurveTo . cmd-args)
[(Q) (send doc quadratic-curve-to . cmd-args)
(values a2 a3 a0 a1 sx sy)]
[(q) (loop 'Q (list (+ cx a0) (+ cy a1)
(+ cx a2) (+ cy a3)))]
@ -79,7 +79,7 @@
(if (not px)
(list cx py)
(list (- cx (- px cx) (- cy (- py cy))))))
(send doc quadraticCurveTo . cmd-args)]
(send doc quadratic-curve-to . cmd-args)]
;; todo other path ops
[(z) (send doc closePath . cmd-args)
(values sx sy px py sx sy)]

@ -159,17 +159,17 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(unless (· options stroke)
(define fill-colorArgs (· this _fill-color))
(send this stroke-color . fill-colorArgs))
(define lineWidth (if (< (· this _fontSize) 10)
(define line-width (if (< (· this _fontSize) 10)
0.5
(floor (/ (· this _fontSize) 10))))
(send this lineWidth lineWidth)
(send this line-width line-width)
(define d (if (· options underline) 1 2))
(define lineY (+ y-in (/ (· this currentLineHeight) d)))
(when (· options underline)
(increment! lineY (- lineWidth)))
(increment! lineY (- line-width)))
(send this moveTo x lineY)
(send this lineTo (+ x (force renderedWidth)) lineY)
(send this move-to x lineY)
(send this line-to (+ x (force renderedWidth)) lineY)
(send this stroke)
(send this restore))

@ -23,12 +23,12 @@
closePath
lineCap
lineJoin
lineWidth
line-width
dash
moveTo
lineTo
bezierCurveTo
quadraticCurveTo
move-to
line-to
bezier-curve-to
quadratic-curve-to
rect
ellipse
circle
@ -37,7 +37,7 @@
_windingRule
fill
stroke
fillAndStroke
fill-and-stroke
clip
shear
transform
@ -90,7 +90,7 @@
""))))
(define/contract (lineWidth this w)
(define/contract (line-width this w)
(number? . ->m . object?)
(send this addContent (format "~a w" (number w))))
@ -112,22 +112,22 @@
[else this]))
(define/contract (moveTo this x y)
(define/contract (move-to this x y)
(number? number? . ->m . object?)
(send this addContent (format "~a ~a m" x y)))
(define/contract (lineTo this x y)
(define/contract (line-to this x y)
(number? number? . ->m . object?)
(send this addContent (format "~a ~a l" x y)))
(define/contract (bezierCurveTo this cp1x cp1y cp2x cp2y x y)
(define/contract (bezier-curve-to this cp1x cp1y cp2x cp2y x y)
(number? number? number? number? number? number? . ->m . object?)
(send this addContent (format "~a c" (string-join (map number (list cp1x cp1y cp2x cp2y x y)) " "))))
(define/contract (quadraticCurveTo this cpx cpy x y)
(define/contract (quadratic-curve-to this cpx cpy x y)
(number? number? number? number . ->m . object?)
(send this addContent (format "~a v" (string-join (map number (list cpx cpy x y)) " "))))
@ -149,11 +149,11 @@
(define ye (+ y (* r2 2))) ; y-end
(define xm (+ x r1)) ; x-middle
(define ym (+ y r2)) ; y-middle
(moveTo this x ym)
(bezierCurveTo this x (- ym oy) (- xm ox) y xm y)
(bezierCurveTo this (+ xm ox) y xe (- ym oy) xe ym)
(bezierCurveTo this xe (+ ym oy) (+ xm ox) ye xm ye)
(bezierCurveTo this (- xm ox) ye x (+ ym oy) x ym)
(move-to this x ym)
(bezier-curve-to this x (- ym oy) (- xm ox) y xm y)
(bezier-curve-to this (+ xm ox) y xe (- ym oy) xe ym)
(bezier-curve-to this xe (+ ym oy) (+ xm ox) ye xm ye)
(bezier-curve-to this (- xm ox) ye x (+ ym oy) x ym)
(closePath this))
@ -166,9 +166,9 @@
(() () #:rest (listof (list/c number? number?)) . ->*m . object?)
(cond
[(pair? points)
(apply moveTo this (car points))
(apply move-to this (car points))
(for ([pt (in-list (cdr points))])
(apply lineTo this pt))
(apply line-to this pt))
(closePath this)]
[else this]))
@ -196,7 +196,7 @@
(send this addContent "S"))
(define/contract (fillAndStroke this [fill #f] [stroke fill] #:rule [rule #f])
(define/contract (fill-and-stroke 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 [fill-color fill] [stroke-color stroke]))
(send this addContent (format "B~a" (_windingRule rule))))

@ -5,9 +5,9 @@
;; Draw a triangle and a circle
(send* doc
[save]
[moveTo 100 150]
[lineTo 100 250]
[lineTo 200 250]
[move-to 100 150]
[line-to 100 250]
[line-to 200 250]
[fill "#FF3300"])
(send* doc

@ -10,11 +10,11 @@
(send doc path (hash-ref part 'path))
(when (hash-has-key? part "stroke-width")
(send doc lineWidth (string->number (hash-ref part "stroke-width"))))
(send doc line-width (string->number (hash-ref part "stroke-width"))))
(if (and (not (string=? (hash-ref part 'fill "none") "none"))
(not (string=? (hash-ref part 'stroke "none") "none")))
(send doc fillAndStroke (hash-ref part 'fill) (hash-ref part 'stroke))
(send doc fill-and-stroke (hash-ref part 'fill) (hash-ref part 'stroke))
(begin
(unless (string=? (hash-ref part 'fill "none") "none")
(send doc fill (hash-ref part 'fill)))

@ -5,11 +5,11 @@
;; curved path as bezier
(send* doc
[moveTo 0 20]
[lineTo 100 160]
[quadraticCurveTo 130 200 150 120]
[bezierCurveTo 190 -40 200 200 300 150]
[lineTo 400 90]
[move-to 0 20]
[line-to 100 160]
[quadratic-curve-to 130 200 150 120]
[bezier-curve-to 190 -40 200 200 300 150]
[line-to 400 90]
[stroke])
(send* doc [translate 0 200])
@ -33,29 +33,29 @@
[save]
[translate 400 0]
[circle 100 50 50]
[lineWidth 3]
[line-width 3]
[fill-opacity 0.8]
[fillAndStroke "red" "#900"]
[fill-and-stroke "red" "#900"]
[restore])
(send* doc [translate 0 200])
;; these examples are easier to see with a large line width
(send* doc [lineWidth 25])
(send* doc [line-width 25])
;; line cap settings
(send* doc [lineCap 'butt]
[moveTo 50 20]
[lineTo 100 20]
[move-to 50 20]
[line-to 100 20]
[stroke]
[lineCap 'round]
[moveTo 150 20]
[lineTo 200 20]
[move-to 150 20]
[line-to 200 20]
[stroke])
;; square line cap shown with a circle instead of a line so you can see it
(send* doc [lineCap 'square]
[moveTo 250 20]
[move-to 250 20]
[circle 275 30 15]
[stroke])

@ -3,11 +3,11 @@
(define (proc doc)
(send* doc
[moveTo 0 20]
[lineTo 100 160]
[quadraticCurveTo 130 200 150 120]
[bezierCurveTo 190 -40 200 200 300 150]
[lineTo 400 90]
[move-to 0 20]
[line-to 100 160]
[quadratic-curve-to 130 200 150 120]
[bezier-curve-to 190 -40 200 200 300 150]
[line-to 400 90]
[stroke])
(send* doc [translate 0 200])

@ -32,9 +32,9 @@ doc.add-page()
# Draw a triangle and a circle
doc.save()
.moveTo(100, 150)
.lineTo(100, 250)
.lineTo(200, 250)
.move-to(100, 150)
.line-to(100, 250)
.line-to(200, 250)
.fill("#FF3300")
doc.circle(280, 200, 50)
@ -69,10 +69,10 @@ for part in tiger
doc.path(part.path) # render an SVG path
if part['stroke-width']
doc.lineWidth part['stroke-width']
doc.line-width part['stroke-width']
if part.fill isnt 'none' and part.stroke isnt 'none'
doc.fillAndStroke(part.fill, part.stroke)
doc.fill-and-stroke(part.fill, part.stroke)
else
unless part.fill is 'none'
doc.fill(part.fill)

Loading…
Cancel
Save