wasm: support default font initialization

Load default font data during initialization. This enables the web system to load when the system font cannot be used.

Added a binary-embeddable font file resource. This file has quite small size while containing essential glyphs.

- DM Sans: https://github.com/googlefonts/dm-fonts
This commit is contained in:
Jinny You 2025-02-20 17:12:06 +08:00 committed by Hermet Park
parent 646e35484f
commit 3737632e13
3 changed files with 118 additions and 1 deletions

View file

@ -1,5 +1,8 @@
if cc.get_id() == 'emscripten' if cc.get_id() == 'emscripten'
source_file = files('tvgWasmLottieAnimation.cpp') source_file = [
'tvgWasmDefaultFont.h',
'tvgWasmLottieAnimation.cpp'
]
thorvg_wasm_dep = declare_dependency(include_directories thorvg_wasm_dep = declare_dependency(include_directories
: include_directories('.'), sources : include_directories('.'), sources
: source_file) : source_file)

File diff suppressed because one or more lines are too long

View file

@ -24,6 +24,7 @@
#include <emscripten.h> #include <emscripten.h>
#include <emscripten/bind.h> #include <emscripten/bind.h>
#include "tvgPicture.h" #include "tvgPicture.h"
#include "tvgWasmDefaultFont.h"
#ifdef THORVG_WG_RASTER_SUPPORT #ifdef THORVG_WG_RASTER_SUPPORT
#include <emscripten/html5_webgpu.h> #include <emscripten/html5_webgpu.h>
#endif #endif
@ -115,6 +116,10 @@ struct TvgEngineMethod
{ {
return val(typed_memory_view<uint8_t>(0, nullptr)); return val(typed_memory_view<uint8_t>(0, nullptr));
} }
void loadFont() {
Text::load("default", reinterpret_cast<const char*>(DEFAULT_FONT), DEFAULT_FONT_SIZE, "ttf", false);
}
}; };
struct TvgSwEngine : TvgEngineMethod struct TvgSwEngine : TvgEngineMethod
@ -130,6 +135,7 @@ struct TvgSwEngine : TvgEngineMethod
Canvas* init(string&) override Canvas* init(string&) override
{ {
Initializer::init(0, tvg::CanvasEngine::Sw); Initializer::init(0, tvg::CanvasEngine::Sw);
loadFont();
return SwCanvas::gen(); return SwCanvas::gen();
} }
@ -175,6 +181,7 @@ struct TvgWgEngine : TvgEngineMethod
#endif #endif
Initializer::init(0, tvg::CanvasEngine::Wg); Initializer::init(0, tvg::CanvasEngine::Wg);
loadFont();
return WgCanvas::gen(); return WgCanvas::gen();
} }
@ -220,6 +227,7 @@ struct TvgGLEngine : TvgEngineMethod
#endif #endif
if (Initializer::init(0, tvg::CanvasEngine::Gl) != Result::Success) return nullptr; if (Initializer::init(0, tvg::CanvasEngine::Gl) != Result::Success) return nullptr;
loadFont();
return GlCanvas::gen(); return GlCanvas::gen();
} }