main
Matthew Butterick 6 years ago
parent 4ace9bc128
commit 77c9f71b0a

@ -1,29 +1,11 @@
#lang racket/base
(require (for-syntax racket/base)
xenomorph
racket/class
racket/match
racket/list
racket/contract
sugar/unstable/class
(require (for-syntax)
sugar/unstable/dict
sugar/unstable/js
sugar/unstable/stub
"unsafe/freetype.rkt"
"helper.rkt")
(provide (all-defined-out))
(module+ test (require rackunit))
#|
/**
* 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.
*/
|#
#|
@ -31,25 +13,22 @@ approximates
https://github.com/mbutterick/fontkit/blob/master/src/glyph/Glyph.js
|#
#;(define-subclass object% (Glyph id codePoints font)
(field [_font font]
[isMark (andmap is-mark? codePoints)]
[isLigature (> (length codePoints) 1)]
[_metrics #f])
; 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.
(as-methods
advanceWidth
_getMetrics))
(struct glyph (id codePoints font _font isMark isLigature _metrics) #:transparent #:mutable)
(struct glyph (id codepoints font is-mark? is-ligature? metrics) #:transparent #:mutable)
(define (+glyph id codePoints font
[_font font]
[isMark (andmap is-mark? codePoints)]
[isLigature (> (length codePoints) 1)]
[_metrics #f]
(define (+glyph id codepoints font
[is-mark? (andmap is-mark? codepoints)]
[is-ligature? (> (length codepoints) 1)]
[metrics #f]
#:constructor [constructor glyph])
(constructor id codePoints font _font isMark isLigature _metrics))
(constructor id codepoints font is-mark? is-ligature? metrics))
#;(define-stub-stop _getPath)
#;(define-stub-stop _getCBox)
@ -57,19 +36,18 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/Glyph.js
#;(define-stub-stop _getTableMetrics)
(define (glyph-advance-width g)
(hash-ref (_getMetrics g) 'advanceWidth))
(hash-ref (get-glyph-metrics g) 'advanceWidth))
(define (_getMetrics g)
(unless (glyph-_metrics g)
(define face (· (glyph-_font g) ft-face))
(define (get-glyph-metrics g)
(unless (glyph-metrics g)
(define face (· (glyph-font g) ft-face))
(FT_Load_Glyph face (glyph-id g) FT_LOAD_NO_RECURSE)
(define glyph (FT_FaceRec-glyph face))
(define glyph-metrics (FT_GlyphSlotRec-metrics glyph))
(set-glyph-_metrics! g (mhash))
(hash-set*! (glyph-_metrics g)
'advanceWidth (FT_Glyph_Metrics-horiAdvance glyph-metrics)
'leftBearing (FT_Glyph_Metrics-horiBearingX glyph-metrics)))
(glyph-_metrics g))
(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))

@ -135,7 +135,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
(hash-update! (get-field hmtx this) 'metrics (λ (ms) (append ms
(list (mhash 'advance (glyph-advance-width glyph)
'bearing (· (_getMetrics glyph) leftBearing))))))
'bearing (· (get-glyph-metrics glyph) leftBearing))))))
(increment-field! offset this (bytes-length buffer))
(sub1 (length (· this glyf))))

@ -104,13 +104,13 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js
;; Decodes the glyph data into points for simple glyphs,
;; or components for composite glyphs
(define (glyph-decode ttfg)
(define offsets (· (glyph-_font ttfg) loca offsets))
(define offsets (· (glyph-font ttfg) loca offsets))
(match-define (list glyfPos nextPos) (take (drop offsets (glyph-id ttfg)) 2))
;; Nothing to do if there is no data for this glyph
(and (not (= glyfPos nextPos))
(let ()
(define port (send (glyph-_font ttfg) _getTableStream 'glyf))
(define port (send (glyph-font ttfg) _getTableStream 'glyf))
(pos port (+ (pos port) glyfPos))
(define startPos (pos port))
(define glyph-data (decode GlyfHeader port))
@ -128,14 +128,14 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js
;; this is a simple glyph
(dict-set! glyph-data 'points empty)
(define endPtsOfContours (decode (+Array uint16be (· glyph-data numberOfContours)) port))
(define endpts-of-contours (decode (+Array uint16be (· glyph-data numberOfContours)) port))
(dict-set! glyph-data 'instructions (decode (+Array uint8be uint16be) port))
(define numCoords (add1 (last endPtsOfContours)))
(define num-coords (add1 (last endpts-of-contours)))
(define flags
(for*/lists (flags)
(for*/lists (flag-acc)
([i (in-naturals)]
#:break (= (length flags) numCoords)
#:break (= (length flag-acc) num-coords)
[flag (in-value (decode uint8 port))]
[count (in-range (add1 (if (not (zero? (bitwise-and flag REPEAT)))
(decode uint8 port)
@ -146,7 +146,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js
(points _ _)
(for/fold ([points empty] [px 0] [py 0])
([(flag i) (in-indexed flags)])
(define point (+tff-glyph-point (zero? (bitwise-and flag ON_CURVE)) (and (index-of endPtsOfContours i) #t) 0 0))
(define point (+tff-glyph-point (zero? (bitwise-and flag ON_CURVE)) (and (index-of endpts-of-contours i) #t) 0 0))
(define next-px (parse-glyph-coord port px (not (zero? (bitwise-and flag X_SHORT_VECTOR))) (not (zero? (bitwise-and flag SAME_X)))))
(define next-py (parse-glyph-coord port py (not (zero? (bitwise-and flag Y_SHORT_VECTOR))) (not (zero? (bitwise-and flag SAME_Y)))))
(set-ttf-glyph-point-x! point next-px)

Loading…
Cancel
Save