diff --git a/fontland/fontland/subset.rkt b/fontland/fontland/subset.rkt index fb7657e5..6b599db5 100644 --- a/fontland/fontland/subset.rkt +++ b/fontland/fontland/subset.rkt @@ -110,7 +110,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js ;; because they just get copied entirely into the subset. ;; it's just used to detect composite glyphs and handle them specially. ;; so an optimization would be to detect composite / noncomposite without full glyph-decode. - (define glyf (glyph-decode glyph)) + (define ttf-glyf-data (glyph-decode glyph)) ;; get the offset to the glyph from the loca table (match-define (list curOffset nextOffset) (take (drop (· this font loca offsets) gid) 2)) @@ -121,11 +121,11 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js (define buffer (read-bytes (- nextOffset curOffset) port)) ;; if it is a compound glyph, include its components - (when (and glyf (negative? (· glyf numberOfContours))) - (for ([component (in-list (· glyf components))]) - (define gid (send this includeGlyph (Component-glyphID component))) - ;; note: this (Component-pos component) is correct. It's a field of a Component object, not a port - (bytes-copy! buffer (Component-pos component) (send uint16be encode #f gid)))) + (when (and ttf-glyf-data (negative? (· ttf-glyf-data numberOfContours))) + (for ([ttf-glyph-component (in-list (· ttf-glyf-data components))]) + (define gid (send this includeGlyph (ttf-glyph-component-glyph-id ttf-glyph-component))) + ;; note: this (ttf-glyph-component-pos component) is correct. It's a field of a Component object, not a port + (bytes-copy! buffer (ttf-glyph-component-pos ttf-glyph-component) (send uint16be encode #f gid)))) ;; skip variation shit diff --git a/fontland/fontland/ttf-glyph.rkt b/fontland/fontland/ttf-glyph.rkt index 0ca01b51..256bfd3d 100644 --- a/fontland/fontland/ttf-glyph.rkt +++ b/fontland/fontland/ttf-glyph.rkt @@ -15,7 +15,6 @@ approximates https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js |# - ;; Represents a TrueType glyph. #;(define-subclass Glyph (TTFGlyph) (inherit-field _font id)) @@ -63,26 +62,23 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js UNSCALED_COMPONENT_OFFSET) ;; Represents a point in a simple glyph -(struct Point (onCurve endContour x y) #:transparent #:mutable) +(struct ttf-glyph-point (on-curve end-contour x y) #:transparent #:mutable) -(define (+Point onCurve endContour [x 0] [y 0]) - (Point onCurve endContour x y)) +(define (+tff-glyph-point on-curve end-contour [x 0] [y 0]) + (ttf-glyph-point on-curve end-contour x y)) (define (copy pt) - (apply +Point (struct->list pt))) + (apply +tff-glyph-point (struct->list pt))) ;; Represents a component in a composite glyph -(struct Component (glyphID dx dy pos scaleX scaleY scale01 scale10) #:transparent #:mutable) - -(define (+Component glyphID dx dy [pos 0] - [scaleX 1] - [scaleY 1] - [scale01 0] - [scale10 0]) - (Component glyphID dx dy pos scaleX scaleY scale01 scale10)) - - +(struct ttf-glyph-component (glyph-id dx dy pos scale-x scale-y scale-01 scale-10) #:transparent #:mutable) +(define (+ttf-glyph-component glyph-id dx dy [pos 0] + [scale-x 1] + [scale-y 1] + [scale-01 0] + [scale-10 0]) + (ttf-glyph-component glyph-id dx dy pos scale-x scale-y scale-01 scale-10)) ;; Parses just the glyph header and returns the bounding box @@ -90,13 +86,13 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js (unfinished)) ;; Parses a single glyph coordinate -(define (_parseGlyphCoord port prev short same) +(define (parse-glyph-coord port prev short same) (unless (input-port? port) - (raise-argument-error '_parseGlyphCoord "input port" port)) + (raise-argument-error 'parse-glyph-coord "input port" port)) (unless (number? prev) - (raise-argument-error '_parseGlyphCoord "number" prev)) + (raise-argument-error 'parse-glyph-coord "number" prev)) (unless (and (boolean? short) (boolean? same)) - (raise-argument-error '_parseGlyphCoord "booleans" (list short same))) + (raise-argument-error 'parse-glyph-coord "booleans" (list short same))) (+ prev (if short ((if (not same) - +) (decode uint8 port)) (if same 0 (decode int16be port))))) @@ -116,11 +112,11 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js (define startPos (pos port)) (define glyph-data (decode GlyfHeader port)) (match (· glyph-data numberOfContours) - [(? positive?) (_decodeSimple glyph-data port)] - [(? negative?) (_decodeComposite glyph-data port startPos)]) + [(? positive?) (decode-simple glyph-data port)] + [(? negative?) (decode-composite glyph-data port startPos)]) glyph-data))) -(define (_decodeSimple glyph-data port) +(define (decode-simple glyph-data port) (unless (dict? glyph-data) (raise-argument-error 'TTFGlyph-_decodeSimple "decoded RGlyfHeader" glyph-data)) @@ -147,15 +143,16 @@ 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 (+Point (zero? (bitwise-and flag ON_CURVE)) (and (index-of endPtsOfContours i) #t) 0 0)) - (define next-px (_parseGlyphCoord port px (not (zero? (bitwise-and flag X_SHORT_VECTOR))) (not (zero? (bitwise-and flag SAME_X))))) - (define next-py (_parseGlyphCoord port py (not (zero? (bitwise-and flag Y_SHORT_VECTOR))) (not (zero? (bitwise-and flag SAME_Y))))) - (set-Point-x! point next-px) - (set-Point-y! point next-py) + (define point (+tff-glyph-point (zero? (bitwise-and flag ON_CURVE)) (and (index-of endPtsOfContours 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) + (set-ttf-glyph-point-y! point next-py) (values (cons point points) next-px next-py))) (dict-set! glyph-data 'points (reverse points))) -(define (_decodeComposite glyph-data port [offset 0]) + +(define (decode-composite glyph-data port [offset 0]) ;; this is a composite glyph (dict-set! glyph-data 'components empty) (define haveInstructions #f) @@ -175,22 +172,22 @@ https://github.com/mbutterick/fontkit/blob/master/src/glyph/TTFGlyph.js (let ([decoder (if (not (zero? (bitwise-and flags ARG_1_AND_2_ARE_WORDS))) int16be int8)]) (list (send decoder decode port) (send decoder decode port)))) - (define component (+Component glyphID dx dy)) - (set-Component-pos! component gPos) + (define component (+ttf-glyph-component glyphID dx dy)) + (set-ttf-glyph-component-pos! component gPos) (cond [(not (zero? (bitwise-and flags WE_HAVE_A_SCALE))) (define scale (read-fixed14 port)) - (set-Component-scaleX! component scale) - (set-Component-scaleY! component scale)] + (set-ttf-glyph-component-scale-x! component scale) + (set-ttf-glyph-component-scale-y! component scale)] [(not (zero? (bitwise-and flags WE_HAVE_AN_X_AND_Y_SCALE))) - (set-Component-scaleX! component (read-fixed14 port)) - (set-Component-scaleY! component (read-fixed14 port))] + (set-ttf-glyph-component-scale-x! component (read-fixed14 port)) + (set-ttf-glyph-component-scale-y! component (read-fixed14 port))] [(not (zero? (bitwise-and flags WE_HAVE_A_TWO_BY_TWO))) - (set-Component-scaleX! component (read-fixed14 port)) - (set-Component-scale01! component (read-fixed14 port)) - (set-Component-scale10! component (read-fixed14 port)) - (set-Component-scaleY! component (read-fixed14 port))]) + (set-ttf-glyph-component-scale-x! component (read-fixed14 port)) + (set-ttf-glyph-component-scale-01! component (read-fixed14 port)) + (set-ttf-glyph-component-scale-10! component (read-fixed14 port)) + (set-ttf-glyph-component-scale-y! component (read-fixed14 port))]) component)) haveInstructions)