用于读取字体文件(如 otf、woff 和 woff2)表的软件包

字体文件阅读器

一个用于读取字体文件表(如 otf、woff 和 woff2)的包。 目前只有OS2支持。

为什么?

我需要一种方法来从给定的字体文件中读取 OS2 表。因为我无法在我正在开发的应用程序中实现字体工具,所以我不得不编写一个自定义的 swift 实现。 请随时做出贡献。

用法

// import Library
import FontFileReader

// --------------------------------- //

// Load NSData from file
guard let data = NSData(contentsOf: url) else {
    // return or throw error
    return
}

// Load data into BinaryFile object
let binary = BinaryFile(data: data)

// Parse the file signature header
let fileSignature = binary.getUInt32()

// Pass the signature into FontFormatHeader objet and check for validity
guard let fontFormat = FontFormatHeader.init(rawValue: fileSignature) else {
    // return or throw error
    return
}

var fontFileData: FileDataProtocol?

// handle binary according to the format
switch fontFormat {
case .opentype:
    fontFileData = OTFFileData(binary: binary)
case .woff:
    fontFileData = WOFFFileData(binary: binary)
case .woff2:
    fontFileData = WOFF2FileData(binary: binary)
}

guard fontFileData != nil else {
    // return or throw error
    return
}

// read data from fontFileData from the tables like (at the moment only OS2 Table supported)
let fontWeight = fontFileData!.os2Table.usWeightClass
let fontStyle = fontFileData!.os2Table.fsSelectionDecoded

Todo

  • CFF Table Support + Decompression in WOFF2
  • implement custom errors to handle errors while parsing (change functions to throwing ones)
  • Add comments

Resources

GitHub

点击跳转