test10 works

main
Matthew Butterick 8 years ago
parent 47331124f4
commit b0f9ee621b

@ -6,9 +6,11 @@
(apply-commands commands doc))
(define (parse path)
(for/list ([str (in-list (string-split (string-replace path "," " ") #px"(?=[A-Za-z])"))]
#:when (not (string=? str "")))
(read (open-input-string (string-append "(" str ")")))))
(let* ([path (string-replace path "," " ")] ; no commas
[path (string-replace path #px"(?<=[A-Za-z])" " ")]) ; at least one space after letters
(for/list ([str (in-list (string-split path #px"(?=[A-Za-z])"))]
#:unless (zero? (string-length str)))
(read (open-input-string (format "(~a)" (string-replace str "-" " -")))))))
(module+ test
(require rackunit)
@ -18,7 +20,17 @@
(L 100 160)
(Q 130 200 150 120)
(C 190 -40 200 200 300 150)
(L 400 90))))
(L 400 90)))
(check-equal?
(parse "M-122.304 84.285C-122.304 84.285 -122.203 86.179 -123.027 86.16C-123.851 86.141 -140.305 38.066 -160.833 40.309C-160.833 40.309 -143.05 32.956 -122.304 84.285z")
'((M -122.304 84.285)
(C -122.304 84.285 -122.203 86.179 -123.027 86.16)
(C -123.851 86.141 -140.305 38.066 -160.833 40.309)
(C -160.833 40.309 -143.05 32.956 -122.304 84.285)
(z)))
(check-equal? (parse "L100-160") '((L 100 -160))))
(define (apply-commands commands doc)
(for/fold ([cx 0][cy 0][px 0][py 0][sx 0][sy 0])
@ -38,4 +50,6 @@
[(Q) (send doc quadraticCurveTo . cmd-args)
(match-define (list a0 a1 a2 a3) cmd-args)
(values a2 a3 a0 a1 sx sy)]
[else (values cx cy px py sx sy)]))))
[(z) (send doc closePath . cmd-args)
(values sx sy px py sx sy)]
[else (report cmd-name) (values cx cy px py sx sy)]))))

File diff suppressed because one or more lines are too long

@ -0,0 +1,27 @@
PDFDocument = require 'pdfkit'
tiger = require './assets/tiger'
fs = require 'fs'
make = (doc) ->
doc.translate(220, 300)
# Render each path that makes up the tiger image
for part in tiger
doc.path(part.path) # render an SVG path
if part['stroke-width']
doc.lineWidth part['stroke-width']
if part.fill isnt 'none' and part.stroke isnt 'none'
doc.fillAndStroke(part.fill, part.stroke)
else
unless part.fill is 'none'
doc.fill(part.fill)
unless part.stroke is 'none'
doc.stroke(part.stroke)
doc.end()
doc = new PDFDocument({compress: yes})
doc.pipe(fs.createWriteStream('test10c.pdf'))
make doc

@ -0,0 +1,25 @@
#lang pitfall/pdftest
(define (proc doc)
(send doc translate 220 300)
(for* ([datum (in-list (read (open-input-string (string-replace (file->string "assets/tiger.json") #rx"[,:]" " "))))]
[part (in-value (apply hash datum))])
(send doc path (hash-ref part 'path))
(when (hash-has-key? part "stroke-width")
(send doc lineWidth (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))
(begin
(unless (string=? (hash-ref part 'fill "none") "none")
(send doc fill (hash-ref part 'fill)))
(unless (string=? (hash-ref part 'stroke "none") "none")
(send doc fill (hash-ref part 'stroke)))))))
(define-runtime-path this "test10rkt.pdf")
(make-doc this #f proc #:pdfkit #f)
(define-runtime-path that "test10crkt.pdf")
(make-doc that #t proc #:pdfkit #f)

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save