Files
Seele/src/Engine/Asset/FontAsset.h
T

62 lines
2.0 KiB
C++

#pragma once
#include "Asset.h"
#include "Containers/Map.h"
#include "Graphics/Texture.h"
#include "Math/Math.h"
#include <freetype/freetype.h>
#include <harfbuzz/hb.h>
namespace Seele {
class FontAsset : public Asset {
public:
static constexpr uint64 IDENTIFIER = 0x10;
FontAsset();
FontAsset(std::string_view folderPath, std::string_view name);
virtual ~FontAsset();
virtual void save(ArchiveBuffer& buffer) const override;
virtual void load(ArchiveBuffer& buffer) override;
struct Glyph {
UVector2 size = {};
UVector2 bearing = {};
uint32 advance = 0;
Array<uint8> textureData;
// rect of this glyph inside the per-(font,size) atlas; the atlas texture is
// shared by every glyph of the same size (FontData::atlas) — no per-glyph texture
Vector2 uvOffset = Vector2(0);
Vector2 uvSize = Vector2(0);
void save(ArchiveBuffer& buffer) const;
void load(ArchiveBuffer& buffer);
};
struct FontData {
Map<uint32, Glyph> glyphs;
// the single atlas holding every glyph of this size (created in loadGlyphs)
Gfx::OTexture2D atlas = nullptr;
};
void loadFace();
// packs every glyph of `fontSize` into one atlas (created lazily on first use by
// shapeText); public so the unit test can drive the real packer
void loadGlyphs(uint32 fontSize);
struct RenderGlyph {
Gfx::PTexture2D texture; // the font's atlas for this size (shared by all its glyphs)
Vector2 uvOffset;
Vector2 uvSize;
UVector2 position;
UVector2 dimensions;
};
UVector2 shapeText(std::string_view view, uint32 fontSize, Array<RenderGlyph>& render);
private:
Gfx::PGraphics graphics;
hb_blob_t* blob;
hb_face_t* hb_face;
hb_font_t* hb_font;
FT_Library ft;
FT_Face ft_face;
Array<char> ttfFile;
Map<uint32, FontData> fontSizes;
friend class FontLoader;
};
DECLARE_REF(FontAsset)
} // namespace Seele