Removed old font parts

This commit is contained in:
2023-06-17 20:26:26 -07:00
parent 4fb73f455f
commit 6a202edf23
12 changed files with 0 additions and 856 deletions

View File

@ -1,150 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "BitmapFont.hpp"
using namespace Dawn;
void BitmapFont::buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh *mesh,
struct FontMeasure *info
) {
assertNotNull(mesh);
assertNotNull(info);
assertTrue(maxWidth == -1 || maxWidth > 0);
assertTrue(this->isReady());
// Initialize primitive
mesh->createBuffers(
QUAD_VERTICE_COUNT * text.size(),
QUAD_INDICE_COUNT * text.size()
);
// Setup Scales
info->length = 0;
info->realLength = 0;
info->lines.clear();
info->lineHeight = this->getLineHeight(fontSize);
// Prepare the line counters
info->addLine(0, 0);
// Reset Dimensions
char c;
Tile tile;
info->width = info->height = 0;
float_t x = 0;
float_t y = 0;
size_t i = 0;
size_t j = 0;
size_t wordStart = 0;
glm::vec2 xy0(0, 0);
glm::vec2 tileSize =
glm::vec2(tileset->getTileWidth(0), tileset->getTileHeight(0)) *
(fontSize / this->getDefaultFontSize())
;
// Buffer quads
while(c = text[i++]) {
if(c == FONT_SPACE) {
wordStart = i;
// Did this space cause a newline?
if(maxWidth != -1 && xy0.x > maxWidth) {
info->addLine(i, 0);
info->width = mathMax<float_t>(info->width, xy0.x);
xy0.x = 0;
xy0.y += tileSize.y;
info->height = mathMax<float_t>(info->height, xy0.y);
continue;
}
xy0.x += tileSize.x;
continue;
}
if(c == FONT_NEWLINE) {
info->addLine(i, 0);
info->width = mathMax<float_t>(info->width, xy0.x);
xy0.x = 0;
xy0.y += tileSize.y;
info->height = mathMax<float_t>(info->height, xy0.y);
wordStart = i;
continue;
}
// Check for wrapping, todo.
if(maxWidth != -1 && (xy0.x+tileSize.x) > maxWidth) {
// We've exceeded the edge on THIS character. We first need to go back to
// the start of the word and push it to the new line
size_t charsToBeRewound = ((i - 1) - wordStart);
info->width = mathMax<float_t>(
info->width,
(xy0.x - (tileSize.x * charsToBeRewound)) -
(wordStart > 0 && text[wordStart - 1] == FONT_SPACE ? tileSize.x : 0)
);
xy0.x = 0;
xy0.y += tileSize.y;
j -= charsToBeRewound;// Rewind j back to wordStart.
for(auto k = wordStart; k < (i-1); k++) {
char c2 = text[k];
tile = this->tileset->getTile(c2);
QuadMesh::bufferQuadMesh(mesh,
xy0, tile.uv0,
xy0+tileSize, tile.uv1,
j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT
);
xy0.x += tileSize.x;
j++;
}
}
tile = this->tileset->getTile(c);
QuadMesh::bufferQuadMesh(mesh,
xy0, tile.uv0,
xy0+tileSize, tile.uv1,
j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT
);
xy0.x += tileSize.x;
j++;
}
info->width = mathMax<float_t>(info->width, xy0.x);
info->height = mathMax<float_t>(info->height, xy0.y + (xy0.x > 0 ? tileSize.y : 0));
}
bool_t BitmapFont::isReady() {
if(this->texture == nullptr) return false;
if(this->tileset == nullptr) return false;
return this->texture->isReady();
}
Texture * BitmapFont::getTexture() {
return this->texture;
}
void BitmapFont::draw(Mesh *mesh, int32_t start, int32_t len) {
assertNotNull(mesh);
mesh->draw(
MESH_DRAW_MODE_TRIANGLES,
start * QUAD_INDICE_COUNT,
len == -1 ? len : len * QUAD_INDICE_COUNT
);
}
float_t BitmapFont::getLineHeight(float_t fontSize) {
return tileset->getTileHeight(0);
}
float_t BitmapFont::getDefaultFontSize() {
return tileset->getTileHeight(0);
}

View File

