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/pitfall/fontkit/GPOS.rkt

197 lines
7.9 KiB
Racket

8 years ago
#lang fontkit/racket
8 years ago
(require xenomorph br/cond "opentype.rkt")
8 years ago
(provide (all-defined-out))
#|
approximates
https://github.com/mbutterick/fontkit/blob/master/src/tables/GPOS.js
|#
(define ValueFormat (+Bitfield uint16be '(xPlacement yPlacement xAdvance yAdvance xPlaDevice yPlaDevice xAdvDevice yAdvDevice)))
(define types
(mhash
'xPlacement int16be
'yPlacement int16be
'xAdvance int16be
'yAdvance int16be
8 years ago
'xPlaDevice (+Pointer uint16be Device (mhash 'type 'global 'relativeTo 'rel))
'yPlaDevice (+Pointer uint16be Device (mhash 'type 'global 'relativeTo 'rel))
'xAdvDevice (+Pointer uint16be Device (mhash 'type 'global 'relativeTo 'rel))
'yAdvDevice (+Pointer uint16be Device (mhash 'type 'global 'relativeTo 'rel))))
8 years ago
(define-subclass object% (ValueRecord [key 'valueFormat])
8 years ago
(define/public (buildStruct parent)
8 years ago
;; set `struct` to the first dict in the chain of ancestors
;; with the target key
(define struct (let loop ([x parent])
(cond
8 years ago
[(and x (dict? x) (dict-ref x key #f)) x]
[(· x parent) => loop]
[else #f])))
(and struct
(let ()
8 years ago
(define format (dict-ref struct key))
(define fields
(append
8 years ago
(dictify 'rel (λ _ (dict-ref struct '_startOffset)))
(for/list ([(key val) (in-dict format)]
#:when val)
8 years ago
(cons key (dict-ref types key)))))
(+Struct fields))))
8 years ago
(define/public (size val ctx)
8 years ago
(send (buildStruct ctx) size val ctx))
(define/public (decode stream parent)
8 years ago
(define res (send (buildStruct parent) decode stream parent))
8 years ago
(dict-remove! res 'rel)
8 years ago
res)
(define/public (encode . args)
8 years ago
(error 'GPOS-encode-not-implemented)))
(define PairValueRecord (+Struct
(dictify
'secondGlyph uint16be
'value1 (+ValueRecord 'valueFormat1)
'value2 (+ValueRecord 'valueFormat2))))
(define PairSet (+Array PairValueRecord uint16be))
(define Class2Record (+Struct
(dictify
'value1 (+ValueRecord 'valueFormat1)
'value2 (+ValueRecord 'valueFormat2))))
(define Anchor (+VersionedStruct uint16be
(dictify
;; Design units only
1 (dictify 'xCoordinate int16be
'yCoordinate int16be)
;; Design units plus contour point
2 (dictify 'xCoordinate int16be
'yCoordinate int16be
'anchorPoint uint16be)
;; Design units plus Device tables
3 (dictify 'xCoordinate int16be
'yCoordinate int16be
8 years ago
'xDeviceTable (+Pointer uint16be Device)
'yDeviceTable (+Pointer uint16be Device)))))
8 years ago
(define EntryExitRecord (+Struct
8 years ago
(dictify 'entryAnchor (+Pointer uint16be Anchor (mhash 'type 'parent))
'exitAnchor (+Pointer uint16be Anchor (mhash 'type 'parent)))))
8 years ago
(define MarkRecord (+Struct
(dictify 'class uint16be
'markAnchor uint16be)))
(define MarkArray (+Array MarkRecord uint16be))
8 years ago
(define BaseRecord (+Array (+Pointer uint16be Anchor) (λ (t) (ref* t 'parent 'classCount))))
8 years ago
(define BaseArray (+Array BaseRecord uint16be))
8 years ago
(define ComponentRecord (+Array (+Pointer uint16be Anchor) (λ (t) (ref* t 'parent 'parent 'classCount))))
8 years ago
(define LigatureAttach (+Array ComponentRecord uint16be))
8 years ago
(define LigatureArray (+Array (+Pointer uint16be LigatureAttach) uint16be))
8 years ago
(define-subclass VersionedStruct (GPOSLookup-VersionedStruct))
(define GPOSLookup
(+GPOSLookup-VersionedStruct
'lookupType
8 years ago
(dictify
;; Single Adjustment
1 (+VersionedStruct uint16be
(dictify
;; Single positioning value
1 (dictify
'coverage (+Pointer uint16be Coverage)
8 years ago
'valueFormat ValueFormat
'value (+ValueRecord))
2 (dictify
8 years ago
'coverage (+Pointer uint16be Coverage)
8 years ago
'valueFormat ValueFormat
'valueCount uint16be
'values (+LazyArray (+ValueRecord) 'valueCount))))
;; Pair Adjustment Positioning
2 (+VersionedStruct uint16be
(dictify
;; Adjustments for glyph pairs
1 (dictify
8 years ago
'coverage (+Pointer uint16be Coverage)
8 years ago
'valueFormat1 ValueFormat
'valueFormat2 ValueFormat
'pairSetCount uint16be
8 years ago
'pairSets (+LazyArray (+Pointer uint16be PairSet) 'pairSetCount))
8 years ago
;; Class pair adjustment
2 (dictify
8 years ago
'coverage (+Pointer uint16be Coverage)
8 years ago
'valueFormat1 ValueFormat
'valueFormat2 ValueFormat
8 years ago
'classDef1 (+Pointer uint16be ClassDef)
'classDef2 (+Pointer uint16be ClassDef)
8 years ago
'class1Count uint16be
'class2Count uint16be
'classRecords (+LazyArray (+LazyArray Class2Record 'class2Count) 'class1Count))))
;; Cursive Attachment Positioning
3 (dictify
'format uint16be
8 years ago
'coverage (+Pointer uint16be Coverage)
8 years ago
'entryExitCount uint16be
'entryExitRecords (+Array EntryExitRecord 'entryExitCount))
;; MarkToBase Attachment Positioning
4 (dictify
'format uint16be
8 years ago
'markCoverage (+Pointer uint16be Coverage)
'baseCoverage (+Pointer uint16be Coverage)
8 years ago
'classCount uint16be
8 years ago
'markArray (+Pointer uint16be MarkArray)
'baseArray (+Pointer uint16be BaseArray))
8 years ago
;; MarkToLigature Attachment Positioning
5 (dictify
'format uint16be
8 years ago
'markCoverage (+Pointer uint16be Coverage)
'ligatureCoverage (+Pointer uint16be Coverage)
8 years ago
'classCount uint16be
8 years ago
'markArray (+Pointer uint16be MarkArray)
'ligatureArray (+Pointer uint16be LigatureArray))
8 years ago
;; MarkToMark Attachment Positioning
6 (dictify
'format uint16be
8 years ago
'mark1Coverage (+Pointer uint16be Coverage)
'mark2Coverage (+Pointer uint16be Coverage)
8 years ago
'classCount uint16be
8 years ago
'mark1Array (+Pointer uint16be MarkArray)
'mark2Array (+Pointer uint16be BaseArray))
8 years ago
7 Context ;; Contextual positioning
8 ChainingContext ;; Chaining contextual positioning
;; Extension positioning
9 (dictify
'posFormat uint16be
'lookupType uint16be ;; cannot also be 9
8 years ago
'extension (+Pointer uint32be (λ () (error 'circular-reference-unfixed))))
8 years ago
)))
;; Fix circular reference
8 years ago
(ref*-set! GPOSLookup 'versions 9 'extension 'type GPOSLookup)
8 years ago
(define-subclass VersionedStruct (GPOS-MainVersionedStruct))
(define GPOS (+GPOS-MainVersionedStruct uint32be
8 years ago
(dictify
8 years ago
'header (dictify 'scriptList (+Pointer uint16be ScriptList)
'featureList (+Pointer uint16be FeatureList)
'lookupList (+Pointer uint16be (LookupList GPOSLookup)))
#x00010000 (dictify)
#;#x00010001 #;(+Pointer uint32be FeatureVariations))))
(test-module)