From 1ae4ff72d360f83cf7870640821447192e1532a2 Mon Sep 17 00:00:00 2001 From: Tobi-D7 Date: Sun, 31 Jul 2022 10:02:29 +0200 Subject: [PATCH] __ --- include/renderd7/Fonts/NFontApi.hpp | 12 ++++++++---- include/renderd7/bmp.hpp | 21 ++++++++++---------- source/BitmapPrinter.cpp | 15 ++++++++------- source/NFontApi.cpp | 30 +++++++++++++++++------------ 4 files changed, 45 insertions(+), 33 deletions(-) diff --git a/include/renderd7/Fonts/NFontApi.hpp b/include/renderd7/Fonts/NFontApi.hpp index 6e3b684..1a94729 100644 --- a/include/renderd7/Fonts/NFontApi.hpp +++ b/include/renderd7/Fonts/NFontApi.hpp @@ -111,6 +111,7 @@ namespace RenderD7 } } +#define I2U82I(val) RenderD7::utf8_decode(RenderD7::IntToUtf8(val).c_str()) class NFontApi { @@ -118,12 +119,13 @@ namespace RenderD7 NFontApi(); ~NFontApi(); void LoadTTF(std::string path); - std::vector GetGlyphBitmap(int glyph); + unsigned char* GetGlyphBitmap(char glyph); std::string GetStatus(){ return status; } float GetScale() {return scale; } - int GetGlyphWidth(int glyph); - int GetGlyphHeight(int glyph); + int GetGlyphWidth(char glyph); + int GetGlyphHeight(char glyph); int GetLineHeight(){ return l_h; } + int GetBaseHeight(){ return height; } private: std::string status; @@ -138,7 +140,9 @@ namespace RenderD7 int h; int x0,y0,x1,y1; - int ascent,baseline, decent; + int ascent,baseline,decent,linegap; + + int linespace; stbtt_fontinfo font; }; diff --git a/include/renderd7/bmp.hpp b/include/renderd7/bmp.hpp index 66dcde5..2eededd 100644 --- a/include/renderd7/bmp.hpp +++ b/include/renderd7/bmp.hpp @@ -288,19 +288,20 @@ struct BMP { } unsigned fill_region(uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, uint8_t B, uint8_t G, uint8_t R, uint8_t A) { - if (x0 + w > (uint32_t)bmp_info_header.width || y0 + h > (uint32_t)bmp_info_header.height) { - return 59; - } - uint32_t channels = bmp_info_header.bit_count / 8; for (uint32_t y = y0; y < y0 + h; ++y) { for (uint32_t x = x0; x < x0 + w; ++x) { - data[channels * (y * bmp_info_header.width + x) + 0] = B; - data[channels * (y * bmp_info_header.width + x) + 1] = G; - data[channels * (y * bmp_info_header.width + x) + 2] = R; - if (channels == 4) { - data[channels * (y * bmp_info_header.width + x) + 3] = A; - } + /*if (x + w > (uint32_t)bmp_info_header.width || y + h > (uint32_t)bmp_info_header.height) { + // + }*/ + //else{ + data[channels * (y * bmp_info_header.width + x) + 0] = B; + data[channels * (y * bmp_info_header.width + x) + 1] = G; + data[channels * (y * bmp_info_header.width + x) + 2] = R; + if (channels == 4) { + data[channels * (y * bmp_info_header.width + x) + 3] = A; + } + //} } } return 0; diff --git a/source/BitmapPrinter.cpp b/source/BitmapPrinter.cpp index 996cc3a..0cd35e6 100644 --- a/source/BitmapPrinter.cpp +++ b/source/BitmapPrinter.cpp @@ -348,15 +348,16 @@ void RenderD7::BitmapPrinter::DrawDebugChar(u32 posX, u32 posY, int t_size, u32 void RenderD7::BitmapPrinter::DrawChar(int posX, int posY, float t_size, u32 color, char character, RenderD7::NFontApi font) { - for(int y = 0; y < font.GetGlyphHeight(RenderD7::utf8_decode(RenderD7::IntToUtf8((int)character).c_str())); y++) + for(int y = 0; y < font.GetGlyphHeight(character); y++) { - char charPos = (char)font.GetGlyphBitmap(RenderD7::utf8_decode(RenderD7::IntToUtf8((int)character).c_str()))[character * font.GetGlyphHeight(RenderD7::utf8_decode(RenderD7::IntToUtf8((int)character).c_str())) + y]; - - for(int x = 0; x < font.GetGlyphWidth(RenderD7::utf8_decode(RenderD7::IntToUtf8((int)character).c_str())); x++) - if(((charPos >> (font.GetGlyphWidth(RenderD7::utf8_decode(RenderD7::IntToUtf8((int)character).c_str()) - 1) - x)) & 1) == 1) + for(int x = 0; x < font.GetGlyphWidth(character); x++) + { + if(((font.GetGlyphBitmap(character)[font.GetGlyphHeight(character) + y] >> (font.GetGlyphWidth(character - 1) - x)) & 1) == 1) { DrawPixel(posX + x + 1, posY + y + 1, UNPACK_BGRA(color)); } + + } } } @@ -420,14 +421,14 @@ void RenderD7::BitmapPrinter::DrawText(int x, int y, float t_size, u32 color, st default: //Make sure we never get out of the screen - if(line_i >= (((u32)this->bitmap.bmp_info_header.width) - (u32)x) / (u32)(font.GetGlyphWidth(RenderD7::utf8_decode(&text[i]))*t_size)) + if(line_i >= (((u32)this->bitmap.bmp_info_header.width) - (u32)x) / (u32)(font.GetGlyphWidth(text[i]))) { y += (SPACING_Y*t_size); line_i = 1; //Little offset so we know the same text continues if(text[i] == ' ') break; //Spaces at the start look weird } - this->DrawChar(x + line_i * (font.GetGlyphWidth(RenderD7::utf8_decode(&text[i])*t_size)), y, t_size, color, text[i], font); + this->DrawChar(x + line_i * (font.GetGlyphWidth(text[i])), y, t_size, color, text[i], font); line_i++; break; diff --git a/source/NFontApi.cpp b/source/NFontApi.cpp index fcc6641..69285ab 100644 --- a/source/NFontApi.cpp +++ b/source/NFontApi.cpp @@ -2,6 +2,9 @@ #define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation #include +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include + RenderD7::NFontApi::NFontApi() { @@ -32,39 +35,42 @@ void RenderD7::NFontApi::LoadTTF(std::string path) return; } status+="success!\n"; + b_h = 128; + b_w = 512; l_h = 24; /* line height */ scale = stbtt_ScaleForPixelHeight(&font, l_h); - stbtt_GetFontVMetrics(&font, &ascent,&decent,0); + stbtt_GetFontVMetrics(&font, &ascent,&decent,&linegap); + linespace = scale * (ascent - decent + linegap); + baseline = (int) (ascent*scale); height = (int) ((ascent - decent)*scale); - } -std::vector RenderD7::NFontApi::GetGlyphBitmap(int glyph) +unsigned char* RenderD7::NFontApi::GetGlyphBitmap(char glyph) { - stbtt_GetGlyphBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1); + //stbtt_GetGlyphBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1); + stbtt_GetCodepointBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1); w = x1-x0; h = y1-y0; - std::vector bitmap; - bitmap.resize(h*w); - stbtt_MakeGlyphBitmap(&font, bitmap.data(), w, h, w, scale, scale, glyph); + unsigned char* bitmap; + bitmap = stbtt_GetCodepointBitmap(&font, scale, scale, glyph, &w, &h, 0, 0); return bitmap; } -int RenderD7::NFontApi::GetGlyphHeight(int glyph) +int RenderD7::NFontApi::GetGlyphHeight(char glyph) { - stbtt_GetGlyphBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1); + stbtt_GetCodepointBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1); w = x1-x0; h = y1-y0; return h; } -int RenderD7::NFontApi::GetGlyphWidth(int glyph) +int RenderD7::NFontApi::GetGlyphWidth(char glyph) { - stbtt_GetGlyphBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1); + stbtt_GetCodepointBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1); w = x1-x0; h = y1-y0; return w; -} \ No newline at end of file +}