diff --git a/pitfall/pdfkit/lib/font/embedded.coffee b/pitfall/pdfkit/lib/font/embedded.coffee index 83203f34..b6004495 100644 --- a/pitfall/pdfkit/lib/font/embedded.coffee +++ b/pitfall/pdfkit/lib/font/embedded.coffee @@ -16,6 +16,9 @@ class EmbeddedFont extends PDFFont encode: (text, features) -> {glyphs, positions} = @font.layout text, features + for glyph, i in glyphs + console.log("glyphid="+glyph.id) + console.log("position="+positions[i].toString()) res = [] for glyph, i in glyphs diff --git a/pitfall/pitfall/embedded.rkt b/pitfall/pitfall/embedded.rkt index 9485ffd3..b42278d6 100644 --- a/pitfall/pitfall/embedded.rkt +++ b/pitfall/pitfall/embedded.rkt @@ -4,8 +4,10 @@ (define-subclass PDFFont (EmbeddedFont document font id) (super-new) - (field #;[subset (· this font createSubset)] + (field [subset (· this font createSubset)] + ;; always include the missing glyph (gid = 0) [unicode '((0))] + ;; always include the width of the missing glyph (gid = 0) [widths (list (send (send (· this font) getGlyph 0) advanceWidth))] [name (· font postscriptName)] @@ -46,7 +48,7 @@ For now, we'll just measure width of the characters. (check-equal? (· ef lineGap) 0) (check-equal? (· ef bbox) '(-161 -236 1193 963)) (define H-gid 41) - (· ef widths) + (check-equal? (· ef widths) '(278)) (check-equal? (send (send (· ef font) getGlyph H-gid) advanceWidth) 738) - +(· ef subset) ) \ No newline at end of file diff --git a/pitfall/pitfall/subset.rkt b/pitfall/pitfall/subset.rkt index ebc923f1..07cfc346 100644 --- a/pitfall/pitfall/subset.rkt +++ b/pitfall/pitfall/subset.rkt @@ -1,15 +1,41 @@ #lang pitfall/racket (provide Subset CFFSubset TTFSubset) -(define-subclass object% (Subset) - (super-new)) +(define index? (and/c (not/c negative?) integer?)) +;; approximates +;; https://github.com/devongovett/fontkit/blob/master/src/subset/Subset.js -(define-subclass Subset (CFFSubset font) +(define-subclass object% (Subset font) + (super-new) + (field [glyphs empty] ; list of glyphs in the subset + [mapping (mhash)] ; mapping of glyph ids to indexes in `glyphs` + ) + + (send this includeGlyph 0) ; always include the missing glyph in subset + + (as-methods + includeGlyph)) + +(define/contract (includeGlyph this glyph) + ((or/c object? index?) . ->m . index?) + (let ([glyph (if (object? glyph) (· glyph id) glyph)]) + (hash-ref! (· this mapping) glyph + (λ () + ;; put the new glyph at the end of `glyphs`, + ;; and put its index in the mapping + (push-end-field! glyphs this glyph) + (sub1 (length (· this glyphs))))))) + + +(define-subclass Subset (CFFSubset) (super-new) (error 'cff-subset-unimplemented)) -(define-subclass Subset (TTFSubset font) +;; approximates +;; https://github.com/devongovett/fontkit/blob/master/src/subset/TTFSubset.js + +(define-subclass Subset (TTFSubset) (super-new) - (error 'ttf-subset-unimplemented)) \ No newline at end of file + ) \ No newline at end of file diff --git a/pitfall/pitfall/test/test12.pdf b/pitfall/pitfall/test/test12.pdf index 4a6a5187..d6e51015 100644 Binary files a/pitfall/pitfall/test/test12.pdf and b/pitfall/pitfall/test/test12.pdf differ diff --git a/pitfall/pitfall/test/test12.rkt b/pitfall/pitfall/test/test12.rkt index 8fca9113..27819d30 100644 --- a/pitfall/pitfall/test/test12.rkt +++ b/pitfall/pitfall/test/test12.rkt @@ -23,4 +23,5 @@ (define doc (make-object PDFDocument)) (send doc registerFont "Charter" (path->string charter-path)) (send* doc [font "Charter"]) - #;doc) + (send doc pipe (open-output-string)) + (send doc end))