@ -1,31 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "Font.hpp"
#include "display/Tileset.hpp"
namespace Dawn {
class BitmapFont : public Font {
protected:
public:
Texture *texture = nullptr;
TilesetGrid *tileset = nullptr;
void buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh *mesh,
struct FontMeasure *info
) override;
bool_t isReady() override;
Texture * getTexture() override;
void draw(Mesh *mesh, int32_t startCharacter, int32_t length) override;
float_t getLineHeight(float_t fontSize) override;
float_t getDefaultFontSize() override;
};
}

View File

@ -3,13 +3,4 @@
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
BitmapFont.cpp
ExampleFont.cpp
TrueTypeFont.cpp
FontMeasure.cpp
)
add_subdirectory(truetype)

View File

@ -1,104 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "ExampleFont.hpp"
using namespace Dawn;
ExampleFont::ExampleFont() : realTilesetGrid(128/8, 64/8, 128, 64, 0, 0, 0, 0) {
this->texture = &this->realTexture;
this->tileset = &this->realTilesetGrid;
}
struct Color ExampleFont::getColor(uint8_t n) {
if(n == 0) return COLOR_TRANSPARENT;
return COLOR_WHITE;
}
void ExampleFont::init() {
struct Color pixels[128*64];
uint8_t data[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x30, 0xD8, 0x50, 0x78, 0xC8, 0x70, 0x30, 0x30, 0x60, 0x48, 0x20, 0x00, 0x00, 0x00, 0x18,
0x00, 0x30, 0xD8, 0xF8, 0xA0, 0xD0, 0xC8, 0x30, 0x60, 0x30, 0x30, 0x20, 0x00, 0x00, 0x00, 0x30,
0x00, 0x30, 0x90, 0x50, 0x70, 0x20, 0x60, 0x60, 0x60, 0x30, 0x78, 0xF8, 0x00, 0xF8, 0x00, 0x60,
0x00, 0x00, 0x00, 0xF8, 0x28, 0x58, 0xD4, 0x00, 0x60, 0x30, 0x30, 0x20, 0x30, 0x00, 0x30, 0xC0,
0x00, 0x30, 0x00, 0x50, 0xF0, 0x98, 0xC8, 0x00, 0x30, 0x60, 0x48, 0x20, 0x30, 0x00, 0x30, 0x80,
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x70, 0x10, 0xF0, 0xF0, 0x38, 0xF8, 0x70, 0xF8, 0x70, 0x70, 0x30, 0x30, 0x18, 0x00, 0x60, 0xF0,
0x88, 0x30, 0x08, 0x08, 0x48, 0x80, 0x80, 0x08, 0x88, 0x88, 0x30, 0x30, 0x30, 0xF8, 0x30, 0x08,
0x88, 0x10, 0x70, 0x30, 0x88, 0xF0, 0xF0, 0x10, 0x70, 0x78, 0x00, 0x00, 0x60, 0x00, 0x18, 0x70,
0x88, 0x10, 0x80, 0x08, 0xF8, 0x08, 0x88, 0x20, 0x88, 0x08, 0x30, 0x30, 0x30, 0xF8, 0x30, 0x00,
0x70, 0x10, 0xF8, 0xF0, 0x08, 0xF0, 0x70, 0x20, 0x70, 0x70, 0x30, 0x10, 0x18, 0x00, 0x60, 0x60,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x70, 0x70, 0xF0, 0x78, 0xF0, 0xF8, 0xF8, 0x78, 0x88, 0xF8, 0x08, 0x48, 0x80, 0x88, 0x88, 0x70,
0x88, 0x88, 0x88, 0x80, 0x88, 0x80, 0x80, 0x80, 0x88, 0x20, 0x08, 0x50, 0x80, 0xD8, 0xC8, 0x88,
0xB8, 0x88, 0xF0, 0x80, 0x88, 0xF0, 0xF0, 0xB8, 0xF8, 0x20, 0x08, 0x60, 0x80, 0xA8, 0xA8, 0x88,
0x80, 0xF8, 0x88, 0x80, 0x88, 0x80, 0x80, 0x88, 0x88, 0x20, 0x88, 0x50, 0x80, 0x88, 0x98, 0x88,
0x70, 0x88, 0xF0, 0x78, 0xF0, 0xF8, 0x80, 0x78, 0x88, 0xF8, 0x70, 0x48, 0xF8, 0x88, 0x88, 0x70,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xF0, 0x70, 0xF0, 0x78, 0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0xF8, 0xF0, 0x80, 0xF0, 0x20, 0x00,
0x88, 0x88, 0x88, 0x80, 0x20, 0x88, 0x88, 0x88, 0x50, 0x50, 0x10, 0xC0, 0xC0, 0x30, 0x50, 0x00,
0xF0, 0x88, 0xF0, 0x70, 0x20, 0x88, 0x88, 0xA8, 0x20, 0x20, 0x20, 0xC0, 0x60, 0x30, 0x88, 0x00,
0x80, 0x88, 0xA0, 0x08, 0x20, 0x88, 0x50, 0xD8, 0x50, 0x20, 0x40, 0xC0, 0x30, 0x30, 0x00, 0x00,
0x80, 0x70, 0x98, 0xF0, 0x20, 0x78, 0x20, 0x88, 0x88, 0x20, 0xF8, 0xF0, 0x18, 0xF0, 0x00, 0xF8,
0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x30, 0x70, 0xF0, 0x78, 0xF0, 0xF8, 0xF8, 0x78, 0x88, 0x70, 0x08, 0x48, 0x40, 0x88, 0x88, 0x70,
0x30, 0x88, 0x88, 0x80, 0x88, 0x80, 0x80, 0x80, 0x88, 0x20, 0x08, 0x50, 0x40, 0xD8, 0xC8, 0x88,
0x18, 0x88, 0xF0, 0x80, 0x88, 0xF0, 0xF0, 0xB8, 0xF8, 0x20, 0x08, 0x60, 0x40, 0xA8, 0xA8, 0x88,
0x00, 0xF8, 0x88, 0x80, 0x88, 0x80, 0x80, 0x88, 0x88, 0x20, 0x88, 0x50, 0x40, 0x88, 0x98, 0x88,
0x00, 0x88, 0xF0, 0x78, 0xF0, 0xF8, 0x80, 0x78, 0x88, 0x70, 0x70, 0x48, 0x78, 0x88, 0x88, 0x70,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xF0, 0x70, 0xF0, 0x78, 0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0xF8, 0x70, 0x30, 0x70, 0x48, 0x00,
0x88, 0x88, 0x88, 0x80, 0x20, 0x88, 0x88, 0x88, 0x50, 0x50, 0x10, 0x60, 0x30, 0x30, 0xB0, 0x00,
0xF0, 0x88, 0xF0, 0x70, 0x20, 0x88, 0x88, 0xA8, 0x20, 0x20, 0x20, 0xC0, 0x30, 0x18, 0x00, 0x00,
0x80, 0x88, 0xA0, 0x08, 0x20, 0x88, 0x50, 0xD8, 0x50, 0x20, 0x40, 0x60, 0x30, 0x30, 0x00, 0x00,
0x80, 0x70, 0x98, 0xF0, 0x20, 0x78, 0x20, 0x88, 0x88, 0x20, 0xF8, 0x70, 0x30, 0x70, 0x00, 0x00,
0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
int32_t j = 0;
for(int32_t i = 0; i < ((128/8) * 64); i++) {
uint8_t n = data[i];
pixels[j++] = this->getColor(n & 0x80);
pixels[j++] = this->getColor(n & 0x40);
pixels[j++] = this->getColor(n & 0x20);
pixels[j++] = this->getColor(n & 0x10);
pixels[j++] = this->getColor(n & 0x08);
pixels[j++] = this->getColor(n & 0x04);
pixels[j++] = this->getColor(n & 0x02);
pixels[j++] = this->getColor(n & 0x01);
}
this->realTexture.setSize(128, 64, TEXTURE_FORMAT_RGBA);
this->realTexture.buffer(pixels);
}

View File

@ -1,22 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "BitmapFont.hpp"
namespace Dawn {
class ExampleFont : public BitmapFont {
protected:
Texture realTexture;
TilesetGrid realTilesetGrid;
struct Color getColor(uint8_t n);
public:
ExampleFont();
void init();
};
}

View File

@ -1,83 +0,0 @@
/**
* Copyright (c) 2022 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/mesh/Mesh.hpp"
#include "util/mathutils.hpp"
#include "display/Texture.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "FontMeasure.hpp"
#define FONT_NEWLINE '\n'
#define FONT_SPACE ' '
namespace Dawn {
class Font {
public:
/**
* Buffer the characters of a string onto a primitive and get the result of the
* buffer back as a resulting generic measurement information structure. Note
* that measure is REQUIRED, and must be DISPOSED after it has been calculated.
*
* @param text String to buffer.
* @param fontSize Font size to use for the buffer operation.
* @param maxWidth Maximum width (in pixels) to use to textwrap. -1 for no wrap.
* @param mesh Mesh to buffer the string on to.
* @param info Pointer to where you want to store resulting measurements.
*/
virtual void buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh *mesh,
struct FontMeasure *info
) = 0;
/**
* Fonts need to be initialized before they can actually be used, but I
* really want to keep things kind-of non-blocking, so for the time being
* this hack works around it, fonts can decide to return false if they are
* not "ready for buffering", and the item intending to use the font is
* required to decide when it should actually request buffering.
*
* @return True if ready for buffering, otherwise false.
*/
virtual bool_t isReady() = 0;
/**
* Returns the texture that is used for a given font.
*
* @return Pointer to the texture used by this font.
*/
virtual Texture * getTexture() = 0;
/**
* Draw a previously buffered font primitive.
*
* @param mesh Mesh to draw.
* @param start Start character to draw.
* @param length Count of characters to draw, set to -1 to draw all.
*/
virtual void draw(Mesh *mesh, int32_t startCharacter, int32_t length) = 0;
/**
* Returns the line height of a given font and font size combination.
*
* @param fontSize Font size to get the line height for.
* @return The line height of this font at this font size.
*/
virtual float_t getLineHeight(float_t fontSize) = 0;
/**
* Retreive the default font size of a given font. Useful if you want to use
* the original font's font size for pixel-perfect rendering.
*
* @return The font size fo that font item.
*/
virtual float_t getDefaultFontSize() = 0;
};
}

View File

@ -1,50 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "FontMeasure.hpp"
using namespace Dawn;
float_t FontMeasure::getWidth() {
return this->width;
}
float_t FontMeasure::getHeight() {
return this->height;
}
int32_t FontMeasure::getQuadCount() {
return this->realLength;
}
float_t FontMeasure::getHeightOfLineCount(size_t lineCount) {
assertTrue(lineCount > 0);
return this->lineHeight * lineCount;
}
size_t FontMeasure::getLineCount() {
return this->lines.size();
}
int32_t FontMeasure::getQuadsOnLine(size_t line) {
assertTrue(line >= 0);
assertTrue(line < this->lines.size());
return this->lines[line].length;
}
int32_t FontMeasure::getQuadIndexOnLine(size_t line) {
assertTrue(line >= 0);
assertTrue(line < this->lines.size());
return this->lines[line].start;
}
void FontMeasure::addLine(int32_t start, int32_t len) {
assertTrue(start >= 0);
assertTrue(len >= 0);
struct FontLineMeasure info;
info.start = start;
info.length = len;
this->lines.push_back(info);
}

View File

@ -1,95 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "assert/assert.hpp"
namespace Dawn {
struct FontLineMeasure {
/** What (real character) index the line starts at */
int32_t start;
/** How many (real) characters the line is in length */
int32_t length;
};
struct FontMeasure {
public:
/** How many raw chars are in the string */
int32_t length;
/** How many real characters (non whitespace) are in the string */
int32_t realLength;
/** The real character info for each line */
std::vector<struct FontLineMeasure> lines;
/** Dimensions of the string */
float_t width, height;
/** Height of a single line */
float_t lineHeight;
/**
* Internal method that adds a line to the text buffer process.
*
* @param start Start character index for the next line.
* @param len Length of the next line.
*/
void addLine(int32_t start, int32_t length);
/**
* Returns the width of this measured string.
*
* @return Width of the pre measured string.
*/
float_t getWidth();
/**
* Returns the height of the measured string.
*
* @return Height of the pre measured string.
*/
float_t getHeight();
/**
* Returns the count of quads on the given line.
*
* @param line Which line to get the count of quads for.
* @return Count of quads on that line.
*/
int32_t getQuadsOnLine(size_t line);
/**
* Returns the index, of which quad is the first quad on the given line.
*
* @param line Line to get the quad index of.
* @return The quad index of that line.
*/
int32_t getQuadIndexOnLine(size_t line);
/**
* Returns the height of the count of lines provided.
*
* @param lineCount Count of lines to get the height of.
* @return Height of the given count of lines.
*/
float_t getHeightOfLineCount(size_t lineCount);
/**
* Returns the count of lines in this string.
*
* @return Count of lines.
*/
size_t getLineCount();
/**
* Returns the count of quads in this string.
*
* @return Total count of quads.
*/
int32_t getQuadCount();
};
}

