Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codepoints from .utf8 presentation not mapped correctly #14

Open
JxD-DeciSion opened this issue Aug 2, 2023 · 0 comments
Open

Codepoints from .utf8 presentation not mapped correctly #14

JxD-DeciSion opened this issue Aug 2, 2023 · 0 comments

Comments

@JxD-DeciSion
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant