Loading fonts is done

This commit is contained in:
2023-06-10 12:01:14 -07:00
parent 1af1f9ef14
commit 86bca79768
12 changed files with 171 additions and 391 deletions

View File

@ -115,29 +115,6 @@ void NewTrueTypeAsset::updateAsync() {
this->state = NEW_TRUE_TYPE_ASSET_STATE_INIT_FREETYPE;
int32_t ret = FT_Init_FreeType(&fontLibrary);
assertTrue(ret == 0);
// Convert to truetype faces
this->state = NEW_TRUE_TYPE_ASSET_STATE_CONVERTING_TO_FACES;
itStyle = assetStyles.begin();
while(itStyle != assetStyles.end()) {
struct NewTrueTypeAssetStyle &style = (*itStyle);
uint8_t *dataBuffer = (uint8_t*)memoryAllocate(sizeof(uint8_t) * style.dataSize);
this->loader.rewind();
this->loader.setPosition(style.dataOffset);
read = this->loader.read(dataBuffer, style.dataSize);
assertTrue(read == style.dataSize);
// Create the face
NewTrueTypeFace *face = new NewTrueTypeFace();
ret = FT_New_Memory_Face(this->fontLibrary, (FT_Byte*)dataBuffer, style.dataSize, 0, &face->face);
memoryFree(dataBuffer);
assertTrue(ret == 0);
// Push
this->faces[style.style] = face;
++itStyle;
}
// Done parsing!
this->state = NEW_TRUE_TYPE_ASSET_STATE_READY;
@ -145,33 +122,67 @@ void NewTrueTypeAsset::updateAsync() {
}
usagelockid_t NewTrueTypeAsset::lock(struct NewTrueTypeFaceTextureStyle style) {
// Try find style
auto it = this->faces.find(style.style);
assertTrue(it != this->faces.end());
assertTrue(this->state == NEW_TRUE_TYPE_ASSET_STATE_READY);
auto lock = it->second->lock(style);
this->locks[lock] = style;
// 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 existing = this->locks.find(id);
assertTrue(existing != this->locks.end());
return this->faces[existing->second.style]->getTexture(id);
auto it = this->textureByLock.find(id);
assertTrue(it != this->textureByLock.end());
return it->second;
}
void NewTrueTypeAsset::unlock(usagelockid_t id) {
auto existing = this->locks.find(id);
assertTrue(existing != this->locks.end());
this->faces[existing->second.style]->unlock(id);
this->locks.erase(existing);
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->faces.begin();
while(it != this->faces.end()) {
FT_Done_Face(it->second->face);
delete it->second;
auto it = this->textures.begin();
while(it != this->textures.end()) {
delete (*it);
it++;
}

View File

@ -7,7 +7,7 @@
#include "../Asset.hpp"
#include "../AssetLoader.hpp"
#include "util/flag.hpp"
#include "display/font/truetype/NewTrueTypeFace.hpp"
#include "display/font/truetype/NewTrueTypeFaceTexture.hpp"
namespace Dawn {
enum NewTrueTypeAssetState {
@ -19,7 +19,6 @@ namespace Dawn {
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_CONVERTING_TO_FACES,
NEW_TRUE_TYPE_ASSET_STATE_READY
};
@ -35,24 +34,18 @@ namespace Dawn {
FT_Library fontLibrary;
enum NewTrueTypeAssetState state = NEW_TRUE_TYPE_ASSET_STATE_INITIAL;
std::vector<struct NewTrueTypeAssetStyle> assetStyles;
std::map<flag_t, NewTrueTypeFace*> faces;
std::map<usagelockid_t, struct NewTrueTypeFaceTextureStyle> locks;
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;
/**
* Locks a specific style for use later.
*
* @param style Style you want to lock.
* @return A unique lock ID for this style.
*/
usagelockid_t lock(struct NewTrueTypeFaceTextureStyle style);
NewTrueTypeFaceTexture * getTexture(usagelockid_t lock);
void unlock(usagelockid_t lock);
~NewTrueTypeAsset();