Implement Experiment of NFontApi
This commit is contained in:
@ -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
145
include/renderd7/Fonts/NFontApi.hpp
Normal file
145
include/renderd7/Fonts/NFontApi.hpp
Normal 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
5077
include/renderd7/external/stb_truetype.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@ -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();
|
||||
}
|
Reference in New Issue
Block a user