From a88ef151d7b965fda6daea981e8f6c453226e4a7 Mon Sep 17 00:00:00 2001 From: daiqingshuang Date: Thu, 10 Apr 2025 17:19:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B0=B4=E5=B9=B3=E5=AF=B9?= =?UTF-8?q?=E9=BD=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmake/mingw_dll.cmake | 12 +++++ .../freetype_font/src/freetype_interface.cpp | 2 +- .../src/font/atlas/bitmap_glyph_atlas.cpp | 2 +- src/mirage_render/src/font/font_system.cpp | 46 +++++++++---------- src/mirage_render/src/font/font_system.h | 6 ++- .../src/render/render_elements.cpp | 8 ++-- .../src/shaders/mirage_text.slang | 1 + .../src/style/default_style.toml | 2 +- 8 files changed, 49 insertions(+), 30 deletions(-) diff --git a/cmake/mingw_dll.cmake b/cmake/mingw_dll.cmake index b2f03c9..da09762 100644 --- a/cmake/mingw_dll.cmake +++ b/cmake/mingw_dll.cmake @@ -10,6 +10,18 @@ function(auto_copy_mingw_dll target) # "libgcc_s_dw2-1.dll" "libgcc_s_seh-1.dll" "libwinpthread-1.dll" + "libbz2-1.dll" + "libbrotlidec.dll" + "libbrotlicommon.dll" + "libharfbuzz-0.dll" + "libpng16-16.dll" + "zlib1.dll" + "libglib-2.0-0.dll" + "libfreetype-6.dll" + "libgraphite2.dll" + "libintl-8.dll" + "libpcre2-8-0.dll" + "libiconv-2.dll" ) foreach(DLL ${MINGW_DLLS}) diff --git a/src/mirage_render/font/freetype_font/src/freetype_interface.cpp b/src/mirage_render/font/freetype_font/src/freetype_interface.cpp index a52ab59..9559d32 100644 --- a/src/mirage_render/font/freetype_font/src/freetype_interface.cpp +++ b/src/mirage_render/font/freetype_font/src/freetype_interface.cpp @@ -119,7 +119,7 @@ float freetype_interface::get_kerning(uint32_t in_first_glyph_id, uint32_t in_se if (FT_Get_Kerning(face_, in_first_glyph_id, in_second_glyph_id, FT_KERNING_DEFAULT, &kerning) != 0) { return 0.0f; } - return static_cast(kerning.x) / 64.0f; // FreeType返回的单位是1/64像素 + return kerning.x >> 6; } glyph_shaped_t freetype_interface::shape_glyph(uint32_t in_glyph_id) const { diff --git a/src/mirage_render/src/font/atlas/bitmap_glyph_atlas.cpp b/src/mirage_render/src/font/atlas/bitmap_glyph_atlas.cpp index 1f13cf1..51ca0e7 100644 --- a/src/mirage_render/src/font/atlas/bitmap_glyph_atlas.cpp +++ b/src/mirage_render/src/font/atlas/bitmap_glyph_atlas.cpp @@ -19,7 +19,7 @@ glyph_atlas_result_t bitmap_glyph_atlas::create_glyph(const char_key_t& in_key) } // 从图集分配空间,添加2像素的padding防止纹理采样时的边缘混合问题 - auto region = atlas_->allocate_region(bitmap->get_size(), { 2, 2 }); + const auto& region = atlas_->allocate_region(bitmap->get_size(), { 2, 2 }); if (!region) { result.reason = glyph_atlas_reason_t::atlas_full; return result; diff --git a/src/mirage_render/src/font/font_system.cpp b/src/mirage_render/src/font/font_system.cpp index 11784cc..e63c2f0 100644 --- a/src/mirage_render/src/font/font_system.cpp +++ b/src/mirage_render/src/font/font_system.cpp @@ -42,11 +42,15 @@ int font_manager::add_font(const std::filesystem::path& in_font_path, const std: return font_id; } -std::shared_ptr font_manager::get_font_for_code_point(uint32_t in_code_point) { - // 首先尝试主字体 - auto primary = get_primary_font(); - if (primary && primary->has_glyph(in_code_point)) { - return primary; +std::shared_ptr font_manager::get_font_for_code_point( + std::shared_ptr in_custom_primary_font, uint32_t in_code_point, uint32_t* out_glyph_index) { + if (in_custom_primary_font) { + uint32_t glyph_id = in_custom_primary_font->find_glyph_index(in_code_point); + if (glyph_id > 0) { + if (out_glyph_index) + *out_glyph_index = glyph_id; + return in_custom_primary_font; + } } // 对表情符号使用表情符号字体 @@ -61,13 +65,16 @@ std::shared_ptr font_manager::get_font_for_code_point(uint3 // 尝试所有其他字体 for (const auto& [id, font] : fonts_) { - if (id != primary_font_id_ && font->has_glyph(in_code_point)) { + uint32_t glyph_id = font->find_glyph_index(in_code_point); + if (id != primary_font_id_ && glyph_id > 0) { + if (out_glyph_index) + *out_glyph_index = glyph_id; return font; } } // 回退到主字体 - return primary; + return in_custom_primary_font; } text_layout_t font_manager::layout_text( @@ -105,7 +112,7 @@ text_layout_t font_manager::layout_text( /** * @brief 完成当前行的布局并准备下一行 */ - auto finish_line = [&]() { + auto finish_line = [&] { if (current_line.has_content) { // 更新总体尺寸 width = std::max(width, current_line.line_width); @@ -136,18 +143,11 @@ text_layout_t font_manager::layout_text( const bool is_emoji = emoji_detector::is_emoji(c); // 查找能够渲染此字符的字体 - auto using_font = primary_font; - auto glyph_index = using_font->find_glyph_index(c); + uint32_t glyph_index = 0; + const auto& using_font = get_font_for_code_point(primary_font, c, &glyph_index); - // 如果主字体不支持该字符,则尝试查找其他字体 if (glyph_index == 0) { - using_font = get_font_for_code_point(c); - if (!using_font) { - return; // 如果没有找到支持的字体,跳过该字符 - } - - // 使用新字体重新获取字形索引 - glyph_index = using_font->find_glyph_index(c); + return; // 如果没有找到支持的字体,跳过该字符 } // 设置当前字体大小 @@ -160,8 +160,7 @@ text_layout_t font_manager::layout_text( const auto& glyph_metrics = using_font->shape_glyph(glyph_index); // 获取或创建字形在字形图集中的区域 - const auto& region = get_or_create_glyph_by_index( - glyph_metrics.glyph_index, using_font, font_size, is_emoji); + const auto& region = get_or_create_glyph_by_index(glyph_metrics.glyph_index, using_font, font_size, is_emoji); if (!region) { return; // 如果无法获取或创建字形区域,跳过该字符 @@ -183,11 +182,12 @@ text_layout_t font_manager::layout_text( current_line.height = std::max(current_line.height, current_metrics.line_height); // 计算基线位置 - 基于当前行的上升距离 - float baseline = cursor_y + current_line.ascent; + const float baseline = cursor_y + current_line.ascent; + const int32_t size_y_diff = glyph_metrics.rect.size().y() - region->rect.size().y(); // 计算Y轴差异(字形位图高度与区域高度) // 计算字形绘制坐标 - float x = cursor_x + glyph_metrics.hori_bearing.x(); - float y = baseline + glyph_metrics.hori_bearing.y(); + float x = cursor_x + glyph_metrics.offset.x(); + float y = baseline + glyph_metrics.offset.y() + size_y_diff; // 添加字形位置信息到布局 auto& glyph_position = layout.glyphs.emplace_back(); diff --git a/src/mirage_render/src/font/font_system.h b/src/mirage_render/src/font/font_system.h index afc31e8..bd3661a 100644 --- a/src/mirage_render/src/font/font_system.h +++ b/src/mirage_render/src/font/font_system.h @@ -81,7 +81,11 @@ public: * @param in_code_point Unicode码点 * @return 最适合渲染该码点的字体 */ - std::shared_ptr get_font_for_code_point(uint32_t in_code_point); + std::shared_ptr get_font_for_code_point(uint32_t in_code_point, uint32_t* out_glyph_index = nullptr) { + auto primary = get_primary_font(); + return get_font_for_code_point(primary, in_code_point, out_glyph_index); + } + std::shared_ptr get_font_for_code_point(std::shared_ptr in_custom_primary_font, uint32_t in_code_point, uint32_t* out_glyph_index = nullptr); /** * @brief 获取主字体 diff --git a/src/mirage_render/src/render/render_elements.cpp b/src/mirage_render/src/render/render_elements.cpp index cd023d0..b36b6a1 100644 --- a/src/mirage_render/src/render/render_elements.cpp +++ b/src/mirage_render/src/render/render_elements.cpp @@ -193,14 +193,16 @@ void render_elements::make_rect( const Eigen::Vector2f& in_scale) { // 从几何体转换到窗口坐标 auto pos = in_geometry.local_to_window(in_pos); + auto size = in_size; if (pos.x() > window_size_.x() || pos.y() > window_size_.y()) { return; } if (has_any_flag(in_effect, draw_effect::pixel_snap)) { pos = dpi_helper::snap_to_pixel(pos); + size = dpi_helper::snap_to_pixel(size); } add_rect_to_batch(pos, - in_size, + size, in_color, in_param_a, in_param_b, @@ -279,10 +281,10 @@ void render_elements::make_text(const text_layout_t& in_layout, const Eigen::Vec for (const auto& position : in_layout.glyphs) { const auto& p = position; const auto& region = p.region; - const auto& size = p.size; + const auto& size = region.rect.size().cast(); const auto& uv = region.uv_rect; - if (auto texture = region.texture.lock()) { + if (const auto& texture = region.texture.lock()) { // 构建完整的批次键 batch_key new_key; new_key.pipeline = position.is_emoji ? image_pipeline_ : text_pipeline_; diff --git a/src/mirage_render/src/shaders/mirage_text.slang b/src/mirage_render/src/shaders/mirage_text.slang index f59d0e9..e98121f 100644 --- a/src/mirage_render/src/shaders/mirage_text.slang +++ b/src/mirage_render/src/shaders/mirage_text.slang @@ -12,6 +12,7 @@ struct PSInput { float4 position : SV_POSITION; float2 uv : TEXCOORD0; float4 color : COLOR; + float pixelSnap : TEXCOORD1; }; [shader("vertex")] diff --git a/src/mirage_widget/src/style/default_style.toml b/src/mirage_widget/src/style/default_style.toml index 4d7f257..0f22368 100644 --- a/src/mirage_widget/src/style/default_style.toml +++ b/src/mirage_widget/src/style/default_style.toml @@ -2,7 +2,7 @@ name = "default_style" version = "0.0.1" description = "mirage的默认样式" -author = "奶酪" +author = "11" [button]