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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions text/skia_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ int SkiaFont::height() const

int SkiaFont::textLength(const std::string& str) const
{
return m_skFont.measureText(
str.c_str(),
str.size(),
SkTextEncoding::kUTF8,
nullptr);
return std::ceil(
m_skFont.measureText(
str.c_str(),
str.size(),
SkTextEncoding::kUTF8,
nullptr)
);
ckaiser marked this conversation as resolved.
Show resolved Hide resolved
}

float SkiaFont::measureText(const std::string& str,
Expand Down
35 changes: 26 additions & 9 deletions text/skia_text_blob_shaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class ShaperRunHandler final : public SkShaper::RunHandler {
const gfx::PointF& offset,
TextBlob::RunHandler* subHandler)
: m_builder(utf8Text, os::to_skia(offset))
, m_subHandler(subHandler) { }
, m_subHandler(subHandler)
, m_buffer() { }
ckaiser marked this conversation as resolved.
Show resolved Hide resolved

sk_sp<SkTextBlob> makeBlob() {
return m_builder.makeBlob();
Expand Down Expand Up @@ -131,17 +132,33 @@ TextBlobRef SkiaTextBlob::MakeWithShaper(
ASSERT(dynamic_cast<SkiaFont*>(font.get()));

SkFont skFont = static_cast<SkiaFont*>(font.get())->skFont();
auto skFontMgr = static_cast<SkiaFontMgr*>(fontMgr.get())->skFontMgr();
sk_sp<SkTextBlob> textBlob;
gfx::RectF bounds;
if (auto shaper = SkShaper::Make(
static_cast<SkiaFontMgr*>(fontMgr.get())->skFontMgr())) {
if (auto shaper = SkShaper::Make(skFontMgr)) {
ShaperRunHandler shaperHandler(text.c_str(), { 0, 0 }, handler);
shaper->shape(
text.c_str(), text.size(),
skFont,
true,
std::numeric_limits<float>::max(),
&shaperHandler);

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);

Comment on lines +141 to +161
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.

textBlob = shaperHandler.makeBlob();
bounds = shaperHandler.bounds();
}
Expand Down
6 changes: 4 additions & 2 deletions text/text_blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ gfx::RectF TextBlob::RunInfo::getGlyphBounds(const size_t i) const
if (bounds.isEmpty()) {
FontMetrics metrics;
font->metrics(&metrics);
bounds.w = metrics.avgCharWidth;
bounds.h = -metrics.capHeight;
// 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.

}

ASSERT(!bounds.isEmpty());
bounds.offset(positions[i].x,
positions[i].y);
if (offsets) {
Expand Down
Loading