From 571a3f31c3d316b53b692082111cea5226a32808 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 13 May 2017 11:47:30 -0700 Subject: [PATCH] tidying --- pitfall/pitfall/kit/document.rkt | 27 ++++---- pitfall/pitfall/kit/helper.rkt | 5 +- .../pitfall/kit/{demo.pdf => out copy.pdf} | 10 +-- pitfall/pitfall/kit/out.pdf | 63 +++++++++++++++++++ pitfall/pitfall/kit/params.rkt | 3 + pitfall/pitfall/kit/{mixins => }/vector.rkt | 0 6 files changed, 90 insertions(+), 18 deletions(-) rename pitfall/pitfall/kit/{demo.pdf => out copy.pdf} (88%) create mode 100644 pitfall/pitfall/kit/out.pdf create mode 100644 pitfall/pitfall/kit/params.rkt rename pitfall/pitfall/kit/{mixins => }/vector.rkt (100%) diff --git a/pitfall/pitfall/kit/document.rkt b/pitfall/pitfall/kit/document.rkt index 3ee97160..c5dc604d 100644 --- a/pitfall/pitfall/kit/document.rkt +++ b/pitfall/pitfall/kit/document.rkt @@ -1,10 +1,10 @@ -#lang racket/base +#lang sugar/debug racket/base (require racket/class racket/draw br/list racket/list racket/format racket/port) (require sugar/debug) (provide PDFDocument) -(require "reference.rkt" "struct.rkt" "object.rkt" "page.rkt" "helper.rkt") -(require "mixins/vector.rkt") +(require "reference.rkt" "struct.rkt" "object.rkt" "page.rkt" "helper.rkt" "params.rkt") +(require "vector.rkt") (define PDFDocument ;; actually is an instance of readable.Stream, which is an input port @@ -58,7 +58,9 @@ (field [(@info info) (mhash 'Producer "PitfallKit" 'Creator "PitfallKit" - 'CreationDate #;(seconds->date (current-seconds)) 0)]) ; debug val + 'CreationDate (seconds->date (if (current-debug) + 0 + (current-seconds)) #f))]) (when (hash-ref @options 'info #f) (for ([(key val) (in-hash (hash-ref @options 'info))]) @@ -100,7 +102,7 @@ ;; create a page object (set! @page (make-object PDFPage this options)) - (push-end @_pageBuffer @page) + (push-end! @_pageBuffer @page) ;; add the page to the object store (define pages (· @_root data Pages data)) (hash-update! pages 'Kids (λ (val) (cons (· @page dictionary) val)) null) @@ -131,9 +133,9 @@ ;; so even a function defined without default values ;; can be called without arguments (public [@ref ref]) - (define (@ref [data (make-hasheq)]) + (define (@ref [data (mhash)]) (define ref (make-object PDFReference this (add1 (length @_offsets)) data)) - (push-end @_offsets #f) ; placeholder for this object's offset once it is finalized + (push-end! @_offsets #f) ; placeholder for this object's offset once it is finalized (++ @_waiting) ref) @@ -184,7 +186,7 @@ (· @_root end) (· @_root data Pages end) - (if (or (zero? @_waiting) 'debug) + (if (zero? @_waiting) (@_finalize) (set! @_ended #t)) @@ -227,7 +229,10 @@ (require rackunit racket/file) (define ob (open-output-bytes)) (send doc pipe ob) - #;(send doc pipe (open-output-file "demo.pdf" #:exists 'replace)) (check-equal? (send doc end) 'done) - #;(display (get-output-bytes ob)) - (check-equal? (get-output-bytes ob) (file->bytes "demo.pdf"))) \ No newline at end of file + (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/kit/helper.rkt index 7ec75985..d2be36fb 100644 --- a/pitfall/pitfall/kit/helper.rkt +++ b/pitfall/pitfall/kit/helper.rkt @@ -56,9 +56,10 @@ (define (newBuffer x) (string->bytes/latin-1 (format "~a" x))) (define buffer-length bytes-length) -(define-syntax-rule (push-end id thing) (set! id (append id (list thing)))) +;; js-style `push`, which appends to end of list +(define-syntax-rule (push-end! id thing) (set! id (append id (list thing)))) (module+ test (define xs '(1 2 3)) - (push-end xs 4) + (push-end! xs 4) (check-equal? xs '(1 2 3 4))) \ No newline at end of file diff --git a/pitfall/pitfall/kit/demo.pdf b/pitfall/pitfall/kit/out copy.pdf similarity index 88% rename from pitfall/pitfall/kit/demo.pdf rename to pitfall/pitfall/kit/out copy.pdf index 2b72418f..f84357ef 100644 --- a/pitfall/pitfall/kit/demo.pdf +++ b/pitfall/pitfall/kit/out copy.pdf @@ -25,9 +25,9 @@ endstream endobj 6 0 obj << -/Producer (PitfallKit) -/CreationDate 0 +/CreationDate (D:19700101000000Z) /Creator (PitfallKit) +/Producer (PitfallKit) >> endobj 2 0 obj @@ -46,8 +46,8 @@ endobj xref 0 7 0000000000 65535 f -0000000385 00000 n -0000000336 00000 n +0000000403 00000 n +0000000354 00000 n 0000000186 00000 n 0000000119 00000 n 0000000015 00000 n @@ -59,5 +59,5 @@ trailer /Size 7 >> startxref -442 +460 %%EOF diff --git a/pitfall/pitfall/kit/out.pdf b/pitfall/pitfall/kit/out.pdf new file mode 100644 index 00000000..f84357ef --- /dev/null +++ b/pitfall/pitfall/kit/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: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/kit/params.rkt b/pitfall/pitfall/kit/params.rkt new file mode 100644 index 00000000..1de9b8fe --- /dev/null +++ b/pitfall/pitfall/kit/params.rkt @@ -0,0 +1,3 @@ +#lang racket/base +(provide (all-defined-out)) +(define current-debug (make-parameter #t)) \ No newline at end of file diff --git a/pitfall/pitfall/kit/mixins/vector.rkt b/pitfall/pitfall/kit/vector.rkt similarity index 100% rename from pitfall/pitfall/kit/mixins/vector.rkt rename to pitfall/pitfall/kit/vector.rkt