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

Improve skia text shaping & bounds calculation #103

Merged
merged 3 commits into from
Aug 26, 2024

Conversation

ckaiser
Copy link
Collaborator

@ckaiser ckaiser commented Aug 19, 2024

Fixes Skia crashes on Windows by using different bounds calculations and a more complete shape() function call.

@dacap dacap self-assigned this Aug 20, 2024
@aseprite-bot
Copy link
Collaborator

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Member

@dacap dacap left a comment

Choose a reason for hiding this comment

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

Hi @ckaiser, some changes to be made/test. You can test with text_shape example the whitespace bounds, we should find a way to fill that glyph bounds correctly.

Comment on lines +141 to +161
auto bidiRun = SkShaper::MakeBiDiRunIterator(text.c_str(), text.size(), 0xfe);
constexpr SkFourByteTag tag = SkSetFourByteTag('Z', 'y', 'y', 'y');
auto scriptRun = SkShaper::MakeScriptRunIterator(text.c_str(), text.size(), tag);
auto languageRun = SkShaper::MakeStdLanguageRunIterator(text.c_str(), text.size());
auto fontRun = SkShaper::MakeFontMgrRunIterator(text.c_str(),
text.size(),
skFont,
skFontMgr,
"Arial", // Fallback
SkFontStyle::Normal(),
&*languageRun);

shaper->shape(text.c_str(),
text.size(),
*fontRun,
*bidiRun,
*scriptRun,
*languageRun,
std::numeric_limits<float>::max(),
&shaperHandler);

Copy link
Member

Choose a reason for hiding this comment

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

It looks like MakeBiDiRunIterator and MakeScriptRunIterator functions are legacy too.

Probably we could do something similar to SkShaperPrimitive::shape() (using values instead of smart pointers for each run iterator):

void SkShaperPrimitive::shape(const char* utf8,
                              size_t utf8Bytes,
                              const SkFont& font,
                              bool leftToRight,
                              SkScalar width,
                              RunHandler* handler) const {
    std::unique_ptr<FontRunIterator> fontRuns(
            MakeFontMgrRunIterator(utf8, utf8Bytes, font, nullptr));
    if (!fontRuns) {
        return;
    }
    // bidi, script, and lang are all unused so we can construct them with empty data.
    TrivialBiDiRunIterator bidi{0, 0};
    TrivialScriptRunIterator script{0, 0};
    TrivialLanguageRunIterator lang{nullptr, 0};
    return this->shape(utf8, utf8Bytes, *fontRuns, bidi, script, lang, nullptr, 0, width, handler);
}

Anyway I tried to replace with these iterators and didn't work (I got a crash), so there is something I'm missing here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll look into it, it's very strange that we're getting different behaviors per-platform

Copy link
Member

Choose a reason for hiding this comment

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

Oh in this case your code worked on Linux 👍 I was just commenting this because it might be better if we avoid calling those legacy Make*RunIterator functions and create those iterators in the stack directly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The thing is that we need to use these functions because they have different implementations depending on what's available, so on a platform without harfbuzz or on a compile without it, it'll use a trivial one - etc. If we don't use them we'd have to include that logic ourselves.

The legacy stuff is kind of interesting too because the only place where that SK_DISABLE_LEGACY_SKSHAPER_FUNCTIONS is declared is in the Bazel compile scripts, not in the GN ones, so it's not even possible to disable these functions for anyone building aseprite atm. I think it's probably ok to keep using them for now.

text/skia_text_blob_shaper.cpp Show resolved Hide resolved
text/skia_font.cpp Show resolved Hide resolved
Comment on lines 72 to 74
// avgCharWidth can be 0, so we grab the next most useful thing, the height
bounds.w = metrics.avgCharWidth > 0 ? metrics.avgCharWidth : metrics.xHeight;
bounds.h = 1.0;
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this one, xHeight is the height of the lowercase x character. It feels wrong to use this value as the whitespace width (and hard coding 1.0 as the height).

To test this we can use the text_shape which is a mini text editor, put the cursor at a whitespace to test the whitespace bounds.

We should be able to set bounds.w = the advance in X axis for whitespaces, but I'm not sure where to find this value on Skia.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah this one's the most contentious change, the hardcoded 1 was to try and fix a specific bug of things being too tall in some circumstances, it should not stay like this. I need to look into more of the documentation

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm trying a new approach with just the normal " " character glyph bounds and it looks to behave correctly - at least more consistently than attempting to use FontMetrics. I think the ideal solution would be to shape the entire set of glyphs in one go with skia using getWidthsBounds but that'd mean a full refactor of how the runs are working currently.

@dacap dacap assigned ckaiser and unassigned dacap Aug 20, 2024
@aseprite-bot
Copy link
Collaborator

clang-tidy review says "All clean, LGTM! 👍"

@aseprite-bot
Copy link
Collaborator

clang-tidy review says "All clean, LGTM! 👍"

1 similar comment
@aseprite-bot
Copy link
Collaborator

clang-tidy review says "All clean, LGTM! 👍"

@ckaiser ckaiser assigned dacap and unassigned ckaiser Aug 22, 2024
@dacap dacap merged commit 90f64fe into aseprite:beta Aug 26, 2024
11 checks passed
@dacap
Copy link
Member

dacap commented Aug 26, 2024

Just merged 👍 with a couple of extra fixes: 583d045 and 06279da

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.

3 participants