View File

@ -1,225 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TrueTypeFont.hpp"
#ifndef STB_TRUETYPE_IMPLEMENTATION
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
#endif
using namespace Dawn;
void TrueTypeFont::bakeQuad(truetypequad_t *quad,float_t *x,float_t *y,char c){
assertNotNull(quad);
assertNotNull(x);
assertNotNull(y);
assertTrue(c >= TRUETYPE_FIRST_CHAR);
assertTrue(c < (TRUETYPE_FIRST_CHAR+TRUETYPE_NUM_CHARS));
stbtt_GetBakedQuad(
this->characterData,
this->texture.getWidth(), this->texture.getHeight(),
((int32_t)c) - TRUETYPE_FIRST_CHAR,
x, y, quad,
TRUETYPE_FILL_MODE
);
}
float_t TrueTypeFont::getScale(float_t scale) {
assertTrue(scale > 0);
return scale / this->fontSize;
}
float_t TrueTypeFont::getSpaceSize(float_t fontSize) {
assertTrue(fontSize > 0);
return this->getScale(fontSize) * 48;
}
float_t TrueTypeFont::getInitialLineHeight(float_t fontSize) {
assertTrue(fontSize > 0);
return 42.0f * this->getScale(fontSize);
}
float_t TrueTypeFont::getLineHeight(float_t fontSize) {
assertTrue(fontSize > 0);
return 128.0f * this->getScale(fontSize);
}
void TrueTypeFont::buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh *mesh,
struct FontMeasure *info
) {
assertNotNull(mesh);
assertNotNull(info);
assertTrue(fontSize > 0);
assertTrue(maxWidth == -1 || maxWidth > 0);
assertTrue(this->isReady());
auto stringLength = text.length();
if(stringLength == 0) {
info->length = 0;
info->realLength = 0;
info->lines.clear();
info->height = 0;
info->width = 0;
info->height = 0.0f;
info->lineHeight = 0.0f;
mesh->createBuffers(0, 0);
return;
}
auto quads = new truetypequad_t[stringLength];
assertNotNull(quads);
// Get the font scale
auto scale = this->getScale(fontSize);
assertTrue(scale > 0);
// Adjust the max width to match the scale, and allow "no max width".
maxWidth = maxWidth == -1 ? 9999999 : maxWidth * (1 / scale);
// Which index in the original text var is the current word from
int32_t wordStart = 0;
// Setup Scales
info->length = 0;
info->realLength = 0;
info->lines.clear();
info->lineHeight = this->getLineHeight(fontSize) * scale;
// Prepare the line counters
info->addLine(0, 0);
// Reset Dimensions
info->width = info->height = 0;
// Setup the initial loop state, and X/Y coords for the quad.
int32_t i = 0;
float_t x = 0;
float_t y = 0;
// Bake the first quad.
this->bakeQuad(quads, &x, &y, 'D');
y = -(quads->y0);
x = 0;
// y = this->getInitialLineHeight(fontSize) / scale;
float_t wordX = 0;
char c;
while(c = text[i++]) {
info->length++;
// When space, start of new word about to begin
if(c == FONT_SPACE) {
x += this->getSpaceSize(fontSize) / scale;
// Did this space cause a newline?
if(x > maxWidth) {
info->addLine(info->realLength, 0);
y += this->getLineHeight(fontSize) / scale;
x = 0;
}
wordX = x;
wordStart = info->realLength;
continue;
}
// New line.
if(c == FONT_NEWLINE) {
info->addLine(info->realLength, 0);
wordStart = info->realLength;
y += this->getLineHeight(fontSize) / scale;
x = 0;
continue;
}
// Generate the quad.
auto quad = quads + info->realLength;
this->bakeQuad(quad, &x, &y, c);
// Now measure the width of the line (take the right side of that quad)
if(quad->x1 > maxWidth) {
// We've exceeded the edge, go back to the start of the word and newline.
x = quad->x1 - wordX;
for(auto j = wordStart; j <= info->realLength; j++) {
quad = quads + j;
quad->x0 -= wordX;
quad->x1 -= wordX;
quad->y0 += this->getLineHeight(fontSize) / scale;
quad->y1 += this->getLineHeight(fontSize) / scale;
}
// Go back to the previous (still current) line and remove the chars
info->lines[info->lines.size() - 1].length -= info->realLength - wordStart;
// Next line begins with this word
y += this->getLineHeight(fontSize) / scale;
info->addLine(wordStart, info->realLength-wordStart);
wordX = 0;
}
info->lines[info->lines.size() - 1].length++;
info->realLength++;
}
// Initialize primitive
mesh->createBuffers(
QUAD_VERTICE_COUNT * info->realLength,
QUAD_INDICE_COUNT * info->realLength
);
for(auto j = 0; j < info->realLength; j++) {
auto quad = quads + j;
// Scale the Quad
if(scale != 1.0) {
quad->x0 *= scale;
quad->x1 *= scale;
quad->y0 *= scale;
quad->y1 *= scale;
}
// Update the dimensions.
info->width = mathMax<float_t>(info->width, quad->x1);
info->height = mathMax<float_t>(info->height, quad->y1);
// Buffer the quad.
QuadMesh::bufferQuadMesh(mesh,
glm::vec2(quad->x0, quad->y0), glm::vec2(quad->s0, quad->t0),
glm::vec2(quad->x1, quad->y1), glm::vec2(quad->s1, quad->t1),
j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT
);
}
delete quads;
}
bool_t TrueTypeFont::isReady() {
return this->texture.isReady();
}
Texture * TrueTypeFont::getTexture() {
return &this->texture;
}
void TrueTypeFont::draw(Mesh *mesh, int32_t startchar, int32_t length) {
assertNotNull(mesh);
mesh->draw(
MESH_DRAW_MODE_TRIANGLES,
startchar * QUAD_INDICE_COUNT,
length == -1 ? length : length * QUAD_INDICE_COUNT
);
}
float_t TrueTypeFont::getDefaultFontSize() {
return (float_t)this->fontSize;
}

