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

@ -18,16 +18,43 @@ UILabelNew::UILabelNew(SceneItem *item) :
}
void UILabelNew::rebufferQuads() {
std::cout << "Rebuffering" << std::endl;
this->mesh.createBuffers(QUAD_VERTICE_COUNT * 128, QUAD_INDICE_COUNT * 128);
auto texture = this->font->getTexture(this->fontLock);
texture.
glm::vec2 position(32, 32);
struct FontShaderBufferData fontData;
auto x = this->bufferQuads(
"Hello World",
fontData,
this->font->getTexture(this->fontLock),
position,
0,
0
);
fontData.colors[0] = COLOR_MAGENTA;
fontData.textures[0] = 0;
// fontData.colors[1] = COLOR_RED;
// fontData.textures[1] = 1;
// fontData.colors[2] = COLOR_GREEN;
// fontData.textures[2] = 2;
shaderBuffer.buffer(&fontData);
}
void UILabelNew::onStart() {
this->shaderBuffer.init();
useEffect([&]{
usagelockid_t newLock = -1;
if(font != nullptr) {
newLock = font->lock(NewTrueTypeFaceTextureStyle{
fontSize,
style
});
}
if(fontLock != -1) {
font.previous->unlock(fontLock);
fontLock = -1;
@ -35,62 +62,25 @@ void UILabelNew::onStart() {
if(font == nullptr) return;
fontLock = font->lock(NewTrueTypeFaceTextureStyle{
fontSize,
style
});
}, font);
fontLock = newLock;
this->rebufferQuads();
}, font)();
useEffect([&]{
if(font == nullptr) return;
auto newLock = font->lock(NewTrueTypeFaceTextureStyle{
fontSize,
style
});
if(fontLock != -1) {
font->unlock(fontLock);
fontLock = -1;
}
fontLock = font->lock(NewTrueTypeFaceTextureStyle{
fontSize,
style
});
}, { &fontSize, &style });
// struct FontShaderBufferData fontData;
// glm::vec2 position = glm::vec2(32, 32);
// mesh.createBuffers(QUAD_VERTICE_COUNT * 128, QUAD_INDICE_COUNT * 128);
// auto x = UILabelNew::bufferQuads(
// "Hello ",
// fontData,
// defFace,
// position,
// 0,
// 0
// );
// x += UILabelNew::bufferQuads(
// " World",
// fontData,
// defFaceItalics,
// position,
// x,
// 1
// );
// x += UILabelNew::bufferQuads(
// " How are you?",
// fontData,
// defFaceBold,
// position,
// x,
// 2
// );
// fontData.colors[0] = COLOR_MAGENTA;
// fontData.textures[0] = 0;
// fontData.colors[1] = COLOR_RED;
// fontData.textures[1] = 1;
// fontData.colors[2] = COLOR_GREEN;
// fontData.textures[2] = 2;
// shaderBuffer.buffer(&fontData);
fontLock = newLock;
this->rebufferQuads();
}, { &fontSize, &style })();
}
std::vector<struct ShaderPassItem> UILabelNew::getUIRenderPasses() {
@ -106,10 +96,11 @@ std::vector<struct ShaderPassItem> UILabelNew::getUIRenderPasses() {
item.parameterBuffers[shader->bufferUiCanvas] = &canvas->shaderBuffer;
item.parameterBuffers[shader->bufferFont] = &this->shaderBuffer;
item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND;
// item.textureSlots[0] = &defFace.texture;
item.textureSlots[0] = &this->font->getTexture(this->fontLock)->texture;
item.textureValues[shader->paramTexture0] = 0;
// item.textureSlots[1] = &defFaceItalics.texture;
// item.textureSlots[2] = &defFaceBold.texture;
// item.textureValues[shader->paramTexture0] = 0;
// item.textureValues[shader->paramTexture1] = 1;
// item.textureValues[shader->paramTexture2] = 2;
@ -133,115 +124,45 @@ float_t UILabelNew::getContentHeight() {
return 0;
}
// void UILabelNew::bakeTexture(
// struct UILabelFontDef &fontDef,
// FT_Face &face,
// uint32_t fontSize
// ) {
// fontDef.face = &face;
// fontDef.fontSize = fontSize;
// if(FT_Set_Pixel_Sizes(face, 0, fontSize)) {
// assertUnreachable();
// }
// size_t w = 0, h = 0;
// FT_ULong c;
// for(c = NEW_LABEL_CHAR_BEGIN; c < NEW_LABEL_CHAR_END; c++) {
// // Load the character
// if(FT_Load_Char(face, c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME)) {
// assertUnreachable();
// }
// // Update the width and height
// w = mathMax<size_t>(w, face->glyph->bitmap.width);
// h += face->glyph->bitmap.rows;
// }
// assertTrue(w > 0);
// assertTrue(h > 0);
// // Now buffer pixels to the texture
// float_t y = 0;
// // I'd love to just buffer straight to the GPU, but it seems that is a bit
// // unstable right now.
// uint8_t *buffer = (uint8_t *)memoryFillWithZero(w * h * sizeof(uint8_t));
// size_t offset = 0;
// for(c = NEW_LABEL_CHAR_BEGIN; c < NEW_LABEL_CHAR_END; c++) {
// // Load the character
// if(FT_Load_Char(face, c, FT_LOAD_RENDER)) {
// assertUnreachable();
// }
// // Store the character information
// struct UILabelChar info;
// info.advanceX = face->glyph->advance.x;
// info.advanceY = face->glyph->advance.y;
// 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;
// fontDef.charStore[c] = info;
// // Buffer the pixels, oh dear GOD there has to be a more efficient way.
// for(int32_t i = 0; i < face->glyph->bitmap.rows; i++) {
// memoryCopy(
// (void *)(face->glyph->bitmap.buffer + (i * face->glyph->bitmap.width)),
// (void *)(buffer + offset),
// face->glyph->bitmap.width * sizeof(uint8_t)
// );
// offset += w * sizeof(uint8_t);
// assertTrue(offset <= (w * h * sizeof(uint8_t)));
// }
// y += face->glyph->bitmap.rows;
// }
// fontDef.texture.setSize(w, h, TEXTURE_FORMAT_R);
// fontDef.texture.buffer(buffer);
// memoryFree(buffer);
// }
// int32_t UILabelNew::bufferQuads(
// std::string text,
// struct FontShaderBufferData &bufferData,
// struct UILabelFontDef &fontDef,
// glm::vec2 &position,
// int32_t quadStart,
// int32_t partIndex
// ) {
// // Get string length
// int32_t len = text.length();
int32_t UILabelNew::bufferQuads(
std::string text,
struct FontShaderBufferData &bufferData,
struct NewTrueTypeFaceTexture *texture,
glm::vec2 &position,
int32_t quadStart,
int32_t partIndex
) {
// Get string length
int32_t len = text.length();
// glm::vec2 wh = glm::vec2(fontDef.texture.getWidth(), fontDef.texture.getHeight());
glm::vec2 wh = glm::vec2(texture->texture.getWidth(), texture->texture.getHeight());
// // For each char
// for(int32_t i = 0; i < len; i++) {
// char ch = text[i];
// int32_t j = quadStart + i;
// FT_ULong c = ch;
// auto &charInfo = fontDef.charStore[c];
// For each char
for(int32_t i = 0; i < len; i++) {
char ch = text[i];
int32_t j = quadStart + i;
FT_ULong c = ch;
auto charInfo = texture->getCharacterData(c);
// // Determine texture coordinates.
// glm::vec2 uv0 = glm::vec2(0.0f, charInfo.textureY) / wh;
// glm::vec2 uv1 = uv0 + (charInfo.bitmapSize / wh);
// Determine texture coordinates.
glm::vec2 uv0 = glm::vec2(0.0f, charInfo.textureY) / wh;
glm::vec2 uv1 = uv0 + (charInfo.bitmapSize / wh);
// // Buffer the quad.
// QuadMesh::bufferQuadMeshWithZ(&this->mesh,
// position + charInfo.bitmapPosition, uv0,
// position + charInfo.bitmapPosition + charInfo.bitmapSize, uv1,
// 0.0f,
// j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT
// );
// Buffer the quad.
QuadMesh::bufferQuadMeshWithZ(&this->mesh,
position + charInfo.bitmapPosition, uv0,
position + charInfo.bitmapPosition + charInfo.bitmapSize, uv1,
0.0f,
j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT
);
// // Move the current position along.
// position.x += (float_t)(charInfo.advanceX >> 6);
// position.y += (float_t)(charInfo.advanceY >> 6);
// Move the current position along.
position.x += charInfo.advanceX;
position.y += charInfo.advanceY;
// // Set the part index to the quad mappings
// bufferData.fontQuadMappings[j] = partIndex;
// }
// Set the part index to the quad mappings
bufferData.fontQuadMappings[j] = partIndex;
}
// return len;
// }
return len;
}

View File

@ -35,20 +35,20 @@ namespace Dawn {
*
* @param text Text to buffer.
* @param bufferData The output quad mappings for the text.
* @param fontDef The font definition to use.
* @param texture The font texture definition to use.
* @param position The 2D position to buffer the quads at.
* @param quadStart The starting quad index.
* @param partIndex The part index to store for each quad buffered.
* @return The number of quads buffered, not the string length.
*/
// int32_t bufferQuads(
// std::string text,
// struct FontShaderBufferData &bufferData,
// struct UILabelFontDef &fontDef,
// glm::vec2 &position,
// int32_t quadStart,
// int32_t partIndex
// );
int32_t bufferQuads(
std::string text,
struct FontShaderBufferData &bufferData,
struct NewTrueTypeFaceTexture *texture,
glm::vec2 &position,
int32_t quadStart,
int32_t partIndex
);
void rebufferQuads();