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/table-stream.rkt

51 lines
2.3 KiB
Racket

6 years ago
#lang debug racket
6 years ago
(require sugar/unstable/js
6 years ago
xenomorph
6 years ago
"tables.rkt"
6 years ago
"struct.rkt"
6 years ago
(for-syntax "tables.rkt"))
(provide (all-defined-out))
(define-syntax (define-table-getters stx)
(syntax-case stx ()
[(_)
(with-syntax ([(TABLE-TAG ...) (hash-keys table-codecs)])
6 years ago
(with-syntax ([(GETTER-ID ...) (map (λ (tag) (datum->syntax stx (string->symbol (format "get-~a-table" (syntax->datum tag)))))
6 years ago
(syntax->list #'(TABLE-TAG ...)))]
[(HAS-ID? ...) (map (λ (tag) (datum->syntax stx (string->symbol (format "has-~a-table?" (syntax->datum tag)))))
6 years ago
(syntax->list #'(TABLE-TAG ...)))])
6 years ago
#'(begin
6 years ago
(define (GETTER-ID this) (get-table this 'TABLE-TAG)) ...
(define (HAS-ID? this) (has-table? this 'TABLE-TAG)) ...)))]))
6 years ago
(define (has-table? this tag)
#;((or/c bytes? symbol?) . ->m . boolean?)
6 years ago
(define directory (force (ttf-font-directory this)))
6 years ago
(hash-has-key? (hash-ref directory 'tables) (match tag
6 years ago
[(? bytes?) (string->symbol (bytes->string/latin-1 tag))]
[_ tag])))
6 years ago
(define (get-table this table-tag)
6 years ago
(unless (has-table? this table-tag)
6 years ago
(raise-argument-error 'get-table "table that exists in font" table-tag))
6 years ago
(hash-ref! (ttf-font-decoded-tables this) table-tag (λ () (decode-table this table-tag))))
6 years ago
(define-table-getters)
6 years ago
(define (get-table-stream this tag)
6 years ago
(define directory (force (ttf-font-directory this)))
6 years ago
(define table (hash-ref (hash-ref directory 'tables) tag))
(and table (pos (ttf-font-port this) (hash-ref table 'offset)) (ttf-font-port this)))
6 years ago
6 years ago
(define (decode-table this table-tag)
6 years ago
(unless (hash-has-key? table-codecs table-tag)
6 years ago
(raise-argument-error 'decode-table "decodable table" table-tag))
6 years ago
(define directory (force (ttf-font-directory this)))
6 years ago
(define table (hash-ref (hash-ref directory 'tables) table-tag))
6 years ago
;; todo: possible to avoid copying the bytes here?
6 years ago
(pos (ttf-font-port this) (hash-ref table 'offset))
(define table-bytes (open-input-bytes (peek-bytes (hash-ref table 'length) 0 (ttf-font-port this))))
6 years ago
(define table-decoder (hash-ref table-codecs table-tag))
6 years ago
(decode table-decoder table-bytes #:parent this))
6 years ago