main
Matthew Butterick 6 years ago
parent 261ecddc95
commit a111263513

@ -30,15 +30,17 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
embed embed
toUnicodeCmap)) toUnicodeCmap))
(define/contract (widthOfString this string size [features #f]) (define width-cache (make-hash))
((string? number?) ((option/c list?)) . ->*m . number?) (define (widthOfString this string size [features #f])
(cond ((string? number?) ((option/c (listof symbol?))) . ->*m . number?)
[features ; #f disables features ; null enables default features ; list adds features ; #f disables features ; null enables default features ; list adds features
(define run (send (· this font) layout string features)) (hash-ref! width-cache
(define width (· run advanceWidth)) (list string size (and features (sort features symbol<?)))
(define scale (/ size (+ (· this font unitsPerEm) 0.0))) (λ ()
(* width scale)] (define run (send (· this font) layout string features))
[else (send (· this font) measure-string string size)])) (define width (· run advanceWidth))
(define scale (/ size (+ (· this font unitsPerEm) 0.0)))
(* width scale))))
;; called from text.rkt ;; called from text.rkt
@ -49,14 +51,14 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
(define glyphRun (send (· this font) layout text features)) (define glyphRun (send (· this font) layout text features))
(define glyphs (· glyphRun glyphs)) (define glyphs (· glyphRun glyphs))
(for ([g (in-list glyphs)]) (for ([g (in-list glyphs)])
(· g id)) (· g id))
(define positions (· glyphRun positions)) (define positions (· glyphRun positions))
#;(report/file (for/list ([p (in-list positions)]) #;(report/file (for/list ([p (in-list positions)])
(list (· p xAdvance) (· p xOffset)))) (list (· p xAdvance) (· p xOffset))))
(define-values (subset-idxs new-positions) (define-values (subset-idxs new-positions)
(for/lists (idxs posns) (for/lists (idxs posns)
([(glyph i) (in-indexed glyphs)] ([(glyph i) (in-indexed glyphs)]
[posn (in-list positions)]) [posn (in-list positions)])
(define gid (send (· this subset) includeGlyph (· glyph id))) (define gid (send (· this subset) includeGlyph (· glyph id)))
(define subset-idx (toHex gid)) (define subset-idx (toHex gid))
(set-field! advanceWidth posn (· glyph advanceWidth)) (set-field! advanceWidth posn (· glyph advanceWidth))
@ -73,7 +75,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
#'(for/sum ([c (in-list (list COND ...))] #'(for/sum ([c (in-list (list COND ...))]
[v (in-list (list VAL ...))] [v (in-list (list VAL ...))]
#:when c) #:when c)
v)) v))
(define/contract (embed this) (define/contract (embed this)
(->m void?) (->m 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') ;; generate a random tag (6 uppercase letters. 65 is the char code for 'A')
(when (test-mode) (random-seed 0)) (when (test-mode) (random-seed 0))
(define tag (list->string (for/list ([i (in-range 6)]) (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 name (string-append tag "+" (· this font postscriptName)))
(define bbox (· this font bbox)) (define bbox (· this font bbox))
@ -143,7 +145,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
'Supplement 0) 'Supplement 0)
'FontDescriptor descriptor 'FontDescriptor descriptor
'W (list 0 (for/list ([idx (in-range (length (hash-keys (· this widths))))]) '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) (· descendantFont end)
#;(report (· descendantFont toString) 'descendantFont) #;(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 cmap (· this document ref))
(define entries (define entries
(for/list ([idx (in-range (length (hash-keys (· this unicode))))]) (for/list ([idx (in-range (length (hash-keys (· this unicode))))])
(define codePoints (hash-ref (· this unicode) idx)) (define codePoints (hash-ref (· this unicode) idx))
(define encoded ; encode codePoints to utf16 (define encoded ; encode codePoints to utf16
;; todo: full utf16 support. for now just utf8 ;; todo: full utf16 support. for now just utf8
(for/list ([value (in-list codePoints)]) (for/list ([value (in-list codePoints)])
(toHex value))) (toHex value)))
(format "<~a>" (string-join encoded " ")))) (format "<~a>" (string-join encoded " "))))
(define unicode-cmap-str #<<HERE (define unicode-cmap-str #<<HERE
/CIDInit /ProcSet findresource begin /CIDInit /ProcSet findresource begin
@ -203,7 +205,7 @@ HERE
(() () #:rest (listof number?) . ->*m . string?) (() () #:rest (listof number?) . ->*m . string?)
(string-append* (string-append*
(for/list ([code (in-list codePoints)]) (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 (module+ test

@ -67,7 +67,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(error 'unimplemented-branch-of-_text)] ; todo (error 'unimplemented-branch-of-_text)] ; todo
[else ; render paragraphs as single lines [else ; render paragraphs as single lines
(for ([line (in-list (string-split text "\n"))]) (for ([line (in-list (string-split text "\n"))])
(lineCallback line options))])) (lineCallback line options))]))
this) this)
@ -93,7 +93,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(when (· this _textOptions) (when (· this _textOptions)
(for ([(key val) (in-hash (· this _textOptions))] (for ([(key val) (in-hash (· this _textOptions))]
#:unless (equal? (key "continued"))) #:unless (equal? (key "continued")))
(hash-ref! options key val))) (hash-ref! options key val)))
;; Update the current position ;; Update the current position
(when x (set-field! x this x)) (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 ;; 180325 suppress the size tracking: we'll do our own line measurement
;; 181120 unsuppress the size tracking for now because it breaks test 04 ;; 181120 unsuppress the size tracking for now because it breaks test 04
(if (not wrapper) (if (not wrapper)
(increment-field! x this (send this widthOfString text)) (increment-field! x this (send this widthOfString text))
(increment-field! y (+ (send this currentLineHeight #t) lineGap))) (increment-field! y (+ (send this currentLineHeight #t) lineGap)))
(void)) (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 ;; since the normal Tw operator only works on character code 32, which isn't
;; used for embedded fonts. ;; used for embedded fonts.
;; 180321: the first call to this operation is very slow from Quad ;; 180321: the first call to this operation is very slow from Quad
;; 181126: because `encode` calls `layout`
(match-define (list encoded-char-strs positions) (match-define (list encoded-char-strs positions)
(if (not (zero? wordSpacing)) (if (not (zero? wordSpacing))
(error 'unimplemented-brach) ; todo (error 'unimplemented-brach) ; todo

Binary file not shown.

@ -27,7 +27,7 @@ endobj
<< <<
/Producer (PDFKit) /Producer (PDFKit)
/Creator (PDFKit) /Creator (PDFKit)
/CreationDate (D:20181120220342Z) /CreationDate (D:20181126220022Z)
>> >>
endobj endobj
2 0 obj 2 0 obj

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.
Loading…
Cancel
Save