Description
Title: Bug in Font Loading Method - Incorrect Character Conversion
Description:
I'm using the 'STREGAsGate/Raylib' library, which provides a method called loadFontEx(...) to load font files into the VRAM. However, it seems that the current implementation of this method is using .utf8 to convert characters to code points, which results in incorrect behavior. Instead, the method should be using .unicodeScalars for accurate code point conversion.
Here is the original implementation of loadFontEx(...):
/// Load font from file with extended parameters
@inlinable
static func loadFontEx(_ fileName: String, _ fontSize: Int32, _ fontChars: [Character]? = nil) -> Font {
return fileName.withCString { cString in
if fontChars == nil {
return RaylibC.LoadFontEx(cString, fontSize, nil, 0)
} else {
var chars: [Int32] = fontChars!.compactMap({$0.utf8.first}).map({Int32($0)})
return chars.withUnsafeMutableBufferPointer { bufferPointer in
return RaylibC.LoadFontEx(cString, fontSize, bufferPointer.baseAddress, Int32(bufferPointer.count))
}
}
}
}
To fix this bug, I suggest modifying the implementation to use .unicodeScalars for character to code point conversion. Here is the updated implementation:
/// Load font from file with extended parameters (fixed bug in character conversion)
@inlinable
static func loadFontEx(_ fileName: String, _ fontSize: Int32, _ fontChars: [Character]? = nil) -> Font {
return fileName.withCString { cString in
if fontChars == nil {
return RaylibC.LoadFontEx(cString, fontSize, nil, 0)
} else {
var chars: [Int32] = fontChars!.map({ Int32($0.unicodeScalars.first?.value ?? 0 )})
return chars.withUnsafeMutableBufferPointer { bufferPointer in
return RaylibC.LoadFontEx(cString, fontSize, bufferPointer.baseAddress, Int32(bufferPointer.count))
}
}
}
}
By making this change, the font loading method should now handle character to code point conversion correctly, resolving the issue with incorrect font loading.
For further information/reproducing take a look into this discord conversation on the Raylib server. If you have any questions or need further assistance, feel free to reach out to me. I'd be happy to help!