Removed old TrueType

This commit is contained in:
2023-06-16 10:46:41 -07:00
parent 4f757c2ea9
commit 2964aa4a95
22 changed files with 455 additions and 748 deletions

View File

@ -12,7 +12,6 @@
#include "assets/TextureAsset.hpp"
#include "assets/TilesetAsset.hpp"
#include "assets/TrueTypeAsset.hpp"
#include "assets/NewTrueTypeAsset.hpp"
namespace Dawn {
class AssetManager {

View File

@ -11,5 +11,4 @@ target_sources(${DAWN_TARGET_NAME}
TextureAsset.cpp
TilesetAsset.cpp
TrueTypeAsset.cpp
NewTrueTypeAsset.cpp
)

View File

@ -1,190 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "NewTrueTypeAsset.hpp"
#include "asset/AssetManager.hpp"
using namespace Dawn;
NewTrueTypeAsset::NewTrueTypeAsset(AssetManager *assMan, std::string name) :
Asset(assMan, name),
loader(name + ".newtruetype")
{
}
void NewTrueTypeAsset::updateSync() {
}
void NewTrueTypeAsset::updateAsync() {
if(this->state != NEW_TRUE_TYPE_ASSET_STATE_INITIAL) return;
this->state = NEW_TRUE_TYPE_ASSET_STATE_OPEN;
this->loader.open();
this->state = NEW_TRUE_TYPE_ASSET_STATE_VALIDATE_HEADER;
uint8_t buffer[64];
this->loader.rewind();
size_t read = this->loader.read(buffer, sizeof(char) * 6);
assertTrue(read == (6 * sizeof(char)));
buffer[6] = '\0';
// Confirm "DE_TTF"
assertTrue(std::string((char *)buffer) == "DE_TTF");
// Vertical bar
this->loader.read(buffer, 1);
assertTrue(buffer[0] == '|');
// Read version
this->state = NEW_TRUE_TYPE_ASSET_STATE_VALIDATE_VERSION;
read = this->loader.read(buffer, sizeof(char) * 5);
assertTrue(buffer[0] == '3');
assertTrue(buffer[1] == '.');
assertTrue(buffer[2] == '0');
assertTrue(buffer[3] == '0');
assertTrue(buffer[4] == '|');
// Read the count of font styles / variants.
size_t styleListBegin = this->loader.getPosition();
this->state = NEW_TRUE_TYPE_ASSET_STATE_READ_VARIANT_COUNT;
read = this->loader.read(buffer, 64);
assertTrue(read > 0);
// Get position of vertical bar.
size_t i = 0;
while(buffer[i] != '|' && i < 64) i++;
assertTrue(buffer[i] == '|');
styleListBegin += i + 1;
buffer[i] = '\0';
int32_t count = atoi((char *)buffer);
assertTrue(count > 0);
// Now begin parsing each font style.
this->state = NEW_TRUE_TYPE_ASSET_STATE_READ_VARIANT;
assetStyles.clear();
while(assetStyles.size() != count) {
struct NewTrueTypeAssetStyle style;
// Buffer
this->loader.rewind();
this->loader.setPosition(styleListBegin);
read = this->loader.read(buffer, 32);
assertTrue(read == 32);
// Read style
i = 0;
while(buffer[i] != ':' && i < 64) i++;
buffer[i] = '\0';
style.style = atoi((char *)buffer);
styleListBegin += i + 1;
// Buffer
this->loader.rewind();
this->loader.setPosition(styleListBegin);
read = this->loader.read(buffer, 32);
assertTrue(read == 32);
// Read length
i = 0;
while(buffer[i] != '|' && i < 64) i++;
buffer[i] = '\0';
styleListBegin += i + 1;
style.dataSize = atol((char *)buffer);
// Push
assetStyles.push_back(style);
}
// Now we are at the first byte of the first style.
this->state = NEW_TRUE_TYPE_ASSET_STATE_ADJUSTING_OFFSETS;
auto itStyle = assetStyles.begin();
while(itStyle != assetStyles.end()) {
(*itStyle).dataOffset = styleListBegin;
styleListBegin += (*itStyle).dataSize;
styleListBegin += 1; // Vertical bar
++itStyle;
}
// Init FreeType
this->state = NEW_TRUE_TYPE_ASSET_STATE_INIT_FREETYPE;
int32_t ret = FT_Init_FreeType(&fontLibrary);
assertTrue(ret == 0);
// Done parsing!
this->state = NEW_TRUE_TYPE_ASSET_STATE_READY;
this->loaded = true;
}
usagelockid_t NewTrueTypeAsset::lock(struct NewTrueTypeFaceTextureStyle style) {
assertTrue(this->state == NEW_TRUE_TYPE_ASSET_STATE_READY);
// Try and find an existing texture that matches this style
auto it = this->textureByStyle.find(style);
NewTrueTypeFaceTexture *texture = nullptr;
if(it == this->textureByStyle.end()) {
// Does not exist, Find asset style
auto itAssetStyle = this->assetStyles.begin();
while(itAssetStyle != this->assetStyles.end()) {
if((*itAssetStyle).style == style.style) {
// Found
break;
}
++itAssetStyle;
}
assertTrue(itAssetStyle != this->assetStyles.end());
// Create and read buffer.
uint8_t *dataBuffer = (uint8_t*)memoryAllocate(sizeof(uint8_t) * itAssetStyle->dataSize);
this->loader.rewind();
this->loader.setPosition(itAssetStyle->dataOffset);
auto read = this->loader.read(dataBuffer, itAssetStyle->dataSize);
assertTrue(read == itAssetStyle->dataSize);
// Create the face
FT_Face face;
auto ret = FT_New_Memory_Face(this->fontLibrary, (FT_Byte*)dataBuffer, itAssetStyle->dataSize, 0, &face);
assertTrue(ret == 0);
texture = new NewTrueTypeFaceTexture(face, style);
memoryFree(dataBuffer);
this->textures.push_back(texture);
this->textureByStyle[style] = texture;
} else {
// Exists
texture = it->second;
}
auto lock = texture->locks.createLock();
this->textureByLock[lock] = texture;
return lock;
}
NewTrueTypeFaceTexture * NewTrueTypeAsset::getTexture(usagelockid_t id) {
auto it = this->textureByLock.find(id);
assertTrue(it != this->textureByLock.end());
return it->second;
}
void NewTrueTypeAsset::unlock(usagelockid_t id) {
auto it = this->textureByLock.find(id);
assertTrue(it != this->textureByLock.end());
it->second->locks.removeLock(id);
this->textureByLock.erase(it);
}
NewTrueTypeAsset::~NewTrueTypeAsset() {
auto it = this->textures.begin();
while(it != this->textures.end()) {
delete (*it);
it++;
}
FT_Done_FreeType(this->fontLibrary);
}

View File

@ -1,72 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "../Asset.hpp"
#include "../AssetLoader.hpp"
#include "util/flag.hpp"
#include "display/font/truetype/NewTrueTypeFaceTexture.hpp"
namespace Dawn {
enum NewTrueTypeAssetState {
NEW_TRUE_TYPE_ASSET_STATE_INITIAL,
NEW_TRUE_TYPE_ASSET_STATE_OPEN,
NEW_TRUE_TYPE_ASSET_STATE_VALIDATE_HEADER,
NEW_TRUE_TYPE_ASSET_STATE_VALIDATE_VERSION,
NEW_TRUE_TYPE_ASSET_STATE_READ_VARIANT_COUNT,
NEW_TRUE_TYPE_ASSET_STATE_READ_VARIANT,
NEW_TRUE_TYPE_ASSET_STATE_ADJUSTING_OFFSETS,
NEW_TRUE_TYPE_ASSET_STATE_INIT_FREETYPE,
NEW_TRUE_TYPE_ASSET_STATE_READY
};
struct NewTrueTypeAssetStyle {
flag_t style;
size_t dataSize;
size_t dataOffset;
};
class NewTrueTypeAsset : public Asset {
protected:
AssetLoader loader;
FT_Library fontLibrary;
enum NewTrueTypeAssetState state = NEW_TRUE_TYPE_ASSET_STATE_INITIAL;
std::vector<struct NewTrueTypeAssetStyle> assetStyles;
std::vector<NewTrueTypeFaceTexture*> textures;
std::map<usagelockid_t, NewTrueTypeFaceTexture*> textureByLock;
std::map<struct NewTrueTypeFaceTextureStyle, NewTrueTypeFaceTexture*> textureByStyle;
public:
NewTrueTypeAsset(AssetManager *assMan, std::string name);
void updateSync() override;
void updateAsync() override;
/**
* Create a lock for a specific style. Locks will ensure that the font is
* held loaded until it is no longer required.
*
* @param style Style to lock.
* @return A unique lock ID for this style.
*/
usagelockid_t lock(struct NewTrueTypeFaceTextureStyle style);
/**
* Get a texture by a previous lock ID.
*
* @param lock Lock to get the texture of.
* @return Matching texture by this ID.
*/
NewTrueTypeFaceTexture * getTexture(usagelockid_t lock);
/**
* Releases a previously held font lock.
*
* @param lock Lock to release/unlock.
*/
void unlock(usagelockid_t lock);
~NewTrueTypeAsset();
};
}

View File

@ -1,86 +1,190 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TrueTypeAsset.hpp"
using namespace Dawn;
TrueTypeAsset::TrueTypeAsset(AssetManager *assMan, std::string name) :
Asset(assMan, name),
loader(name + ".truetype")
{
}
void TrueTypeAsset::updateSync() {
if(this->state != 0x04) return;
this->font.texture.setSize(this->width, this->height, TEXTURE_FORMAT_R);
this->font.texture.buffer(this->pixels);
auto i = this->pixels;
memoryCopy(
this->characterData,
this->font.characterData,
sizeof(truetypechar_t) * TRUETYPE_NUM_CHARS
);
memoryFree(this->buffer);
this->buffer = nullptr;
this->state = 0x05;
this->loaded = true;
}
void TrueTypeAsset::updateAsync() {
int32_t fontSize;
size_t i, j;
char intBuffer[32];
char c;
if(this->state != 0x00) return;
this->state = 0x01;
this->loader.loadRaw(&this->buffer);
this->state = 0x02;
// Parse header data.
i = j = 0;
width = -1, height = -1, fontSize = -1;
while(true) {
c = this->buffer[i++];
if(c == '|') {
intBuffer[j] = '\0';
if(width == -1) {
this->width = atoi(intBuffer);
assertTrue(this->width > 0);
j = 0;
continue;
} else if(height == -1) {
this->height = atoi(intBuffer);
assertTrue(this->height > 0);
j = 0;
continue;
} else {
fontSize = atoi(intBuffer);
assertTrue(fontSize > 0);
break;
}
}
intBuffer[j++] = c;
}
this->state = 0x03;
this->font.fontSize = fontSize;
this->pixels = (uint8_t*)(this->buffer + i);
this->characterData = (truetypechar_t*)(
(uint8_t*)this->pixels + (this->width * this->height * sizeof(uint8_t))
);
this->state = 0x04;
}
TrueTypeAsset::~TrueTypeAsset() {
if(this->buffer != nullptr) {
memoryFree(this->buffer);
this->buffer = nullptr;
}
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TrueTypeAsset.hpp"
#include "asset/AssetManager.hpp"
using namespace Dawn;
TrueTypeAsset::TrueTypeAsset(AssetManager *assMan, std::string name) :
Asset(assMan, name),
loader(name + ".truetype")
{
}
void TrueTypeAsset::updateSync() {
}
void TrueTypeAsset::updateAsync() {
if(this->state != TRUE_TYPE_ASSET_STATE_INITIAL) return;
this->state = TRUE_TYPE_ASSET_STATE_OPEN;
this->loader.open();
this->state = TRUE_TYPE_ASSET_STATE_VALIDATE_HEADER;
uint8_t buffer[64];
this->loader.rewind();
size_t read = this->loader.read(buffer, sizeof(char) * 6);
assertTrue(read == (6 * sizeof(char)));
buffer[6] = '\0';
// Confirm "DE_TTF"
assertTrue(std::string((char *)buffer) == "DE_TTF");
// Vertical bar
this->loader.read(buffer, 1);
assertTrue(buffer[0] == '|');
// Read version
this->state = TRUE_TYPE_ASSET_STATE_VALIDATE_VERSION;
read = this->loader.read(buffer, sizeof(char) * 5);
assertTrue(buffer[0] == '3');
assertTrue(buffer[1] == '.');
assertTrue(buffer[2] == '0');
assertTrue(buffer[3] == '0');
assertTrue(buffer[4] == '|');
// Read the count of font styles / variants.
size_t styleListBegin = this->loader.getPosition();
this->state = TRUE_TYPE_ASSET_STATE_READ_VARIANT_COUNT;
read = this->loader.read(buffer, 64);
assertTrue(read > 0);
// Get position of vertical bar.
size_t i = 0;
while(buffer[i] != '|' && i < 64) i++;
assertTrue(buffer[i] == '|');
styleListBegin += i + 1;
buffer[i] = '\0';
int32_t count = atoi((char *)buffer);
assertTrue(count > 0);
// Now begin parsing each font style.
this->state = TRUE_TYPE_ASSET_STATE_READ_VARIANT;
assetStyles.clear();
while(assetStyles.size() != count) {
struct TrueTypeAssetStyle style;
// Buffer
this->loader.rewind();
this->loader.setPosition(styleListBegin);
read = this->loader.read(buffer, 32);
assertTrue(read == 32);
// Read style
i = 0;
while(buffer[i] != ':' && i < 64) i++;
buffer[i] = '\0';
style.style = atoi((char *)buffer);
styleListBegin += i + 1;
// Buffer
this->loader.rewind();
this->loader.setPosition(styleListBegin);
read = this->loader.read(buffer, 32);
assertTrue(read == 32);
// Read length
i = 0;
while(buffer[i] != '|' && i < 64) i++;
buffer[i] = '\0';
styleListBegin += i + 1;
style.dataSize = atol((char *)buffer);
// Push
assetStyles.push_back(style);
}
// Now we are at the first byte of the first style.
this->state = TRUE_TYPE_ASSET_STATE_ADJUSTING_OFFSETS;
auto itStyle = assetStyles.begin();
while(itStyle != assetStyles.end()) {
(*itStyle).dataOffset = styleListBegin;
styleListBegin += (*itStyle).dataSize;
styleListBegin += 1; // Vertical bar
++itStyle;
}
// Init FreeType
this->state = TRUE_TYPE_ASSET_STATE_INIT_FREETYPE;
int32_t ret = FT_Init_FreeType(&fontLibrary);
assertTrue(ret == 0);
// Done parsing!
this->state = TRUE_TYPE_ASSET_STATE_READY;
this->loaded = true;
}
usagelockid_t TrueTypeAsset::lock(struct TrueTypeFaceTextureStyle style) {
assertTrue(this->state == TRUE_TYPE_ASSET_STATE_READY);
// Try and find an existing texture that matches this style
auto it = this->textureByStyle.find(style);
TrueTypeFaceTexture *texture = nullptr;
if(it == this->textureByStyle.end()) {
// Does not exist, Find asset style
auto itAssetStyle = this->assetStyles.begin();
while(itAssetStyle != this->assetStyles.end()) {
if((*itAssetStyle).style == style.style) {
// Found
break;
}
++itAssetStyle;
}
assertTrue(itAssetStyle != this->assetStyles.end());
// Create and read buffer.
uint8_t *dataBuffer = (uint8_t*)memoryAllocate(sizeof(uint8_t) * itAssetStyle->dataSize);
this->loader.rewind();
this->loader.setPosition(itAssetStyle->dataOffset);
auto read = this->loader.read(dataBuffer, itAssetStyle->dataSize);
assertTrue(read == itAssetStyle->dataSize);
// Create the face
FT_Face face;
auto ret = FT_New_Memory_Face(this->fontLibrary, (FT_Byte*)dataBuffer, itAssetStyle->dataSize, 0, &face);
assertTrue(ret == 0);
texture = new TrueTypeFaceTexture(face, style);
memoryFree(dataBuffer);
this->textures.push_back(texture);
this->textureByStyle[style] = texture;
} else {
// Exists
texture = it->second;
}
auto lock = texture->locks.createLock();
this->textureByLock[lock] = texture;
return lock;
}
TrueTypeFaceTexture * TrueTypeAsset::getTexture(usagelockid_t id) {
auto it = this->textureByLock.find(id);
assertTrue(it != this->textureByLock.end());
return it->second;
}
void TrueTypeAsset::unlock(usagelockid_t id) {
auto it = this->textureByLock.find(id);
assertTrue(it != this->textureByLock.end());
it->second->locks.removeLock(id);
this->textureByLock.erase(it);
}
TrueTypeAsset::~TrueTypeAsset() {
auto it = this->textures.begin();
while(it != this->textures.end()) {
delete (*it);
it++;
}
FT_Done_FreeType(this->fontLibrary);
}

View File

@ -1,40 +1,72 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "../Asset.hpp"
#include "../AssetLoader.hpp"
#include "display/font/TrueTypeFont.hpp"
namespace Dawn {
class TrueTypeAsset : public Asset {
protected:
AssetLoader loader;
uint8_t *buffer = nullptr;
truetypechar_t *characterData = nullptr;
uint8_t *pixels = nullptr;
int32_t width, height;
public:
TrueTypeFont font;
/**
* Constructs a new True Type Asset. As with all other assets you should
* instead use the AssetManaager.load method.
*
* @param assMan Asset manager that this asset belongs to.
* @param name Filename of this asset.
*/
TrueTypeAsset(AssetManager *assMan, std::string name);
void updateSync() override;
void updateAsync() override;
/**
* Disposes / Cleans up the truetype asset.
*/
~TrueTypeAsset();
};
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "../Asset.hpp"
#include "../AssetLoader.hpp"
#include "util/flag.hpp"
#include "display/font/truetype/TrueTypeFaceTexture.hpp"
namespace Dawn {
enum TrueTypeAssetState {
TRUE_TYPE_ASSET_STATE_INITIAL,
TRUE_TYPE_ASSET_STATE_OPEN,
TRUE_TYPE_ASSET_STATE_VALIDATE_HEADER,
TRUE_TYPE_ASSET_STATE_VALIDATE_VERSION,
TRUE_TYPE_ASSET_STATE_READ_VARIANT_COUNT,
TRUE_TYPE_ASSET_STATE_READ_VARIANT,
TRUE_TYPE_ASSET_STATE_ADJUSTING_OFFSETS,
TRUE_TYPE_ASSET_STATE_INIT_FREETYPE,
TRUE_TYPE_ASSET_STATE_READY
};
struct TrueTypeAssetStyle {
flag_t style;
size_t dataSize;
size_t dataOffset;
};
class TrueTypeAsset : public Asset {
protected:
AssetLoader loader;
FT_Library fontLibrary;
enum TrueTypeAssetState state = TRUE_TYPE_ASSET_STATE_INITIAL;
std::vector<struct TrueTypeAssetStyle> assetStyles;
std::vector<TrueTypeFaceTexture*> textures;
std::map<usagelockid_t, TrueTypeFaceTexture*> textureByLock;
std::map<struct TrueTypeFaceTextureStyle, TrueTypeFaceTexture*> textureByStyle;
public:
TrueTypeAsset(AssetManager *assMan, std::string name);
void updateSync() override;
void updateAsync() override;
/**
* Create a lock for a specific style. Locks will ensure that the font is
* held loaded until it is no longer required.
*
* @param style Style to lock.
* @return A unique lock ID for this style.
*/
usagelockid_t lock(struct TrueTypeFaceTextureStyle style);
/**
* Get a texture by a previous lock ID.
*
* @param lock Lock to get the texture of.
* @return Matching texture by this ID.
*/
TrueTypeFaceTexture * getTexture(usagelockid_t lock);
/**
* Releases a previously held font lock.
*
* @param lock Lock to release/unlock.
*/
void unlock(usagelockid_t lock);
~TrueTypeAsset();
};
}