Implement Experiment of NFontApi

This commit is contained in:
tobid7 2022-07-31 01:23:39 +02:00
parent ca52e6dae6
commit 9213a1d5dd
11 changed files with 5387 additions and 1656 deletions

View File

@ -56,6 +56,7 @@
"regex": "cpp",
"shared_mutex": "cpp",
"valarray": "cpp",
"random": "cpp"
"random": "cpp",
"cuchar": "cpp"
}
}

View File

@ -10,6 +10,8 @@
#include <renderd7/Time.hpp>
#include <renderd7/Screen.hpp>
#include <renderd7/Fonts/NFontApi.hpp>
namespace RenderD7
{
enum Encoder
@ -59,13 +61,13 @@ namespace RenderD7
void SetupBenchmark(int framerate);
bool IsBenchmarkRunning() { return this->benchmark; }
void DrawDebugText(int x, int y, float t_size, u32 color, std::string text);
void DrawText(int x, int y, int t_size, u32 color, std::string text);
void DrawDebugText(int x, int y, int t_size, u32 color, std::string text);
void DrawText(int x, int y, float t_size, u32 color, std::string text, RenderD7::NFontApi font);
private:
//funcs
bool Decode(Decoder deccc);
void DrawDebugChar(u32 posX, u32 posY, u32 color, char character);
void DrawChar(u32 posX, u32 posY, int t_size, u32 color, char character);
void DrawDebugChar(u32 posX, u32 posY, int t_size, u32 color, char character);
void DrawChar(int posX, int posY, float t_size, u32 color, char character, RenderD7::NFontApi font);
//parameter
int frame = 0;
RenderD7::Image renderframe;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,145 @@
#pragma once
#include <iostream>
#include <string>
#include <bitset>
#include <vector>
#include <renderd7/external/stb_truetype.h>
#define MAXUNICODE 0x10FFFF
namespace RenderD7
{
inline int utf8_decode(const char* o) {
static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
const unsigned char *s = (const unsigned char *)o;
unsigned int c = s[0];
unsigned int res = 0; /* final result */
if (c < 0x80) /* ascii? */
res = c;
else {
int count = 0; /* to count number of continuation bytes */
while (c & 0x40) { /* still have continuation bytes? */
int cc = s[++count]; /* read next byte */
if ((cc & 0xC0) != 0x80) /* not a continuation byte? */
return -1; /* invalid byte sequence */
res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
c <<= 1; /* to test next bit */
}
res |= ((c & 0x7F) << (count * 5)); /* add first byte */
if (count > 3 || res > MAXUNICODE || res <= limits[count])
return -1; /* invalid byte sequence */
s += count; /* skip continuation bytes read */
}
return res;
}
inline std::string IntToUtf8(int convertval){
// We only care about plane 1 right now,
// but know that we have other options (0x10FFFF)
// Technically UTF-8 is "limited" to 4 bytes, so it's not
// Like it matters much anyways these days
if(convertval == 0)
return " ";
if( (convertval <= 0x7F) && (convertval > 0x00) ){
std::string out(".");
std::bitset<8> x(convertval);
unsigned long l = x.to_ulong();
unsigned char c = static_cast<unsigned char>(l);
out[0] = c;
return out;
} else if ( (convertval >= 0x80) && (convertval <= 0x07FF) ) {
std::string out("..");
int firstShift = (convertval >> 0x06) ^ 0xC0;
int secondShift = ((convertval ^ 0xFFC0) | 0x80) & ~0x40;
std::bitset<8> first(firstShift);
std::bitset<8> last(secondShift);
unsigned long l = first.to_ulong();
unsigned char c = static_cast<unsigned char>(l);
out[0] = c;
unsigned long ltwo = last.to_ulong();
unsigned char ctwo = static_cast<unsigned char>(ltwo);
out[1] = ctwo;
return out;
} else if( (convertval >= 0x0800) && (convertval <= 0xFFFF) ){
std::string out("...");
int firstShift = ((convertval ^ 0xFC0FFF) >> 0x0C) | 0xE0;
int secondShift = (((convertval ^ 0xFFF03F) >> 0x06) | 0x80) & ~0x40;
int thirdShift = ((convertval ^ 0xFFFC0) | 0x80) & ~0x40;
std::bitset<8> first(firstShift);
std::bitset<8> second(secondShift);
std::bitset<8> third(thirdShift);
unsigned long lone = first.to_ulong();
unsigned char cone = static_cast<unsigned char>(lone);
out[0] = cone;
unsigned long ltwo = second.to_ulong();
unsigned char ctwo = static_cast<unsigned char>(ltwo);
out[1] = ctwo;
unsigned long lthree = third.to_ulong();
unsigned char cthree = static_cast<unsigned char>(lthree);
out[2] = cthree;
return out;
} else{
return " ";
}
}
class NFontApi
{
public:
NFontApi();
~NFontApi();
void LoadTTF(std::string path);
std::vector<unsigned char> GetGlyphBitmap(int glyph);
std::string GetStatus(){ return status; }
float GetScale() {return scale; }
int GetGlyphWidth(int glyph);
int GetGlyphHeight(int glyph);
int GetLineHeight(){ return l_h; }
private:
std::string status;
int height;
float scale;
int b_w;
int b_h;
int l_h;
int w;
int h;
int x0,y0,x1,y1;
int ascent,baseline, decent;
stbtt_fontinfo font;
};
}

5077
include/renderd7/external/stb_truetype.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,7 @@
#pragma once
#include <string>
#include <iostream>
#include <iomanip>
namespace RenderD7
{
@ -30,3 +32,13 @@ T remove_ext(T const & filename)
typename T::size_type const p(filename.find_last_of('.'));
return p > 0 && p != T::npos ? filename.substr(0, p) : filename;
}
template< typename T >
std::string Int_To_Hex( T i )
{
std::stringstream stream;
stream << "0x"
<< std::setfill ('0') << std::setw(sizeof(T)*2)
<< std::hex << i;
return stream.str();
}

View File

@ -254,7 +254,7 @@ void RenderD7::BitmapPrinter::Benchmark()
std::string resultt = "TestMode: " + std::to_string(testfpsd) + "fps" + "\nRendered Frames: " + renderedf + "\nMax Cpu Time: " + avgdtt + "\nAvg Cpu Time: " + avgcpu + "\nAvg Fps: " + avgfps + "\nAvg EncodeTime: " + avgcpu2 + "ms/f\nAvg DecodeTime: " + avgcpu3 + "ms\n";
this->ClearBlank();
this->DrawRectFilled(0, 0, this->bitmap.bmp_info_header.width, this->bitmap.bmp_info_header.height, 0, 0, 0, 255);
this->DrawText(0, 0, 0, RenderD7::Color::Hex("#ffffff"), resultt);
this->DrawDebugText(0, 0, 0, RenderD7::Color::Hex("#ffffff"), resultt);
std::string outname = csvpc + "/benchmark_" + RenderD7::GetTimeStr() + ".png";
this->SavePng(outname);
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("Benchmark", "Saved to: \n" + outname));
@ -275,12 +275,12 @@ void RenderD7::BitmapPrinter::Benchmark()
this->ClearBlank();
this->DrawRectFilled(0, 0, this->bitmap.bmp_info_header.width, this->bitmap.bmp_info_header.width, 255, 255, 255, 255);
this->DrawRect(5, 5, this->bitmap.bmp_info_header.width - 10, this->bitmap.bmp_info_header.height - 10, 5, 0, 0, 0, 0);
//this->DrawText(20, 20, 0, RenderD7::Color::Hex("#ffffff"), "Fps: " + std::to_string(fps));
this->DrawText(0, 0, 0.5f, RenderD7::Color::Hex("#ff0000"), "Time: " + std::to_string(timer));
this->DrawText(0, 10, 0.5f, RenderD7::Color::Hex("#ff0000"), "Fps: " + std::to_string(fps));
this->DrawText(0, 20, 0.5f, RenderD7::Color::Hex("#ff0000"), "dt: " + std::to_string(dtt));
this->DrawText(0, 30, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxEncodeTime: " + std::to_string(mdtt2*1000) + "ms/f");
this->DrawText(0, 40, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxDecodeTime: " + std::to_string(mdtt3*1000) + "ms");
//this->DrawDebugText(20, 20, 0, RenderD7::Color::Hex("#ffffff"), "Fps: " + std::to_string(fps));
this->DrawDebugText(0, 0, 0.5f, RenderD7::Color::Hex("#ff0000"), "Time: " + std::to_string(timer));
this->DrawDebugText(0, 10, 0.5f, RenderD7::Color::Hex("#ff0000"), "Fps: " + std::to_string(fps));
this->DrawDebugText(0, 20, 0.5f, RenderD7::Color::Hex("#ff0000"), "dt: " + std::to_string(dtt));
this->DrawDebugText(0, 30, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxEncodeTime: " + std::to_string(mdtt2*1000) + "ms/f");
this->DrawDebugText(0, 40, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxDecodeTime: " + std::to_string(mdtt3*1000) + "ms");
uint64_t currentTime2 = svcGetSystemTick();
dtt2 = ((float)(currentTime2 / (float)TICKS_PER_MSEC) - (float)(lastTime2 / (float)TICKS_PER_MSEC)) / 1000.f;
hdttt2.push_back(dtt2);
@ -330,8 +330,9 @@ void RenderD7::BitmapPrinter::SetupBenchmark(int framerate)
#include <renderd7/debugfont.h>
void RenderD7::BitmapPrinter::DrawDebugChar(u32 posX, u32 posY, u32 color, char character)
void RenderD7::BitmapPrinter::DrawDebugChar(u32 posX, u32 posY, int t_size, u32 color, char character)
{
bool isscale = (t_size > 1) ? true : false;
for(u32 y = 0; y < 8; y++)
{
char charPos = debugfont[character * 8 + y];
@ -339,60 +340,31 @@ void RenderD7::BitmapPrinter::DrawDebugChar(u32 posX, u32 posY, u32 color, char
for(u32 x = 0; x < 8; x++)
if(((charPos >> (7 - x)) & 1) == 1)
{
DrawPixel((int)posX + x + 1, (int)posY + y + 1, UNPACK_BGRA(color));
if (!isscale) DrawPixel((int)posX + x + 1, (int)posY + y + 1, UNPACK_BGRA(color));
if (isscale) DrawRectFilled(((int)posX) + (x*t_size) + 1, ((int)posY) + (y*t_size) + 1, t_size, t_size, UNPACK_BGRA(color));
}
}
}
void RenderD7::BitmapPrinter::DrawChar(u32 posX, u32 posY, int t_size, u32 color, char character)
void RenderD7::BitmapPrinter::DrawChar(int posX, int posY, float t_size, u32 color, char character, RenderD7::NFontApi font)
{
for(u32 y = 0; y < (uint32_t)(8*t_size); y++)
for(int y = 0; y < font.GetGlyphHeight(RenderD7::utf8_decode(RenderD7::IntToUtf8((int)character).c_str())); y++)
{
char charPos = debugfont[character * 8 + y/t_size];
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(u32 x = 0; x < (uint32_t)(8*t_size); x++)
if(((charPos >> (7 - x)) & 1) == 1)
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)
{
DrawPixel((int)posX + x + 1, (int)posY + y + 1, UNPACK_BGRA(color));
DrawPixel(posX + x + 1, posY + y + 1, UNPACK_BGRA(color));
}
}
}
#define SPACING_Y 10
#define SPACING_X 8
void RenderD7::BitmapPrinter::DrawDebugText(int x, int y, float t_size, u32 color, std::string text)
{
for(u32 i = 0, line_i = 0; i < strlen(text.c_str()); i++)
switch(text[i])
{
case '\n':
y += SPACING_Y;
line_i = 0;
break;
case '\t':
line_i += 2;
break;
default:
//Make sure we never get out of the screen
if(line_i >= (((u32)this->bitmap.bmp_info_header.width) - (u32)x) / SPACING_X)
{
y += SPACING_Y;
line_i = 1; //Little offset so we know the same text continues
if(text[i] == ' ') break; //Spaces at the start look weird
}
this->DrawDebugChar((u32)x + line_i * SPACING_X, (u32)y, color, text[i]);
line_i++;
break;
}
}
void RenderD7::BitmapPrinter::DrawText(int x, int y, int t_size, u32 color, std::string text)
void RenderD7::BitmapPrinter::DrawDebugText(int x, int y, int t_size, u32 color, std::string text)
{
if (t_size < 1)
{
@ -403,24 +375,59 @@ void RenderD7::BitmapPrinter::DrawText(int x, int y, int t_size, u32 color, std:
switch(text[i])
{
case '\n':
y += SPACING_Y*t_size;
y += (SPACING_Y*t_size);
line_i = 0;
break;
case '\t':
line_i += 2*t_size;
line_i += 2;
break;
default:
//Make sure we never get out of the screen
if(line_i >= (((u32)this->bitmap.bmp_info_header.width) - (u32)x) / SPACING_X*t_size)
if(line_i >= (((u32)this->bitmap.bmp_info_header.width) - (u32)x) / (SPACING_X*t_size))
{
y += SPACING_Y*t_size;
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((u32)x + line_i * SPACING_X, (u32)y, t_size, color, text[i]);
this->DrawDebugChar((u32)x + line_i * (SPACING_X*t_size), (u32)y, t_size, color, text[i]);
line_i++;
break;
}
}
void RenderD7::BitmapPrinter::DrawText(int x, int y, float t_size, u32 color, std::string text, RenderD7::NFontApi font)
{
if (t_size < 1)
{
t_size = 1;
}
for(u32 i = 0, line_i = 0; i < strlen(text.c_str()); i++)
switch(text[i])
{
case '\n':
y += (font.GetLineHeight());
line_i = 0;
break;
case '\t':
line_i += 2;
break;
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))
{
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);
line_i++;
break;

70
source/NFontApi.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <renderd7/Fonts/NFontApi.hpp>
#define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation
#include <renderd7/external/stb_truetype.h>
RenderD7::NFontApi::NFontApi()
{
}
RenderD7::NFontApi::~NFontApi()
{
}
void RenderD7::NFontApi::LoadTTF(std::string path)
{
/////READ FILE
unsigned char* buffer;
long size = 0;
FILE *ttf__ = fopen(path.c_str(), "rb");
fseek(ttf__, 0, SEEK_END);
size = ftell(ttf__);
fseek(ttf__, 0, SEEK_SET);
buffer = (unsigned char*)malloc(size);
fread(buffer, size, 1, ttf__);
fclose(ttf__);
/////Setup Font
if (!stbtt_InitFont(&font, buffer, 0))
{
printf("failed\n");
status+="failed\n";
return;
}
status+="success!\n";
l_h = 24; /* line height */
scale = stbtt_ScaleForPixelHeight(&font, l_h);
stbtt_GetFontVMetrics(&font, &ascent,&decent,0);
baseline = (int) (ascent*scale);
height = (int) ((ascent - decent)*scale);
}
std::vector<unsigned char> RenderD7::NFontApi::GetGlyphBitmap(int glyph)
{
stbtt_GetGlyphBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1);
w = x1-x0;
h = y1-y0;
std::vector<unsigned char> bitmap;
bitmap.resize(h*w);
stbtt_MakeGlyphBitmap(&font, bitmap.data(), w, h, w, scale, scale, glyph);
return bitmap;
}
int RenderD7::NFontApi::GetGlyphHeight(int glyph)
{
stbtt_GetGlyphBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1);
w = x1-x0;
h = y1-y0;
return h;
}
int RenderD7::NFontApi::GetGlyphWidth(int glyph)
{
stbtt_GetGlyphBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1);
w = x1-x0;
h = y1-y0;
return w;
}

View File

@ -3,6 +3,11 @@
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#include <fstream>
#include <string.h>
#include <string>
std::string RenderD7::FormatString(std::string fmt_str, ...)
{
@ -17,7 +22,10 @@ std::string RenderD7::FormatString(std::string fmt_str, ...)
std::string RenderD7::GetTimeStr(void)
{
time_t unixTime = time(NULL);
struct tm* timeStruct = gmtime((const time_t*)&unixTime);
return RenderD7::FormatString("%02i-%02i-%02i", timeStruct->tm_hour, timeStruct->tm_min, timeStruct->tm_sec);
time_t unixTime;
struct tm timeStruct;
time(&unixTime);
localtime_r(&unixTime, &timeStruct);
return FormatString("%04i-%02i-%02i_%02i-%02i-%02i", timeStruct.tm_year + 1900, timeStruct.tm_mon + 1, timeStruct.tm_mday,
timeStruct.tm_hour, timeStruct.tm_min, timeStruct.tm_sec);
}

View File

@ -8,8 +8,8 @@ RenderD7::Toast::Toast(std::string head, std::string msg)
this->toast.ClearBlank();
this->toast.DrawRectFilled(0, 0, 400, 70, 40, 40, 40, 255);
this->toast.DrawRectFilled(0, 0, 400, 25, 70, 70, 70, 255);
this->toast.DrawText(4, 5, 0, RenderD7::Color::Hex("#ffffff"), this->head);
this->toast.DrawText(4, 40, 0, RenderD7::Color::Hex("#ffffff"), this->msg);
this->toast.DrawDebugText(4, 5, 0, RenderD7::Color::Hex("#ffffff"), this->head);
this->toast.DrawDebugText(4, 40, 0, RenderD7::Color::Hex("#ffffff"), this->msg);
this->toastrendered->LoadPFromBuffer(BitmapConverter::ConvertData(toast.GetBitmap().DATA()));
}

View File

@ -242,9 +242,9 @@ void RenderD7::Error::DisplayFatalError(std::string toptext, std::string errorte
C2D_TargetClear(Bottom, DSEVENBLACK);
RenderD7::BitmapPrinter errorss(400, 240);
errorss.DrawRectFilled(0, 0, 400, 240, 0, 0, 0, 255);
errorss.DrawText(4, 4, 2, RenderD7::Color::Hex("#ff0000"), toptext);
errorss.DrawText(4, 26, 1, RenderD7::Color::Hex("#000000"), errortext);
errorss.DrawText(4, 230, 1, RenderD7::Color::Hex("#000000"), "Press Start to Exit!");
errorss.DrawDebugText(4, 4, 2, RenderD7::Color::Hex("#ff0000"), toptext);
errorss.DrawDebugText(4, 26, 1, RenderD7::Color::Hex("#000000"), errortext);
errorss.DrawDebugText(4, 230, 1, RenderD7::Color::Hex("#000000"), "Press Start to Exit!");
RenderD7::Image img;
img.LoadFromBitmap(errorss.GetBitmap());
RenderD7::OnScreen(Top);