and another

main
Matthew Butterick 6 years ago
parent 253f5fc170
commit e0c0b9a6d6

@ -25,15 +25,7 @@
sugar/list
racket/promise
crc32c)
(provide (except-out (all-defined-out)
head
post
hhea
maxp
OS/2
cvt_
prep
fpgm))
(provide (all-defined-out))
#|
approximates
@ -70,6 +62,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
[_hb-buf (delay (hb_buffer_create))]
[_crc (begin0 (crc32c-input-port _port) (pos _port 0))])
(define/public (_get-head-table) (get-head-table this))
(as-methods
postscriptName
measure-string
@ -91,15 +85,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
directory
ft-face
hb-font
hb-buf
head
post
hhea
maxp
OS/2
cvt_
prep
fpgm))
hb-buf))
(define (directory this) (force (· this _directory)))
@ -108,14 +94,6 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(define (hb-buf this) (force (· this _hb-buf)))
(require "table-stream.rkt")
(define (head this) (_getTable this 'head))
(define (post this) (_getTable this 'post))
(define (hhea this) (_getTable this 'hhea))
(define (OS/2 this) (_getTable this 'OS/2))
(define (maxp this) (_getTable this 'maxp))
(define (cvt_ this) (_getTable this 'cvt_))
(define (prep this) (_getTable this 'prep))
(define (fpgm this) (_getTable this 'fpgm))
;; The unique PostScript name for this font
(define/contract (postscriptName this)
@ -126,7 +104,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
;; The size of the fonts internal coordinate grid
(define/contract (unitsPerEm this)
(->m number?)
(· this head unitsPerEm))
(· (get-head-table this) unitsPerEm))
(test-module
(check-equal? (· f unitsPerEm) 1000))
@ -134,7 +112,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
;; The fonts [ascender](https://en.wikipedia.org/wiki/Ascender_(typography))
(define/contract (ascent this)
(->m number?)
(· this hhea ascent))
(· (get-hhea-table this) ascent))
(test-module
(check-equal? (· f ascent) 980))
@ -143,7 +121,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
;; The fonts [descender](https://en.wikipedia.org/wiki/Descender)
(define/contract (descent this)
(->m number?)
(· this hhea descent))
(· (get-hhea-table this) descent))
(test-module
(check-equal? (· f descent) -238))
@ -151,7 +129,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
;; The amount of space that should be included between lines
(define/contract (lineGap this)
(->m number?)
(· this hhea lineGap))
(· (get-hhea-table this) lineGap))
(test-module
(check-equal? (· f lineGap) 0))
@ -159,7 +137,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(define/contract (underlinePosition this)
(->m number?)
(· this post underlinePosition))
(· (get-post-table this) underlinePosition))
(test-module
(check-equal? (· f underlinePosition) -178))
@ -167,7 +145,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(define/contract (underlineThickness this)
(->m number?)
(· this post underlineThickness))
(· (get-post-table this) underlineThickness))
(test-module
(check-equal? (· f underlineThickness) 58))
@ -176,7 +154,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
;; If this is an italic font, the angle the cursor should be drawn at to match the font design
(define/contract (italicAngle this)
(->m number?)
(· this post italicAngle))
(· (get-post-table this) italicAngle))
(test-module
(check-equal? (· f italicAngle) 0))
@ -186,7 +164,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(define/contract (capHeight this)
(->m number?)
(if (has-table? this #"OS/2")
(· this OS/2 capHeight)
(· (get-OS/2-table this) capHeight)
(· this ascent)))
(test-module
@ -197,7 +175,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(define/contract (xHeight this)
(->m number?)
(if (has-table? this #"OS/2")
(· this OS/2 xHeight)
(· (get-OS/2-table this) xHeight)
0))
(test-module
@ -207,7 +185,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
;; The fonts bounding box, i.e. the box that encloses all glyphs in the font.
(define/contract (bbox this)
(->m BBox?)
(make-BBox (· this head xMin) (· this head yMin) (· this head xMax) (· this head yMax)))
(define head-table (get-head-table this))
(make-BBox (· head-table xMin) (· head-table yMin) (· head-table xMax) (· head-table yMax)))
(test-module
(check-equal? (bbox->list (· f bbox)) '(-161 -236 1193 963)))
@ -393,7 +372,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/base.js
(test-module
(check-equal? (measure-string f "f" (· f unitsPerEm)) 321.0)
(check-true (has-table? f #"cmap"))
(check-exn exn:fail:contract? (λ () (_getTable f 'nonexistent-table-tag)))
(check-exn exn:fail:contract? (λ () (get-table f 'nonexistent-table-tag)))
(check-true
(let ([h (layout fira "Rifle" #:debug #t)])
(and (equal? (hash-ref h 'hb-gids) '(227 480 732 412))

@ -72,9 +72,9 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
(define ttf-glyf-data (glyph-decode glyph))
;; get the offset to the glyph from the loca table
(match-define (list this-offset next-offset) (take (drop (hash-ref (dump (_getTable (subset-font ss) 'loca)) 'offsets) gid) 2))
(match-define (list this-offset next-offset) (take (drop (hash-ref (dump (get-table (subset-font ss) 'loca)) 'offsets) gid) 2))
(define port (_getTableStream (subset-font ss) 'glyf))
(define port (get-table-stream (subset-font ss) 'glyf))
(pos port (+ (pos port) this-offset))
(define buffer (read-bytes (- next-offset this-offset) port))
@ -122,18 +122,18 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
#:break (= idx (length (subset-glyphs ss))))
(define gid (list-ref (subset-glyphs ss) idx))
(ttf-subset-add-glyph ss gid))
(define maxp (clone-deep (· (subset-font ss) maxp to-hash)))
(define maxp (clone-deep (· (get-maxp-table (subset-font ss)) to-hash)))
(dict-set! maxp '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 head (clone-deep (· (subset-font ss) head to-hash)))
(define head (clone-deep (· (get-head-table (subset-font ss)) to-hash)))
(dict-set! head 'indexToLocFormat (· (ttf-subset-loca ss) version))
(define hhea (clone-deep (· (subset-font ss) hhea to-hash)))
(define hhea (clone-deep (· (get-hhea-table (subset-font ss)) to-hash)))
(dict-set! hhea 'numberOfMetrics (length (· (ttf-subset-hmtx ss) metrics)))
(define table-mhash
@ -142,11 +142,11 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
'hhea hhea
'loca (ttf-subset-loca ss)
'maxp maxp
'cvt_ (· (subset-font ss) cvt_)
'prep (· (subset-font ss) prep)
'cvt_ (get-cvt_-table (subset-font ss))
'prep (get-prep-table (subset-font ss))
'glyf (ttf-subset-glyf ss)
'hmtx (ttf-subset-hmtx ss)
'fpgm (· (subset-font ss) fpgm)))
'fpgm (get-fpgm-table (subset-font ss))))
(for ([kv (in-slice 2 kvs)])
(unless (second kv)
(error 'encode (format "missing value for ~a" (first kv))))

@ -1,4 +1,4 @@
#lang racket
#lang debug racket
(require sugar/unstable/js
(only-in xenomorph pos decode)
"tables.rkt"
@ -9,8 +9,10 @@
(syntax-case stx ()
[(_)
(with-syntax ([(TABLE-TAG ...) (hash-keys table-codecs)])
(with-syntax ([(GETTER-ID ...) (map (λ (tag) (datum->syntax stx (string->symbol (format "get-~a-table" (syntax->datum tag)))))
(syntax->list #'(TABLE-TAG ...)))])
#'(begin
(define (TABLE-TAG this) (_getTable this 'TABLE-TAG)) ...))]))
(define (GETTER-ID this) (get-table this 'TABLE-TAG)) ...)))]))
(define (has-table? this tag)
#;((or/c bytes? symbol?) . ->m . boolean?)
@ -20,20 +22,20 @@
(define (_getTable this table-tag)
(define (get-table this table-tag)
(unless (has-table? this table-tag)
(raise-argument-error '_getTable "table that exists in font" table-tag))
(hash-ref! (· this _decoded-tables) table-tag (λ () (_decodeTable this table-tag))))
(raise-argument-error 'get-table "table that exists in font" table-tag))
(hash-ref! (· this _decoded-tables) table-tag (λ () (decode-table this table-tag))))
(define-table-getters)
(define (_getTableStream this tag)
(define (get-table-stream this tag)
(define table (hash-ref (· this directory tables) tag))
(and table (pos (· this _port) (· table offset)) (· this _port)))
(define (_decodeTable this table-tag)
(define (decode-table this table-tag)
(unless (hash-has-key? table-codecs table-tag)
(raise-argument-error '_decodeTable "decodable table" table-tag))
(raise-argument-error 'decode-table "decodable table" table-tag))
(define table (hash-ref (· this directory tables) table-tag))
;; todo: possible to avoid copying the bytes here?
(pos (· this _port) (· table offset))

@ -43,7 +43,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/loca.js
(dict-update! this 'offsets (λ (offsets) (map (λ (x) (/ x 2)) offsets))))))
(define loca (+Rloca
(λ (o) (· o head indexToLocFormat))
(λ (o) (· o _get-head-table indexToLocFormat))
(dictify
0 (dictify 'offsets (+Array uint16be))
1 (dictify 'offsets (+Array uint32be)))))

@ -103,13 +103,13 @@ 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 (_getTable (glyph-font ttfg) 'loca)) 'offsets))
(define offsets (hash-ref (dump (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
(and (not (= glyfPos nextPos))
(let ()
(define port (_getTableStream (glyph-font ttfg) 'glyf))
(define port (get-table-stream (glyph-font ttfg) 'glyf))
(pos port (+ (pos port) glyfPos))
(define startPos (pos port))
(define glyph-data (decode GlyfHeader port))

Loading…
Cancel
Save