This repository was archived by the owner on Dec 10, 2022. It is now read-only.
forked from ataulien/ZenLib
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzCFont.cpp
84 lines (67 loc) · 2.08 KB
/
zCFont.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// Created by desktop on 21.10.16.
//
#include "zCFont.h"
#include <algorithm>
#include "zenParser.h"
#include <utils/logger.h>
#include <vdfs/fileIndex.h>
using namespace ZenLoad;
zCFont::zCFont(const char *fileName, const VDFS::FileIndex& fileIndex)
{
std::vector<uint8_t> data;
fileIndex.getFileData(fileName, data);
if (data.empty())
return; // TODO: Throw an exception or something
parseFNTData(data);
}
zCFont::~zCFont()
{
}
bool zCFont::parseFNTData(const std::vector<uint8_t>& fntData)
{
try
{
// Create parser from memory
// FIXME: There is an internal copy of the data here. Optimize!
ZenLoad::ZenParser parser(fntData.data(), fntData.size());
/**
* FNT-format is pretty simple:
* [string]: version\n
* [string]: name\n
* [uint32_t]: height
* [uint32_t]: magicNumber? (MUST be always 0xFF)
* [uint8_t]: width[FONT_NUM_MAX_LETTERS]
* [float2]: fontUV1[FONT_NUM_MAX_LETTERS]
* {float2]: fontUV2[FONT_NUM_MAX_LETTERS]
*/
std::string version = parser.readLine();
// Only version 1 is used by gothic
if (version != "1")
{
LogError() << "Unknown font-version: " << version;
return false;
}
std::string name = parser.readLine(false);
uint32_t height = parser.readBinaryDWord();
uint32_t magic = parser.readBinaryDWord();
if (magic != 256)
{
LogError() << "Invalid font-file! Magic: " << magic;
return false;
}
// Checks are through, directly write to font-info now
parser.readBinaryRaw(m_Info.glyphWidth, sizeof(m_Info.glyphWidth));
parser.readBinaryRaw(m_Info.fontUV1, sizeof(m_Info.fontUV1));
parser.readBinaryRaw(m_Info.fontUV2, sizeof(m_Info.fontUV2));
// Plug the other information in
m_Info.fontName = name;
m_Info.fontHeight = height;
}
catch (std::exception& e)
{
LogError() << e.what();
return false;
}
return true;
}