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

67 lines
2.4 KiB
Racket

#lang debug racket/base
6 years ago
(require sugar/unstable/dict
6 years ago
"unsafe/freetype.rkt"
6 years ago
"table-stream.rkt"
6 years ago
"struct.rkt"
6 years ago
"helper.rkt")
6 years ago
(provide (all-defined-out))
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/glyph/Glyph.js
|#
6 years ago
; Glyph objects represent a glyph in the font. They have various properties for accessing metrics and
; the actual vector path the glyph represents, and methods for rendering the glyph to a graphics context.
;
; You do not create glyph objects directly. They are created by various methods on the font object.
; There are several subclasses of the base Glyph class internally that may be returned depending
; on the font format, but they all inherit from this class.
6 years ago
6 years ago
(define (is-mark? codepoint)
;; mark classes = Mn Me Mc
(regexp-match #px"\\p{Mn}|\\p{Me}|\\p{Mc}" (string (integer->char codepoint))))
6 years ago
(define (+glyph id codepoints font
[is-mark? (andmap is-mark? codepoints)]
[is-ligature? (> (length codepoints) 1)]
[metrics #f]
6 years ago
#:constructor [constructor glyph])
6 years ago
(constructor id codepoints font is-mark? is-ligature? metrics))
6 years ago
(define (glyph-advance-width g)
6 years ago
(hash-ref (get-glyph-metrics g) 'advanceWidth))
6 years ago
6 years ago
(define (get-glyph-metrics g)
(unless (glyph-metrics g)
6 years ago
(define face (ft-face (glyph-font g)))
(FT_Load_Glyph face (glyph-id g))
6 years ago
(define glyph (FT_FaceRec-glyph face))
6 years ago
(define ft-glyph-metrics (FT_GlyphSlotRec-metrics glyph))
(set-glyph-metrics! g (mhash))
;; todo: get vertical metrics
6 years ago
(hash-set*! (glyph-metrics g)
'advanceWidth (FT_Glyph_Metrics-horiAdvance ft-glyph-metrics)
'leftBearing (FT_Glyph_Metrics-horiBearingX ft-glyph-metrics)
'advanceHeight 'unfinished
'topBearing 'unfinished))
6 years ago
(glyph-metrics g))
6 years ago
6 years ago
(define (+ttf-glyph . args)
(apply +glyph #:constructor ttf-glyph args))
6 years ago
(define (+cff-glyph . args)
6 years ago
(apply +glyph #:constructor make-cff-glyph args))
6 years ago
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFFont.js
|#
6 years ago
;; Returns a glyph object for the given glyph id.
;; You can pass the array of code points this glyph represents for
;; your use later, and it will be stored in the glyph object.
6 years ago
(define (get-glyph font gid [codepoints null])
6 years ago
((if (has-table? font #"CFF_")
6 years ago
+cff-glyph
6 years ago
+ttf-glyph) gid codepoints font))