SkFont::measureText() with UTF8

291 views
Skip to first unread message

Heidi Kurz

unread,
Mar 22, 2021, 7:25:03 AM3/22/21
to skia-discuss
Hi Together,

I use this code SkFont::measureText() for my text_on_path method:

 const size_t letterNumber = wtext.size();

  std::vector<SkPoint> xy(letterNumber);
    SkScalar x = 0; 
    for (size_t i = 0; i < letterNumber; ++i) {
        xy[i].set(x, 0);
        x += font.measureText(&wtext[i], 1, SkTextEncoding::kUTF8, nullptr, &paint);
    }

The string is UTF8 encoded. 
This means that some letters has 2 bytes and not 1!
For example I have the string Kurfürstendamm. 
Kurfürstendamm has 14 letters, but it has 1 special letter ü and for this special letter UTF8 needs 2 bytes. So Kurfürstendamm needs 15 bytes.
I need for each letter the width!
But how can I now distinguish this two cases?







 

Shachar Langbeheim

unread,
Mar 22, 2021, 8:31:02 AM3/22/21
to skia-discuss
I copied the SkUTF::NextUTF8 / CountUTF8 in order to break the string into UTF8 segments. Skia maintainers - can this code become public?

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/52d637df-dc5b-4b45-9638-cbbada629737n%40googlegroups.com.

Ben Wagner

unread,
Mar 22, 2021, 10:12:01 AM3/22/21
to skia-d...@googlegroups.com
SkFont::measureText() with kUTF8 encoding adds up the default advance
width for each glyph that each codepoint maps to by default in the
font's character mapping table. It makes no attempt at text shaping or
layout. It is very low level and relying on it to measure a string of
text is likely not what is desired in any event. If this is really
what you want to do, you'll probably want SkFont::textToGlyphs and
then SkFont::getWidths to get the advance widths of the individual
glyphs. However, this will not do shaping and isn't generally useful
except for strings and fonts which are known constants (like in
tests). Generally one would want to use SkShaper in some way to do
full text shaping and then draw positioned glyphs.
> To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/CA%2B_KjGZhWUw8dyd1osSWpiB2Y2rENX9oz-4X3bqmdL12hc-bWg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages