Font Testing

This commit is contained in:
2022-10-24 13:48:17 -07:00
parent 2764a2ac42
commit ab725fcdc5
14 changed files with 270 additions and 93 deletions

View File

@ -152,8 +152,8 @@ Transform * Transform::getParent() {
Transform::~Transform() {
this->setParent(nullptr);
auto it = this->parent->children.begin();
while(it != this->parent->children.end()) {
auto it = this->children.begin();
while(it != this->children.end()) {
(*it)->setParent(nullptr);
++it;
}

View File

@ -7,4 +7,15 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
TrueTypeFont.cpp
)
FontMeasure.cpp
)
tool_truetype(truetype_ark
ark-pixel.ttf
truetype_ark
96
96
10
)
add_dependencies(${DAWN_TARGET_NAME} truetype_ark)

View File

@ -10,40 +10,25 @@
#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 FontMeasure {
public:
virtual float_t getWidth() = 0;
virtual float_t getHeight() = 0;
virtual int32_t getQuadsOnLine(int32_t line);
virtual int32_t getQuadIndexOnLine(int32_t line);
virtual int32_t getLineCount();
virtual int32_t getQuadCount();
};
class Font {
public:
Font();
virtual void init() = 0;
virtual void buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh &mesh,
FontMeasure &measure
struct FontMeasure *info
) = 0;
virtual Texture & getTexture() = 0;
virtual void draw(Mesh &mesh, int32_t startCharacter, int32_t length) = 0;
virtual float_t getLineHeight(float_t fontSize) = 0;
virtual float_t getDefaultFontSize() = 0;
virtual ~Font();
};
}

View File

@ -0,0 +1,39 @@
// 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;
}
size_t FontMeasure::getLineCount() {
return this->lines.size();
}
int32_t FontMeasure::getQuadsOnLine(int32_t line) {
return this->lines[line].length;
}
int32_t FontMeasure::getQuadIndexOnLine(int32_t line) {
return this->lines[line].start;
}
void FontMeasure::addLine(int32_t start, int32_t len) {
struct FontLineMeasure info;
info.start = start;
info.length = len;
this->lines.push_back(info);
}

View File

@ -0,0 +1,46 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.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;
/**
* 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);
float_t getWidth();
float_t getHeight();
int32_t getQuadsOnLine(int32_t line);
int32_t getQuadIndexOnLine(int32_t line);
size_t getLineCount();
int32_t getQuadCount();
};
}

View File

@ -5,6 +5,11 @@
#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){
@ -17,10 +22,12 @@ void TrueTypeFont::bakeQuad(truetypequad_t *quad,float_t *x,float_t *y,char c){
);
}
float_t TrueTypeFont::getScale(float_t scale) {
return scale / this->fontSize;
}
float_t TrueTypeFont::getSpaceSize(float_t fontSize) {
return mathRoundFloat(this->fontSize * 0.3f);
}
@ -34,21 +41,14 @@ void TrueTypeFont::buffer(
float_t fontSize,
float_t maxWidth,
Mesh &mesh,
FontMeasure &measure
struct FontMeasure *info
) {
// truetypequad_t *quads;
// int32_t stringLength, wordStart, i, j;
// float_t x, y, wordX;
// float_t scale;
// char c;
// truetypequad_t *quad;
auto stringLength = text.length();
if(stringLength == 0) {
info->length = 0;
info->realLength = 0;
info->lineCount = 0;
info->lines.clear();
info->lines[0].start = 0;
info->lines[0].length = 0;
info->height = 0;
@ -71,11 +71,10 @@ void TrueTypeFont::buffer(
// Setup Scales
info->length = 0;
info->realLength = 0;
info->lineCount = 0;
info->lines.clear();
// Prepare the line counters
info->lines[0].start = 0;
info->lines[0].length = 0;
info->addLine(0, 0);
// Reset Dimensions
info->width = info->height = 0;
@ -95,7 +94,7 @@ void TrueTypeFont::buffer(
// Did this space cause a newline?
if(x > maxWidth) {
trueTypeTextBufferAddLine(info, info->realLength, 0);
info->addLine(info->realLength, 0);
y += this->getLineHeight(fontSize);
x = 0;
}
@ -106,7 +105,7 @@ void TrueTypeFont::buffer(
// New line.
if(c == FONT_NEWLINE) {
trueTypeTextBufferAddLine(info, info->realLength, 0);
info->addLine(info->realLength, 0);
wordStart = info->realLength;
y += this->getLineHeight(fontSize);
x = 0;
@ -130,19 +129,17 @@ void TrueTypeFont::buffer(
}
// Go back to the previous (still current) line and remove the chars
info->lines[info->lineCount].length -= info->realLength - wordStart;
info->lines[info->lines.size() - 1].length -= info->realLength - wordStart;
// Next line begins with this word
y += this->getLineHeight(fontSize);
trueTypeTextBufferAddLine(info, wordStart, info->realLength-wordStart);
info->addLine(wordStart, info->realLength-wordStart);
wordX = 0;
}
info->lines[info->lineCount].length++;
info->lines[info->lines.size() - 1].length++;
info->realLength++;
}
info->lineCount++;
// Initialize primitive
mesh.createBuffers(
@ -192,5 +189,6 @@ float_t TrueTypeFont::getLineHeight(float_t fontSize) {
}
float_t TrueTypeFont::getDefaultFontSize() {
return this->fontSize;
}
return (float_t)this->fontSize;
}

View File

@ -6,11 +6,6 @@
#pragma once
#include "Font.hpp"
#ifndef STB_TRUETYPE_IMPLEMENTATION
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
#endif
namespace Dawn {
/** Which character (ASCII) to start the font from */
#define TRUETYPE_FIRST_CHAR 32
@ -24,33 +19,6 @@ namespace Dawn {
typedef stbtt_bakedchar truetypechar_t;
typedef stbtt_aligned_quad truetypequad_t;
class TrueTypeFontMeasure : public FontMeasure {
public:
// typedef struct {
// /** What (real character) index the line starts at */
// int32_t start;
// /** How many (real) characters the line is in length */
// int32_t length;
// } truetypefontinfoline_t;
// typedef struct {
// /** How many raw chars are in the string */
// int32_t length;
// /** How many real characters (non whitespace) are in the string */
// int32_t realLength;
// /** How many lines is the string? Trailing newlines will count */
// int32_t lineCount;
// /** The real character info for each line */
// truetypefontinfoline_t lines[256];
// /** Dimensions of the string */
// float_t width, height;
// } truetypefontinfo_t;
};
class TrueTypeFont : public Font {
protected:
/**
@ -96,17 +64,15 @@ namespace Dawn {
public:
Texture texture;
float_t fontSize;
int32_t fontSize;
truetypechar_t characterData[TRUETYPE_NUM_CHARS];
void init() override;
void buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh &mesh,
FontMeasure &measure
struct FontMeasure *info
) override;
Texture & getTexture() override;