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

58 lines
2.2 KiB
Racket

6 years ago
#lang racket/base
6 years ago
(require (for-syntax)
6 years ago
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
(struct glyph (id codepoints font is-mark? is-ligature? metrics) #:transparent #:mutable)
6 years ago
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)))
6 years ago
(FT_Load_Glyph face (glyph-id g) FT_LOAD_NO_RECURSE)
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))
(hash-set*! (glyph-metrics g)
'advanceWidth (FT_Glyph_Metrics-horiAdvance ft-glyph-metrics)
'leftBearing (FT_Glyph_Metrics-horiBearingX ft-glyph-metrics)))
(glyph-metrics g))
6 years ago
6 years ago
;; Represents a TrueType glyph.
(struct ttf-glyph glyph () #:transparent)
(define (+ttf-glyph . args)
(apply +glyph #:constructor ttf-glyph args))
;; 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])
((if (has-table? font #"cff_")
(error 'cff-fonts-unsupported)
+ttf-glyph) gid codepoints font))