You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/fontland/fontland/directory.rkt

74 lines
2.7 KiB
Racket

6 years ago
#lang racket/base
(require xenomorph
6 years ago
"tables.rkt"
6 years ago
racket/dict
6 years ago
sugar/unstable/dict
racket/string
sugar/unstable/class
sugar/unstable/js
racket/class)
6 years ago
(provide (all-defined-out))
#|
https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js
|#
6 years ago
(define table-entry (x:struct
'tag (x:symbol #:length 4)
6 years ago
'checkSum uint32be
6 years ago
'offset (x:pointer #:offset-type uint32be
6 years ago
#:type 'void
#:relative-to 'global)
6 years ago
'length uint32be))
6 years ago
;; for stupid tags like 'cvt '
(define (symbol-replace sym this that)
(string->symbol (string-replace (if (string? sym) sym (symbol->string sym)) this that)))
(define (escape-tag tag) (symbol-replace tag " " "_"))
(define (unescape-tag tag) (symbol-replace tag "_" " "))
6 years ago
(define (directory-post-decode this-res)
(define new-tables-val (mhash))
(for ([table (in-list (· this-res tables))])
(hash-set! new-tables-val (escape-tag (· table tag)) table))
(dict-set! this-res 'tables new-tables-val)
this-res)
6 years ago
6 years ago
(define (directory-pre-encode this-val)
(define tables (for/list ([(tag table) (in-hash (· this-val tables))])
(define table-codec (hash-ref table-codecs tag))
(mhash 'tag (unescape-tag tag)
'checkSum 0
6 years ago
'offset (x:void-pointer table-codec table)
'length (size table-codec table))))
6 years ago
(define numTables (length tables))
(define searchRange (* (floor (log numTables 2)) 16))
(hash-set*! this-val
'tag 'true
'numTables numTables
'tables tables
'searchRange searchRange
'entrySelector (floor (/ searchRange (log 2)))
'rangeShift (- (* numTables 16) searchRange))
this-val)
6 years ago
6 years ago
(define Directory (x:struct #:pre-encode directory-pre-encode
6 years ago
#:post-decode directory-post-decode
6 years ago
'tag (x:symbol #:length 4)
6 years ago
'numTables uint16be
'searchRange uint16be
'entrySelector uint16be
'rangeShift uint16be
6 years ago
'tables (x:array #:type table-entry #:length 'numTables)))
6 years ago
(define (directory-decode ip [options (mhash)])
6 years ago
(decode Directory ip))
6 years ago
(define (file-directory-decode ps)
(directory-decode (open-input-file ps)))
#;(test-module
(define ip (open-input-file charter-path))
(define decoded-dir (deserialize (read (open-input-file charter-directory-path))))
(check-equal? (directory-decode ip) decoded-dir))