working on subset

main
Matthew Butterick 7 years ago
parent 9fdeb7e7b8
commit fa4eef1325

@ -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

@ -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)
)

@ -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))
)

Binary file not shown.

@ -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))

Loading…
Cancel
Save