I copied a piece of tutorial code to my project, trying to see if it works fine on my computer. However, it renders everthing except the text. My OS is Windows 11 and IDE is Microsoft Visual Studio 2022. Below is the code:
#include <include/core/SkCanvas.h>
#include <include/core/SkBitmap.h>
#include <include/core/SkPaint.h>
#include <include/core/SkTypeface.h>
#include <include/core/SkFont.h>
#include <include/codec/SkCodec.h>
#include <include/core/SkStream.h>
#include <include/encode/SkPngEncoder.h>
#include <include/core/SkFontMgr.h>
#include "file.h"
#include <iostream>
int main(int argc, char* const argv[]) {
SkBitmap bitmap;
SkImageInfo imageInfo = SkImageInfo::Make(480, 320, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
bitmap.allocPixels(imageInfo, imageInfo.minRowBytes());
SkCanvas canvas(bitmap);
SkPaint paint;
canvas.clear(SkColorSetARGB(0xFF, 0x14, 0x14, 0x14));
paint.setColor(SK_ColorWHITE);
SkFont font;
font.setSize(64);
sk_sp<SkFontMgr> fontMgr = SkFontMgr::RefEmpty();
sk_sp<SkTypeface> fTypeface = fontMgr->legacyMakeTypeface("Arial", SkFontStyle::Normal());
font.setTypeface(fTypeface);
SkString text("Hello, Skia!");
canvas.drawSimpleText(text.c_str(), text.size(), SkTextEncoding::kUTF8, 0, 64, font, paint);
SkFILEWStream file("hello_skia.png");
if (!file.isValid()) {
return 1;
}
return SkPngEncoder::Encode(&file, bitmap.pixmap(), {});
}
The expected output should be
Expected Output
However, my code outputs
My Output
I have tried to compile skia source code for different edtions. Here are the PowerShell commmands that I have tried:
bin/gn gen out/debug --args='clang_win="C:Program FilesLLVM" cc="clang" cxx="clang++" extra_cflags=["/MTd"] is_official_build=true is_debug=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_harfbuzz=false skia_use_icu=false'
and
bin/gn gen out/all --ide=vs --args="clang_win="C:Program FilesLLVM""
For the second command, it generates all.sin for me and HelloWorld.cpp works fine, which is really confusing to me.
2
Answers
Found the solution to the problem. To render the text, a SkTypeface variable needs to be declared, otherwise the string will not be rendered. I guess it is because of an update so SkFont stops setting the SkTypeface parameter to default when it is not explicitly declared.
I believe the correct way to get the fonts and set them for the typeface is to make use of fontConfig provided via
SkFontMgr_New_FontConfig
API.Here’s the code I got working on my system based of the above provided code and making use of SkFontMgr_New_FontConfig.
The above code will print the number of fonts available on the machine and will create the typeface based on what font is available.
NOTE:
fontconfig
library by running the following command:sudo apt-get install libfontconfig1-dev
target_link_libraries
in your CMakeLists.Hope this resolves the issue of the font rendering with Skia c++.