diff --git a/pitfall/pdfkit/lib/~object.js b/pitfall/pdfkit/lib/~object.js deleted file mode 100644 index e69de29b..00000000 diff --git a/pitfall/pitfall/kit/buffer.rkt b/pitfall/pitfall/kit/buffer.rkt new file mode 100644 index 00000000..51af2e68 --- /dev/null +++ b/pitfall/pitfall/kit/buffer.rkt @@ -0,0 +1,25 @@ +#lang br + +;; nodejs Buffer object = Racket byte string + +(define Buffer + (class object% + (super-new) + + (init-field [bstr #""]) + + (define/public (isBuffer x) + (is-a? x Buffer)) + + (define/public (length) + (bytes-length bstr)))) + +(define isBuffer (generic Buffer isBuffer)) + + +(module+ test + (require rackunit) + (define good-buffer (make-object Buffer #"foo")) + (check-true (send-generic good-buffer isBuffer good-buffer)) + (check-false (send-generic (new Buffer) isBuffer "foo"))) + diff --git a/pitfall/pitfall/kit/object.rkt b/pitfall/pitfall/kit/object.rkt index 7e08ad66..2d76d249 100644 --- a/pitfall/pitfall/kit/object.rkt +++ b/pitfall/pitfall/kit/object.rkt @@ -1,18 +1,38 @@ -#lang br - -(define (string-slice str length) - (let loop ([length length]) - (if (negative? length) - (loop (+ (string-length str) length)) - (substring str length (string-length str))))) +#lang at-exp br (define PDFObject (class object% (super-new) + (define (string-slice str length) + (if (negative? length) + (string-slice str (+ (string-length str) length)) + (substring str length))) + (define/public (pad str length) (define newstr (string-append (string-join (make-list (add1 length) "") "0") str)) - (string-slice newstr (- length))))) + (string-slice newstr (- length))) + + (define escaped-chars '(#\newline #\return #\tab #\backspace #\page #\( #\) #\\)) + (define escaped-char-strings '("\\n" "\\r" "\\t" "\\b" "\\f" "\\(" "\\)" "\\\\")) + + (field [escapableRe + (regexp (format "[~a]" (regexp-quote (list->string escaped-chars))))]) + + (field [escapable (for/hash ([k (in-list escaped-chars)] + [v (in-list escaped-char-strings)]) + (values (string k) v))]) + + ; Convert little endian UTF-16 to big endian + (define/public (swapBytes buff) + (define bufflen (bytes-length buff)) + (when (odd? bufflen) + (raise-argument-error 'swapBytes "even number of bytes" (bytes-length buff))) + (define newbuff (make-bytes bufflen)) + (for ([bidx (in-range 0 bufflen 2)]) + (bytes-set! newbuff bidx (bytes-ref buff (add1 bidx))) + (bytes-set! newbuff (add1 bidx) (bytes-ref buff bidx))) + newbuff))) (module+ test @@ -22,4 +42,12 @@ (check-equal? (send o pad "foobar" 0) "foobar") (check-equal? (send o pad "foobar" 3) "bar") (check-equal? (send o pad "foobar" 6) "foobar") - (check-equal? (send o pad "foobar" 10) "0000foobar")) \ No newline at end of file + (check-equal? (send o pad "foobar" 10) "0000foobar") + + (check-equal? (regexp-replace* (get-field escapableRe o) "foo\nba\nr" "x") "fooxbaxr") + (check-equal? (regexp-replace* (get-field escapableRe o) "foo\fba\tr" "x") "fooxbaxr") + + (check-equal? (regexp-replace* (get-field escapableRe o) "foo\nba\tr" (λ (c) (hash-ref (get-field escapable o) c))) "foo\\nba\\tr") + + (check-equal? (send o swapBytes #"foobar") #"ofbora")) + diff --git a/pitfall/pktest/test.pdf b/pitfall/pktest/test.pdf index 72e4facb..bdda8f24 100644 Binary files a/pitfall/pktest/test.pdf and b/pitfall/pktest/test.pdf differ