Drawing text using skparagraph

843 views
Skip to first unread message

Hemanth

unread,
Aug 14, 2024, 6:05:50 AM8/14/24
to skia-discuss
Hello everyone,

I'm trying to draw text in canvas using skparagraph
I included skia_m118 libs with c++/winrt wrapper class for windows application (UWP app)
I did similarly for both Android and MacOs there it is working fine

but i'm getting exception at this line
paragraph->layout(200);
exception :
Unhandled exception at 0x00007FF94CC743D5 (SwapchainCpp.dll) in SwapchainCircle.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.

builder creation:
std::unique_ptr<ParagraphBuilder> builder = skia::textlayout::ParagraphBuilder::make(paraStyle, fontCollection);
build for paragraph:
std::unique_ptr<Paragraph> paragraph = builder->Build();

instead of creating paragraph and pragraphbuilder pointers its creating
paragraphimpl and paragraphbuilderimpl which are not even in my include and source folders 

i attached both screenshots below
ss-1.pngss-2.png


Thank you very much.

jlav...@google.com

unread,
Aug 14, 2024, 9:56:49 AM8/14/24
to skia-discuss
ParagraphBuilder and Paragraph are abstract classes (interfaces). We provide one implementation (ParagraphBuilderImpl and ParagraphImpl) but a user can change it (not advised).
The sources for these implementations are in skia library (skparagraph).
Something else causes your unhandled exception. Could you provide more information, please? Call stack at the point of the exception?

Hemanth

unread,
Aug 19, 2024, 8:44:44 AM8/19/24
to skia-discuss

Hello,

Sorry for the late reply
I've created c# uwp application and used c++/winrt application as wrapper for integrating it with skia.
Here i've created SkFontStyle, TextStyle, ParagraphStyle and ParagraphBuilder for drawing text.

But it's throwing exception:
Exception thrown at 0x00007FF90DEA1A17 (SwapchainCpp.dll) in SwapchainCircle.exe: 0xC0000005: Access violation reading location 0x0000000000000010.
at line - 1711
para.get()->layout();

I included both my code and call stack below
ss5.pngss6.png

Any help would be much appreciated

Thank you.

jlav...@google.com

unread,
Aug 19, 2024, 9:30:03 AM8/19/24
to skia-discuss
The crash is unacceptable and should be fixed but I noticed also that you are using GetMaxIntrinsicWidth before calling Layout.
That would not work. We only calculate all metrics as a part of layout.
If I understand correctly what you want to do you will have to call Layout twice: first time to get your width and then the second - to
do the actual layout. Don't worry, the second call is going to be cheap, this is the standard approach for formatting text.

Hemanth

unread,
Aug 19, 2024, 1:24:58 PM8/19/24
to skia-discuss
I'll give brief overview of what I have done so far,

I created C# uwp blank app and created SwapchainPanel and then I send it to C++/winrt for using it as surface for rendering.

Then I've generated skia libs using below arguments in MSVC compiler for windows:
bin/gn gen out/Release --args="is_debug=false is_official_build=true is_component_build=false skia_use_system_expat=false skia_use_system_libjpeg_turbo=false skia_use_system_libpng=false skia_use_system_libwebp=false skia_use_system_zlib=false skia_use_system_icu=false skia_use_system_harfbuzz=false skia_use_gl=false skia_use_direct3d=true target_cpu=\"x64\" skia_enable_skparagraph=true skia_enable_skshaper=true cc=\"clang-cl\" cxx=\"clang-cl\" extra_cflags=[\"/MD\"]"

I've tried both skia m113 and m118 versions

Previously, I've tried with drawtextblob for drawing it is working fine.
When I try to use SkParagraph I'm getting the exception.

As per your advice, I tried to give direct value for paraWidth, but it still giving same error when I try to call layout.
I followed same process for MacOs and Android. There it is working properly.

I'll add windows code, windows call stack and Android call stack pictures below for comparison. 

ss7.pngss8.pngandroidstack.png

Thank you.


jlav...@google.com

unread,
Aug 20, 2024, 10:35:05 AM8/20/24
to skia-discuss
Ok. The crash is still unacceptable and must be fixed (I will fix it once I figure out how you get there).
The fact that this code works on MacOs and Android however tells me that this time the difference is in the environment.
The usual suspect in such a case is the font. I would suggest that the Roboto font is not found and as the result the layout data not filled,
and as the result any attempt to use them fails (should not crash, though). Could you try another font/make sure Roboto is available?
And probably set fontCollection->enableFontFallback(); - that SHOULD work.
Also, I noticed that you are using the very old version of Skia. In January 2024 we switched from SkFontMgr::RefDefault() to ToolUtils::TestFontMgr().
I am not sure if it's going to fix your problem but it's a good idea in general to switch to the current Skia version.
If you switch and it still does not work I suggest something like debugging session online. 

Hemanth

unread,
Aug 21, 2024, 1:38:53 AM8/21/24
to skia-discuss
Hello,

As you said "Roboto" is not available
I checked it using typeface
bool IsFontAvailable(const char* fontFamily, SkFontStyle fontStyle) {
    sk_sp<SkFontMgr> fontMgr = SkFontMgr::RefDefault();
    sk_sp<SkTypeface> typeface = sk_ref_sp(fontMgr->matchFamilyStyle(fontFamily, fontStyle));

    return typeface != nullptr;
}

"Marlett" font is available and I set fontCollection->enableFontFallback();
but still I'm getting same crash, so kindly check this and let me know to do further changes to get this fixed.

This code is m113 version. I have tried the same with m118 version but getting same crash at para.get()->layout(SK_ScalarInfinity);

Thank you.

jlav...@google.com

