优化可读性

This commit is contained in:
Nanako 2025-04-06 20:15:49 +08:00
parent 0ac8367b43
commit 3d58348c12
29 changed files with 106 additions and 88 deletions

3
.gitmodules vendored
View File

@ -4,3 +4,6 @@
[submodule "src/mirage_render/font/freetype_font/harfbuzz"]
path = src/mirage_render/font/freetype_font/harfbuzz
url = https://github.com/harfbuzz/harfbuzz
[submodule "src/mirage_widget/third_party/tomlplusplus"]
path = src/mirage_widget/third_party/tomlplusplus
url = https://github.com/marzer/tomlplusplus.git

View File

@ -1,92 +1,106 @@
#include "font_atlas_manager.h"
/**
* @brief
*
* @tparam AtlasType ( glyph_atlas_t)
* @param key
* @param atlases / ()
* @param current_atlas_index ()
* @param create_new_atlas_func
* @return std::optional std::nullopt
*/
template <typename AtlasType>
std::optional<atlas_region_t> find_or_create_in_atlases(
const char_key_t& key,
std::vector<AtlasType>& atlases,
size_t& current_atlas_index,
const std::function<void(const Eigen::Vector2i&)>& create_new_atlas_func)
{
glyph_atlas_result_t result{};
result.reason = glyph_atlas_reason_t::unknown; // 默认为失败
// **1. 尝试在现有图集中查找**
for (auto& atlas: atlases) {
glyph_atlas_result_t find_result = atlas.find_in_cache(key);
if (find_result.reason == glyph_atlas_reason_t::success) {
return find_result.region; // 找到了!
}
}
// **2. 尝试在当前图集中创建**
if (!atlases.empty() && current_atlas_index < atlases.size()) {
result = atlases[current_atlas_index].create_glyph(key);
if (result.reason == glyph_atlas_reason_t::success) {
return result.region; // 在当前图集中创建成功!
}
}
else {
// 如果没有图集或当前索引无效,则模拟图集已满以触发创建流程
result.reason = glyph_atlas_reason_t::atlas_full;
}
// **3. 如果当前图集已满 (或不存在), 创建一个新图集并尝试创建**
if (result.reason == glyph_atlas_reason_t::atlas_full) {
const Eigen::Vector2i new_atlas_size = { 2048, 2048 }; // 示例大小
// 调用传入的函数来创建特定类型的新图集
create_new_atlas_func(new_atlas_size);
// 检查新图集是否真的被创建了
if (atlases.empty()) {
// 错误处理:图集创建失败?
// 可以添加日志记录
return std::nullopt; // 无法继续
}
// 更新当前图集索引为新创建的图集 (最后一个)
current_atlas_index = atlases.size() - 1;
// 尝试在新图集中创建字形 (直接访问最后一个元素)
result = atlases.back().create_glyph(key);
if (result.reason == glyph_atlas_reason_t::success) {
return result.region; // 在新图集中创建成功!
}
}
// **4. 如果所有尝试都失败,返回 nullopt**
return std::nullopt;
}
std::optional<atlas_region_t> font_atlas_manager::get_or_create_glyph(
int32_t in_glyph_id,
const std::shared_ptr<font_face_interface>& in_font,
float in_font_size,
bool is_emoji) {
glyph_atlas_result_t result{};
// 初始化原因,循环/创建尝试会更新它
result.reason = glyph_atlas_reason_t::unknown; // 默认为失败
const int32_t in_glyph_id,
const std::shared_ptr<font_face_interface>& in_font,
const float in_font_size,
const bool is_emoji) {
const char_key_t key(in_glyph_id, in_font, in_font_size);
char_key_t key(in_glyph_id, in_font, in_font_size);
if (is_emoji) {
// **处理表情符号**
// 使用 lambda 传递特定的新图集创建函数
auto create_new_atlas_lambda = [this](const Eigen::Vector2i& size) {
this->create_new_emoji_atlas(size);
};
return find_or_create_in_atlases(
key,
emoji_atlases_, // 表情图集列表
current_emoji_atlas_, // 当前表情图集索引
create_new_atlas_lambda // 创建新表情图集的函数
);
}
if (is_emoji) {
// **1. 尝试在现有表情符号图集中查找**
for (auto& atlas : emoji_atlases_) {
glyph_atlas_result_t find_result = atlas.find_in_cache(key);
if (find_result.reason == glyph_atlas_reason_t::success) {
return find_result.region; // 找到了!
}
}
// **2. 尝试在当前表情符号图集中创建**
if (!emoji_atlases_.empty() && current_emoji_atlas_ < emoji_atlases_.size()) {
result = emoji_atlases_[current_emoji_atlas_].create_glyph(key);
if (result.reason == glyph_atlas_reason_t::success) {
return result.region;
}
} else {
result.reason = glyph_atlas_reason_t::atlas_full; // 模拟已满以触发创建
}
// **3. 如果当前表情符号图集已满(或不存在),创建一个新图集**
if (result.reason == glyph_atlas_reason_t::atlas_full) {
const Eigen::Vector2i new_atlas_size = { 2048, 2048 }; // 示例大小
// 创建新的表情符号图集
create_new_emoji_atlas(new_atlas_size);
// 更新当前表情符号图集索引为新创建的图集
current_emoji_atlas_ = emoji_atlases_.size() - 1;
// 尝试在新图集中创建字形
result = emoji_atlases_[current_emoji_atlas_].create_glyph(key);
if (result.reason == glyph_atlas_reason_t::success) {
return result.region;
}
}
} else { // 普通字形(非表情符号)
// **1. 尝试在现有字形图集中查找**
for (auto& atlas : glyph_atlases_) {
glyph_atlas_result_t find_result = atlas.find_in_cache(key);
if (find_result.reason == glyph_atlas_reason_t::success) {
return find_result.region; // 找到了!
}
}
// **2. 尝试在当前字形图集中创建**
if (!glyph_atlases_.empty() && current_glyph_atlas_ < glyph_atlases_.size()) {
result = glyph_atlases_[current_glyph_atlas_].create_glyph(key);
if (result.reason == glyph_atlas_reason_t::success) {
return result.region;
}
} else {
result.reason = glyph_atlas_reason_t::atlas_full; // 模拟已满以触发创建
}
// **3. 如果当前字形图集已满(或不存在),创建一个新图集**
if (result.reason == glyph_atlas_reason_t::atlas_full) {
const Eigen::Vector2i new_atlas_size = { 2048, 2048}; // 示例大小
// 创建新的字形图集
create_new_glyph_atlas(new_atlas_size);
// 更新当前字形图集索引为新创建的图集
current_glyph_atlas_ = glyph_atlases_.size() - 1;
// 尝试在新图集中创建字形
result = glyph_atlases_[current_glyph_atlas_].create_glyph(key);
if (result.reason == glyph_atlas_reason_t::success) {
return result.region;
}
}
}
// **4. 如果所有尝试都失败返回nullopt**
return std::nullopt;
// **处理普通字形**
// 使用 lambda 传递特定的新图集创建函数
auto create_new_atlas_lambda = [this](const Eigen::Vector2i& size) {
this->create_new_glyph_atlas(size);
};
return find_or_create_in_atlases(
key,
glyph_atlases_, // 普通字形图集列表
current_glyph_atlas_, // 当前普通字形图集索引
create_new_atlas_lambda // 创建新普通字形图集的函数
);
}
bitmap_glyph_atlas& font_atlas_manager::create_new_glyph_atlas(const Eigen::Vector2i& atlas_size) {

View File

@ -61,8 +61,8 @@ public:
private:
std::vector<bitmap_glyph_atlas> glyph_atlases_; ///< 普通字形图集列表
std::vector<color_emoji_atlas> emoji_atlases_; ///< 表情符号图集列表
int32_t current_glyph_atlas_ = 0; ///< 当前字形图集索引
int32_t current_emoji_atlas_ = 0; ///< 当前表情图集索引
size_t current_glyph_atlas_ = 0; ///< 当前字形图集索引
size_t current_emoji_atlas_ = 0; ///< 当前表情图集索引
/**
* @brief

View File

@ -1,8 +1,8 @@
project(mirage_widget)
set(SRC_FILES)
retrieve_files(${CMAKE_CURRENT_SOURCE_DIR} SRC_FILES)
retrieve_files(${CMAKE_CURRENT_SOURCE_DIR}/src SRC_FILES)
add_library(${PROJECT_NAME} STATIC ${SRC_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(${PROJECT_NAME} PUBLIC mirage_core mirage_render)

@ -0,0 +1 @@
Subproject commit fea1d905f2d2a8ad830f1985fe879f4fd4601fe5