metaforce/specter/include/Specter/FontCache.hpp

107 lines
2.8 KiB
C++
Raw Normal View History

2015-11-21 23:45:02 +00:00
#ifndef SPECTER_FONTCACHE_HPP
#define SPECTER_FONTCACHE_HPP
#include <ft2build.h>
#include FT_FREETYPE_H
2015-11-22 04:32:12 +00:00
#include <boo/boo.hpp>
#include <HECL/Runtime.hpp>
2015-11-23 08:47:21 +00:00
#include <Athena/FileReader.hpp>
#include <Athena/FileWriter.hpp>
namespace Specter
{
class FontTag
{
friend class FontCache;
uint64_t m_hash;
FontTag(const std::string& name, bool subpixel, float points, unsigned dpi);
public:
uint64_t hash() const {return m_hash;}
bool operator==(const FontTag& other) const {return m_hash == other.m_hash;}
};
}
namespace std
{
template <> struct hash<Specter::FontTag>
{
size_t operator() (const Specter::FontTag& handle) const NOEXCEPT
{return size_t(handle.hash());}
};
}
2015-11-22 04:32:12 +00:00
2015-11-21 23:45:02 +00:00
namespace Specter
{
2015-11-22 22:30:42 +00:00
class FreeTypeGZipMemFace
{
FT_StreamRec m_comp = {};
FT_StreamRec m_decomp = {};
FT_Face m_face;
public:
FreeTypeGZipMemFace(FT_Library lib, const uint8_t* data, size_t sz);
~FreeTypeGZipMemFace();
operator FT_Face() {return m_face;}
};
2015-11-22 04:32:12 +00:00
class FontAtlas
{
2015-11-23 08:47:21 +00:00
friend class FontCache;
2015-11-22 04:32:12 +00:00
FT_Face m_face;
2015-11-23 08:47:21 +00:00
boo::ITextureS* m_tex;
struct Glyph
{
atUint32 m_unicodePoint;
atUint32 m_layerIdx;
float m_uv[4];
atInt8 m_leftPadding;
atInt8 m_advance;
atInt8 m_rightPadding;
atUint8 m_width;
atUint8 m_height;
atInt8 m_verticalOffset;
atUint16 m_kernIdx;
};
std::vector<Glyph> m_glyphs;
std::map<atUint32, size_t> m_glyphLookup;
public:
FontAtlas(boo::IGraphicsDataFactory* gf, FT_Face face, bool subpixel, Athena::io::FileWriter& writer);
FontAtlas(boo::IGraphicsDataFactory* gf, FT_Face face, bool subpixel, Athena::io::FileReader& reader);
2015-11-22 04:32:12 +00:00
};
2015-11-21 23:45:02 +00:00
class FontCache
{
2015-11-22 04:32:12 +00:00
const HECL::Runtime::FileStoreManager& m_fileMgr;
2015-11-23 08:47:21 +00:00
HECL::SystemString m_cacheRoot;
2015-11-22 23:51:44 +00:00
struct Library
{
FT_Library m_lib;
Library();
~Library();
operator FT_Library() {return m_lib;}
} m_fontLib;
2015-11-22 22:30:42 +00:00
FreeTypeGZipMemFace m_regFace;
FreeTypeGZipMemFace m_monoFace;
2015-11-23 08:47:21 +00:00
std::unordered_map<FontTag, std::unique_ptr<FontAtlas>> m_cachedAtlases;
2015-11-22 04:32:12 +00:00
public:
FontCache(const HECL::Runtime::FileStoreManager& fileMgr);
2015-11-23 08:47:21 +00:00
FontTag prepCustomFont(boo::IGraphicsDataFactory* gf,
const std::string& name, FT_Face face, bool subpixel=false,
float points=10.0, uint32_t dpi=72);
FontTag prepMainFont(boo::IGraphicsDataFactory* gf,
bool subpixel=false, float points=10.0, uint32_t dpi=72)
{return prepCustomFont(gf, "droidsans-permissive", m_regFace, subpixel, points, dpi);}
FontTag prepMonoFont(boo::IGraphicsDataFactory* gf,
bool subpixel=false, float points=10.0, uint32_t dpi=72)
{return prepCustomFont(gf, "bmonofont", m_monoFace, subpixel, points, dpi);}
2015-11-21 23:45:02 +00:00
};
}
#endif // SPECTER_FONTCACHE_HPP