From fa4eef1325fe5f37c34d4ee2d3e3cbe1caf2ab8f Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 30 May 2017 13:50:13 -0700 Subject: [PATCH] working on subset --- pitfall/pdfkit/lib/font/embedded.coffee | 3 ++ pitfall/pitfall/embedded.rkt | 8 ++++-- pitfall/pitfall/subset.rkt | 36 ++++++++++++++++++++---- pitfall/pitfall/test/test12.pdf | Bin 6345 -> 6345 bytes pitfall/pitfall/test/test12.rkt | 3 +- 5 files changed, 41 insertions(+), 9 deletions(-) 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 4a6a5187798dba12261d97f88e11ecc227f06a95..d6e51015f65af2252422fb52ede04043a24f3d82 100644 GIT binary patch delta 55 zcmX?Uc+zl#EE9*ZfsuiUg|YEuRVGU|H$Qjh0H4VzOjn@H&34RhnP6-~wi!YIxlIq+ delta 55 zcmX?Uc+zl#EE9*3rGc@jnUTR{RVGU|XZOg6NUzB$Ojn@H&34RhnP6-~wi!YI!b%WI 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))