Implemented LessRoundBox custom font and FT2 extension font rendering. Enabled Emscripten compatible FT2 extension capabilities. Replaced original TTF PGEX with newline compatible version.

This commit is contained in:
2023-11-29 06:05:29 -06:00
parent 2af830d400
commit f9b00a8931
14 changed files with 152 additions and 110 deletions
+40 -78
View File
@@ -72,28 +72,8 @@ namespace olc {
Font(std::string path, int fontSize) : fontSize(fontSize) {
FT_Error error = FT_New_Face(library, path.c_str(), 0, &fontFace);
if (error) {
const char *errorString = FT_Error_String(error);
if (errorString == nullptr) {
std::cerr
<< "An unknown error occured while loading the font! Error code: "
<< error << "\n";
} else {
std::cerr << errorString << "\n";
}
}
error = FT_Set_Pixel_Sizes(fontFace, 0, fontSize);
if (error) {
const char *errorString = FT_Error_String(error);
if (errorString == nullptr) {
std::cerr
<< "An unknown error occured while loading the font!Error Code: "
<< error << "\n";
} else {
std::cerr << errorString << "\n";
}
}
}
Font(const Font &other) = delete;
@@ -134,6 +114,8 @@ namespace olc {
for (size_t i = 0; i < string.size(); i++) {
char32_t chr = string[i];
Font *toUse = this;
FT_UInt chrIndex = GetCharIndex(chr);
@@ -159,26 +141,23 @@ namespace olc {
FT_Set_Transform(toUse->fontFace, &rotMat, &pen);
FT_Error error = FT_Load_Char(toUse->fontFace, chr,
FT_LOAD_RENDER | FT_LOAD_COLOR);
if (error) {
const char *errorString = FT_Error_String(error);
if (errorString == nullptr) {
std::cerr
<< "An unknown error occured while rendering a glyph! Error code: "
<< error << "\n";
} else {
std::cerr << errorString << "\n";
}
return;
}
FT_Bitmap bmp = toUse->fontFace->glyph->bitmap;
FT_GlyphSlot slot = toUse->fontFace->glyph;
DrawBitmap(slot->bitmap_left,
pge->ScreenHeight() - slot->bitmap_top, bmp, color);
pen.x += toUse->fontFace->glyph->advance.x;
pen.y += toUse->fontFace->glyph->advance.y;
if(chr=='\n'){
int scaled_line_spacing = fontFace->size->metrics.height;
pen.y -=scaled_line_spacing;
pen.x=x*64;
} else {
DrawBitmap(slot->bitmap_left,
pge->ScreenHeight() - slot->bitmap_top, bmp, color);
}
prevToUse = toUse;
}
@@ -189,8 +168,8 @@ namespace olc {
olc::Pixel color = olc::BLACK, float angle = 0.0f) {
DrawString(string, pos.x, pos.y, color, angle);
}
FontRect GetStringBounds(std::u32string string, float angle = 0.0f) {
private:
FontRect _GetStringBounds(std::u32string string, float angle = 0.0f) {
FT_Matrix rotMat;
rotMat.xx = (FT_Fixed)(std::cos(angle) * 0x10000L);
rotMat.xy = (FT_Fixed)(-std::sin(angle) * 0x10000L);
@@ -214,6 +193,7 @@ namespace olc {
for (size_t i = 0; i < string.size(); i++) {
char32_t chr = string[i];
if(chr=='\r')continue;
Font *toUse = this;
FT_UInt chrIndex = GetCharIndex(chr);
@@ -238,19 +218,7 @@ namespace olc {
}
FT_Set_Transform(toUse->fontFace, &rotMat, &pen);
FT_Error error = FT_Load_Char(toUse->fontFace, chr,
FT_LOAD_BITMAP_METRICS_ONLY);
if (error) {
const char *errorString = FT_Error_String(error);
if (errorString == nullptr) {
std::cerr
<< "An unknown error occured while loading a glyph! Error code: "
<< error << "\n";
} else {
std::cerr << errorString << "\n";
}
return olc::FontRect{{0, 0}, {0, 0}};
}
FT_Error error = FT_Load_Char(toUse->fontFace, chr, FT_LOAD_RENDER);
FT_GlyphSlot slot = toUse->fontFace->glyph;
FT_Glyph glyph;
@@ -275,6 +243,11 @@ namespace olc {
pen.x += slot->advance.x;
pen.y += slot->advance.y;
if(chr=='\n'){
int scaled_line_spacing = fontFace->size->metrics.height;
pen.y -=scaled_line_spacing;
pen.x=minX;
}
FT_Done_Glyph(glyph);
}
@@ -285,9 +258,15 @@ namespace olc {
return olc::FontRect{{minX, -maxY}, {maxX - minX, maxY - minY}};
}
public:
FontRect GetStringBounds(std::u32string string, float angle = 0.0f) {
FontRect temp=_GetStringBounds(string,angle);
return {temp.offset/2,{temp.size/2}};
}
olc::Sprite *RenderStringToSprite(std::u32string string,
olc::Pixel color) {
olc::FontRect rect = GetStringBounds(string);
olc::FontRect rect = _GetStringBounds(string);
olc::Sprite *sprite = new olc::Sprite{rect.size.x, rect.size.y};
for (int x = 0; x < rect.size.x; x++) {
@@ -307,7 +286,7 @@ namespace olc {
for (size_t i = 0; i < string.size(); i++) {
char32_t chr = string[i];
if(chr=='\r')continue;
Font *toUse = this;
FT_UInt chrIndex = GetCharIndex(chr);
@@ -333,26 +312,22 @@ namespace olc {
FT_Set_Transform(toUse->fontFace, nullptr, &pen);
FT_Error error = FT_Load_Char(toUse->fontFace, chr,
FT_LOAD_RENDER | FT_LOAD_COLOR);
if (error) {
const char *errorString = FT_Error_String(error);
if (errorString == nullptr) {
std::cerr
<< "An unknown error occured while rendering a glyph! Error code: "
<< error << "\n";
} else {
std::cerr << errorString << "\n";
}
return nullptr;
}
FT_Bitmap bmp = toUse->fontFace->glyph->bitmap;
FT_GlyphSlot slot = toUse->fontFace->glyph;
DrawBitmapTo(slot->bitmap_left,
pge->ScreenHeight() - slot->bitmap_top, bmp, color,
sprite);
pen.x += toUse->fontFace->glyph->advance.x;
pen.y += toUse->fontFace->glyph->advance.y;
if(chr=='\n'){
int scaled_line_spacing = fontFace->size->metrics.height;
pen.y -=scaled_line_spacing;
pen.x=rect.offset.x;
} else {
DrawBitmapTo(slot->bitmap_left,
pge->ScreenHeight() - slot->bitmap_top, bmp, color,
sprite);
}
}
pge->SetPixelMode(prevMode);
@@ -393,19 +368,6 @@ namespace olc {
static bool init() {
FT_Error error = FT_Init_FreeType(&library);
if (error) {
const char *errorString = FT_Error_String(error);
if (errorString == nullptr) {
std::cerr
<< "An unknown error occured while loading the font library! "
"Error code: "
<< error << "\n";
} else {
std::cerr << errorString << "\n";
}
return false;
}
return true;
}
@@ -431,7 +393,7 @@ namespace olc {
if (byte == 0) {
continue;
}
color.a = byte;
color.a = 255;
pge->Draw(x + bx, y + by, color);
}
}