Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix: Enable FreeType font rendering for Web platform

esengine379c2c4b9e0a
diff --git a/CMakeLists.txt b/CMakeLists.txt
index df8b2f7ef..57f14cd11 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -244,7 +244,8 @@ if(NOT ES_BUILD_WEB AND NOT ES_BUILD_WXGAME)
target_include_directories(esengine SYSTEM PRIVATE ${QUICKJS_INCLUDE_DIR})
endif()
-target_link_libraries(esengine PUBLIC glm)
+target_link_libraries(esengine PUBLIC glm freetype)
+target_include_directories(esengine PUBLIC ${CMAKE_SOURCE_DIR}/third_party/freetype/include)
# Platform-specific settings
if(ES_BUILD_WEB OR ES_BUILD_WXGAME)
@@ -254,15 +255,14 @@ if(ES_BUILD_WEB OR ES_BUILD_WXGAME)
target_compile_definitions(esengine PUBLIC ES_PLATFORM_WXGAME)
endif()
else()
- # Native platform - link GLFW, GLAD, OpenGL, QuickJS, and FreeType
+ # Native platform - link GLFW, GLAD, OpenGL, QuickJS
find_package(OpenGL REQUIRED)
target_compile_definitions(esengine PUBLIC ES_PLATFORM_NATIVE)
# Development mode: use source directory for editor assets
target_compile_definitions(esengine PUBLIC ES_SOURCE_DIR="${CMAKE_SOURCE_DIR}")
- target_link_libraries(esengine PUBLIC glfw glad OpenGL::GL quickjs freetype)
+ target_link_libraries(esengine PUBLIC glfw glad OpenGL::GL quickjs)
target_include_directories(esengine PUBLIC
${CMAKE_SOURCE_DIR}/third_party/glad/include
- ${CMAKE_SOURCE_DIR}/third_party/freetype/include
)
endif()
diff --git a/src/esengine/ui/font/SDFFont.cpp b/src/esengine/ui/font/SDFFont.cpp
index 12428b8aa..9af8bb287 100644
--- a/src/esengine/ui/font/SDFFont.cpp
+++ b/src/esengine/ui/font/SDFFont.cpp
@@ -12,6 +12,10 @@
#include "SDFFont.hpp"
#include "../../core/Log.hpp"
+#include <ft2build.h>
+#include FT_FREETYPE_H
+#include FT_MODULE_H
+
#include <algorithm>
#include <cstring>
#include <fstream>
@@ -19,9 +23,6 @@
#ifdef ES_PLATFORM_WEB
#include <GLES3/gl3.h>
#else
- #include <ft2build.h>
- #include FT_FREETYPE_H
- #include FT_MODULE_H
#ifdef _WIN32
#include <windows.h>
#endif
@@ -34,18 +35,14 @@
namespace esengine::ui {
// =============================================================================
-// FreeType Data (Native only)
+// FreeType Data
// =============================================================================
-#ifndef ES_PLATFORM_WEB
struct SDFFont::FTData {
FT_Library library = nullptr;
FT_Face face = nullptr;
std::vector<u8> fontBuffer;
};
-#else
-struct SDFFont::FTData {};
-#endif
// =============================================================================
// Constructor / Destructor
@@ -57,7 +54,6 @@ SDFFont::~SDFFont() {
atlasTextureId_ = 0;
}
-#ifndef ES_PLATFORM_WEB
if (ftData_) {
if (ftData_->face) {
FT_Done_Face(ftData_->face);
@@ -66,7 +62,6 @@ SDFFont::~SDFFont() {
FT_Done_FreeType(ftData_->library);
}
}
-#endif
}
SDFFont::SDFFont(SDFFont&& other) noexcept
@@ -95,12 +90,10 @@ SDFFont& SDFFont::operator=(SDFFont&& other) noexcept {
if (atlasTextureId_ != 0) {
glDeleteTextures(1, &atlasTextureId_);
}
-#ifndef ES_PLATFORM_WEB
if (ftData_) {
if (ftData_->face) FT_Done_Face(ftData_->face);
if (ftData_->library) FT_Done_FreeType(ftData_->library);
}
-#endif
ftData_ = std::move(other.ftData_);
sdfSize_ = other.sdfSize_;
@@ -140,13 +133,6 @@ Unique<SDFFont> SDFFont::create(const std::string& path, f32 sdfSize, f32 sdfSpr
// Loading
// =============================================================================
-#ifdef ES_PLATFORM_WEB
-bool SDFFont::loadFromFile(const std::string& path, f32 sdfSize, f32 sdfSpread) {
- (void)path; (void)sdfSize; (void)sdfSpread;
- ES_LOG_WARN("SDFFont: Dynamic font loading not supported on Web platform");
- return false;
-}
-#else
bool SDFFont::loadFromFile(const std::string& path, f32 sdfSize, f32 sdfSpread) {
sdfSize_ = sdfSize;
sdfSpread_ = sdfSpread;
@@ -216,9 +202,14 @@ bool SDFFont::loadFromFile(const std::string& path, f32 sdfSize, f32 sdfSpread)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+#ifdef ES_PLATFORM_WEB
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, atlasWidth_, atlasHeight_, 0,
+ GL_LUMINANCE, GL_UNSIGNED_BYTE, atlasData_.data());
+#else
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, atlasWidth_, atlasHeight_, 0,
GL_RED, GL_UNSIGNED_BYTE, atlasData_.data());
+#endif
glBindTexture(GL_TEXTURE_2D, 0);
@@ -229,7 +220,6 @@ bool SDFFont::loadFromFile(const std::string& path, f32 sdfSize, f32 sdfSpread)
return true;
}
-#endif
// =============================================================================
// Glyph Loading
@@ -250,12 +240,6 @@ const SDFGlyphInfo* SDFFont::getGlyph(u32 codepoint) {
return loadGlyph(codepoint);
}
-#ifdef ES_PLATFORM_WEB
-SDFGlyphInfo* SDFFont::loadGlyph(u32 codepoint) {
- (void)codepoint;
- return nullptr;
-}
-#else
SDFGlyphInfo* SDFFont::loadGlyph(u32 codepoint) {
if (!ftData_ || !ftData_->face) return nullptr;
@@ -337,8 +321,13 @@ SDFGlyphInfo* SDFFont::loadGlyph(u32 codepoint) {
glyph.v1 = static_cast<f32>(atlasY + glyphH) / static_cast<f32>(atlasHeight_);
glBindTexture(GL_TEXTURE_2D, atlasTextureId_);
+#ifdef ES_PLATFORM_WEB
+ glTexSubImage2D(GL_TEXTURE_2D, 0, atlasX, atlasY, glyphW, glyphH,
+ GL_LUMINANCE, GL_UNSIGNED_BYTE, bitmap.buffer);
+#else
glTexSubImage2D(GL_TEXTURE_2D, 0, atlasX, atlasY, glyphW, glyphH,
GL_RED, GL_UNSIGNED_BYTE, bitmap.buffer);
+#endif
glBindTexture(GL_TEXTURE_2D, 0);
markAtlasRegionUsed(atlasX, atlasY, glyphW + padding, glyphH + padding);
@@ -352,7 +341,6 @@ SDFGlyphInfo* SDFFont::loadGlyph(u32 codepoint) {
return &insertIt->second;
}
-#endif
// =============================================================================
// LRU Cache Management
diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt
index b05135b5e..81e04744b 100644
--- a/third_party/CMakeLists.txt
+++ b/third_party/CMakeLists.txt
@@ -92,13 +92,14 @@ if(NOT ES_BUILD_WEB AND NOT ES_BUILD_WXGAME)
target_compile_definitions(quickjs PUBLIC ES_SCRIPTING_ENABLED)
- # =============================================================================
- # FreeType - Font rendering library
- # =============================================================================
- set(FT_DISABLE_ZLIB ON CACHE BOOL "" FORCE)
- set(FT_DISABLE_BZIP2 ON CACHE BOOL "" FORCE)
- set(FT_DISABLE_PNG ON CACHE BOOL "" FORCE)
- set(FT_DISABLE_HARFBUZZ ON CACHE BOOL "" FORCE)
- set(FT_DISABLE_BROTLI ON CACHE BOOL "" FORCE)
- add_subdirectory(freetype)
endif()
+
+# =============================================================================
+# FreeType - Font rendering library (All platforms including Web)
+# =============================================================================
+set(FT_DISABLE_ZLIB ON CACHE BOOL "" FORCE)
+set(FT_DISABLE_BZIP2 ON CACHE BOOL "" FORCE)
+set(FT_DISABLE_PNG ON CACHE BOOL "" FORCE)
+set(FT_DISABLE_HARFBUZZ ON CACHE BOOL "" FORCE)
+set(FT_DISABLE_BROTLI ON CACHE BOOL "" FORCE)
+add_subdirectory(freetype)