Added a fvar table test. This might be a bit more complicated...

This commit is contained in:
Pietro Gagliardi 2017-11-02 02:46:16 -04:00
parent 6b295b2d3f
commit 03b089c972
1 changed files with 61 additions and 0 deletions

61
doc/export/fvar.swift Normal file
View File

@ -0,0 +1,61 @@
// 2 november 2017
import Cocoa
import CoreText
func fourccString(_ k: FourCharCode) -> String {
var c: [UInt8] = [0, 0, 0, 0]
c[0] = UInt8((k >> 24) & 0xFF)
c[1] = UInt8((k >> 16) & 0xFF)
c[2] = UInt8((k >> 8) & 0xFF)
c[3] = UInt8(k & 0xFF)
return String(bytes: c, encoding: .utf8)!
}
var weightMin: Double = 0
var weightMax: Double = 0
var weightDef: Double = 0
var weightVals: [String: Double] = [:]
let attrs: [String: String] = [
kCTFontFamilyNameAttribute as String: "Skia",
]
let bd = CTFontDescriptorCreateWithAttributes(attrs as CFDictionary)
let matches = CTFontDescriptorCreateMatchingFontDescriptors(bd, nil) as! [CTFontDescriptor]
let mfont = CTFontCreateWithFontDescriptor(matches[0], 0.0, nil)
let master = CTFontCopyVariationAxes(mfont) as! [NSDictionary]
master.forEach { d in
print("axis {")
d.forEach { k, v in
if (k as! String) == (kCTFontVariationAxisIdentifierKey as String) {
let c = v as! FourCharCode
print("\t\(k) \(fourccString(c))")
} else {
print("\t\(k) \(v)")
}
}
print("}")
if (d[kCTFontVariationAxisNameKey] as! String) == "Weight" {
weightMin = d[kCTFontVariationAxisMinimumValueKey] as! Double
weightMax = d[kCTFontVariationAxisMaximumValueKey] as! Double
weightDef = d[kCTFontVariationAxisDefaultValueKey] as! Double
}
}
print("")
matches.forEach { d in
let f = CTFontDescriptorCopyAttribute(d, kCTFontVariationAttribute) as! [FourCharCode: Double]
let n = CTFontDescriptorCopyAttribute(d, kCTFontStyleNameAttribute) as! String
print("\(n) {")
f.forEach { k, v in
print("\t\(fourccString(k)) \(v)")
}
print("}")
weightVals[n] = weightDef
if let v = f[2003265652] {
weightVals[n] = v
}
}
print("")
weightVals.forEach { k, v in
let scaled = (v - weightMin) / (weightMax - weightMin)
print("\(k) scaled = \(scaled) (OS2 \(UInt16(scaled * 1000)))")
}