unread,
Aug 21, 2024, 9:21:37 AM8/21/24
to skia-discuss
Ok. At this point all I could think of as a next step is to run your code in my environment.
Could you please send it to me (exactly as it crashes) in a text form (I don't want to retype it from the image above).
I have little hope that I could reproduce your problem (especially given that we have different versions) but
I may figure out what could go wrong in general.

Hemanth

unread,
Aug 21, 2024, 2:08:05 PM8/21/24
to skia-discuss
Hi,

I'm using windows machine, Visual Studio 2022, Skia version - m113
Getting crash at line "para.get()->layout(SK_ScalarInfinity);"

Code:
auto fontSize = 10 * 1.33;// 1.33 for converting points to pixel
SkFontStyle fontStyle;
TextStyle textStyle;
setFontProps(&fontStyle, false, false);
textStyle.setFontSize(fontSize);
textStyle.setFontStyle(fontStyle);
textStyle.setForegroundColor(paint1);
textStyle.setFontFamilies({ SkString("Marlett") });
setTextDecoration(&textStyle, false, false);
skia::textlayout::ParagraphStyle paraStyle;
paraStyle.setMaxLines(1);
switch (vAlignmentTag) {
case 0:paraStyle.setTextAlign(skia::textlayout::TextAlign::kLeft);break;
case 1:paraStyle.setTextAlign(skia::textlayout::TextAlign::kRight);break;
case 2:paraStyle.setTextAlign(skia::textlayout::TextAlign::kCenter);break;
}
auto fontCollection = sk_make_sp<FontCollection>();
fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
fontCollection->enableFontFallback();

std::unique_ptr<ParagraphBuilder> builder = ParagraphBuilder::make(paraStyle, fontCollection);
builder->pushStyle(textStyle);
builder->addText("cell", 4);
std::unique_ptr<Paragraph> para = builder->Build();
auto rect = SkRect();
rect.fTop = y;
rect.fBottom = y + rowHt;
rect.fLeft = x;
rect.fRight = x + colWidth - 1;
auto paraLeft = rect.fLeft;
auto paraTop = rect.fTop;
para.get()->layout(SK_ScalarInfinity); // crash here
std::vector<SkColor> colorVector(skColors, skColors + sizeof(skColors) / sizeof(SkColor));
SkColor randomColor = colorVector[std::rand() % colorVector.size()];
canvas->save();
canvas->clipRect(rect, true);
SkPaint paint = SkPaint();
paint.setColor(randomColor);
canvas->drawPaint(paint);
canvas->restore();

Thank you.

jlav...@google.com

unread,
Aug 21, 2024, 4:02:53 PM8/21/24
to skia-discuss
I am looking for a Windows machine on this but even before I try it...
In the command line you had above these is nothing about unicode which SkParagraph is using.
--skia_use_icu=true

I would imagine that if unicode is not present it could give you an error like you have.
Could you please build/run your test with --skia_use_icu=true ?

Hemanth

unread,
Aug 22, 2024, 3:48:20 AM8/22/24
to skia-discuss

I have tried with this command "skia_use_icu=true" but still same crash is happening.

jlav...@google.com

unread,
Aug 22, 2024, 10:17:50 AM8/22/24
to skia-discuss
I tried your code on my home Windows machine and it does not crash :(
I think the only option we have is a video call trying to debug you code (if you can).
For that however I need a debug version of Skia built.

Brian Osman

unread,
Aug 22, 2024, 10:24:19 AM8/22/24
to skia-d...@googlegroups.com
One other thing to double-check: When you build Skia, are can you try setting `use_trivial_abi` to false in your GN arguments?

--
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/8c840af7-4e60-403c-b11a-ea224cf2b48cn%40googlegroups.com.

jlav...@google.com

unread,
Aug 22, 2024, 10:26:33 AM8/22/24
to skia-discuss
What is that? I will try it of course but I didn't find any mention of it in the entire Skia folder.

Brian Osman

unread,
Aug 22, 2024, 10:29:50 AM8/22/24
to skia-d...@googlegroups.com
Sorry - that's for Hemanth to try. Depending on how the application is being built (especially if it's with a different compiler or build system), it could result in incompatible code generation, causing the library to make different assumptions about function calling vs. the code that's calling into Skia. Setting use_trivial_abi to false should ensure that the code is built in a compatible way.

Hemanth

unread,
Aug 23, 2024, 5:23:44 AM8/23/24
to skia-discuss
Hello,

I've tried it in many ways still getting same crash.
If possible we can have a call today whenever you are free (IST time).
Please tell me, what are all the requirements that are needed before the call.

Thank you.

Hemanth

unread,
Aug 30, 2024, 10:15:12 AM8/30/24
to skia-discuss
SkUnicode_icu.cpp file
ss9.png

if I comment #if condition completely from line 488 to 496 then Text is rendering properly using SkParagraph

I'm unable find out why SkLoadICU() is returning false value here

icudtl.dat file is generating in out/Release folder and i included it in my C++/winrt project

i generated icudtl.dat file using (skia_use_icu=true  skia_use_system_icu=false) and (skia_use_system_icu=true)

in both ways it is not working

can someone help me how should i proceed further in this approach? 


Thank you.

Hemanth

unread,
Sep 2, 2024, 3:42:51 AM9/2/24
to skia-discuss
Hello,

Text is drawing using SkParagraph now.
The main reason for that crash was because it is trying to find icudt.dat file in bin/x64/Debug/Appx path but it was not present there.
MakeIcuBasedUnicode() function in SkUnicode_icu.cpp it is calling SkLoadICU() function(SkLoadICU.cpp) in that it's expecting icudt.dat file either in executable_directory or in library_directory.
I included dat file in that path and it is working fine now.

Is it a proper way to fix the issue or is there any better way to do it?

Thanks for your help.
Reply all
Reply to author
Forward
0 new messages