载入中...
搜索中...
未找到
Font.cpp
浏览该文件的文档.
1#include "font/Font.h"
2
3#include "common/Data.h"
4#include "common/Exception.h"
5#include "common/Resource.h"
8#include "image/ImageData.h"
9
10#include <simplesquirrel/simplesquirrel.hpp>
11
12#include <cstdint>
13#include <functional>
14#include <string>
15#include <vector>
16
17namespace eve {
18namespace font {
19
21
22Font::Font() = default;
23Font::~Font() = default;
24
25FontData *Font::newFontData(Data *data, int size) {
26 if (data == nullptr || data->getData() == nullptr || data->getSize() == 0)
27 throw eve::Exception("Cannot decode empty font data");
28
29 const auto *ptr = static_cast<const uint8_t *>(data->getData());
30 std::vector<uint8_t> bytes(ptr, ptr + data->getSize());
31
32 std::string uri;
33 if (auto *fd = dynamic_cast<filesystem::FileData *>(data))
34 uri = "file://" + fd->getFilename();
35
36 return new FontData(std::move(bytes), size, std::move(uri));
37}
38
39FontData *Font::newFontDataFromFile(std::string path, int size) {
40 if (path.empty())
41 throw eve::Exception("Font::newFontDataFromFile: empty path");
42
43 // Route through the unified resource cache: one decoded face per
44 // (path, size) pair, refreshed in place on file change.
45 const std::string key = eve::ResourceManager::makeKey(path, "size=" + std::to_string(size));
47 if (!resource)
48 throw eve::Exception("Could not load font file: %s", path.c_str());
49 return static_cast<FontData *>(resource);
50}
51
52void Font::expose(ssq::Table &table) {
53 auto cls = table.addClass(name, Font::create, false);
54 expose(cls);
55
56 auto fd = table.addClass<FontData>(
57 "FontData", std::function<FontData *()>([]() -> FontData * { return nullptr; }), true);
58 fd.addFunc("getSize", &FontData::getSize);
59 fd.addFunc("getAscent", &FontData::getAscent);
60 fd.addFunc("getDescent", &FontData::getDescent);
61 fd.addFunc("getLineHeight", &FontData::getLineHeight);
62 fd.addFunc("getBaseline", &FontData::getBaseline);
63 fd.addFunc("getFamilyName", &FontData::getFamilyName);
64 fd.addFunc("getStyleName", &FontData::getStyleName);
65 fd.addFunc("getGlyphCount", &FontData::getGlyphCount);
66 fd.addFunc("hasGlyph", &FontData::hasGlyph);
67 fd.addFunc("hasGlyphs", &FontData::hasGlyphs);
68 fd.addFunc("getWidth", &FontData::getWidth);
69 fd.addFunc("getKerning", &FontData::getKerning);
70 fd.addFunc("getGlyphWidth", &FontData::getGlyphWidth);
71 fd.addFunc("getGlyphHeight", &FontData::getGlyphHeight);
72 fd.addFunc("getGlyphBearingX", &FontData::getGlyphBearingX);
73 fd.addFunc("getGlyphBearingY", &FontData::getGlyphBearingY);
74 fd.addFunc("getGlyphAdvance", &FontData::getGlyphAdvance);
75 fd.addFunc("newGlyphImageData", &FontData::newGlyphImageData);
76}
77
78void Font::expose(ssq::Class &cls) {
79 cls.addFunc("getName", &Font::getName);
80 cls.addFunc("newFontData", &Font::newFontData);
81 cls.addFunc("newFontDataFromFile", &Font::newFontDataFromFile);
82}
83
84} // namespace font
85} // namespace eve
HSQOBJECT cls
Definition ECS.cpp:21
void * ptr
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:24
Light2D::Data * data
const char * name
Definition RockMesh.cpp:21
virtual std::string getName() const =0
Resource * get(std::string key)
Get the cached resource for key; on a miss, load it through the registered IAssetReloader providers a...
Definition Resource.cpp:56
static std::string makeKey(const std::string &path, const std::string &query="")
Build a cache key from a path and optional ?query parameters.
Definition Resource.cpp:30
static ResourceManager & getInstance()
Definition Resource.cpp:8
Resource is a game object that is managed by the ResourceManager. It can be loaded from a file or gen...
Definition Resource.h:35
Data buffer paired with a filename (used for type identification).
Definition FileData.h:12
CPU-side decoded font face (FreeType FT_Face + owned font bytes). Does not upload to GPU — rasterize ...
Definition FontData.h:23
float getWidth(std::string text) const
Definition FontData.cpp:212
int getGlyphHeight(int codepoint) const
Definition FontData.cpp:230
int getGlyphAdvance(int codepoint) const
Definition FontData.cpp:233
float getBaseline() const
Definition FontData.cpp:125
std::string getFamilyName() const
字体元信息。
Definition FontData.cpp:127
bool hasGlyphs(std::string text) const
Definition FontData.cpp:155
int getGlyphBearingX(int codepoint) const
Definition FontData.cpp:231
image::ImageData * newGlyphImageData(int codepoint)
Rasterize one glyph to RGBA8 ImageData (RGB white, A from FreeType gray). Returns a 0x0 ImageData if ...
Definition FontData.cpp:235
int getGlyphBearingY(int codepoint) const
Definition FontData.cpp:232
float getKerning(int leftCodepoint, int rightCodepoint) const
Definition FontData.cpp:167
float getAscent() const
Definition FontData.cpp:106
int getSize() const
字号 / 度量信息。
Definition FontData.cpp:104
int getGlyphCount() const
Definition FontData.cpp:139
std::string getStyleName() const
Definition FontData.cpp:133
float getLineHeight() const
Definition FontData.cpp:119
float getDescent() const
Definition FontData.cpp:112
bool hasGlyph(int codepoint) const
字形 / 文本测量。
Definition FontData.cpp:151
int getGlyphWidth(int codepoint) const
单字形度量(像素)。
Definition FontData.cpp:229
Resource module for decoding TrueType / OpenType fonts via FreeType. Produces FontData (CPU metrics +...
Definition Font.h:17
FontData * newFontData(Data *data, int size=16)
Decode a font from in-memory bytes at the given pixel size.
Definition Font.cpp:25
FontData * newFontDataFromFile(std::string path, int size=16)
Decode a font from a VFS path (physfs) at the given pixel size.
Definition Font.cpp:39
~Font() override
Definition Build.cpp:11