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

Fix journals not rendering Unicode text correctly (fixes #1403) #1405

Merged
merged 1 commit into from
Jun 25, 2023

Conversation

dgelessus
Copy link
Contributor

For the ST::string overloads of the text rendering methods, the firstClippedChar output parameter still counted in wchar_t units, not UTF-8 bytes as needed for indexing into ST::string.

For the ST::string overloads of the text rendering methods, the
firstClippedChar output parameter still counted in wchar_t units, not
UTF-8 bytes as needed for indexing into ST::string.
@Hoikas Hoikas linked an issue Jun 24, 2023 that may be closed by this pull request
Comment on lines +600 to +603
// Convert the firstClippedChar offset from wchar_t units to UTF-8 byte units.
// This is a bit inefficient, because it creates an actual UTF-8 string
// even though we just need the count, but string_theory has no better alternative.
*firstClippedChar = ST::string::from_wchar(wcharBuf.data(), firstClippedWchar).size();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any thoughts about this, @zrax?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, this conversion will probably go away again in the long term. I assume at some point we'll convert the rendering APIs to use ST::string all the way through, which makes it trivial to calculate firstClippedChar as an UTF-8 count.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string_theory does have a private API (_ST_PRIVATE::utf8_measure_from_utf16/32) that could do this, but I don't think we really want to rely on that. Another option, since the font rendering code currently (unfortunately) assumes that all renderable characters fit into a single wchar_t, is to write a simplified length calculation that just ignores the possibility of UTF-16 surrogates. It could look something like (completely untested)

static uint32_t WcharToUtf8Position(const wchar_t *text, uint32_t position)
{
    const wchar_t *end = text + position;
    uint32_t u8pos = 0;
    while (text < end) {
        if (*text < 0x80)
            u8pos += 1;
        else if (*text < 0x800)
            u8pos += 2;
        else /* Assumes all input characters are 0x0000 - 0xffff */
            u8pos += 3;
        ++text;
    }
    return u8pos;
}

Then you could just call *firstClippedChar = WcharToUtf8Position(wcharBuf.data(), firstClippedWchar)

But yeah, at some point we'll need to make the whole font rendering code work better with ST types, and break some of its faulty assumptions around wchar_t

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wanted to avoid writing a manual UTF-8 length calculation here - seems easy to get wrong in some way :/ I don't think it's a big enough performance/memory issue to be worth the effort, especially if we're going to throw it out again soon.

(Actually, the previous line wrapping code did an identical conversion elsewhere, which I was able to remove now, so this change shouldn't even make a difference at all.)

@dpogue dpogue merged commit 3c3264e into H-uru:master Jun 25, 2023
@dgelessus dgelessus deleted the fix_journal_unicode_rendering branch November 29, 2023 21:22
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

Successfully merging this pull request may close these issues.

Journal text rendering broken for non-English languages
4 participants