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

@ -6,6 +6,5 @@
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
NewTrueTypeFace.cpp
NewTrueTypeFaceTexture.cpp
)

View File

@ -1,57 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "asset/assets/NewTrueTypeAsset.hpp"
using namespace Dawn;
NewTrueTypeFace::NewTrueTypeFace() {
}
usagelockid_t NewTrueTypeFace::lock(struct NewTrueTypeFaceTextureStyle style) {
// Find by style
NewTrueTypeFaceTexture *texture = nullptr;
auto existing = this->texturesByStyle.find(style);
if(existing == this->texturesByStyle.end()) {
// Does not exist, create it.
texture = new NewTrueTypeFaceTexture(this, face, style);
this->textures.push_back(texture);
this->texturesByStyle[style] = texture;
} else {
texture = existing->second;
}
assertNotNull(texture);
auto lock = texture->locks.createLock();
this->locksByStyle[style].push_back(lock);
this->stylesByLock[lock] = style;
return lock;
}
NewTrueTypeFaceTexture * NewTrueTypeFace::getTexture(usagelockid_t lock) {
auto styleByLock = this->stylesByLock.find(lock);
assertTrue(styleByLock != this->stylesByLock.end());
auto texture = this->texturesByStyle.find(styleByLock->second);
assertTrue(texture != this->texturesByStyle.end());
return texture->second;
}
void NewTrueTypeFace::unlock(usagelockid_t lock) {
auto texture = this->getTexture(lock);
assertNotNull(texture);
texture->locks.removeLock(lock);
this->stylesByLock.erase(lock);
auto it = std::find(this->locksByStyle[texture->style].begin(), this->locksByStyle[texture->style].end(), lock);
assertTrue(it != this->locksByStyle[texture->style].end());
this->locksByStyle[texture->style].erase(it);
}
NewTrueTypeFace::~NewTrueTypeFace() {
auto it = this->textures.begin();
while(it != this->textures.end()) {
delete *it;
}
}

View File

@ -1,55 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "NewTrueTypeFaceTexture.hpp"
namespace Dawn {
class NewTrueTypeFace {
public:
std::vector<NewTrueTypeFaceTexture*> textures;
std::map<struct NewTrueTypeFaceTextureStyle, NewTrueTypeFaceTexture*> texturesByStyle;
std::map<struct NewTrueTypeFaceTextureStyle, std::vector<usagelockid_t>> locksByStyle;
std::map<usagelockid_t, struct NewTrueTypeFaceTextureStyle> stylesByLock;
FT_Face face;
/**
* Create a new TrueTypeFace object. TrueType face holds multiple textures
* of various styles of this face. This face may be "Arial Bold" and have
* a texture for "Arial Bold 12", "Arial Bold 14", etc.
*/
NewTrueTypeFace();
/**
* Locks a texture for use. This will create a new texture if one does not
* exist for the given style.
*
* @param style Style to lock.
* @return A unique lock ID for this style.
*/
usagelockid_t lock(struct NewTrueTypeFaceTextureStyle style);
/**
* Unlocks a previously created lock. If the texture becomes unused it
* will be deleted.
*
* @param lock Lock to remove.
*/
void unlock(usagelockid_t lock);
/**
* Gets a texture from a lock.
*
* @param lock Lock to get texture from.
* @return Texture from lock.
*/
NewTrueTypeFaceTexture * getTexture(usagelockid_t lock);
/**
* Destroys this TrueTypeFace object, and all textures associated with it.
*/
~NewTrueTypeFace();
};
}

View File

@ -3,28 +3,25 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "NewTrueTypeFace.hpp"
#include "NewTrueTypeFaceTexture.hpp"
using namespace Dawn;
NewTrueTypeFaceTexture::NewTrueTypeFaceTexture(
NewTrueTypeFace *trueTypeFace,
FT_Face &face,
FT_Face face,
struct NewTrueTypeFaceTextureStyle style
) {
assertNotNull(trueTypeFace);
assertTrue(style.fontSize < 256);
this->trueTypeFace = trueTypeFace;
this->face = &face;
this->face = face;
this->style = style;
this->locks.onEmpty = [&]() {
delete this;
assertUnreachable();
};
// Set freetype font size prior to baking.
if(FT_Set_Pixel_Sizes(face, 0, this->style.fontSize)) {
if(FT_Set_Pixel_Sizes(face, 0, style.fontSize)) {
assertUnreachable();
}
@ -34,7 +31,8 @@ NewTrueTypeFaceTexture::NewTrueTypeFaceTexture(
// First pass, determine the textures' dimensions.
for(c = NEW_TRUETYPE_CHAR_BEGIN; c < NEW_TRUETYPE_CHAR_END; c++) {
// Load the character
if(FT_Load_Char(face, c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME)) {
auto ret = FT_Load_Char(face, c, FT_LOAD_BITMAP_METRICS_ONLY);
if(ret) {
assertUnreachable();
}
@ -62,11 +60,15 @@ NewTrueTypeFaceTexture::NewTrueTypeFaceTexture(
}
// Store the character information
info.advanceX = face->glyph->advance.x;
info.advanceY = face->glyph->advance.y;
info.advanceX = (float_t)(face->glyph->advance.x >> 6);
info.advanceY = (float_t)(face->glyph->advance.y >> 6);
info.bitmapSize = glm::vec2(face->glyph->bitmap.width, face->glyph->bitmap.rows);
info.bitmapPosition = glm::vec2(face->glyph->bitmap_left, -face->glyph->bitmap_top);
info.textureY = y;
char c2 = (char)c;
if(c2 == 'H') {
std::cout << "H" << std::endl;
}
this->characterData[c] = info;
// Buffer the pixels, oh dear GOD there has to be a more efficient way.
@ -92,11 +94,5 @@ struct NewTrueTypeCharacter NewTrueTypeFaceTexture::getCharacterData(FT_ULong c)
}
NewTrueTypeFaceTexture::~NewTrueTypeFaceTexture() {
auto it = std::find(
this->trueTypeFace->textures.begin(),
this->trueTypeFace->textures.end(),
this
);
assertTrue(it != this->trueTypeFace->textures.end());
this->trueTypeFace->textures.erase(it);
FT_Done_Face(this->face);
}

View File

@ -10,13 +10,11 @@
#include "util/UsageLock.hpp"
namespace Dawn {
class NewTrueTypeFace;
typedef uint32_t newtruetypelock_t;
class NewTrueTypeAsset;
struct NewTrueTypeCharacter {
int32_t advanceX;
int32_t advanceY;
float_t advanceX;
float_t advanceY;
glm::vec2 bitmapSize;
glm::vec2 bitmapPosition;
float_t textureY;
@ -33,8 +31,7 @@ namespace Dawn {
class NewTrueTypeFaceTexture {
public:
FT_Face *face;
NewTrueTypeFace *trueTypeFace;
FT_Face face;
std::map<FT_ULong, struct NewTrueTypeCharacter> characterData;
struct NewTrueTypeFaceTextureStyle style;
UsageLock locks;
@ -43,13 +40,11 @@ namespace Dawn {
/**
* Construct a new New True Type Face Texture object
*
* @param trueTypeFace Face that this texture belongs to.
* @param face The freetype face object.
* @param style Style that this font has, used for locking.
*/
NewTrueTypeFaceTexture(
NewTrueTypeFace *trueTypeFace,
FT_Face &face,
FT_Face face,
struct NewTrueTypeFaceTextureStyle style
);
@ -66,6 +61,6 @@ namespace Dawn {
*/
~NewTrueTypeFaceTexture();
friend class NewTrueTypeFace;
friend class NewTrueTypeAsset;
};
}