修复文本垂直大小计算
This commit is contained in:
parent
9282b9f214
commit
a0487a75c1
@ -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");
|
||||
|
@ -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) {
|
||||
// 添加字形间距
|
||||
|
@ -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<glyph_position_t> glyphs; // 所有字形位置
|
||||
|
@ -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<float>(rand()) / static_cast<float>(RAND_MAX);
|
||||
random_advance_color.g = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
|
||||
random_advance_color.b = static_cast<float>(rand()) / static_cast<float>(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,
|
||||
|
@ -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_face_interface> font_;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user