Add support for opening TTC fonts

This commit is contained in:
Magnus Norddahl 2024-01-08 17:53:08 +01:00 committed by Christoph Oelckers
parent b7e5d3d052
commit 96b812e880
2 changed files with 19 additions and 3 deletions

View file

@ -5,17 +5,32 @@
#include "core/pathfill.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#ifdef DUMP_GLYPH
#include <fstream>
#endif
TrueTypeFont::TrueTypeFont(std::vector<uint8_t> initdata) : data(std::move(initdata))
TrueTypeFont::TrueTypeFont(std::vector<uint8_t> initdata, int ttcFontIndex) : data(std::move(initdata))
{
if (data.size() > 0x7fffffff)
throw std::runtime_error("TTF file is larger than 2 gigabytes!");
TrueTypeFileReader reader(data.data(), data.size());
ttf_Tag versionTag = reader.ReadTag();
if (memcmp(versionTag.data(), "ttcf", 4) == 0) // TTC header
{
ttcHeader.Load(reader);
if (ttcFontIndex >= ttcHeader.numFonts)
throw std::runtime_error("TTC font index out of bounds");
reader.Seek(ttcHeader.tableDirectoryOffsets[ttcFontIndex]);
}
else
{
reader.Seek(0);
}
directory.Load(reader);
if (!directory.ContainsTTFOutlines())

View file

@ -431,7 +431,7 @@ public:
class TrueTypeFont
{
public:
TrueTypeFont(std::vector<uint8_t> data);
TrueTypeFont(std::vector<uint8_t> data, int ttcFontIndex = 0);
TrueTypeTextMetrics GetTextMetrics(double height) const;
uint32_t GetGlyphIndex(uint32_t codepoint) const;
@ -443,7 +443,8 @@ private:
static float F2DOT14_ToFloat(ttf_F2DOT14 v);
std::vector<uint8_t> data;
TTC_Header ttcHeader;
TTF_TableDirectory directory;
// Required for all OpenType fonts: