我正在用 Swift 编写 PDF 解析器,并且已经达到了使用回调函数 (CGPDFDictionaryApplyFunction) 获取所有字体数据的地步,getFont 函数应该填充 PDFFontCollection 类中的字体字典。在“getFont”回调函数中,集合变量被正确填充 - 但是当回调完成时,字体字典仍然有 0 个条目。
class PDFFontCollection{
var fonts: [AnyHashable:Any]!
init(page: CGPDFPage){
fonts = [AnyHashable:Any]()
let fontsdict = self.findFontDictionary(page: page)
if(fontsdict != nil){
CGPDFDictionaryApplyFunction(fontsdict!, self.getFont , &self.fonts)
}
}
private var getFont: CGPDFDictionaryApplierFunction = { (key, object, info) in
var collection = info?.assumingMemoryBound(to: [AnyHashable: Any].self).pointee
var name = String(cString: key, encoding: String.Encoding.ascii)
var dict: CGPDFDictionaryRef?
if (CGPDFObjectGetValue(object, .dictionary, &dict)){
var font = PDFFont.pdfFont(withFontDictionary: dict!)
collection?.updateValue(font!, forKey: name!)
}
}