修复文本布局总大小计算

This commit is contained in:
Nanako 2025-04-04 13:57:35 +08:00
parent 759f482afe
commit 0c32af832d
2 changed files with 5 additions and 7 deletions

View File

@ -12,13 +12,13 @@ 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");
const auto& text_block = std::make_shared<mtext_block>();
text_block->set_text(U"Hello, World! 你好,世界!\n换行测试1111测试测试测试测试测试测试😀🐵🙏");
text_block->set_text(U"Hello, World! 你好,世界!\n换行测试1111测试测试测试测试,测试测试😀🐵🙏");
const auto& text_block2 = std::make_shared<mtext_block>();
text_block2->set_text(U"Hello, World!");

View File

@ -99,10 +99,6 @@ text_layout_t font_manager::layout_text(
// 完成当前行的布局
auto finish_line = [&] {
if (current_line.has_content) {
// 确保行高至少能容纳当前行的内容
float actual_line_height = current_line.ascent + current_line.descent;
current_line.height = std::max(current_line.height, actual_line_height);
// 更新总体尺寸
width = std::max(width, current_line.line_width);
height += current_line.height * line_spacing;
@ -169,7 +165,7 @@ text_layout_t font_manager::layout_text(
// 更新当前行的度量信息
current_line.ascent = std::max(current_line.ascent, current_metrics.ascent);
current_line.descent = std::max(current_line.descent, current_metrics.descent);
current_line.descent = std::min(current_line.descent, current_metrics.descent);
// 使用字体的行高
current_line.height = std::max(current_line.height, current_metrics.line_height);
@ -201,6 +197,8 @@ text_layout_t font_manager::layout_text(
prev_glyph_id = glyph_index; // 更新上一个字形索引
}
height += current_line.descent; // 最后一行需要减去下降高度
// 处理最后一行
finish_line();