From a0487a75c1ca706bc8d4f5cf46db4db565d8bd89 Mon Sep 17 00:00:00 2001 From: Nanako <469449812@qq.com> Date: Fri, 4 Apr 2025 13:10:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E6=9C=AC=E5=9E=82?= =?UTF-8?q?=E7=9B=B4=E5=A4=A7=E5=B0=8F=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/src/main.cpp | 2 +- src/mirage_render/src/font/font_system.cpp | 18 +++++-------- src/mirage_render/src/font/font_type.h | 4 --- .../src/render/render_elements.cpp | 25 +------------------ .../widget/leaf_widget/mtext_block.h | 4 +-- 5 files changed, 10 insertions(+), 43 deletions(-) diff --git a/example/src/main.cpp b/example/src/main.cpp index 17ac21e..db609cc 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -12,7 +12,7 @@ int main(int argc, char* argv[]) { mirage_app::get().init(); auto& manager = font_manager::instance(); - manager.add_font(L"C:/Users/46944/AppData/Local/Microsoft/Windows/Fonts/MapleMono-NF-CN-Regular.ttf"); + // manager.add_font(L"C:/Users/46944/AppData/Local/Microsoft/Windows/Fonts/MapleMono-NF-CN-Regular.ttf"); manager.add_font(L"C:/Windows/Fonts/msyh.ttc"); // manager.add_font(L"D:/Projects/noto-emoji-2.047/fonts/Noto-COLRv1.ttf"); manager.add_font(L"C:/Windows/Fonts/seguiemj.ttf"); diff --git a/src/mirage_render/src/font/font_system.cpp b/src/mirage_render/src/font/font_system.cpp index 54dded9..86d18cf 100644 --- a/src/mirage_render/src/font/font_system.cpp +++ b/src/mirage_render/src/font/font_system.cpp @@ -86,14 +86,14 @@ text_layout_t font_manager::layout_text( uint32_t prev_glyph_id = 0; // 上一个字形索引 // 当前行信息 - struct LineInfo { + struct line_info { float height = 0.0f; // 行总高度 float ascent = 0.0f; // 最大上升距离 float descent = 0.0f; // 最大下降距离 bool has_content = false; }; - LineInfo current_line; + line_info current_line; // 完成当前行的布局 auto finish_line = [&] { @@ -104,14 +104,14 @@ text_layout_t font_manager::layout_text( // 更新总体尺寸 width = std::max(width, cursor_x); - height += current_line.height; + height += current_line.height - current_line.descent; // 移到下一行 - cursor_y += current_line.height + line_spacing; + cursor_y += current_line.height * line_spacing; cursor_x = 0.0f; // 重置行信息 - current_line = LineInfo{}; + current_line = line_info{}; } }; @@ -173,7 +173,7 @@ text_layout_t font_manager::layout_text( // 特殊处理表情符号的高度 if (is_emoji) { // 确保有足够的空间容纳表情符号 - float emoji_height = glyph_metrics.rect.height() * 1.2f; + float emoji_height = glyph_metrics.rect.height() * line_spacing; current_line.height = std::max(current_line.height, emoji_height); } else { // 使用字体的行高 @@ -195,14 +195,8 @@ text_layout_t font_manager::layout_text( glyph_position.size = glyph_metrics.rect.size(); glyph_position.region = *region; - glyph_position.advance = glyph_metrics.advance.x(); - glyph_position.left_bearing = glyph_metrics.hori_bearing.x(); - glyph_position.hori_bearing_x = glyph_metrics.hori_bearing.x(); - // 更新光标位置 cursor_x += glyph_metrics.advance.x(); // 添加字形间距 - // cursor_x += glyph_metrics.hori_bearing.x() + glyph_metrics.rect.width(); - // cursor_x += glyph_metrics.lsb - glyph_metrics.rsb; // 添加字形间距 if (prev_glyph_id != 0) { // 添加字形间距 diff --git a/src/mirage_render/src/font/font_type.h b/src/mirage_render/src/font/font_type.h index 6a0b9dc..30d26e2 100644 --- a/src/mirage_render/src/font/font_type.h +++ b/src/mirage_render/src/font/font_type.h @@ -122,10 +122,6 @@ struct text_layout_t { Eigen::Vector2f position; // 屏幕位置 Eigen::Vector2f size; // 字形尺寸 atlas_region_t region; // 纹理图集区域 - - float advance; // 前进量 - float left_bearing; // 左侧间距 - float hori_bearing_x; // 水平基线X坐标 }; std::vector glyphs; // 所有字形位置 diff --git a/src/mirage_render/src/render/render_elements.cpp b/src/mirage_render/src/render/render_elements.cpp index 47f71a8..a837ed0 100644 --- a/src/mirage_render/src/render/render_elements.cpp +++ b/src/mirage_render/src/render/render_elements.cpp @@ -273,7 +273,7 @@ void render_elements::make_image(const Eigen::Vector2f& in_pos, const Eigen::Vec void render_elements::make_text(const text_layout_t& in_layout, const Eigen::Vector2f& in_pos, const Eigen::Vector2f& in_size, const geometry_t& in_geometry, const draw_effect& in_effect, const rect_color& in_color, float in_rotation_radians, const Eigen::Vector2f& in_pivot, const Eigen::Vector2f& in_scale) { - const auto& glyph_sampler = texture_sampler_builder::get_sampler(sampler_type::pixel_art); + const auto& glyph_sampler = texture_sampler_builder::get_sampler(sampler_type::anisotropic); const auto& emoji_sampler = texture_sampler_builder::get_sampler(sampler_type::anisotropic); for (const auto& position : in_layout.glyphs) { @@ -293,31 +293,8 @@ void render_elements::make_text(const text_layout_t& in_layout, const Eigen::Vec ensure_batch_compatibility(new_key); make_rect(real_pos, size, in_color, in_geometry, in_effect, {}, {}, {}, uv, in_rotation_radians, in_pivot, in_scale); - - // Debug 绘制字形图元线框 - // make_wireframe(real_pos, size, in_geometry, 1.0f, in_effect, { {1, 0, 0, 0.5 } }, in_rotation_radians, in_pivot, in_scale); - // Debug 绘制字形前进线 - make_line(real_pos, real_pos - Eigen::Vector2f{ p.left_bearing, 0 }, in_geometry, 5.0f, in_effect, { {0, 1, 0, 0.5 } }, in_rotation_radians, in_pivot, in_scale); - // Debug 绘制字形advance线 - Eigen::Vector2f advance_start = real_pos - Eigen::Vector2f{ p.left_bearing, -size.y() - 2 }; - static linear_color random_advance_color = { 0, 1, 1, 1 }; - // 随机颜色 - random_advance_color.r = static_cast(rand()) / static_cast(RAND_MAX); - random_advance_color.g = static_cast(rand()) / static_cast(RAND_MAX); - random_advance_color.b = static_cast(rand()) / static_cast(RAND_MAX); - make_line(advance_start, advance_start + Eigen::Vector2f{ p.advance, 0 }, in_geometry, 4.0f, in_effect, { random_advance_color }, in_rotation_radians, in_pivot, in_scale); } } - // Eigen::Vector2f font_ascent_start = in_pos; - // font_ascent_start.y() += in_layout; - // Eigen::Vector2f font_ascent_end = font_ascent_start; - // font_ascent_end.x() += in_layout.total_size.x(); - - // 绘制文本的基线 - // make_line(font_ascent_start, font_ascent_end, in_geometry, 1.0f, in_effect, in_color, in_rotation_radians, in_pivot, in_scale); - - // 绘制文本的线框 - make_wireframe(in_pos, in_size, in_geometry, 1.0f, in_effect, in_color, in_rotation_radians, in_pivot, in_scale); } void render_elements::make_wireframe(const Eigen::Vector2f& in_pos, const Eigen::Vector2f& in_size, diff --git a/src/mirage_widget/widget/leaf_widget/mtext_block.h b/src/mirage_widget/widget/leaf_widget/mtext_block.h index 5268266..1bed34b 100644 --- a/src/mirage_widget/widget/leaf_widget/mtext_block.h +++ b/src/mirage_widget/widget/leaf_widget/mtext_block.h @@ -45,8 +45,8 @@ private: std::u32string text_; text_layout_t layout_{}; - float font_size_ = 24.0f; - float line_spacing_ = 1.f; + float font_size_ = 15.0f; + float line_spacing_ = 1.2f; float max_width_ = 0.0f; std::shared_ptr font_; };