metaforce/specter/lib/FontCache.cpp

763 lines
25 KiB
C++
Raw Normal View History

2015-11-27 22:20:22 +00:00
#ifndef NOMINMAX
#define NOMINMAX 1
#endif
2016-03-04 23:03:47 +00:00
#include "specter/FontCache.hpp"
#include "logvisor/logvisor.hpp"
#include <athena/MemoryReader.hpp>
2015-11-22 04:32:12 +00:00
#include <stdint.h>
2015-11-28 21:45:38 +00:00
#include <zlib.h>
2015-11-22 04:32:12 +00:00
2015-11-22 22:30:42 +00:00
#include FT_GZIP_H
#include FT_SYSTEM_H
2015-11-24 02:10:47 +00:00
#include FT_OUTLINE_H
2015-11-22 22:30:42 +00:00
#include <freetype/internal/internal.h>
#include <freetype/internal/ftstream.h>
2015-11-26 00:24:01 +00:00
#include <freetype/internal/tttypes.h>
2015-11-22 22:30:42 +00:00
extern "C" const uint8_t DROIDSANS_PERMISSIVE[];
2015-11-22 04:32:12 +00:00
extern "C" size_t DROIDSANS_PERMISSIVE_SZ;
2015-11-22 22:30:42 +00:00
extern "C" const uint8_t BMONOFONT[];
2015-11-22 04:32:12 +00:00
extern "C" size_t BMONOFONT_SZ;
2015-12-07 00:52:07 +00:00
extern "C" const uint8_t SPECTERCURVES[];
extern "C" size_t SPECTERCURVES_SZ;
2015-11-27 22:20:22 +00:00
extern "C" const FT_Driver_ClassRec tt_driver_class;
2015-11-26 00:24:01 +00:00
2016-03-04 23:03:47 +00:00
namespace specter
2015-11-22 04:32:12 +00:00
{
2016-03-04 23:03:47 +00:00
static logvisor::Module Log("specter::FontCache");
2015-11-22 04:32:12 +00:00
2016-01-18 00:37:13 +00:00
const FCharFilter AllCharFilter =
std::make_pair("all-glyphs", [](uint32_t)->bool
{return true;});
const FCharFilter LatinCharFilter =
std::make_pair("latin-glyphs", [](uint32_t c)->bool
{return c <= 0xff || ((c - 0x2200) <= (0x23FF - 0x2200));});
const FCharFilter LatinAndJapaneseCharFilter =
std::make_pair("latin-and-jp-glyphs", [](uint32_t c)->bool
{return LatinCharFilter.second(c) ||
((c - 0x2E00) <= (0x30FF - 0x2E00)) ||
((c - 0x4E00) <= (0x9FFF - 0x4E00)) ||
((c - 0xFF00) <= (0xFFEF - 0xFF00));});
2015-11-23 08:47:21 +00:00
FontTag::FontTag(const std::string& name, bool subpixel, float points, uint32_t dpi)
{
XXH64_state_t st;
XXH64_reset(&st, 0);
XXH64_update(&st, name.data(), name.size());
XXH64_update(&st, &subpixel, 1);
XXH64_update(&st, &points, 4);
XXH64_update(&st, &dpi, 4);
m_hash = XXH64_digest(&st);
}
2015-11-22 22:30:42 +00:00
FreeTypeGZipMemFace::FreeTypeGZipMemFace(FT_Library lib, const uint8_t* data, size_t sz)
2015-11-23 23:28:32 +00:00
: m_lib(lib)
2015-11-22 22:30:42 +00:00
{
m_comp.base = (unsigned char*)data;
m_comp.size = sz;
m_comp.memory = lib->memory;
2015-11-23 23:28:32 +00:00
}
void FreeTypeGZipMemFace::open()
{
if (m_face)
return;
2015-11-22 22:30:42 +00:00
if (FT_Stream_OpenGzip(&m_decomp, &m_comp))
2016-03-04 23:03:47 +00:00
Log.report(logvisor::Fatal, "unable to open FreeType gzip stream");
2015-11-22 22:30:42 +00:00
FT_Open_Args args =
{
FT_OPEN_STREAM,
nullptr,
0,
nullptr,
&m_decomp
};
2015-11-23 23:28:32 +00:00
if (FT_Open_Face(m_lib, &args, 0, &m_face))
2016-03-04 23:03:47 +00:00
Log.report(logvisor::Fatal, "unable to open FreeType gzip face");
2015-11-22 22:30:42 +00:00
}
2015-11-23 23:28:32 +00:00
void FreeTypeGZipMemFace::close()
2015-11-22 04:32:12 +00:00
{
2015-11-23 23:28:32 +00:00
if (!m_face)
return;
2015-11-22 22:30:42 +00:00
FT_Done_Face(m_face);
2015-11-23 23:28:32 +00:00
m_face = nullptr;
2015-11-22 23:51:44 +00:00
}
2015-11-23 08:47:21 +00:00
#define TEXMAP_DIM 1024
static unsigned RoundUpPow2(unsigned v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
using GreyPixel = uint8_t;
static void MemcpyRect(GreyPixel* img, const FT_Bitmap* bmp, unsigned slice, unsigned x, unsigned y)
{
unsigned sy = TEXMAP_DIM * slice + y;
for (unsigned i=0 ; i<bmp->rows ; ++i)
{
const unsigned char* s = &bmp->buffer[bmp->pitch*i];
2015-11-23 23:28:32 +00:00
GreyPixel* t = &img[TEXMAP_DIM*(sy+i)+x];
2015-11-23 08:47:21 +00:00
memcpy(t, s, bmp->width);
}
}
union RgbaPixel
{
uint8_t rgba[4];
uint32_t pixel;
};
static void MemcpyRect(RgbaPixel* img, const FT_Bitmap* bmp, unsigned slice, unsigned x, unsigned y)
{
unsigned sy = TEXMAP_DIM * slice + y;
for (unsigned i=0 ; i<bmp->rows ; ++i)
{
const unsigned char* s = &bmp->buffer[bmp->pitch*i];
2015-11-23 23:28:32 +00:00
RgbaPixel* t = &img[TEXMAP_DIM*(sy+i)+x];
2015-11-23 08:47:21 +00:00
for (unsigned j=0 ; j<bmp->width/3 ; ++j)
{
t[j].rgba[0] = s[j*3];
t[j].rgba[1] = s[j*3+1];
t[j].rgba[2] = s[j*3+2];
t[j].rgba[3] = 0xff;
}
}
}
2016-01-18 00:37:13 +00:00
2015-11-24 02:10:47 +00:00
static inline void GridFitGlyph(FT_GlyphSlot slot, FT_UInt& width, FT_UInt& height)
{
width = slot->metrics.width >> 6;
height = slot->metrics.height >> 6;
}
2015-11-23 08:47:21 +00:00
2015-11-26 00:24:01 +00:00
void FontAtlas::buildKernTable(FT_Face face)
{
if (face->driver->clazz == &tt_driver_class)
{
TT_Face ttface = reinterpret_cast<TT_Face>(face);
2015-11-26 07:35:43 +00:00
if (!ttface->kern_table)
return;
2016-03-04 23:03:47 +00:00
athena::io::MemoryReader r(ttface->kern_table, ttface->kern_table_size);
2015-12-30 22:04:58 +00:00
auto it = m_kernAdjs.end();
2015-11-26 00:24:01 +00:00
atUint32 nSubs = r.readUint32Big();
for (atUint32 i=0 ; i<nSubs ; ++i)
{
TT_KernHead kernHead;
kernHead.read(r);
if (kernHead.coverage >> 8 != 0)
{
2016-03-04 23:03:47 +00:00
r.seek(kernHead.length - 6, athena::Current);
2015-11-26 00:24:01 +00:00
continue;
}
TT_KernSubHead subHead;
subHead.read(r);
for (atUint16 p=0 ; p<subHead.nPairs ; ++p)
{
TT_KernPair pair;
pair.read(r);
if (it == m_kernAdjs.end() || it->first != pair.left)
if ((it = m_kernAdjs.find(pair.left)) == m_kernAdjs.end())
it = m_kernAdjs.insert(std::make_pair(pair.left, std::vector<std::pair<atUint16, atInt16>>())).first;
it->second.emplace_back(pair.right, pair.value);
}
}
}
}
2016-01-18 00:37:13 +00:00
2015-12-02 03:05:22 +00:00
#define NO_ZLIB 0
2015-12-30 22:04:58 +00:00
#define ZLIB_BUF_SZ 32768
2015-11-26 00:24:01 +00:00
2016-03-04 23:03:47 +00:00
static void WriteCompressed(athena::io::FileWriter& writer, const atUint8* data, size_t sz)
2015-11-28 21:45:38 +00:00
{
2015-12-02 03:05:22 +00:00
#if NO_ZLIB
writer.writeUBytes(data, sz);
return;
#endif
2016-01-18 00:37:13 +00:00
2015-12-30 22:04:58 +00:00
atUint8 compBuf[ZLIB_BUF_SZ];
2015-11-28 21:45:38 +00:00
z_stream z = {};
deflateInit(&z, Z_DEFAULT_COMPRESSION);
z.next_in = (Bytef*)data;
z.avail_in = sz;
writer.writeUint32Big(sz);
2015-11-30 02:55:08 +00:00
atUint64 adlerPos = writer.position();
writer.writeUint32Big(0); /* Space for adler32 */
2015-11-28 21:45:38 +00:00
while (z.avail_in)
{
z.next_out = compBuf;
2015-12-30 22:04:58 +00:00
z.avail_out = ZLIB_BUF_SZ;
2015-11-28 21:45:38 +00:00
deflate(&z, Z_NO_FLUSH);
2015-12-30 22:04:58 +00:00
writer.writeUBytes(compBuf, ZLIB_BUF_SZ - z.avail_out);
2015-11-28 21:45:38 +00:00
}
int finishCycle = Z_OK;
while (finishCycle != Z_STREAM_END)
{
z.next_out = compBuf;
2015-12-30 22:04:58 +00:00
z.avail_out = ZLIB_BUF_SZ;
2015-11-28 21:45:38 +00:00
finishCycle = deflate(&z, Z_FINISH);
2015-12-30 22:04:58 +00:00
writer.writeUBytes(compBuf, ZLIB_BUF_SZ - z.avail_out);
2015-11-28 21:45:38 +00:00
}
2016-03-04 23:03:47 +00:00
writer.seek(adlerPos, athena::Begin);
2015-11-30 02:55:08 +00:00
writer.writeUint32Big(z.adler);
2015-11-28 21:45:38 +00:00
deflateEnd(&z);
}
2016-03-04 23:03:47 +00:00
static bool ReadDecompressed(athena::io::FileReader& reader, atUint8* data, size_t sz)
2015-11-28 21:45:38 +00:00
{
2015-12-02 03:05:22 +00:00
#if NO_ZLIB
reader.readUBytesToBuf(data, sz);
return true;
#endif
2016-01-18 00:37:13 +00:00
2015-12-30 22:04:58 +00:00
atUint8 compBuf[ZLIB_BUF_SZ];
2015-11-28 21:45:38 +00:00
z_stream z = {};
inflateInit(&z);
z.next_out = data;
atUint32 targetSz = reader.readUint32Big();
2015-11-30 02:55:08 +00:00
atUint32 adler32 = reader.readUint32Big();
2015-11-28 21:45:38 +00:00
z.avail_out = std::min(sz, size_t(targetSz));
size_t readSz;
2015-12-30 22:04:58 +00:00
while ((readSz = reader.readUBytesToBuf(compBuf, ZLIB_BUF_SZ)))
2015-11-28 21:45:38 +00:00
{
z.next_in = compBuf;
z.avail_in = readSz;
2015-11-30 02:55:08 +00:00
if (inflate(&z, Z_NO_FLUSH) == Z_STREAM_END)
break;
2015-11-28 21:45:38 +00:00
}
inflateEnd(&z);
2015-11-30 06:06:45 +00:00
return adler32 == z.adler;
2015-11-28 21:45:38 +00:00
}
2015-11-25 01:46:30 +00:00
FontAtlas::FontAtlas(boo::IGraphicsDataFactory* gf, FT_Face face, uint32_t dpi,
2016-03-04 23:03:47 +00:00
bool subpixel, FCharFilter& filter, athena::io::FileWriter& writer)
2015-11-26 07:35:43 +00:00
: m_dpi(dpi),
m_ftXscale(face->size->metrics.x_scale),
2015-11-29 02:55:30 +00:00
m_ftXPpem(face->size->metrics.x_ppem),
m_lineHeight(face->size->metrics.height),
m_subpixel(subpixel)
2015-11-23 08:47:21 +00:00
{
FT_Int32 baseFlags = FT_LOAD_NO_BITMAP;
if (subpixel)
baseFlags |= FT_LOAD_TARGET_LCD;
else
baseFlags |= FT_LOAD_TARGET_NORMAL;
2016-01-18 00:37:13 +00:00
2015-11-23 08:47:21 +00:00
/* First count glyphs exposed by unicode charmap and tally required area */
size_t glyphCount = 0;
FT_UInt gindex;
FT_ULong charcode = FT_Get_First_Char(face, &gindex);
2015-11-23 23:28:32 +00:00
unsigned curLineWidth = 1;
2015-11-23 08:47:21 +00:00
unsigned curLineHeight = 0;
2015-11-23 23:28:32 +00:00
unsigned totalHeight = 1;
unsigned fullTexmapLayers = 0;
2015-11-23 08:47:21 +00:00
while (gindex != 0)
{
if (!filter.second(charcode))
2015-11-28 21:45:38 +00:00
{
charcode = FT_Get_Next_Char(face, charcode, &gindex);
continue;
}
2015-11-23 08:47:21 +00:00
++glyphCount;
FT_Load_Glyph(face, gindex, baseFlags);
2015-11-24 02:10:47 +00:00
FT_UInt width, height;
GridFitGlyph(face->glyph, width, height);
if (curLineWidth + width + 1 > TEXMAP_DIM)
2015-11-23 08:47:21 +00:00
{
2015-11-24 02:10:47 +00:00
totalHeight += curLineHeight + 1;
curLineHeight = 0;
curLineWidth = 1;
}
curLineHeight = std::max(curLineHeight, height);
if (totalHeight + curLineHeight + 1 > TEXMAP_DIM)
{
totalHeight = 1;
++fullTexmapLayers;
//printf("StagedB: %u\n", gindex);
2015-11-23 08:47:21 +00:00
curLineHeight = 0;
2015-11-23 23:28:32 +00:00
curLineWidth = 1;
2015-11-23 08:47:21 +00:00
}
2015-11-23 23:28:32 +00:00
curLineWidth += width + 1;
2015-11-23 08:47:21 +00:00
charcode = FT_Get_Next_Char(face, charcode, &gindex);
}
2015-12-07 00:52:07 +00:00
if (curLineHeight)
totalHeight += curLineHeight + 1;
2015-11-23 08:47:21 +00:00
m_glyphs.reserve(glyphCount);
2015-11-25 01:46:30 +00:00
m_glyphLookup.reserve(glyphCount);
2015-11-23 08:47:21 +00:00
totalHeight = RoundUpPow2(totalHeight);
2015-11-23 23:28:32 +00:00
unsigned finalHeight = fullTexmapLayers ? TEXMAP_DIM : totalHeight;
writer.writeUint32Big(fullTexmapLayers + 1);
2015-11-23 08:47:21 +00:00
writer.writeUint32Big(TEXMAP_DIM);
writer.writeUint32Big(finalHeight);
if (subpixel)
{
/* Allocate texmap */
std::unique_ptr<RgbaPixel[]> texmap;
size_t bufSz;
2015-11-23 23:28:32 +00:00
if (fullTexmapLayers)
2015-11-23 08:47:21 +00:00
{
2015-11-24 02:10:47 +00:00
//printf("ALLOC: %u\n", fullTexmapLayers + 1);
2015-11-23 23:28:32 +00:00
size_t count = TEXMAP_DIM * TEXMAP_DIM * (fullTexmapLayers + 1);
2015-11-23 08:47:21 +00:00
texmap.reset(new RgbaPixel[count]);
bufSz = count * sizeof(RgbaPixel);
memset(texmap.get(), 0, bufSz);
}
else
{
size_t count = TEXMAP_DIM * totalHeight;
2015-11-30 06:06:45 +00:00
texmap.reset(new RgbaPixel[count]);
2015-11-23 08:47:21 +00:00
bufSz = count * sizeof(RgbaPixel);
memset(texmap.get(), 0, bufSz);
}
/* Assemble glyph texmaps and internal data structures */
charcode = FT_Get_First_Char(face, &gindex);
2015-11-23 23:28:32 +00:00
curLineWidth = 1;
2015-11-23 08:47:21 +00:00
curLineHeight = 0;
2015-11-23 23:28:32 +00:00
totalHeight = 1;
fullTexmapLayers = 0;
2015-11-23 08:47:21 +00:00
while (gindex != 0)
{
if (!filter.second(charcode))
2015-11-28 21:45:38 +00:00
{
charcode = FT_Get_Next_Char(face, charcode, &gindex);
continue;
}
2015-11-23 08:47:21 +00:00
FT_Load_Glyph(face, gindex, FT_LOAD_RENDER | baseFlags);
2015-12-02 03:05:22 +00:00
FT_UInt width, height;
GridFitGlyph(face->glyph, width, height);
2015-11-25 01:46:30 +00:00
m_glyphLookup[charcode] = m_glyphs.size();
2015-11-23 08:47:21 +00:00
m_glyphs.emplace_back();
Glyph& g = m_glyphs.back();
2016-01-18 00:37:13 +00:00
2015-12-02 03:05:22 +00:00
if (curLineWidth + width + 1 > TEXMAP_DIM)
2015-11-24 02:10:47 +00:00
{
totalHeight += curLineHeight + 1;
curLineHeight = 0;
curLineWidth = 1;
}
2015-12-02 03:05:22 +00:00
curLineHeight = std::max(curLineHeight, height);
2015-11-24 02:10:47 +00:00
if (totalHeight + curLineHeight + 1 > TEXMAP_DIM)
2015-11-23 08:47:21 +00:00
{
2015-11-24 02:10:47 +00:00
totalHeight = 1;
++fullTexmapLayers;
//printf("RealB: %u\n", gindex);
2015-11-23 08:47:21 +00:00
curLineHeight = 0;
2015-11-23 23:28:32 +00:00
curLineWidth = 1;
2015-11-23 08:47:21 +00:00
}
2016-01-18 00:37:13 +00:00
2015-12-02 03:05:22 +00:00
g.m_unicodePoint = charcode;
2015-12-02 06:13:43 +00:00
g.m_glyphIdx = gindex;
2015-12-02 03:05:22 +00:00
g.m_layerIdx = fullTexmapLayers;
g.m_layerFloat = float(g.m_layerIdx);
g.m_width = face->glyph->bitmap.width / 3;
g.m_height = face->glyph->bitmap.rows;
g.m_uv[0] = curLineWidth / float(TEXMAP_DIM);
g.m_uv[1] = totalHeight / float(finalHeight);
g.m_uv[2] = g.m_uv[0] + g.m_width / float(TEXMAP_DIM);
g.m_uv[3] = g.m_uv[1] + g.m_height / float(finalHeight);
g.m_leftPadding = face->glyph->metrics.horiBearingX >> 6;
g.m_advance = face->glyph->advance.x >> 6;
g.m_verticalOffset = (face->glyph->metrics.horiBearingY - face->glyph->metrics.height) >> 6;
2015-11-23 23:28:32 +00:00
MemcpyRect(texmap.get(), &face->glyph->bitmap, fullTexmapLayers, curLineWidth, totalHeight);
2015-12-02 03:05:22 +00:00
curLineWidth += width + 1;
2015-11-23 08:47:21 +00:00
charcode = FT_Get_Next_Char(face, charcode, &gindex);
}
2015-11-28 21:45:38 +00:00
WriteCompressed(writer, (atUint8*)texmap.get(), bufSz);
2015-11-23 08:47:21 +00:00
m_tex =
2015-11-23 23:28:32 +00:00
gf->newStaticArrayTexture(TEXMAP_DIM, finalHeight, fullTexmapLayers + 1,
2015-11-23 08:47:21 +00:00
boo::TextureFormat::RGBA8, texmap.get(), bufSz);
2015-12-08 05:41:30 +00:00
m_token = gf->commit();
2015-11-23 08:47:21 +00:00
}
else
{
/* Allocate texmap */
std::unique_ptr<GreyPixel[]> texmap;
size_t bufSz;
2015-11-23 23:28:32 +00:00
if (fullTexmapLayers)
2015-11-23 08:47:21 +00:00
{
2015-11-24 02:10:47 +00:00
//printf("ALLOC: %u\n", fullTexmapLayers + 1);
2015-11-23 23:28:32 +00:00
size_t count = TEXMAP_DIM * TEXMAP_DIM * (fullTexmapLayers + 1);
2015-11-23 08:47:21 +00:00
texmap.reset(new GreyPixel[count]);
bufSz = count * sizeof(GreyPixel);
memset(texmap.get(), 0, bufSz);
}
else
{
size_t count = TEXMAP_DIM * totalHeight;
2015-11-30 06:06:45 +00:00
texmap.reset(new GreyPixel[count]);
2015-11-23 08:47:21 +00:00
bufSz = count * sizeof(GreyPixel);
memset(texmap.get(), 0, bufSz);
}
/* Assemble glyph texmaps and internal data structures */
charcode = FT_Get_First_Char(face, &gindex);
2015-11-23 23:28:32 +00:00
curLineWidth = 1;
2015-11-23 08:47:21 +00:00
curLineHeight = 0;
2015-11-23 23:28:32 +00:00
totalHeight = 1;
fullTexmapLayers = 0;
2015-11-23 08:47:21 +00:00
while (gindex != 0)
{
if (!filter.second(charcode))
2015-11-28 21:45:38 +00:00
{
charcode = FT_Get_Next_Char(face, charcode, &gindex);
continue;
}
2015-11-23 08:47:21 +00:00
FT_Load_Glyph(face, gindex, FT_LOAD_RENDER | baseFlags);
2015-12-02 03:05:22 +00:00
FT_UInt width, height;
GridFitGlyph(face->glyph, width, height);
2015-11-25 01:46:30 +00:00
m_glyphLookup[charcode] = m_glyphs.size();
2015-11-23 08:47:21 +00:00
m_glyphs.emplace_back();
Glyph& g = m_glyphs.back();
2015-12-02 03:05:22 +00:00
if (curLineWidth + width + 1 > TEXMAP_DIM)
2015-11-24 02:10:47 +00:00
{
totalHeight += curLineHeight + 1;
curLineHeight = 0;
curLineWidth = 1;
}
2015-12-02 03:05:22 +00:00
curLineHeight = std::max(curLineHeight, height);
2015-11-24 02:10:47 +00:00
if (totalHeight + curLineHeight + 1 > TEXMAP_DIM)
2015-11-23 08:47:21 +00:00
{
2015-11-24 02:10:47 +00:00
totalHeight = 1;
++fullTexmapLayers;
//printf("RealB: %u\n", gindex);
2015-11-23 08:47:21 +00:00
curLineHeight = 0;
2015-11-23 23:28:32 +00:00
curLineWidth = 1;
2015-11-23 08:47:21 +00:00
}
2016-01-18 00:37:13 +00:00
2015-12-02 03:05:22 +00:00
g.m_unicodePoint = charcode;
2015-12-02 06:13:43 +00:00
g.m_glyphIdx = gindex;
2015-12-02 03:05:22 +00:00
g.m_layerIdx = fullTexmapLayers;
g.m_layerFloat = float(g.m_layerIdx);
g.m_width = face->glyph->bitmap.width;
g.m_height = face->glyph->bitmap.rows;
g.m_uv[0] = curLineWidth / float(TEXMAP_DIM);
g.m_uv[1] = totalHeight / float(finalHeight);
g.m_uv[2] = g.m_uv[0] + g.m_width / float(TEXMAP_DIM);
g.m_uv[3] = g.m_uv[1] + g.m_height / float(finalHeight);
g.m_leftPadding = face->glyph->metrics.horiBearingX >> 6;
g.m_advance = face->glyph->advance.x >> 6;
g.m_verticalOffset = (face->glyph->metrics.horiBearingY - face->glyph->metrics.height) >> 6;
2015-11-23 23:28:32 +00:00
MemcpyRect(texmap.get(), &face->glyph->bitmap, fullTexmapLayers, curLineWidth, totalHeight);
2015-12-02 03:05:22 +00:00
curLineWidth += width + 1;
2015-11-23 08:47:21 +00:00
charcode = FT_Get_Next_Char(face, charcode, &gindex);
}
2015-11-28 21:45:38 +00:00
WriteCompressed(writer, (atUint8*)texmap.get(), bufSz);
2015-11-23 08:47:21 +00:00
m_tex =
2015-11-23 23:28:32 +00:00
gf->newStaticArrayTexture(TEXMAP_DIM, finalHeight, fullTexmapLayers + 1,
2015-11-23 08:47:21 +00:00
boo::TextureFormat::I8, texmap.get(), bufSz);
2015-12-08 05:41:30 +00:00
m_token = gf->commit();
2015-11-23 08:47:21 +00:00
}
2015-11-26 00:24:01 +00:00
buildKernTable(face);
2015-11-23 08:47:21 +00:00
}
2015-11-25 01:46:30 +00:00
FontAtlas::FontAtlas(boo::IGraphicsDataFactory* gf, FT_Face face, uint32_t dpi,
2016-03-04 23:03:47 +00:00
bool subpixel, FCharFilter& filter, athena::io::FileReader& reader)
2015-11-26 07:35:43 +00:00
: m_dpi(dpi),
m_ftXscale(face->size->metrics.x_scale),
2015-11-29 02:55:30 +00:00
m_ftXPpem(face->size->metrics.x_ppem),
m_lineHeight(face->size->metrics.height),
m_subpixel(subpixel)
2015-11-23 08:47:21 +00:00
{
2015-11-25 01:46:30 +00:00
FT_Int32 baseFlags = FT_LOAD_NO_BITMAP;
if (subpixel)
baseFlags |= FT_LOAD_TARGET_LCD;
else
baseFlags |= FT_LOAD_TARGET_NORMAL;
/* First count glyphs exposed by unicode charmap */
size_t glyphCount = 0;
FT_UInt gindex;
FT_ULong charcode = FT_Get_First_Char(face, &gindex);
while (gindex != 0)
{
if (!filter.second(charcode))
2015-11-28 21:45:38 +00:00
{
charcode = FT_Get_Next_Char(face, charcode, &gindex);
continue;
}
2015-11-25 01:46:30 +00:00
++glyphCount;
charcode = FT_Get_Next_Char(face, charcode, &gindex);
}
m_glyphs.reserve(glyphCount);
m_glyphLookup.reserve(glyphCount);
unsigned fullTexmapLayers = reader.readUint32Big() - 1;
reader.readUint32Big();
unsigned finalHeight = reader.readUint32Big();
if (subpixel)
{
/* Allocate texmap */
std::unique_ptr<RgbaPixel[]> texmap;
size_t bufSz;
if (fullTexmapLayers)
{
//printf("ALLOC: %u\n", fullTexmapLayers + 1);
size_t count = TEXMAP_DIM * TEXMAP_DIM * (fullTexmapLayers + 1);
texmap.reset(new RgbaPixel[count]);
bufSz = count * sizeof(RgbaPixel);
memset(texmap.get(), 0, bufSz);
}
else
{
size_t count = TEXMAP_DIM * finalHeight;
texmap.reset(new RgbaPixel[TEXMAP_DIM * finalHeight]);
bufSz = count * sizeof(RgbaPixel);
memset(texmap.get(), 0, bufSz);
}
/* Assemble glyph texmaps and internal data structures */
charcode = FT_Get_First_Char(face, &gindex);
unsigned curLineWidth = 1;
unsigned curLineHeight = 0;
unsigned totalHeight = 1;
fullTexmapLayers = 0;
while (gindex != 0)
{
if (!filter.second(charcode))
2015-11-28 21:45:38 +00:00
{
charcode = FT_Get_Next_Char(face, charcode, &gindex);
continue;
}
2015-11-25 01:46:30 +00:00
FT_Load_Glyph(face, gindex, baseFlags);
FT_UInt width, height;
GridFitGlyph(face->glyph, width, height);
2015-11-28 21:45:38 +00:00
m_glyphLookup[charcode] = m_glyphs.size();
2015-11-25 01:46:30 +00:00
m_glyphs.emplace_back();
Glyph& g = m_glyphs.back();
2015-12-02 03:05:22 +00:00
if (curLineWidth + width + 1 > TEXMAP_DIM)
2015-11-25 01:46:30 +00:00
{
totalHeight += curLineHeight + 1;
curLineHeight = 0;
curLineWidth = 1;
}
curLineHeight = std::max(curLineHeight, height);
if (totalHeight + curLineHeight + 1 > TEXMAP_DIM)
{
totalHeight = 1;
++fullTexmapLayers;
//printf("RealB: %u\n", gindex);
curLineHeight = 0;
curLineWidth = 1;
}
2016-01-18 00:37:13 +00:00
2015-12-02 03:05:22 +00:00
g.m_unicodePoint = charcode;
2015-12-02 06:13:43 +00:00
g.m_glyphIdx = gindex;
2015-12-02 03:05:22 +00:00
g.m_layerIdx = fullTexmapLayers;
g.m_layerFloat = float(g.m_layerIdx);
g.m_width = width;
g.m_height = height;
g.m_uv[0] = curLineWidth / float(TEXMAP_DIM);
g.m_uv[1] = totalHeight / float(finalHeight);
g.m_uv[2] = g.m_uv[0] + g.m_width / float(TEXMAP_DIM);
g.m_uv[3] = g.m_uv[1] + g.m_height / float(finalHeight);
g.m_leftPadding = face->glyph->metrics.horiBearingX >> 6;
g.m_advance = face->glyph->advance.x >> 6;
g.m_verticalOffset = (face->glyph->metrics.horiBearingY - face->glyph->metrics.height) >> 6;
curLineWidth += width + 1;
2015-11-25 01:46:30 +00:00
charcode = FT_Get_Next_Char(face, charcode, &gindex);
}
2015-11-30 02:55:08 +00:00
if (!ReadDecompressed(reader, (atUint8*)texmap.get(), bufSz))
return;
2015-11-25 01:46:30 +00:00
m_tex =
gf->newStaticArrayTexture(TEXMAP_DIM, finalHeight, fullTexmapLayers + 1,
boo::TextureFormat::RGBA8, texmap.get(), bufSz);
2015-12-08 05:41:30 +00:00
m_token = gf->commit();
2015-11-25 01:46:30 +00:00
}
else
{
/* Allocate texmap */
std::unique_ptr<GreyPixel[]> texmap;
size_t bufSz;
if (fullTexmapLayers)
{
//printf("ALLOC: %u\n", fullTexmapLayers + 1);
size_t count = TEXMAP_DIM * TEXMAP_DIM * (fullTexmapLayers + 1);
texmap.reset(new GreyPixel[count]);
bufSz = count * sizeof(GreyPixel);
memset(texmap.get(), 0, bufSz);
}
else
{
2015-11-26 00:24:01 +00:00
size_t count = TEXMAP_DIM * finalHeight;
texmap.reset(new GreyPixel[TEXMAP_DIM * finalHeight]);
2015-11-25 01:46:30 +00:00
bufSz = count * sizeof(GreyPixel);
memset(texmap.get(), 0, bufSz);
}
/* Assemble glyph texmaps and internal data structures */
charcode = FT_Get_First_Char(face, &gindex);
unsigned curLineWidth = 1;
unsigned curLineHeight = 0;
unsigned totalHeight = 1;
fullTexmapLayers = 0;
while (gindex != 0)
{
if (!filter.second(charcode))
2015-11-28 21:45:38 +00:00
{
charcode = FT_Get_Next_Char(face, charcode, &gindex);
continue;
}
2015-11-25 01:46:30 +00:00
FT_Load_Glyph(face, gindex, baseFlags);
FT_UInt width, height;
GridFitGlyph(face->glyph, width, height);
2015-11-28 21:45:38 +00:00
m_glyphLookup[charcode] = m_glyphs.size();
2015-11-25 01:46:30 +00:00
m_glyphs.emplace_back();
Glyph& g = m_glyphs.back();
2015-12-02 03:05:22 +00:00
if (curLineWidth + width + 1 > TEXMAP_DIM)
2015-11-25 01:46:30 +00:00
{
totalHeight += curLineHeight + 1;
curLineHeight = 0;
curLineWidth = 1;
}
curLineHeight = std::max(curLineHeight, height);
if (totalHeight + curLineHeight + 1 > TEXMAP_DIM)
{
totalHeight = 1;
++fullTexmapLayers;
//printf("RealB: %u\n", gindex);
curLineHeight = 0;
curLineWidth = 1;
}
2016-01-18 00:37:13 +00:00
2015-12-02 03:05:22 +00:00
g.m_unicodePoint = charcode;
2015-12-02 06:13:43 +00:00
g.m_glyphIdx = gindex;
2015-12-02 03:05:22 +00:00
g.m_layerIdx = fullTexmapLayers;
g.m_layerFloat = float(g.m_layerIdx);
g.m_width = width;
g.m_height = height;
g.m_uv[0] = curLineWidth / float(TEXMAP_DIM);
g.m_uv[1] = totalHeight / float(finalHeight);
g.m_uv[2] = g.m_uv[0] + g.m_width / float(TEXMAP_DIM);
g.m_uv[3] = g.m_uv[1] + g.m_height / float(finalHeight);
g.m_leftPadding = face->glyph->metrics.horiBearingX >> 6;
g.m_advance = face->glyph->advance.x >> 6;
g.m_verticalOffset = (face->glyph->metrics.horiBearingY - face->glyph->metrics.height) >> 6;
curLineWidth += width + 1;
2015-11-25 01:46:30 +00:00
charcode = FT_Get_Next_Char(face, charcode, &gindex);
}
2015-11-30 02:55:08 +00:00
if (!ReadDecompressed(reader, (atUint8*)texmap.get(), bufSz))
return;
2015-11-25 01:46:30 +00:00
m_tex =
gf->newStaticArrayTexture(TEXMAP_DIM, finalHeight, fullTexmapLayers + 1,
boo::TextureFormat::I8, texmap.get(), bufSz);
2015-12-08 05:41:30 +00:00
m_token = gf->commit();
2015-11-25 01:46:30 +00:00
}
2015-11-26 00:24:01 +00:00
buildKernTable(face);
2015-11-23 08:47:21 +00:00
}
2015-11-22 23:51:44 +00:00
FontCache::Library::Library()
{
FT_Error err = FT_Init_FreeType(&m_lib);
if (err)
2016-03-04 23:03:47 +00:00
Log.report(logvisor::Fatal, "unable to FT_Init_FreeType");
2015-11-22 23:51:44 +00:00
}
FontCache::Library::~Library()
{
FT_Done_FreeType(m_lib);
2015-11-22 22:30:42 +00:00
}
2016-03-04 23:03:47 +00:00
FontCache::FontCache(const hecl::Runtime::FileStoreManager& fileMgr)
2015-11-22 22:30:42 +00:00
: m_fileMgr(fileMgr),
2015-11-23 08:47:21 +00:00
m_cacheRoot(m_fileMgr.getStoreRoot() + _S("/fontcache")),
2015-11-22 22:30:42 +00:00
m_regFace(m_fontLib, DROIDSANS_PERMISSIVE, DROIDSANS_PERMISSIVE_SZ),
2015-12-07 00:52:07 +00:00
m_monoFace(m_fontLib, BMONOFONT, BMONOFONT_SZ),
m_curvesFace(m_fontLib, SPECTERCURVES, SPECTERCURVES_SZ)
2016-03-04 23:03:47 +00:00
{hecl::MakeDir(m_cacheRoot.c_str());}
2015-11-23 08:47:21 +00:00
2015-11-28 21:45:38 +00:00
FontTag FontCache::prepCustomFont(boo::IGraphicsDataFactory* gf, const std::string& name, FT_Face face,
FCharFilter filter, bool subpixel,
2015-11-23 08:47:21 +00:00
float points, uint32_t dpi)
{
/* Quick validation */
if (!face)
2016-03-04 23:03:47 +00:00
Log.report(logvisor::Fatal, "invalid freetype face");
2015-11-23 08:47:21 +00:00
2015-11-26 00:24:01 +00:00
if (!face->charmap || face->charmap->encoding != FT_ENCODING_UNICODE)
2016-03-04 23:03:47 +00:00
Log.report(logvisor::Fatal, "font does not contain a unicode char map");
2015-11-22 22:30:42 +00:00
2015-11-23 08:47:21 +00:00
/* Set size with FreeType */
2015-11-23 23:28:32 +00:00
FT_Set_Char_Size(face, 0, points * 64.0, 0, dpi);
2015-11-23 08:47:21 +00:00
/* Make tag and search for cached version */
FontTag tag(name + '_' + filter.first, subpixel, points, dpi);
2015-11-23 08:47:21 +00:00
auto search = m_cachedAtlases.find(tag);
if (search != m_cachedAtlases.end())
return tag;
/* Now check filesystem cache */
2016-03-04 23:03:47 +00:00
hecl::SystemString cachePath = m_cacheRoot + _S('/') + hecl::SysFormat(_S("%" PRIx64), tag.hash());
hecl::Sstat st;
if (!hecl::Stat(cachePath.c_str(), &st) && S_ISREG(st.st_mode))
2015-11-23 08:47:21 +00:00
{
2016-03-04 23:03:47 +00:00
athena::io::FileReader r(cachePath);
2015-11-23 08:47:21 +00:00
if (!r.hasError())
{
2015-11-28 21:45:38 +00:00
atUint32 magic = r.readUint32Big();
2015-11-23 08:47:21 +00:00
if (r.position() == 4 && magic == 'FONT')
{
2015-11-30 02:55:08 +00:00
std::unique_ptr<FontAtlas> fa = std::make_unique<FontAtlas>(gf, face, dpi, subpixel, filter, r);
if (fa->m_tex)
{
m_cachedAtlases.emplace(tag, std::move(fa));
return tag;
}
2015-11-23 08:47:21 +00:00
}
}
}
/* Nada, build and cache now */
2016-03-04 23:03:47 +00:00
athena::io::FileWriter w(cachePath);
2015-11-23 08:47:21 +00:00
if (w.hasError())
2016-03-04 23:03:47 +00:00
Log.report(logvisor::Fatal, "unable to open '%s' for writing", cachePath.c_str());
2015-11-23 08:47:21 +00:00
w.writeUint32Big('FONT');
2015-11-28 21:45:38 +00:00
m_cachedAtlases.emplace(tag, std::make_unique<FontAtlas>(gf, face, dpi, subpixel, filter, w));
2015-11-23 08:47:21 +00:00
return tag;
}
2015-11-22 04:32:12 +00:00
2015-11-25 01:46:30 +00:00
const FontAtlas& FontCache::lookupAtlas(FontTag tag) const
{
auto search = m_cachedAtlases.find(tag);
if (search == m_cachedAtlases.cend())
2016-03-04 23:03:47 +00:00
Log.report(logvisor::Fatal, "invalid font");
2015-11-25 01:46:30 +00:00
return *search->second.get();
}
2015-11-22 04:32:12 +00:00
}