start CFF tables

main
Matthew Butterick 6 years ago
parent e06ec95603
commit cdba59c1eb

@ -0,0 +1,5 @@
#lang fontkit/racket
(require "CFF_.rkt")
(provide (rename-out [CFF_ CFF2]))
(test-module)

@ -1,11 +1,18 @@
#lang fontkit/racket
(require xenomorph)
(provide (all-defined-out))
(provide CFF_)
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/tables/CFF.js
https://github.com/mbutterick/fontkit/blob/master/src/cff/CFFFont.js
|#
(define CFF_ (+Array (+BufferT)))
;; the CFFFont object acts as the decoder for the `CFF ` table.
(define-subclass BufferT (RCFF_)
)
(define CFF_ (+RCFF_))
(test-module)

@ -1,4 +1,4 @@
#lang fontkit/racket
#lang debug fontkit/racket
(require "freetype-ffi.rkt" (except-in ffi/unsafe array?) racket/runtime-path "subset.rkt" "glyph.rkt" "layout-engine.rkt" "bbox.rkt" "glyphrun.rkt" "cmap-processor.rkt" "directory.rkt" xenomorph "tables.rkt" "ttfglyph.rkt")
(provide (all-defined-out))
@ -20,10 +20,11 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(check-equal? (postscriptName f) "Charter"))
;; This is the base class for all SFNT-based font formats in fontkit.
;; (including CFF)
;; It supports TrueType, and PostScript glyphs, and several color glyph formats.
(define-subclass object% (TTFFont port [_src #f])
(when port (unless (input-port? port)
(raise-argument-error 'TTFFont "input port" port)))
(raise-argument-error 'TTFFont "input port" port)))
(unless (member (peek-bytes 4 0 port) (list #"true" #"OTTO" (bytes 0 1 0 0)))
(raise 'probe-fail))
@ -278,6 +279,11 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(measure-char-width this c))) (· this unitsPerEm)))
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/index.js
|#
;; Register font formats
(define formats (list TTFFont))
;;fontkit.registerFormat(WOFFFont); ;; todo
@ -286,6 +292,10 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
;;fontkit.registerFormat(DFont); ;; todo
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/base.js
|#
(define/contract (openSync str-or-path [postscriptName #f])
(((or/c path? string?)) ((option/c string?)) . ->* . TTFFont?)

@ -1,4 +1,4 @@
#lang fontkit/racket
#lang debug fontkit/racket
(require "clone.rkt" "ttfglyphencoder.rkt" "loca.rkt" "directory.rkt" xenomorph)
(provide Subset CFFSubset TTFSubset)
@ -33,8 +33,42 @@ https://github.com/devongovett/fontkit/blob/master/src/subset/Subset.js
(sub1 (length (· this glyphs)))))))
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/subset/CFFSubset.js
|#
(define-subclass Subset (CFFSubset)
(error 'cff-subset-unimplemented))
#R (· this font)
(field [cff (send (· this font) _getTable 'CFF_)])
(unless (· this cff) (error 'not-a-cff-font))
(field [charStrings #f]
[subrs #f])
(as-methods
subsetCharstrings
#;subsetSubrs
#;subsetFontdict
#;createCIDFontdict
#;addString
#;encode))
(define/contract (subsetCharstrings this)
(->m void?)
(set-field! charStrings this null)
(define gsubrs (make-hash))
(for ([gid (in-list (· this glyphs))])
(push-end-field! charStrings this (· this cff getCharString gid))
(define glyph (· this font getGlyph gid))
(define path (· glyph path)) ; this causes the glyph to be parsed
(for ([subr (in-list (· glyph _usedGsubrs))])
(hash-set! gsubrs subr #true)))
(set-field! this gsubrs (send this subsetSubrs (· this cff globalSubrIndex) gsubrs))
(void))
#|
@ -75,9 +109,9 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
;; 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)))
;; note: this (· component pos) is correct. It's a field of a Component object, not a port
(bytes-copy! buffer (· component pos) (send uint16be encode #f gid))))
(define gid (send this includeGlyph (· component glyphID)))
;; note: this (· component pos) is correct. It's a field of a Component object, not a port
(bytes-copy! buffer (· component pos) (send uint16be encode #f gid))))
;; skip variation shit
@ -110,8 +144,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
;; glyphs to the array as component glyphs are discovered & enqueued
(for ([idx (in-naturals)]
#:break (= idx (length (· this glyphs))))
(define gid (list-ref (· this glyphs) idx))
(send this _addGlyph gid))
(define gid (list-ref (· this glyphs) idx))
(send this _addGlyph gid))
(define maxp (cloneDeep (· this font maxp to-hash)))
(dict-set! maxp 'numGlyphs (length (· this glyf)))

@ -2,6 +2,11 @@
(provide (all-defined-out))
(require (for-syntax racket/string))
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/tables/index.js
|#
(define-macro (define-table-codecs ID TABLE-ID ...)
(with-pattern ([(TABLE-ID-STRING ...) (pattern-case-filter #'(TABLE-ID ...)
[STX (datum->syntax caller-stx (string-replace (format "~a.rkt" (syntax->datum #'STX)) "/" ""))])])
@ -10,4 +15,12 @@
(test-module (require (submod TABLE-ID-STRING test) ...))
(define ID (make-hasheq (map cons (list 'TABLE-ID ...) (list TABLE-ID ...)))))))
(define-table-codecs table-codecs maxp hhea head loca prep fpgm hmtx cvt_ glyf OS/2 post GPOS GSUB CFF_)
(define-table-codecs table-codecs
;; required tables
#;cmap head hhea hmtx maxp #;name OS/2 post
;; TrueType outlines
cvt_ fpgm loca prep glyf
;; PostScript outlines
CFF_ CFF2 #;VORG
;; Advanced OpenType Tables
#;BASE #;GDEF GPOS GSUB #;JSTF)
Loading…
Cancel
Save