text bounds problem for sktextblob

140 views
Skip to first unread message

Xinhua Chen

unread,
Apr 18, 2022, 10:20:20 AM4/18/22
to skia-discuss
dear everyone:
I make the textBlob according to skshaper.main function is the below.

sk_sp<SkTextBlob> CreateSkTextBlob(const std::string& content,  const FontData& font, double maxWidth, Alignment type) {
    std::unique_ptr<SkShaper> fShaper;
    fShaper = SkShaper::Make();
    const char* utf8 = content.c_str();
    size_t utf8Bytes = content.size();
    SkShaper::PurgeCaches();
    SkTextBlobBuilderRunHandler builder(utf8, {0, 0});
    fShaper->shape(utf8, utf8Bytes, get_skia_Font(), true, maxWidth, &builder);
    return builder.makeBlob();
}

i get the textblob. and i draw the text the backgroud which is filled by the bounds of text. i found the bounds is larger in the right when the characters are letter or number.However the characters are chinese,the bounds is right. i use the font family is  Arial.The effect is below a picture. 
pic.png
In addition, i develop in the macos and win 10. i can display chinese In the mac but in the win10, i can not display chinese,if i use use Arial font.
I do not konw why the come out,and do not konw how to solve this problem.Hoping for everyone help.Give my best regards for your help.

Ben Wagner

unread,
Apr 18, 2022, 10:58:38 AM4/18/22
to skia-d...@googlegroups.com
So first of all, I'm not sure what your `content` is. If it has spaces at the end they will be included.

Secondly, SkTextBlob::bounds() usually reports the conservative (fast to calculate) bounds. In many fonts this means taking the pre-calculated (given by the font) bounds that will enclose any glyph in the font and using that as the conservative bounds of all glyphs. This will be fast (calculating the tight bounds of each glyph may be slow) but usually tight enough to allow things like quick rejection to work as expected. (like when the glyphs would be drawn completely outside the current clip they can just not be considered for drawing). The font used for the latin characters in your example must have some glyphs which are rather wide (not unexpected) so the bounds are not very tight.

Note that some fonts do not provide this pre-computed bounding box which encloses all glyphs, in which case SkTextBlob::bounds() will report a tight bounding box (it will need to do more work in this case, retrieving the bounds of all the glyphs used). See the private SkTextBlobBuilder::ConservativeRunBounds vs SkTextBlobBuilder::TightRunBounds. Unfortunately, SkTextBlobBuilder::make() does not currently take a flag to force the use of tight bounds. At the moment if you really want the tight bounds from an SkTextBlob it is unfortunately necessary to duplicate more or less what SkTextBlobBuilder::TightRunBounds does.

That being said, if you want to use SkShaper, the SkTextBlobBuilderRunHandler is provided only as a reference for how to use SkShaper to allow simple examples to work. In practice we have found that almost every user of SkShaper wants to handle something differently, be it how inter-line spacing works (like leading be all at the bottom or all at the top, or what double spacing means) or how intra-line spacing works (how do baselines align between different runs). As a result, every serious user of SkShaper has their own subclass of SkShaper::RunHandler which accumulates the data into their own structures.

--
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/e55778da-77a4-4ce1-a885-7cee769ff42an%40googlegroups.com.

Xinhua Chen

unread,
Apr 18, 2022, 9:55:58 PM4/18/22
to skia-discuss
Thank you very much for your detailed explanation.The content do not have  spaces at the end. In addition,there are some strange appearence, when i use the  SkShaper for different platforms.For example,chinese display,using different fonts in different platforms,some can display but some can not.  A continuous string without space can auto newline  in win 10, but can not  in macos when we set the special width for the shaper. i do not know why it happens,and how to solve it.Maybe those problems can solve when i use the   subclass of SkShaper::RunHandler. Hoping for further explanation,Your help is greatly appreciated.

bin he

unread,
May 2, 2022, 3:52:03 AM5/2/22
to skia-discuss
I have the same question, how to you deal ? thank you for any help.

Leon Tepe

unread,
Jun 5, 2022, 6:52:49 PM6/5/22
to skia-discuss
I'm trying to do something simple: center a TextBlob in the canvas, accounting for content width. For that reason, I need tight bounds. Currently, I'm using the conservative bounds. Is this there an alternative approach? This type of centering should be done all the time in Blink & co. I can't imagine that Skia doesn't provide an API for this? 

Xinhua Chen

unread,
Jun 5, 2022, 8:01:56 PM6/5/22
to skia-d...@googlegroups.com


'Leon Tepe' via skia-discuss <skia-d...@googlegroups.com>于2022年6月6日 周一06:52写道:

Roni Keller

unread,
Jun 8, 2022, 10:49:06 AM6/8/22
to skia-discuss
Calculate tight bounds of a string:
I usually make use of  SkFont::measureText() [https://api.skia.org/classSkFont.html#a490fd4fcd700036ed5965c609a8710c9]

Arial and Chinese symbols:
To my knowledge, there is no font that contains the complete UNICODE set.
The default font "Arial" of Windows 10 does not contain any Chinese characters.
You should give "Arial Unicode MS" a try.

Xinhua Chen

unread,
Jun 8, 2022, 9:40:43 PM6/8/22
to skia-discuss
Thank you very much for your reply. i use measureText api,too. for chinese symbols it can not give a accurate  bounds,maybe because of  not using arial unicode ms to replace arial. At last,  i resolve this problem by  using bungeman 's advice  to   derive SkShaper::RunHandler.  Thanks. Give my best regards to you.
Reply all
Reply to author
Forward
0 new messages