View File

@ -1,83 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "Font.hpp"
namespace Dawn {
/** Which character (ASCII) to start the font from */
#define TRUETYPE_FIRST_CHAR 32
/** How many characters (after the first char) to generate */
#define TRUETYPE_NUM_CHARS 96
/** Refer to STBTT docs for OpenGL Fill Mode v d3d Fill Modes */
#define TRUETYPE_FILL_MODE 0
typedef stbtt_bakedchar truetypechar_t;
typedef stbtt_aligned_quad truetypequad_t;
class TrueTypeFont : public Font {
protected:
/**
* Calculate the quad information for a given character.
*
* @param font Font to get the character from.
* @param quad Pointer to the quad to store the quad information in.
* @param x Pointer to the X position for the quad.
* @param y Pointer to the Y position for the quad.
* @param c Character to get the quad and position information for.
*/
void bakeQuad(truetypequad_t *quad, float_t *x, float_t *y, char c);
/**
* Returns the font scale to use for rendering your desired font size at a
* font size that is different than the precompiled font size for this
* true type font. For example, let's say you render your font size at 36
* when you are precompiling it, but rather than creating a new font just
* to add a size 24, you can instead use this method to get the scale to
* use to downscale your font to match.
*
* @param font TrueType font to get the scale of.
* @param fontSize Font size you desire.
* @return The scale used to get the font size that will match.
*/
float_t getScale(float_t fontSize);
/**
* Returns the size of a space character for a given font.
*
* @param fontSize Font size of the font to get the space size for.
* @return The size of the space character.
*/
float_t getSpaceSize(float_t fontSize);
/**
* Returns the initial line height of a font.
*
* @param fontSize Font size for the font to get the line height of.
* @return The line height initial value.
*/
float_t getInitialLineHeight(float_t fontSize);
public:
Texture texture;
int32_t fontSize;
truetypechar_t characterData[TRUETYPE_NUM_CHARS];
void buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh *mesh,
struct FontMeasure *info
) override;
bool_t isReady() override;
Texture * getTexture() override;
void draw(Mesh *mesh, int32_t startCharacter, int32_t length) override;
float_t getLineHeight(float_t fontSize) override;
float_t getDefaultFontSize() override;
};
}

View File

@ -35,8 +35,6 @@ void RenderManager::init() {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
this->defaultFont.init();
}
RenderTarget * RenderManager::getBackBuffer() {

View File

@ -10,7 +10,6 @@
#include "display/shader/shaders/FontShader.hpp"
#include "display/shader/shaders/UIShader.hpp"
#include "display/RenderPipeline.hpp"
#include "display/font/ExampleFont.hpp"
namespace Dawn {
class RenderManager : public IRenderManager {
@ -26,7 +25,6 @@ namespace Dawn {
SimpleTexturedShader *simpleTexturedShader = nullptr;
UIShader *uiShader = nullptr;
FontShader *fontShader = nullptr;
ExampleFont defaultFont;
/**
* Construct a new RenderManager for a game instance.