From 0905b02bfb28de3a4f881584a0d5ee216f9af2da Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 14 May 2017 10:55:07 -0700 Subject: [PATCH] rearrangement servant --- pitfall/pitfall/{kit => }/buffer.rkt | 0 pitfall/pitfall/{kit => }/color.rkt | 0 pitfall/pitfall/{kit => }/document.rkt | 49 +++++----- pitfall/pitfall/{kit => }/helper.rkt | 11 ++- pitfall/pitfall/kit/struct.rkt | 6 -- pitfall/pitfall/{kit => }/object.rkt | 0 pitfall/pitfall/{ => old}/charter.otf | Bin pitfall/pitfall/{ => old}/fira.otf | Bin pitfall/pitfall/{ => old}/foo.rkt | 0 pitfall/pitfall/{ => old}/freetype-ffi.rkt | 0 pitfall/pitfall/{ => old}/glyph-list.rkt | 0 pitfall/pitfall/{ => old}/info.rkt | 0 pitfall/pitfall/{ => old}/main.rkt | 0 pitfall/pitfall/{ => old}/measure.rkt | 0 .../{ => old}/minimal-annotation-square.rkt | 0 .../{ => old}/minimal-annotation-text.rkt | 0 .../{ => old}/minimal-embedded-font.rkt | 0 .../{ => old}/minimal-measured-font.rkt | 0 .../pitfall/{ => old}/minimal-pdf-source.rkt | 0 pitfall/pitfall/{ => old}/minimal-pdf.rkt | 0 pitfall/pitfall/{ => old}/minimal.pdf | 0 pitfall/pitfall/{ => old}/miso.otf | Bin pitfall/pitfall/{ => old}/parse.rkt | 0 pitfall/pitfall/{ => old}/parser-test.rkt | 0 pitfall/pitfall/{ => old}/parser.rkt | 0 pitfall/pitfall/{ => old}/render.rkt | 0 .../pitfall/{ => old}/sample-ext-encoding.rkt | 0 .../pitfall/{ => old}/sample-unicode-text.rkt | 0 pitfall/pitfall/old/struct.rkt | 16 +++ pitfall/pitfall/{ => old}/test.rkt | 0 pitfall/pitfall/{ => old}/tokenizer.rkt | 0 .../{ => old}/triple-measured-font.rkt | 0 pitfall/pitfall/{kit => }/out copy.pdf | 0 pitfall/pitfall/out.pdf | 63 ++++++++++++ pitfall/pitfall/{kit => }/page-test.rkt | 0 pitfall/pitfall/{kit => }/page.rkt | 0 pitfall/pitfall/{kit => }/params.rkt | 2 +- pitfall/pitfall/{kit => }/reference.rkt | 0 pitfall/pitfall/struct.rkt | 18 +--- pitfall/pitfall/test-helper.rkt | 7 ++ .../test}/assets/charter.ttf | Bin .../{pktest => pitfall/test}/assets/test.jpeg | Bin .../{pktest => pitfall/test}/assets/test.png | Bin .../{pktest => pitfall/test}/assets/tiger.js | 0 .../test}/node_modules/pdfkit | 0 pitfall/{pktest => pitfall/test}/outrkt.pdf | 0 pitfall/pitfall/test/test-all.rkt | 3 + pitfall/{pktest => pitfall/test}/test.coffee | 0 pitfall/{pktest => pitfall/test}/test.pdf | Bin .../test}/test0-uncompressed.pdf | 0 pitfall/{pktest => pitfall/test}/test0.coffee | 0 pitfall/{pktest => pitfall/test}/test0.pdf | 0 pitfall/pitfall/test/test0.rkt | 14 +++ .../{kit/out.pdf => test/test0rkt copy.pdf} | 0 pitfall/{pktest => pitfall/test}/test0rkt.pdf | 0 pitfall/{pktest => pitfall/test}/test1.coffee | 0 pitfall/{pktest => pitfall/test}/test1.pdf | 0 pitfall/pitfall/test/test1.rkt | 27 ++++++ .../test/test1rkt copy.pdf} | 0 pitfall/pitfall/test/test1rkt.pdf | 91 ++++++++++++++++++ pitfall/{pktest => pitfall/test}/testrkt.rkt | 0 pitfall/pitfall/test0rkt.pdf | 63 ++++++++++++ pitfall/pitfall/test1rkt.pdf | 91 ++++++++++++++++++ pitfall/pitfall/{kit => }/vector.rkt | 68 ++++++++----- pitfall/pktest/test0.rkt | 7 -- pitfall/pktest/test1.rkt | 18 ---- 66 files changed, 458 insertions(+), 96 deletions(-) rename pitfall/pitfall/{kit => }/buffer.rkt (100%) rename pitfall/pitfall/{kit => }/color.rkt (100%) rename pitfall/pitfall/{kit => }/document.rkt (85%) rename pitfall/pitfall/{kit => }/helper.rkt (92%) delete mode 100644 pitfall/pitfall/kit/struct.rkt rename pitfall/pitfall/{kit => }/object.rkt (100%) rename pitfall/pitfall/{ => old}/charter.otf (100%) rename pitfall/pitfall/{ => old}/fira.otf (100%) rename pitfall/pitfall/{ => old}/foo.rkt (100%) rename pitfall/pitfall/{ => old}/freetype-ffi.rkt (100%) rename pitfall/pitfall/{ => old}/glyph-list.rkt (100%) rename pitfall/pitfall/{ => old}/info.rkt (100%) rename pitfall/pitfall/{ => old}/main.rkt (100%) rename pitfall/pitfall/{ => old}/measure.rkt (100%) rename pitfall/pitfall/{ => old}/minimal-annotation-square.rkt (100%) rename pitfall/pitfall/{ => old}/minimal-annotation-text.rkt (100%) rename pitfall/pitfall/{ => old}/minimal-embedded-font.rkt (100%) rename pitfall/pitfall/{ => old}/minimal-measured-font.rkt (100%) rename pitfall/pitfall/{ => old}/minimal-pdf-source.rkt (100%) rename pitfall/pitfall/{ => old}/minimal-pdf.rkt (100%) rename pitfall/pitfall/{ => old}/minimal.pdf (100%) rename pitfall/pitfall/{ => old}/miso.otf (100%) rename pitfall/pitfall/{ => old}/parse.rkt (100%) rename pitfall/pitfall/{ => old}/parser-test.rkt (100%) rename pitfall/pitfall/{ => old}/parser.rkt (100%) rename pitfall/pitfall/{ => old}/render.rkt (100%) rename pitfall/pitfall/{ => old}/sample-ext-encoding.rkt (100%) rename pitfall/pitfall/{ => old}/sample-unicode-text.rkt (100%) create mode 100644 pitfall/pitfall/old/struct.rkt rename pitfall/pitfall/{ => old}/test.rkt (100%) rename pitfall/pitfall/{ => old}/tokenizer.rkt (100%) rename pitfall/pitfall/{ => old}/triple-measured-font.rkt (100%) rename pitfall/pitfall/{kit => }/out copy.pdf (100%) create mode 100644 pitfall/pitfall/out.pdf rename pitfall/pitfall/{kit => }/page-test.rkt (100%) rename pitfall/pitfall/{kit => }/page.rkt (100%) rename pitfall/pitfall/{kit => }/params.rkt (52%) rename pitfall/pitfall/{kit => }/reference.rkt (100%) create mode 100644 pitfall/pitfall/test-helper.rkt rename pitfall/{pktest => pitfall/test}/assets/charter.ttf (100%) rename pitfall/{pktest => pitfall/test}/assets/test.jpeg (100%) rename pitfall/{pktest => pitfall/test}/assets/test.png (100%) rename pitfall/{pktest => pitfall/test}/assets/tiger.js (100%) rename pitfall/{pktest => pitfall/test}/node_modules/pdfkit (100%) rename pitfall/{pktest => pitfall/test}/outrkt.pdf (100%) create mode 100644 pitfall/pitfall/test/test-all.rkt rename pitfall/{pktest => pitfall/test}/test.coffee (100%) rename pitfall/{pktest => pitfall/test}/test.pdf (100%) rename pitfall/{pktest => pitfall/test}/test0-uncompressed.pdf (100%) rename pitfall/{pktest => pitfall/test}/test0.coffee (100%) rename pitfall/{pktest => pitfall/test}/test0.pdf (100%) create mode 100644 pitfall/pitfall/test/test0.rkt rename pitfall/pitfall/{kit/out.pdf => test/test0rkt copy.pdf} (100%) rename pitfall/{pktest => pitfall/test}/test0rkt.pdf (100%) rename pitfall/{pktest => pitfall/test}/test1.coffee (100%) rename pitfall/{pktest => pitfall/test}/test1.pdf (100%) create mode 100644 pitfall/pitfall/test/test1.rkt rename pitfall/{pktest/test1rkt.pdf => pitfall/test/test1rkt copy.pdf} (100%) create mode 100644 pitfall/pitfall/test/test1rkt.pdf rename pitfall/{pktest => pitfall/test}/testrkt.rkt (100%) create mode 100644 pitfall/pitfall/test0rkt.pdf create mode 100644 pitfall/pitfall/test1rkt.pdf rename pitfall/pitfall/{kit => }/vector.rkt (58%) delete mode 100644 pitfall/pktest/test0.rkt delete mode 100644 pitfall/pktest/test1.rkt diff --git a/pitfall/pitfall/kit/buffer.rkt b/pitfall/pitfall/buffer.rkt similarity index 100% rename from pitfall/pitfall/kit/buffer.rkt rename to pitfall/pitfall/buffer.rkt diff --git a/pitfall/pitfall/kit/color.rkt b/pitfall/pitfall/color.rkt similarity index 100% rename from pitfall/pitfall/kit/color.rkt rename to pitfall/pitfall/color.rkt diff --git a/pitfall/pitfall/kit/document.rkt b/pitfall/pitfall/document.rkt similarity index 85% rename from pitfall/pitfall/kit/document.rkt rename to pitfall/pitfall/document.rkt index b51ee880..3c8e42ac 100644 --- a/pitfall/pitfall/kit/document.rkt +++ b/pitfall/pitfall/document.rkt @@ -42,7 +42,8 @@ ;; The current page (field [(@page page) #f]) - ;; other fields, hoisted from below (why is this necessary?) + ;; other fields, moved up from below + ;; (why is this necessary? order seems to matter) (field [(@x x) 0]) (field [(@y y) 0]) @@ -58,13 +59,13 @@ (field [(@info info) (mhash 'Producer "PitfallKit" 'Creator "PitfallKit" - 'CreationDate (seconds->date (if (current-debug) + 'CreationDate (seconds->date (if (test-mode) 0 (current-seconds)) #f))]) (when (hash-ref @options 'info #f) (for ([(key val) (in-hash (hash-ref @options 'info))]) - (hash-set! @info key val))) + (hash-set! @info key val))) ;; Write the header ;; PDF version @@ -158,9 +159,9 @@ (define/public (_refEnd ref) (set! @_offsets (for/list ([(offset idx) (in-indexed @_offsets)]) - (if (= (· ref id) (add1 idx)) - (· ref offset) - offset))) + (if (= (· ref id) (add1 idx)) + (· ref offset) + offset))) (-- @_waiting) (if (and (zero? @_waiting) @_ended) (@_finalize) @@ -175,8 +176,8 @@ (@flushPages) (set! @_info (@ref)) (for ([(key val) (in-hash @info)]) - ;; upgrade string literal to String struct - (hash-set! (· @_info data) key (if (string? val) (String val) val))) + ;; upgrade string literal to String struct + (hash-set! (· @_info data) key (if (string? val) (String val) val))) (· @_info end) ;; todo: fonts @@ -190,7 +191,7 @@ (@_finalize) (set! @_ended #t)) - 'done) + #t) (public (@_finalize _finalize)) (define (@_finalize [fn #f]) @@ -200,9 +201,9 @@ (@_write (format "0 ~a" (add1 (length @_offsets)))) (@_write "0000000000 65535 f ") (for ([offset (in-list @_offsets)]) - (@_write (string-append - (~r offset #:min-width 10 #:pad-string "0") - " 00000 n "))) + (@_write (string-append + (~r offset #:min-width 10 #:pad-string "0") + " 00000 n "))) ;; trailer (@_write "trailer") ;; todo: make `PDFObject:convert` a static method @@ -223,16 +224,16 @@ (close-output-port op)))) -(define doc (new PDFDocument)) -(module+ test - (require rackunit racket/file) - (define ob (open-output-bytes)) - (send doc pipe ob) - (check-equal? (send doc end) 'done) - (define result-str (get-output-bytes ob)) - (define fn "out") - (with-output-to-file (string-append fn ".pdf") - (λ () (display result-str)) #:exists 'replace) - (check-equal? (file->bytes (string-append fn ".pdf")) (file->bytes (string-append fn " copy.pdf"))) - (display (bytes->string/latin-1 result-str))) \ No newline at end of file +#;(module+ test + (define doc (new PDFDocument)) + (require rackunit racket/file) + (define ob (open-output-bytes)) + (send doc pipe ob) + (check-true (send doc end)) + (define result-str (get-output-bytes ob)) + (define fn "out") + (with-output-to-file (string-append fn ".pdf") + (λ () (display result-str)) #:exists 'replace) + (check-equal? (file->bytes (string-append fn ".pdf")) (file->bytes (string-append fn " copy.pdf"))) + (display (bytes->string/latin-1 result-str))) \ No newline at end of file diff --git a/pitfall/pitfall/kit/helper.rkt b/pitfall/pitfall/helper.rkt similarity index 92% rename from pitfall/pitfall/kit/helper.rkt rename to pitfall/pitfall/helper.rkt index 2b8f921e..f478dcbc 100644 --- a/pitfall/pitfall/kit/helper.rkt +++ b/pitfall/pitfall/helper.rkt @@ -106,4 +106,13 @@ (check-equal? (bounded 1 -2 0) 0) (check-equal? (bounded 0 .5 1) 0.5) (check-equal? (bounded 0 0 1) 0) - (check-equal? (bounded 0 1 1) 1)) \ No newline at end of file + (check-equal? (bounded 0 1 1) 1)) + + +(struct exn:pitfall:test exn (data)) + +(define (raise-test-exn val) + (raise (exn:pitfall:test "pitfall test exn" (current-continuation-marks) val))) + +(define-syntax-rule (test-when cond expr) + (if cond (raise-test-exn expr) expr)) diff --git a/pitfall/pitfall/kit/struct.rkt b/pitfall/pitfall/kit/struct.rkt deleted file mode 100644 index e60cabff..00000000 --- a/pitfall/pitfall/kit/struct.rkt +++ /dev/null @@ -1,6 +0,0 @@ -#lang br -(provide (struct-out String)) - -;; use structs to sub for missing node types - -(struct String (string) #:transparent) \ No newline at end of file diff --git a/pitfall/pitfall/kit/object.rkt b/pitfall/pitfall/object.rkt similarity index 100% rename from pitfall/pitfall/kit/object.rkt rename to pitfall/pitfall/object.rkt diff --git a/pitfall/pitfall/charter.otf b/pitfall/pitfall/old/charter.otf similarity index 100% rename from pitfall/pitfall/charter.otf rename to pitfall/pitfall/old/charter.otf diff --git a/pitfall/pitfall/fira.otf b/pitfall/pitfall/old/fira.otf similarity index 100% rename from pitfall/pitfall/fira.otf rename to pitfall/pitfall/old/fira.otf diff --git a/pitfall/pitfall/foo.rkt b/pitfall/pitfall/old/foo.rkt similarity index 100% rename from pitfall/pitfall/foo.rkt rename to pitfall/pitfall/old/foo.rkt diff --git a/pitfall/pitfall/freetype-ffi.rkt b/pitfall/pitfall/old/freetype-ffi.rkt similarity index 100% rename from pitfall/pitfall/freetype-ffi.rkt rename to pitfall/pitfall/old/freetype-ffi.rkt diff --git a/pitfall/pitfall/glyph-list.rkt b/pitfall/pitfall/old/glyph-list.rkt similarity index 100% rename from pitfall/pitfall/glyph-list.rkt rename to pitfall/pitfall/old/glyph-list.rkt diff --git a/pitfall/pitfall/info.rkt b/pitfall/pitfall/old/info.rkt similarity index 100% rename from pitfall/pitfall/info.rkt rename to pitfall/pitfall/old/info.rkt diff --git a/pitfall/pitfall/main.rkt b/pitfall/pitfall/old/main.rkt similarity index 100% rename from pitfall/pitfall/main.rkt rename to pitfall/pitfall/old/main.rkt diff --git a/pitfall/pitfall/measure.rkt b/pitfall/pitfall/old/measure.rkt similarity index 100% rename from pitfall/pitfall/measure.rkt rename to pitfall/pitfall/old/measure.rkt diff --git a/pitfall/pitfall/minimal-annotation-square.rkt b/pitfall/pitfall/old/minimal-annotation-square.rkt similarity index 100% rename from pitfall/pitfall/minimal-annotation-square.rkt rename to pitfall/pitfall/old/minimal-annotation-square.rkt diff --git a/pitfall/pitfall/minimal-annotation-text.rkt b/pitfall/pitfall/old/minimal-annotation-text.rkt similarity index 100% rename from pitfall/pitfall/minimal-annotation-text.rkt rename to pitfall/pitfall/old/minimal-annotation-text.rkt diff --git a/pitfall/pitfall/minimal-embedded-font.rkt b/pitfall/pitfall/old/minimal-embedded-font.rkt similarity index 100% rename from pitfall/pitfall/minimal-embedded-font.rkt rename to pitfall/pitfall/old/minimal-embedded-font.rkt diff --git a/pitfall/pitfall/minimal-measured-font.rkt b/pitfall/pitfall/old/minimal-measured-font.rkt similarity index 100% rename from pitfall/pitfall/minimal-measured-font.rkt rename to pitfall/pitfall/old/minimal-measured-font.rkt diff --git a/pitfall/pitfall/minimal-pdf-source.rkt b/pitfall/pitfall/old/minimal-pdf-source.rkt similarity index 100% rename from pitfall/pitfall/minimal-pdf-source.rkt rename to pitfall/pitfall/old/minimal-pdf-source.rkt diff --git a/pitfall/pitfall/minimal-pdf.rkt b/pitfall/pitfall/old/minimal-pdf.rkt similarity index 100% rename from pitfall/pitfall/minimal-pdf.rkt rename to pitfall/pitfall/old/minimal-pdf.rkt diff --git a/pitfall/pitfall/minimal.pdf b/pitfall/pitfall/old/minimal.pdf similarity index 100% rename from pitfall/pitfall/minimal.pdf rename to pitfall/pitfall/old/minimal.pdf diff --git a/pitfall/pitfall/miso.otf b/pitfall/pitfall/old/miso.otf similarity index 100% rename from pitfall/pitfall/miso.otf rename to pitfall/pitfall/old/miso.otf diff --git a/pitfall/pitfall/parse.rkt b/pitfall/pitfall/old/parse.rkt similarity index 100% rename from pitfall/pitfall/parse.rkt rename to pitfall/pitfall/old/parse.rkt diff --git a/pitfall/pitfall/parser-test.rkt b/pitfall/pitfall/old/parser-test.rkt similarity index 100% rename from pitfall/pitfall/parser-test.rkt rename to pitfall/pitfall/old/parser-test.rkt diff --git a/pitfall/pitfall/parser.rkt b/pitfall/pitfall/old/parser.rkt similarity index 100% rename from pitfall/pitfall/parser.rkt rename to pitfall/pitfall/old/parser.rkt diff --git a/pitfall/pitfall/render.rkt b/pitfall/pitfall/old/render.rkt similarity index 100% rename from pitfall/pitfall/render.rkt rename to pitfall/pitfall/old/render.rkt diff --git a/pitfall/pitfall/sample-ext-encoding.rkt b/pitfall/pitfall/old/sample-ext-encoding.rkt similarity index 100% rename from pitfall/pitfall/sample-ext-encoding.rkt rename to pitfall/pitfall/old/sample-ext-encoding.rkt diff --git a/pitfall/pitfall/sample-unicode-text.rkt b/pitfall/pitfall/old/sample-unicode-text.rkt similarity index 100% rename from pitfall/pitfall/sample-unicode-text.rkt rename to pitfall/pitfall/old/sample-unicode-text.rkt diff --git a/pitfall/pitfall/old/struct.rkt b/pitfall/pitfall/old/struct.rkt new file mode 100644 index 00000000..c870abd6 --- /dev/null +++ b/pitfall/pitfall/old/struct.rkt @@ -0,0 +1,16 @@ +#lang racket/base +(provide (all-defined-out)) + +(struct co-dict (dict) #:transparent) +(struct co-array (items) #:transparent) +(struct co-stream (dict data) #:transparent) +(struct co-version (num) #:transparent) +(struct co-header (string) #:transparent) +(struct co-string (string) #:transparent) +(struct co-io (idx rev thing) #:transparent) +(struct co-io-ref (idx rev) #:transparent) +(struct co-comment (text) #:transparent) +(struct co-trailer (dict) #:transparent) + +(struct co-hash (hash) #:transparent) +(struct co-encoding-datum (datum) #:transparent) \ No newline at end of file diff --git a/pitfall/pitfall/test.rkt b/pitfall/pitfall/old/test.rkt similarity index 100% rename from pitfall/pitfall/test.rkt rename to pitfall/pitfall/old/test.rkt diff --git a/pitfall/pitfall/tokenizer.rkt b/pitfall/pitfall/old/tokenizer.rkt similarity index 100% rename from pitfall/pitfall/tokenizer.rkt rename to pitfall/pitfall/old/tokenizer.rkt diff --git a/pitfall/pitfall/triple-measured-font.rkt b/pitfall/pitfall/old/triple-measured-font.rkt similarity index 100% rename from pitfall/pitfall/triple-measured-font.rkt rename to pitfall/pitfall/old/triple-measured-font.rkt diff --git a/pitfall/pitfall/kit/out copy.pdf b/pitfall/pitfall/out copy.pdf similarity index 100% rename from pitfall/pitfall/kit/out copy.pdf rename to pitfall/pitfall/out copy.pdf diff --git a/pitfall/pitfall/out.pdf b/pitfall/pitfall/out.pdf new file mode 100644 index 00000000..028a593f --- /dev/null +++ b/pitfall/pitfall/out.pdf @@ -0,0 +1,63 @@ +%PDF-1.3 +%ÿÿÿÿ +5 0 obj +<< +/Parent 1 0 R +/Resources 4 0 R +/Contents 3 0 R +/MediaBox [0 0 612 792] +/Type /Page +>> +endobj +4 0 obj +<< +/ProcSet [/PDF /Text /ImageB /ImageC /ImageI] +>> +endobj +3 0 obj +<< +/Length 18 +>> +stream +1 0 0 -1 0 792 cm + +endstream +endobj +6 0 obj +<< +/CreationDate (D:20170514163610Z) +/Creator (PitfallKit) +/Producer (PitfallKit) +>> +endobj +2 0 obj +<< +/Pages 1 0 R +/Type /Catalog +>> +endobj +1 0 obj +<< +/Kids [5 0 R] +/Count 1 +/Type /Pages +>> +endobj +xref +0 7 +0000000000 65535 f +0000000403 00000 n +0000000354 00000 n +0000000186 00000 n +0000000119 00000 n +0000000015 00000 n +0000000254 00000 n +trailer +<< +/Info 6 0 R +/Root 2 0 R +/Size 7 +>> +startxref +460 +%%EOF diff --git a/pitfall/pitfall/kit/page-test.rkt b/pitfall/pitfall/page-test.rkt similarity index 100% rename from pitfall/pitfall/kit/page-test.rkt rename to pitfall/pitfall/page-test.rkt diff --git a/pitfall/pitfall/kit/page.rkt b/pitfall/pitfall/page.rkt similarity index 100% rename from pitfall/pitfall/kit/page.rkt rename to pitfall/pitfall/page.rkt diff --git a/pitfall/pitfall/kit/params.rkt b/pitfall/pitfall/params.rkt similarity index 52% rename from pitfall/pitfall/kit/params.rkt rename to pitfall/pitfall/params.rkt index 1de9b8fe..f6ec2c38 100644 --- a/pitfall/pitfall/kit/params.rkt +++ b/pitfall/pitfall/params.rkt @@ -1,3 +1,3 @@ #lang racket/base (provide (all-defined-out)) -(define current-debug (make-parameter #t)) \ No newline at end of file +(define test-mode (make-parameter #f)) \ No newline at end of file diff --git a/pitfall/pitfall/kit/reference.rkt b/pitfall/pitfall/reference.rkt similarity index 100% rename from pitfall/pitfall/kit/reference.rkt rename to pitfall/pitfall/reference.rkt diff --git a/pitfall/pitfall/struct.rkt b/pitfall/pitfall/struct.rkt index c870abd6..e60cabff 100644 --- a/pitfall/pitfall/struct.rkt +++ b/pitfall/pitfall/struct.rkt @@ -1,16 +1,6 @@ -#lang racket/base -(provide (all-defined-out)) +#lang br +(provide (struct-out String)) -(struct co-dict (dict) #:transparent) -(struct co-array (items) #:transparent) -(struct co-stream (dict data) #:transparent) -(struct co-version (num) #:transparent) -(struct co-header (string) #:transparent) -(struct co-string (string) #:transparent) -(struct co-io (idx rev thing) #:transparent) -(struct co-io-ref (idx rev) #:transparent) -(struct co-comment (text) #:transparent) -(struct co-trailer (dict) #:transparent) +;; use structs to sub for missing node types -(struct co-hash (hash) #:transparent) -(struct co-encoding-datum (datum) #:transparent) \ No newline at end of file +(struct String (string) #:transparent) \ No newline at end of file diff --git a/pitfall/pitfall/test-helper.rkt b/pitfall/pitfall/test-helper.rkt new file mode 100644 index 00000000..8e62bcdd --- /dev/null +++ b/pitfall/pitfall/test-helper.rkt @@ -0,0 +1,7 @@ +#lang racket/base +(require rackunit pitfall/helper) +(provide (all-defined-out)) + +(define-syntax-rule (check-exn-equal? expr val) + (check-equal? (with-handlers ([exn:pitfall:test? (λ (e) (exn:pitfall:test-data e))]) + expr) val)) \ No newline at end of file diff --git a/pitfall/pktest/assets/charter.ttf b/pitfall/pitfall/test/assets/charter.ttf similarity index 100% rename from pitfall/pktest/assets/charter.ttf rename to pitfall/pitfall/test/assets/charter.ttf diff --git a/pitfall/pktest/assets/test.jpeg b/pitfall/pitfall/test/assets/test.jpeg similarity index 100% rename from pitfall/pktest/assets/test.jpeg rename to pitfall/pitfall/test/assets/test.jpeg diff --git a/pitfall/pktest/assets/test.png b/pitfall/pitfall/test/assets/test.png similarity index 100% rename from pitfall/pktest/assets/test.png rename to pitfall/pitfall/test/assets/test.png diff --git a/pitfall/pktest/assets/tiger.js b/pitfall/pitfall/test/assets/tiger.js similarity index 100% rename from pitfall/pktest/assets/tiger.js rename to pitfall/pitfall/test/assets/tiger.js diff --git a/pitfall/pktest/node_modules/pdfkit b/pitfall/pitfall/test/node_modules/pdfkit similarity index 100% rename from pitfall/pktest/node_modules/pdfkit rename to pitfall/pitfall/test/node_modules/pdfkit diff --git a/pitfall/pktest/outrkt.pdf b/pitfall/pitfall/test/outrkt.pdf similarity index 100% rename from pitfall/pktest/outrkt.pdf rename to pitfall/pitfall/test/outrkt.pdf diff --git a/pitfall/pitfall/test/test-all.rkt b/pitfall/pitfall/test/test-all.rkt new file mode 100644 index 00000000..562d844c --- /dev/null +++ b/pitfall/pitfall/test/test-all.rkt @@ -0,0 +1,3 @@ +#lang racket +(require pitfall/test/test0 + pitfall/test/test1) \ No newline at end of file diff --git a/pitfall/pktest/test.coffee b/pitfall/pitfall/test/test.coffee similarity index 100% rename from pitfall/pktest/test.coffee rename to pitfall/pitfall/test/test.coffee diff --git a/pitfall/pktest/test.pdf b/pitfall/pitfall/test/test.pdf similarity index 100% rename from pitfall/pktest/test.pdf rename to pitfall/pitfall/test/test.pdf diff --git a/pitfall/pktest/test0-uncompressed.pdf b/pitfall/pitfall/test/test0-uncompressed.pdf similarity index 100% rename from pitfall/pktest/test0-uncompressed.pdf rename to pitfall/pitfall/test/test0-uncompressed.pdf diff --git a/pitfall/pktest/test0.coffee b/pitfall/pitfall/test/test0.coffee similarity index 100% rename from pitfall/pktest/test0.coffee rename to pitfall/pitfall/test/test0.coffee diff --git a/pitfall/pktest/test0.pdf b/pitfall/pitfall/test/test0.pdf similarity index 100% rename from pitfall/pktest/test0.pdf rename to pitfall/pitfall/test/test0.pdf diff --git a/pitfall/pitfall/test/test0.rkt b/pitfall/pitfall/test/test0.rkt new file mode 100644 index 00000000..3355ef7e --- /dev/null +++ b/pitfall/pitfall/test/test0.rkt @@ -0,0 +1,14 @@ +#lang racket +(require pitfall/document pitfall/params rackunit) + +(test-mode #t) +(check-true + (let () + (define doc (new PDFDocument)) + (send doc pipe (open-output-file "test0rkt.pdf" #:exists 'replace)) + (send doc end))) + +(require racket/runtime-path) +(define-runtime-path this "test0rkt.pdf") +(define-runtime-path control "test0rkt copy.pdf") +(check-equal? (file->bytes this) (file->bytes control)) \ No newline at end of file diff --git a/pitfall/pitfall/kit/out.pdf b/pitfall/pitfall/test/test0rkt copy.pdf similarity index 100% rename from pitfall/pitfall/kit/out.pdf rename to pitfall/pitfall/test/test0rkt copy.pdf diff --git a/pitfall/pktest/test0rkt.pdf b/pitfall/pitfall/test/test0rkt.pdf similarity index 100% rename from pitfall/pktest/test0rkt.pdf rename to pitfall/pitfall/test/test0rkt.pdf diff --git a/pitfall/pktest/test1.coffee b/pitfall/pitfall/test/test1.coffee similarity index 100% rename from pitfall/pktest/test1.coffee rename to pitfall/pitfall/test/test1.coffee diff --git a/pitfall/pktest/test1.pdf b/pitfall/pitfall/test/test1.pdf similarity index 100% rename from pitfall/pktest/test1.pdf rename to pitfall/pitfall/test/test1.pdf diff --git a/pitfall/pitfall/test/test1.rkt b/pitfall/pitfall/test/test1.rkt new file mode 100644 index 00000000..fa1e2e9c --- /dev/null +++ b/pitfall/pitfall/test/test1.rkt @@ -0,0 +1,27 @@ +#lang racket + +(require pitfall/document pitfall/helper pitfall/params rackunit) + +(require racket/runtime-path) +(define-runtime-path this "test1rkt.pdf") +(define-runtime-path control "test1rkt copy.pdf") + +;; Create a new PDFDocument +(test-mode #t) +(check-true + (let () + (define doc (new PDFDocument)) + (send doc pipe (open-output-file this #:exists 'replace)) + + ;; Draw a triangle and a circle + (send*/fold doc [save] + [moveTo 100 150] + [lineTo 100 250] + [lineTo 200 250] + [fill "#FF3300"]) + + (send*/fold doc [circle 280 200 50] [fill "#6600FF"]) + + (send doc end))) + +(check-equal? (file->bytes this) (file->bytes control)) \ No newline at end of file diff --git a/pitfall/pktest/test1rkt.pdf b/pitfall/pitfall/test/test1rkt copy.pdf similarity index 100% rename from pitfall/pktest/test1rkt.pdf rename to pitfall/pitfall/test/test1rkt copy.pdf diff --git a/pitfall/pitfall/test/test1rkt.pdf b/pitfall/pitfall/test/test1rkt.pdf new file mode 100644 index 00000000..f1c5089b --- /dev/null +++ b/pitfall/pitfall/test/test1rkt.pdf @@ -0,0 +1,91 @@ +%PDF-1.3 +%ÿÿÿÿ +6 0 obj +<< +/ca 1 +/Type /ExtGState +>> +endobj +5 0 obj +<< +/Parent 1 0 R +/Resources 4 0 R +/Contents 3 0 R +/MediaBox [0 0 612 792] +/Type /Page +>> +endobj +4 0 obj +<< +/ExtGState << +/Gs1 6 0 R +>> +/ProcSet [/PDF /Text /ImageB /ImageC /ImageI] +>> +endobj +3 0 obj +<< +/Length 294 +>> +stream +1 0 0 -1 0 792 cm +q +100 150 m +100 250 l +200 250 l +/DeviceRGB cs +1 0.2 0 scn +/Gs1 gs +f +230 200 m +230 172.385763 252.385763 150 280 150 c +307.614237 150 330 172.385763 330 200 c +330 227.614237 307.614237 250 280 250 c +252.385763 250 230 227.614237 230 200 c +h +/DeviceRGB cs +0.4 0 1 scn +/Gs1 gs +f + +endstream +endobj +7 0 obj +<< +/CreationDate (D:19700101000000Z) +/Creator (PitfallKit) +/Producer (PitfallKit) +>> +endobj +2 0 obj +<< +/Pages 1 0 R +/Type /Catalog +>> +endobj +1 0 obj +<< +/Kids [5 0 R] +/Count 1 +/Type /Pages +>> +endobj +xref +0 8 +0000000000 65535 f +0000000752 00000 n +0000000703 00000 n +0000000258 00000 n +0000000163 00000 n +0000000059 00000 n +0000000015 00000 n +0000000603 00000 n +trailer +<< +/Info 7 0 R +/Root 2 0 R +/Size 8 +>> +startxref +809 +%%EOF diff --git a/pitfall/pktest/testrkt.rkt b/pitfall/pitfall/test/testrkt.rkt similarity index 100% rename from pitfall/pktest/testrkt.rkt rename to pitfall/pitfall/test/testrkt.rkt diff --git a/pitfall/pitfall/test0rkt.pdf b/pitfall/pitfall/test0rkt.pdf new file mode 100644 index 00000000..f84357ef --- /dev/null +++ b/pitfall/pitfall/test0rkt.pdf @@ -0,0 +1,63 @@ +%PDF-1.3 +%ÿÿÿÿ +5 0 obj +<< +/Parent 1 0 R +/Resources 4 0 R +/Contents 3 0 R +/MediaBox [0 0 612 792] +/Type /Page +>> +endobj +4 0 obj +<< +/ProcSet [/PDF /Text /ImageB /ImageC /ImageI] +>> +endobj +3 0 obj +<< +/Length 18 +>> +stream +1 0 0 -1 0 792 cm + +endstream +endobj +6 0 obj +<< +/CreationDate (D:19700101000000Z) +/Creator (PitfallKit) +/Producer (PitfallKit) +>> +endobj +2 0 obj +<< +/Pages 1 0 R +/Type /Catalog +>> +endobj +1 0 obj +<< +/Kids [5 0 R] +/Count 1 +/Type /Pages +>> +endobj +xref +0 7 +0000000000 65535 f +0000000403 00000 n +0000000354 00000 n +0000000186 00000 n +0000000119 00000 n +0000000015 00000 n +0000000254 00000 n +trailer +<< +/Info 6 0 R +/Root 2 0 R +/Size 7 +>> +startxref +460 +%%EOF diff --git a/pitfall/pitfall/test1rkt.pdf b/pitfall/pitfall/test1rkt.pdf new file mode 100644 index 00000000..6e454228 --- /dev/null +++ b/pitfall/pitfall/test1rkt.pdf @@ -0,0 +1,91 @@ +%PDF-1.3 +%ÿÿÿÿ +6 0 obj +<< +/ca 1 +/Type /ExtGState +>> +endobj +5 0 obj +<< +/Parent 1 0 R +/Resources 4 0 R +/Contents 3 0 R +/MediaBox [0 0 612 792] +/Type /Page +>> +endobj +4 0 obj +<< +/ExtGState << +/Gs1 6 0 R +>> +/ProcSet [/PDF /Text /ImageB /ImageC /ImageI] +>> +endobj +3 0 obj +<< +/Length 238 +>> +stream +1 0 0 -1 0 792 cm +q +100 150 m +100 250 l +200 250 l +/DeviceRGB cs +1 0.2 0 scn +/Gs1 gs +f +230 200 m +230 150 230 150 280 150 c +330 150 330 150 330 200 c +330 250 330 250 280 250 c +230 250 230 250 230 200 c +h +/DeviceRGB cs +0.4 0 1 scn +/Gs1 gs +f + +endstream +endobj +7 0 obj +<< +/CreationDate (D:19700101000000Z) +/Creator (PitfallKit) +/Producer (PitfallKit) +>> +endobj +2 0 obj +<< +/Pages 1 0 R +/Type /Catalog +>> +endobj +1 0 obj +<< +/Kids [5 0 R] +/Count 1 +/Type /Pages +>> +endobj +xref +0 8 +0000000000 65535 f +0000000696 00000 n +0000000647 00000 n +0000000258 00000 n +0000000163 00000 n +0000000059 00000 n +0000000015 00000 n +0000000547 00000 n +trailer +<< +/Info 7 0 R +/Root 2 0 R +/Size 8 +>> +startxref +753 +%%EOF diff --git a/pitfall/pitfall/kit/vector.rkt b/pitfall/pitfall/vector.rkt similarity index 58% rename from pitfall/pitfall/kit/vector.rkt rename to pitfall/pitfall/vector.rkt index c89b8189..1ee4b26f 100644 --- a/pitfall/pitfall/kit/vector.rkt +++ b/pitfall/pitfall/vector.rkt @@ -1,17 +1,39 @@ #lang racket/base -(require racket/class racket/match racket/string racket/format) -(provide vector-mixin) -(require "helper.rkt") +(require racket/class racket/match racket/string racket/format racket/contract) +(require "helper.rkt" "params.rkt") +(provide (contract-out + [vector-mixin (class? . -> . + (class/c [initVector (->m void?)] + [save (->m object?)] + [restore (->m object?)] + [closePath (->m object?)] + [moveTo (number? number? . ->m . object?)] + [lineTo (number? number? . ->m . object?)] + [bezierCurveTo ( number? number? number? number? number? number? . ->m . object?)] + [ellipse ((number? number? number?) (number?) . ->*m . object?)] + [circle (number? number? number? . ->m . object?)] + [_windingRule (string? . ->m . string?)] + [fill ((string?) ((or/c string? #f)) . ->*m . object?)] + [transform ((number? number? number? number? number? number?) (#:debug boolean?) . ->*m . (or/c object? string?))] + [translate (number? number? . ->m . object?)] + [scale (case->m + (number? . -> . object?) + (number? hash? . -> . object?) + (number? number? . -> . object?) + (number? number? hash? . -> . object?))]))])) + ;; This constant is used to approximate a symmetrical arc using a cubic ;; Bezier curve. -(define KAPPA (* 4 (/ (- (sqrt 2) 1) 3.0))) +(define kappa (* 4 (/ (- (sqrt 2) 1) 3.0))) (define default-ctm-value '(1 0 0 1 0 0)) + (define (vector-mixin %) (class % (super-new) + (init-field [(@test-mode test-mode) #f]) (field [(@_ctm _ctm) default-ctm-value] [(@_ctmStack _ctmStack) null]) @@ -27,6 +49,7 @@ (set! @_ctm (if (pair? @_ctmStack) (pop! @_ctmStack) default-ctm-value)) (send this addContent "Q")) + (public [@closePath closePath]) (define (@closePath) (send this addContent "h")) @@ -34,7 +57,6 @@ (define (@moveTo x y) (send this addContent (format "~a ~a m" x y))) - (public [@lineTo lineTo]) (define (@lineTo x y) (send this addContent (format "~a ~a l" x y))) @@ -51,12 +73,12 @@ ;; based on http://stackoverflow.com/questions/2172798/how-to-draw-an-oval-in-html5-canvas/2173084#2173084 (-= x r1) (-= y r2) - (define ox (* r1 KAPPA)) - (define oy (* r2 KAPPA)) - (define xe (+ x (* r1 2))) - (define ye (+ y (* r2 2))) - (define xm (+ x r1)) - (define ym (+ y r2)) + (define ox (* r1 kappa)) ; control point offset horizontal + (define oy (* r2 kappa)) ; control point offset vertical + (define xe (+ x (* r1 2))) ; x-end + (define ye (+ y (* r2 2))) ; y-end + (define xm (+ x r1)) ; x-middle + (define ym (+ y r2)) ; y-middle (@moveTo x ym) (@bezierCurveTo x (- ym oy) (- xm ox) y xm y) (@bezierCurveTo (+ xm ox) y xe (- ym oy) xe ym) @@ -77,9 +99,7 @@ (public [@_windingRule _windingRule]) (define (@_windingRule rule) - (if (and (string? rule) (regexp-match #rx"^even-?odd$" rule)) - "*" - "")) + (if (and (string? rule) (regexp-match #rx"^even-?odd$" rule)) "*" "")) (define/public (fill color [rule #f]) (when (regexp-match #rx"^(even-?odd)|(non-?zero)$" color) @@ -101,9 +121,8 @@ (+ (* m1 dx) (* m3 dy) m5))) (define values (string-join (map number (list m11 m12 m21 m22 dx dy)) " ")) (define result (format "~a cm" values)) - (if debug - result - (send this addContent result))) + (test-when @test-mode result) + (send this addContent result)) (public [@translate translate]) @@ -121,17 +140,16 @@ (match-let ([(list xo yo) (hash-ref options 'origin '(0 0))]) (list (* xo (- 1 xFactor)) (* yo (- 1 yFactor))))) (@transform xFactor 0 0 yFactor x y)])) - - + )) (module+ test - (require rackunit) - (define v (new (vector-mixin object%))) - (check-equal? (· v _ctm) default-ctm-value) - (check-equal? (send v transform 1 2 3 4 5 6 #:debug #t) "1 2 3 4 5 6 cm") + (require rackunit pitfall/test-helper) + (define v (make-object (vector-mixin object%) #t)) + (check-equal? (· v _ctm) default-ctm-value) + (check-exn-equal? (send v transform 1 2 3 4 5 6 #:debug #t) "1 2 3 4 5 6 cm") (check-equal? (· v _ctm) '(1 2 3 4 5 6)) - (check-equal? (send v transform 1 2 3 4 5 6 #:debug #t) "1 2 3 4 5 6 cm") + (check-exn-equal? (send v transform 1 2 3 4 5 6 #:debug #t) "1 2 3 4 5 6 cm") (check-equal? (· v _ctm) '(7 10 15 22 28 40)) - (check-equal? (send v transform 1 2 3 4 5 6 #:debug #t) "1 2 3 4 5 6 cm") + (check-exn-equal? (send v transform 1 2 3 4 5 6 #:debug #t) "1 2 3 4 5 6 cm") (check-equal? (· v _ctm) '(37 54 81 118 153 222))) \ No newline at end of file diff --git a/pitfall/pktest/test0.rkt b/pitfall/pktest/test0.rkt deleted file mode 100644 index 1df60153..00000000 --- a/pitfall/pktest/test0.rkt +++ /dev/null @@ -1,7 +0,0 @@ -#lang br - -(require pitfall/kit/document) - -(define doc (new PDFDocument)) -(send doc pipe (open-output-file "test0rkt.pdf" #:exists 'replace)) -(send doc end) \ No newline at end of file diff --git a/pitfall/pktest/test1.rkt b/pitfall/pktest/test1.rkt deleted file mode 100644 index 5829a16a..00000000 --- a/pitfall/pktest/test1.rkt +++ /dev/null @@ -1,18 +0,0 @@ -#lang br - -(require pitfall/kit/document pitfall/kit/helper) - -;; Create a new PDFDocument -(define doc (new PDFDocument)) -(send doc pipe (open-output-file "test1rkt.pdf" #:exists 'replace)) - -;; Draw a triangle and a circle -(send*/fold doc [save] - [moveTo 100 150] - [lineTo 100 250] - [lineTo 200 250] - [fill "#FF3300"]) - -(send*/fold doc [circle 280 200 50] [fill "#6600FF"]) - -(send doc end) \ No newline at end of file