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/font.rkt

243 lines
9.8 KiB
Racket

6 years ago
#lang debug racket/base
6 years ago
(require "helper.rkt"
6 years ago
"unsafe/freetype.rkt"
6 years ago
"glyph.rkt"
"bbox.rkt"
"glyphrun.rkt"
6 years ago
"directory.rkt"
6 years ago
"db.rkt"
6 years ago
"struct.rkt"
"table-stream.rkt"
6 years ago
xenomorph
racket/match
sugar/unstable/dict
6 years ago
sugar/unstable/js
6 years ago
"unsafe/harfbuzz.rkt"
6 years ago
"glyph-position.rkt"
sugar/list
6 years ago
racket/promise
crc32c)
6 years ago
(provide (all-defined-out))
6 years ago
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
|#
(test-module
6 years ago
(define f (open-font charter-path))
(define fira (open-font (path->string fira-path)))
(define otf (open-font (path->string fira-otf-path)))
(check-equal? (font-postscript-name f) "Charter"))
6 years ago
(define ft-library (delay (FT_Init_FreeType)))
6 years ago
6 years ago
(define (+ttf-font port
[decoded-tables (mhash)]
[src (path->string (object-name port))]
[directory (delay (decode Directory port #:parent (mhash '_startOffset 0)))]
[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))]
[crc (begin0 (crc32c-input-port port) (pos port 0))]
[get-head-table-proc #f])
(unless (input-port? port)
(raise-argument-error '+ttf-font "input port" port))
(unless (member (peek-bytes 4 0 port) (list #"true" #"OTTO" (bytes 0 1 0 0)))
(do-probe-fail!))
(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))))
font)
(define (font-postscript-name font) (FT_Get_Postscript_Name (ft-face font)))
(define (font-units-per-em font) (· (get-head-table font) unitsPerEm))
(define (font-ascent font) (· (get-hhea-table font) ascent))
(define (font-descent font) (· (get-hhea-table font) descent))
(define (font-linegap font) (· (get-hhea-table font) lineGap))
(define (font-underline-position font) (· (get-post-table font) underlinePosition))
(define (font-underline-thickness font) (· (get-post-table font) underlineThickness))
(define (font-italic-angle font) (· (get-post-table font) italicAngle))
(define (font-cap-height font)
(if (has-table? font #"OS/2")
(· (get-OS/2-table font) capHeight)
(font-ascent font)))
(define (font-x-height font)
(if (has-table? font #"OS/2")
(· (get-OS/2-table font) xHeight)
6 years ago
0))
(test-module
6 years ago
(check-equal? (font-units-per-em f) 1000)
(check-equal? (font-ascent f) 980)
(check-equal? (font-descent f) -238)
(check-equal? (font-linegap f) 0)
(check-equal? (font-underline-position f) -178)
(check-equal? (font-underline-thickness f) 58)
(check-equal? (font-italic-angle f) 0)
(check-equal? (font-cap-height f) 671)
(check-equal? (font-x-height f) 481))
(define (font-bbox font)
(define head-table (get-head-table font))
6 years ago
(+bbox (· head-table xMin) (· head-table yMin) (· head-table xMax) (· head-table yMax)))
6 years ago
6 years ago
(test-module
6 years ago
(check-equal? (bbox->list (font-bbox f)) '(-161 -236 1193 963)))
6 years ago
6 years ago
(define current-layout-caching (make-parameter #false))
6 years ago
6 years ago
(struct hb-gid (val) #:transparent)
(struct hb-cluster (chars) #:transparent)
(struct hb-position (xad yad xoff yoff etc) #:transparent)
(struct hb-layout (hb-gids hb-clusters hb-positions) #:transparent)
(define hb-output (+Struct (dictify
'hb-gids (+Array uint16 uint16)
'hb-clusters (+Array (+Array uint16 uint16) uint16)
'hb-positions (+Array (+Array uint16 5) uint16))))
6 years ago
(define (hb-layout->glyphrun font hbr)
6 years ago
(match hbr
6 years ago
[(hash-table ('hb-gids gids)
6 years ago
('hb-clusters clusters)
('hb-positions posns))
6 years ago
(define glyphs (for/list ([gidx (in-list gids)]
6 years ago
[cluster (in-list clusters)])
6 years ago
(get-glyph font gidx cluster)))
(define positions (for/list ([posn (in-list posns)])
(apply +glyph-position posn)))
(glyphrun glyphs positions)]))
6 years ago
6 years ago
(define (harfbuzz-layout font codepoints features script language)
(define buf (hb-buf font))
6 years ago
(hb_buffer_reset buf)
6 years ago
(hb_buffer_add_codepoints buf codepoints)
6 years ago
(define chars (map hb_glyph_info_t-codepoint (hb_buffer_get_glyph_infos buf)))
6 years ago
(hb_shape (hb-font font) buf (map tag->hb-feature (or features null)))
6 years ago
(define gis (hb_buffer_get_glyph_infos buf))
(dictify 'hb-gids (map hb_glyph_info_t-codepoint gis)
'hb-clusters (break-at chars (map hb_glyph_info_t-cluster gis))
'hb-positions (map hb_glyph_position_t->list (hb_buffer_get_glyph_positions buf))))
(define layout-cache (make-hasheqv))
(define hb-input (+Struct (dictify
'font-crc uint32
'codepoints (+Array uint16)
'userFeatures (+Array (+String uint8)))))
(define (layout-cache-key font-crc codepoints user-features . _)
(crc32c-bytes (encode hb-input (dictify
'font-crc font-crc
'codepoints codepoints
'userFeatures user-features) #f)))
6 years ago
6 years ago
;; Returns a GlyphRun object, which includes an array of Glyphs and GlyphPositions for the given string.
6 years ago
(define (layout font string [user-features #f] [script #f] [language #f] #:test [test #f])
6 years ago
#;((string?) ((option/c (listof symbol?)) (option/c symbol?) (option/c symbol?)) . ->*m . GlyphRun?)
6 years ago
(define (get-layout string)
6 years ago
(define codepoints (map char->integer (string->list string)))
6 years ago
(define args (list codepoints (if user-features (sort user-features symbol<?) null) script language))
6 years ago
(define key (apply layout-cache-key (ttf-font-crc font) args))
6 years ago
(hash-ref! layout-cache key
(λ ()
6 years ago
#;(encode hb-output (apply harfbuzz-layout font args) #f)
6 years ago
(match (get-layout-from-db key)
[(? bytes? res) (dump (decode hb-output res))]
6 years ago
[_ (define new-layout (apply harfbuzz-layout font args))
6 years ago
(add-record! (cons key (encode hb-output new-layout #f)))
(make-hasheq new-layout)])))) ;; `dump` converts to hash
6 years ago
;; work on substrs to reuse cached pieces
;; caveat: no shaping / positioning that involve word spaces
6 years ago
;; todo: why does caching produce slightly different results in test files
;; theory: because word space is not included in shaping
6 years ago
(cond
[(current-layout-caching)
(define substrs (for/list ([substr (in-list (regexp-match* " " string #:gap-select? #t))]
#:when (positive? (string-length substr)))
6 years ago
substr))
6 years ago
(apply append-glyphruns (map (λ (layout) (hb-layout->glyphrun font layout)) (map get-layout substrs)))]
[else (if test
6 years ago
(get-layout string)
6 years ago
(hb-layout->glyphrun font (get-layout string)))]))
6 years ago
;; Returns an array of Glyph objects for the given string.
;; This is only a one-to-one mapping from characters to glyphs.
;; For most uses, you should use font.layout (described below), which
;; provides a much more advanced mapping supporting AAT and OpenType shaping.
6 years ago
(define (glyphs-for-string font string)
6 years ago
#;(string? . ->m . (listof glyph?))
6 years ago
;; todo: make this handle UTF-16 with surrogate bytes
;; for now, just use UTF-8
(define codepoints (map char->integer (string->list string)))
(for/list ([cp (in-list codepoints)])
6 years ago
(glyph-for-codepoint font cp)))
6 years ago
;; Maps a single unicode code point to a Glyph object.
;; Does not perform any advanced substitutions (there is no context to do so).
6 years ago
(define (glyph-for-codepoint font codepoint)
(define glyph-idx (FT_Get_Char_Index (· font ft-face) codepoint))
(get-glyph font glyph-idx (list codepoint)))
(define (measure-char-width font char)
(define glyph-idx (FT_Get_Char_Index (ft-face font) (char->integer char)))
(FT_Load_Glyph (ft-face font) glyph-idx FT_LOAD_NO_RECURSE)
(define width (FT_Vector-x (FT_GlyphSlotRec-advance (FT_FaceRec-glyph (ft-face font)))))
6 years ago
(* width 1.0))
6 years ago
(define (measure-string font str size)
6 years ago
(/ (* size
(for/sum ([c (in-string str)])
6 years ago
(measure-char-width font c))) (font-units-per-em font)))
6 years ago
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/index.js
|#
;; Register font formats
6 years ago
(define font-formats (list +ttf-font))
6 years ago
;;fontkit.registerFormat(WOFFFont); ;; todo
;;fontkit.registerFormat(WOFF2Font); ;; todo
;;fontkit.registerFormat(TrueTypeCollection); ;; todo
;;fontkit.registerFormat(DFont); ;; todo
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/base.js
|#
6 years ago
(define (open-font str-or-path)
6 years ago
(define filename (if (path? str-or-path) (path->string str-or-path) str-or-path))
6 years ago
(create-font (open-input-file filename)))
6 years ago
6 years ago
(struct probe-fail exn ())
(define (do-probe-fail!)
(raise (probe-fail "fail" (current-continuation-marks))))
6 years ago
6 years ago
(define (create-font port)
6 years ago
(or
6 years ago
;; 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)])
6 years ago
(with-handlers ([probe-fail? (λ (exn) #f)])
6 years ago
(font-format port)))
6 years ago
(error 'create-font "unknown font format")))
6 years ago
(test-module
6 years ago
(check-equal? (measure-string f "f" (font-units-per-em f)) 321.0)
6 years ago
(check-true (has-table? f #"cmap"))
6 years ago
(check-exn exn:fail:contract? (λ () (get-table f 'nonexistent-table-tag)))
6 years ago
(check-true
6 years ago
(let ([h (layout fira "Rifle" #:test #t)])
6 years ago
(and (equal? (hash-ref h 'hb-gids) '(227 480 732 412))
(equal? (hash-ref h 'hb-clusters) '((82) (105) (102 108) (101)))
(equal? (hash-ref h 'hb-positions) '((601 0 0 0 0) (279 0 0 0 0) (580 0 0 0 0) (547 0 0 0 0)))))))