Files
playdate-kit/Sources/PlaydateKit/Graphics/Classes/Font.swift
T

87 lines
3.9 KiB
Swift
Raw Normal View History

2026-07-24 10:33:45 +02:00
internal import CPlaydate
extension Graphics {
2026-09-18 13:15:08 +00:00
/// A .pft bitmap font. Wraps `LCDFont`. Glyph bitmaps are borrowed and don't retain
/// the font; keep it alive while using them.
2026-07-24 10:33:45 +02:00
public final class Font {
let pointer: OpaquePointer
2026-09-18 13:15:08 +00:00
/// `makeFontFromData` doesn't copy its buffer, so it lives as long as the font.
2026-07-24 10:33:45 +02:00
private let retainedData: UnsafeRawPointer?
init(pointer: OpaquePointer, retainedData: UnsafeRawPointer? = nil) {
self.pointer = pointer
self.retainedData = retainedData
}
public convenience init(path: String) throws(PlaydateError) {
2026-07-24 10:33:45 +02:00
var error: UnsafePointer<CChar>?
2026-09-18 13:15:08 +00:00
let pointer = path.withCString { gfx.pointee.loadFont.unsafelyUnwrapped($0, &error) }
guard let pointer else { throw PlaydateError(cString: error) }
2026-07-24 10:33:45 +02:00
self.init(pointer: pointer)
}
2026-09-18 13:15:08 +00:00
/// `data`: an uncompressed .pft file minus its 16-byte header; copied for the font's
/// lifetime. `wide` must match the header flag for glyphs above U+1FFFF.
public convenience init?(data: Span<UInt8>, wide: Bool = false) {
2026-07-24 10:33:45 +02:00
let copy = UnsafeMutableRawPointer.allocate(byteCount: data.count, alignment: 4)
2026-09-18 13:15:08 +00:00
data.withUnsafeBytes { bytes in
copy.copyMemory(from: bytes.baseAddress.unsafelyUnwrapped, byteCount: bytes.count)
}
2026-07-24 10:33:45 +02:00
let fontData = OpaquePointer(copy)
guard let pointer = gfx.pointee.makeFontFromData.unsafelyUnwrapped(
2026-07-24 10:33:45 +02:00
fontData, wide ? 1 : 0, Int32(data.count)) else {
copy.deallocate()
return nil
}
self.init(pointer: pointer, retainedData: UnsafeRawPointer(copy))
}
deinit {
2026-09-18 13:15:08 +00:00
// There is no freeFont; fonts are released with `realloc(font, 0)`.
System.systemFree(UnsafeMutableRawPointer(pointer))
2026-07-24 10:33:45 +02:00
retainedData?.deallocate()
}
2026-09-18 13:15:08 +00:00
/// Height, in pixels.
2026-07-24 10:33:45 +02:00
public var height: Int {
Int(gfx.pointee.getFontHeight.unsafelyUnwrapped(pointer))
2026-07-24 10:33:45 +02:00
}
2026-09-18 13:15:08 +00:00
/// Width in pixels; `tracking` is pixels between characters.
2026-07-24 10:33:45 +02:00
public func textWidth(_ text: String, tracking: Int = 0) -> Int {
2026-09-18 13:15:08 +00:00
text.withCString { cString in
Int(gfx.pointee.getTextWidth.unsafelyUnwrapped(pointer, cString, text.utf8.count,
kUTF8Encoding, Int32(tracking)))
2026-07-24 10:33:45 +02:00
}
}
2026-09-18 13:15:08 +00:00
/// Height in pixels of `text` wrapped to `maxWidth` pixels.
2026-07-24 10:33:45 +02:00
public func textHeight(_ text: String, maxWidth: Int, wrap: TextWrappingMode = .word,
tracking: Int = 0, extraLeading: Int = 0) -> Int {
2026-09-18 13:15:08 +00:00
text.withCString { cString in
Int(gfx.pointee.getTextHeightForMaxWidth.unsafelyUnwrapped(
2026-09-18 13:15:08 +00:00
pointer, cString, text.utf8.count, Int32(maxWidth), kUTF8Encoding,
2026-07-24 10:33:45 +02:00
wrap.cValue, Int32(tracking), Int32(extraLeading)))
}
}
2026-09-18 13:15:08 +00:00
/// `nil` if none. Codepoints differing only in their low 8 bits share a page.
2026-07-24 10:33:45 +02:00
public func page(for codepoint: UInt32) -> FontPage? {
guard let page = gfx.pointee.getFontPage.unsafelyUnwrapped(pointer, codepoint) else { return nil }
2026-07-24 10:33:45 +02:00
return FontPage(pointer: page, font: self)
}
2026-09-18 13:15:08 +00:00
/// `nil` if the font has no glyph for `codepoint`.
2026-07-24 10:33:45 +02:00
public func glyph(for codepoint: UInt32) -> (glyph: Glyph, bitmap: Bitmap?, advance: Int)? {
var bitmap: OpaquePointer?
var advance: Int32 = 0
guard let glyph = gfx.pointee.getFontGlyph.unsafelyUnwrapped(pointer, codepoint, &bitmap, &advance) else {
2026-07-24 10:33:45 +02:00
return nil
}
return (Glyph(pointer: glyph, font: self),
bitmap.map { Bitmap(pointer: $0, isOwned: false) },
Int(advance))
}
}
}