diff --git a/pitfall/pitfall/embedded.rkt b/pitfall/pitfall/embedded.rkt index 1d2b1a75..4ba27a25 100644 --- a/pitfall/pitfall/embedded.rkt +++ b/pitfall/pitfall/embedded.rkt @@ -30,15 +30,17 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee embed toUnicodeCmap)) -(define/contract (widthOfString this string size [features #f]) - ((string? number?) ((option/c list?)) . ->*m . number?) - (cond - [features ; #f disables features ; null enables default features ; list adds features - (define run (send (· this font) layout string features)) - (define width (· run advanceWidth)) - (define scale (/ size (+ (· this font unitsPerEm) 0.0))) - (* width scale)] - [else (send (· this font) measure-string string size)])) +(define width-cache (make-hash)) +(define (widthOfString this string size [features #f]) + ((string? number?) ((option/c (listof symbol?))) . ->*m . number?) + ; #f disables features ; null enables default features ; list adds features + (hash-ref! width-cache + (list string size (and features (sort features symbolm void?) @@ -104,7 +106,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee ;; generate a random tag (6 uppercase letters. 65 is the char code for 'A') (when (test-mode) (random-seed 0)) (define tag (list->string (for/list ([i (in-range 6)]) - (integer->char (random 65 (+ 65 26)))))) + (integer->char (random 65 (+ 65 26)))))) (define name (string-append tag "+" (· this font postscriptName))) (define bbox (· this font bbox)) @@ -143,7 +145,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee 'Supplement 0) 'FontDescriptor descriptor 'W (list 0 (for/list ([idx (in-range (length (hash-keys (· this widths))))]) - (hash-ref (· this widths) idx (λ () (error 'embed (format "hash key ~a not found" idx))))))))) + (hash-ref (· this widths) idx (λ () (error 'embed (format "hash key ~a not found" idx))))))))) (· descendantFont end) #;(report (· descendantFont toString) 'descendantFont) @@ -163,12 +165,12 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee (define cmap (· this document ref)) (define entries (for/list ([idx (in-range (length (hash-keys (· this unicode))))]) - (define codePoints (hash-ref (· this unicode) idx)) - (define encoded ; encode codePoints to utf16 - ;; todo: full utf16 support. for now just utf8 - (for/list ([value (in-list codePoints)]) - (toHex value))) - (format "<~a>" (string-join encoded " ")))) + (define codePoints (hash-ref (· this unicode) idx)) + (define encoded ; encode codePoints to utf16 + ;; todo: full utf16 support. for now just utf8 + (for/list ([value (in-list codePoints)]) + (toHex value))) + (format "<~a>" (string-join encoded " ")))) (define unicode-cmap-str #<*m . string?) (string-append* (for/list ([code (in-list codePoints)]) - (~r code #:base 16 #:min-width 4 #:pad-string "0")))) + (~r code #:base 16 #:min-width 4 #:pad-string "0")))) (module+ test diff --git a/pitfall/pitfall/text.rkt b/pitfall/pitfall/text.rkt index 5bc724c3..967066a4 100644 --- a/pitfall/pitfall/text.rkt +++ b/pitfall/pitfall/text.rkt @@ -67,7 +67,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee (error 'unimplemented-branch-of-_text)] ; todo [else ; render paragraphs as single lines (for ([line (in-list (string-split text "\n"))]) - (lineCallback line options))])) + (lineCallback line options))])) this) @@ -93,7 +93,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee (when (· this _textOptions) (for ([(key val) (in-hash (· this _textOptions))] #:unless (equal? (key "continued"))) - (hash-ref! options key val))) + (hash-ref! options key val))) ;; Update the current position (when x (set-field! x this x)) @@ -117,8 +117,8 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee ;; 180325 suppress the size tracking: we'll do our own line measurement ;; 181120 unsuppress the size tracking for now because it breaks test 04 (if (not wrapper) - (increment-field! x this (send this widthOfString text)) - (increment-field! y (+ (send this currentLineHeight #t) lineGap))) + (increment-field! x this (send this widthOfString text)) + (increment-field! y (+ (send this currentLineHeight #t) lineGap))) (void)) @@ -197,6 +197,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee ;; since the normal Tw operator only works on character code 32, which isn't ;; used for embedded fonts. ;; 180321: the first call to this operation is very slow from Quad + ;; 181126: because `encode` calls `layout` (match-define (list encoded-char-strs positions) (if (not (zero? wordSpacing)) (error 'unimplemented-brach) ; todo diff --git a/pitfall/ptest/out.bin b/pitfall/ptest/out.bin index 20df7521..cedbe02c 100644 Binary files a/pitfall/ptest/out.bin and b/pitfall/ptest/out.bin differ diff --git a/pitfall/ptest/test0.pdf b/pitfall/ptest/test0.pdf index eaec4037..624d6d2e 100644 --- a/pitfall/ptest/test0.pdf +++ b/pitfall/ptest/test0.pdf @@ -27,7 +27,7 @@ endobj << /Producer (PDFKit) /Creator (PDFKit) -/CreationDate (D:20181120220342Z) +/CreationDate (D:20181126220022Z) >> endobj 2 0 obj diff --git a/pitfall/ptest/test0c.pdf b/pitfall/ptest/test0c.pdf index 2be69d7c..46b6aa1c 100644 Binary files a/pitfall/ptest/test0c.pdf and b/pitfall/ptest/test0c.pdf differ diff --git a/pitfall/ptest/test18.pdf b/pitfall/ptest/test18.pdf index ebaa46e6..c71736ed 100644 Binary files a/pitfall/ptest/test18.pdf and b/pitfall/ptest/test18.pdf differ diff --git a/pitfall/ptest/test18c.pdf b/pitfall/ptest/test18c.pdf index 64adef12..df0b1424 100644 Binary files a/pitfall/ptest/test18c.pdf and b/pitfall/ptest/test18c.pdf differ diff --git a/pitfall/ptest/test4crkt.pdf b/pitfall/ptest/test4crkt.pdf index 7f8afb77..e69de29b 100644 Binary files a/pitfall/ptest/test4crkt.pdf and b/pitfall/ptest/test4crkt.pdf differ