dump free

main
Matthew Butterick 6 years ago
parent 9dd05cf37c
commit 0c90a06512

@ -35,7 +35,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(define (+ttf-font port
[decoded-tables (mhash)]
[src (path->string (object-name port))]
[directory (delay (decode Directory port #:parent (mhash '_startOffset 0)))]
[directory (delay (parameterize ([current-parent (mhash '_startOffset 0)])
(xdecode Directory port)))]
[ft-face (delay (and src (FT_New_Face (force ft-library) src)))]
[hb-font (delay (and src (hb_ft_font_create (force ft-face))))]
[hb-buf (delay (hb_buffer_create))]
@ -48,7 +49,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(define font
(ttf-font port decoded-tables src directory ft-face hb-font hb-buf crc get-head-table-proc))
;; needed for `loca` table decoding cross-reference
(set-ttf-font-get-head-table-proc! font (delay (dump (get-head-table font))))
(set-ttf-font-get-head-table-proc! font (delay (get-head-table font)))
font)
(define (font-postscript-name font) (FT_Get_Postscript_Name (ft-face font)))
@ -106,9 +107,9 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
('hb-positions posns))
(define glyphs (for/list ([gidx (in-list gids)]
[cluster (in-list clusters)])
(get-glyph font gidx cluster)))
(get-glyph font gidx cluster)))
(define positions (for/list ([posn (in-list posns)])
(apply +glyph-position posn)))
(apply +glyph-position posn)))
(glyphrun glyphs positions)]))
(define (harfbuzz-layout font codepoints features script language)
@ -146,7 +147,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(λ ()
#;(encode hb-output (apply harfbuzz-layout font args) #f)
(match (get-layout-from-db key)
[(? bytes? res) (dump (decode hb-output res))]
[(? bytes? res) (decode hb-output res)]
[_ (define new-layout (apply harfbuzz-layout font args))
(add-record! (cons key (encode hb-output new-layout #f)))
(make-hasheq new-layout)])))) ;; `dump` converts to hash
@ -158,7 +159,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
[(current-layout-caching)
(define substrs (for/list ([substr (in-list (regexp-match* " " string #:gap-select? #t))]
#:when (positive? (string-length substr)))
substr))
substr))
(apply append-glyphruns (map (λ (layout) (hb-layout->glyphrun font layout)) (map get-layout substrs)))]
[else (if test
(get-layout string)
@ -175,7 +176,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
;; for now, just use UTF-8
(define codepoints (map char->integer (string->list string)))
(for/list ([cp (in-list codepoints)])
(glyph-for-codepoint font cp)))
(glyph-for-codepoint font cp)))
;; Maps a single unicode code point to a Glyph object.
;; Does not perform any advanced substitutions (there is no context to do so).
@ -192,7 +193,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(define (measure-string font str size)
(/ (* size
(for/sum ([c (in-string str)])
(measure-char-width font c))) (font-units-per-em font)))
(measure-char-width font c))) (font-units-per-em font)))
#|
approximates
@ -225,8 +226,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/base.js
;; rather than use a `probe` function,
;; just try making a font with each format and see what happens
(for/first ([font-format (in-list font-formats)])
(with-handlers ([probe-fail? (λ (exn) #f)])
(font-format port)))
(with-handlers ([probe-fail? (λ (exn) #f)])
(font-format port)))
(error 'create-font "unknown font format")))
(module+ test

@ -77,7 +77,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
;; get the offset to the glyph from the loca table
(match-define (list this-offset next-offset)
(take (drop (hash-ref (dump (get-table (subset-font ss) 'loca)) 'offsets) gid) 2))
(take (drop (hash-ref (get-table (subset-font ss) 'loca) 'offsets) gid) 2))
(define port (get-table-stream (subset-font ss) 'glyf))
(pos port (+ (pos port) this-offset))
@ -122,17 +122,17 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
(define gid (list-ref (subset-glyphs ss) idx))
(ttf-subset-add-glyph ss gid))
(define new-maxp-table (clone-deep (dump-mutable (get-maxp-table (subset-font ss)))))
(define new-maxp-table (clone-deep (get-maxp-table (subset-font ss))))
(dict-set! new-maxp-table 'numGlyphs (length (ttf-subset-glyf ss)))
;; populate the new loca table
(dict-update! (ttf-subset-loca ss) 'offsets (λ (vals) (append vals (list (ttf-subset-offset ss)))))
(loca-pre-encode (ttf-subset-loca ss))
(define new-head-table (clone-deep (dump-mutable (get-head-table (subset-font ss)))))
(define new-head-table (clone-deep (get-head-table (subset-font ss))))
(dict-set! new-head-table 'indexToLocFormat (dict-ref (ttf-subset-loca ss) 'version))
(define new-hhea-table (clone-deep (dump-mutable (get-hhea-table (subset-font ss)))))
(define new-hhea-table (clone-deep (get-hhea-table (subset-font ss))))
(dict-set! new-hhea-table 'numberOfMetrics (length (dict-ref (ttf-subset-hmtx ss) 'metrics)))
(define new-tables

@ -1,6 +1,6 @@
#lang debug racket
(require sugar/unstable/js
(only-in xenomorph pos decode)
xenomorph
"tables.rkt"
"struct.rkt"
(for-syntax "tables.rkt"))
@ -39,7 +39,7 @@
(define table (hash-ref (· directory tables) tag))
(and table (pos (ttf-font-port this) (· table offset)) (ttf-font-port this)))
(define (decode-table this table-tag)
(define (decode-table this table-tag)
(unless (hash-has-key? table-codecs table-tag)
(raise-argument-error 'decode-table "decodable table" table-tag))
(define directory (force (ttf-font-directory this)))
@ -48,5 +48,6 @@
(pos (ttf-font-port this) (· table offset))
(define table-bytes (open-input-bytes (peek-bytes (· table length) 0 (ttf-font-port this))))
(define table-decoder (hash-ref table-codecs table-tag))
(decode table-decoder table-bytes #:parent this))
(parameterize ([current-parent this])
(decode table-decoder table-bytes)))

@ -34,5 +34,5 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/hmtx.js
(define hmtx-data (decode hmtx-test hmtx-bytes))
(check-equal? (size hmtx-test) (* 229 (size hmtx-entry)))
(define H-gid 41) (define OE-gid 142)
(check-equal? (dump (stream-ref (· hmtx-data metrics) H-gid)) '((bearing . 33) (advance . 738)))
(check-equal? (dump (stream-ref (· hmtx-data metrics) OE-gid)) '((bearing . 43) (advance . 993))))
(check-equal? (stream-ref (· hmtx-data metrics) H-gid) (make-hasheq '((bearing . 33) (advance . 738))))
(check-equal? (stream-ref (· hmtx-data metrics) OE-gid) (make-hasheq '((bearing . 43) (advance . 993)))))

@ -98,7 +98,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js
;; or components for composite glyphs
(require "table-stream.rkt")
(define (glyph-decode ttfg)
(define offsets (hash-ref (dump (get-table (glyph-font ttfg) 'loca)) 'offsets))
(define offsets (hash-ref (get-table (glyph-font ttfg) 'loca) 'offsets))
(match-define (list glyfPos nextPos) (take (drop offsets (glyph-id ttfg)) 2))
;; Nothing to do if there is no data for this glyph

Loading…
Cancel
Save