Skip to content

Wrapper method loadFontEx does not handle loading the default character set properly #14

Open

Description

@qrokodial

The loadFontEx method does not properly handle when the codepoints argument is null.

As per the raylib documentation (emphasis added):

Font LoadFontEx(const char *fileName, int fontSize, int *codepoints, int codepointCount); // Load font from file with extended parameters, use NULL for codepoints and 0 for codepointCount to load the default character set, font size is provided in pixels height

In other words, codepoints should be null when we want to load the default character set, but under the hood, the loadFontEx wrapper function does not handle the case when codepoints is null.

The workaround is to not use the wrapper function and instead do something like the following (Kotlin code, but I'm sure you get the point):

Arena.ofConfined().use { localArena: Arena ->
    return RaylibFont(
        LoadFontEx(
            Arena.ofAuto(),
            localArena.allocateFrom(fileName),
            fontSize,
            MemorySegment.NULL, // this is usually where `MemorySegment.ofBuffer(codepoints)` would be
            0 // codepointCount
        )
    )
}

It seems like the loadFontEx wrapper method needs to be tweaked so it can handle the case where codepoints is null and substitute in MemorySegment.NULL.

Activity

  1. electronstudio commented on Dec 26, 2024

    @electronstudio
    Owner

    Thanks for finding this. Looks like another shit thing about Raylib API, no way to specify in the signature that the pointer is nullable yet behaviour requires it to be.

    Yes only solution I can think of is an if statement in the wrapper. Would need to check if there are any other such functions that require one too.

  2. added
    bug
    on Dec 26, 2024
  3. electronstudio commented on Dec 26, 2024

    @electronstudio
    Owner

    However the if statement will add overhead. The intention is that the majority of the wrapper should be able to be optimized away by a smart compiler, but I don't think think the if statement would be. (In theory function call could be inlined and an if statement that is always false could be removed, but I'd be surprised if java does this. We could investigate.)

    A nicer solution would be to overload the function and have one that doesn't have the codepoints parameter at all, but that would probably have to be written manually, which is bit annoying for an auto-generated binding.

  4. electronstudio commented on Dec 26, 2024

    @electronstudio
    Owner

    Have implemented the if statement fix because it was easiest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions