Extra2D/tools/msdf_font_builder/msdf_font_builder.h

144 lines
2.4 KiB
C
Raw Normal View History

#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