I want to show fonts in NSTableView. If I use fonts inited by NSFont(name: fontName, size: size)
everything is OK. But In this case I can use only fonts installed in system. So I made an NSFont Extension:
public extension NSFont {
static func read(from path: String, size: CGFloat) throws -> NSFont {
guard let dataProvider = CGDataProvider(filename: path) else {
throw NSError(domain: "file not found", code: 77, userInfo: ["fileName" : path])
}
guard let fontRef = CGFont ( dataProvider ) else {
throw NSError(domain: "Not a font file", code: 77, userInfo: ["fileName" : path])
}
return CTFontCreateWithGraphicsFont(fontRef, size, nil, nil) as NSFont
}
}
It seems to work, fonts made this way founds their place in [NSFont]
arrays.
But if try to bind them to NSTextFieldCell
font in NSTableView
program explodes:
(this goes forever and throws Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8))
......
......
#261509 0x00007fff6c023e63 in -[NSCTFont isEqual:] ()
#261510 0x00007fff4539908c in _CFNonObjCEqual ()
#261511 0x00007fff6c023e63 in -[NSCTFont isEqual:] ()
#261512 0x00007fff4539908c in _CFNonObjCEqual ()
#261513 0x00007fff6c023e63 in -[NSCTFont isEqual:] ()
#261514 0x00007fff6bfccf63 in -[NSAttributeDictionary isEqualToDictionary:] ()
#261515 0x00007fff6bfccc11 in attributeDictionaryIsEqual ()
#261516 0x00007fff475c6712 in hashProbe ()
#261517 0x00007fff475c6518 in -[NSConcreteHashTable getItem:] ()
#261518 0x00007fff6bfc5be8 in +[NSAttributeDictionary newWithDictionary:] ()
#261519 0x00007fff6bff7cbe in -[_NSCachedAttributedString initWithString:attributes:] ()
#261520 0x00007fff6bfdac1f in __NSStringDrawingEngine ()
#261521 0x00007fff6bff7380 in _NSStringDrawingCore ()
#261522 0x00007fff42b16477 in _NSDrawTextCell2 ()
#261523 0x00007fff42b15328 in __45-[NSTextFieldCell _drawForegroundOfTextLayer]_block_invoke ()
#261524 0x00007fff42a8c529 in -[NSFocusStack performWithFocusView:inWindow:usingBlock:] ()
#261525 0x00007fff42b14bff in -[NSTextFieldCell _drawForegroundOfTextLayer] ()
#261526 0x00007fff42b1445a in -[NSTextFieldCell updateLayerWithFrame:inView:] ()
#261527 0x00007fff42b14322 in -[NSControl updateLayer] ()
#261528 0x00007fff42afe301 in _NSViewUpdateLayer ()
......
......
I think there is something missing in CTFont what NSFont has. But what?
I didn’t found an answer. But because infinite loop started when I tried to use NSPredicate, on comparision
NSCTFont isEqual:
, I made a controllers for fonts and used them to compare by NSPredicate. No infinite loop anymore.