144 lines
2.4 KiB
C++
144 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include <glm/glm.hpp>
|
|
#include <nlohmann/json.hpp>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace extra2d {
|
|
namespace tools {
|
|
|
|
/**
|
|
* @brief 字符字形信息
|
|
*/
|
|
struct GlyphData {
|
|
char32_t codepoint = 0;
|
|
glm::vec2 uvMin;
|
|
glm::vec2 uvMax;
|
|
glm::vec2 size;
|
|
glm::vec2 bearing;
|
|
float advance = 0.0f;
|
|
|
|
double left = 0.0;
|
|
double bottom = 0.0;
|
|
double right = 0.0;
|
|
double top = 0.0;
|
|
};
|
|
|
|
/**
|
|
* @brief MSDF 字体图集元数据
|
|
*/
|
|
struct MSDFMetadata {
|
|
int fontSize = 48;
|
|
float pxRange = 4.0f;
|
|
int atlasWidth = 2048;
|
|
int atlasHeight = 2048;
|
|
int lineHeight = 60;
|
|
int baseline = 12;
|
|
std::vector<GlyphData> glyphs;
|
|
};
|
|
|
|
/**
|
|
* @brief MSDF 字体构建器
|
|
*
|
|
* 直接使用 msdfgen 库将 TTF 字体转换为 MSDF PNG 图集。
|
|
*/
|
|
class MSDFFontBuilder {
|
|
public:
|
|
MSDFFontBuilder();
|
|
~MSDFFontBuilder();
|
|
|
|
/**
|
|
* @brief 设置输入 TTF 字体文件路径
|
|
*/
|
|
void setInputFont(const std::string &path);
|
|
|
|
/**
|
|
* @brief 设置输出 PNG 文件路径
|
|
*/
|
|
void setOutputPath(const std::string &path);
|
|
|
|
/**
|
|
* @brief 设置字符集
|
|
*/
|
|
void setCharset(const std::string &charset);
|
|
|
|
/**
|
|
* @brief 设置字符集文件路径
|
|
*/
|
|
void setCharsetFile(const std::string &path);
|
|
|
|
/**
|
|
* @brief 设置字体大小
|
|
*/
|
|
void setFontSize(int size);
|
|
|
|
/**
|
|
* @brief 设置像素范围
|
|
*/
|
|
void setPxRange(float range);
|
|
|
|
/**
|
|
* @brief 设置图集尺寸
|
|
*/
|
|
void setAtlasSize(int width, int height);
|
|
|
|
/**
|
|
* @brief 构建字体图集
|
|
*/
|
|
bool build();
|
|
|
|
/**
|
|
* @brief 获取错误信息
|
|
*/
|
|
const std::string &getError() const { return error_; }
|
|
|
|
/**
|
|
* @brief 获取元数据
|
|
*/
|
|
const MSDFMetadata &getMetadata() const { return metadata_; }
|
|
|
|
private:
|
|
std::string inputFont_;
|
|
std::string outputPath_;
|
|
std::string charset_;
|
|
std::string charsetFile_;
|
|
int fontSize_ = 48;
|
|
float pxRange_ = 4.0f;
|
|
int atlasWidth_ = 2048;
|
|
int atlasHeight_ = 2048;
|
|
MSDFMetadata metadata_;
|
|
std::string error_;
|
|
|
|
struct Impl;
|
|
Impl *impl_ = nullptr;
|
|
|
|
/**
|
|
* @brief 加载字符集
|
|
*/
|
|
bool loadCharset();
|
|
|
|
/**
|
|
* @brief 生成所有字形的 MSDF
|
|
*/
|
|
bool generateAllGlyphs();
|
|
|
|
/**
|
|
* @brief 打包字形到图集
|
|
*/
|
|
bool packGlyphs();
|
|
|
|
/**
|
|
* @brief 保存 PNG 并嵌入元数据
|
|
*/
|
|
bool savePngWithMetadata();
|
|
|
|
/**
|
|
* @brief 生成 JSON 元数据
|
|
*/
|
|
std::string generateMetadataJson();
|
|
};
|
|
|
|
} // namespace tools
|
|
} // namespace extra2d
|