2022-07-31 01:23:39 +02:00
|
|
|
#include <renderd7/Fonts/NFontApi.hpp>
|
|
|
|
#define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation
|
|
|
|
#include <renderd7/external/stb_truetype.h>
|
|
|
|
|
2022-07-31 10:02:29 +02:00
|
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
|
|
#include <renderd7/external/stb_image_write.h>
|
|
|
|
|
2022-07-31 01:23:39 +02:00
|
|
|
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";
|
2022-07-31 10:02:29 +02:00
|
|
|
b_h = 128;
|
|
|
|
b_w = 512;
|
2022-07-31 01:23:39 +02:00
|
|
|
l_h = 24; /* line height */
|
|
|
|
scale = stbtt_ScaleForPixelHeight(&font, l_h);
|
|
|
|
|
2022-07-31 10:02:29 +02:00
|
|
|
stbtt_GetFontVMetrics(&font, &ascent,&decent,&linegap);
|
|
|
|
linespace = scale * (ascent - decent + linegap);
|
|
|
|
|
2022-07-31 01:23:39 +02:00
|
|
|
baseline = (int) (ascent*scale);
|
|
|
|
height = (int) ((ascent - decent)*scale);
|
|
|
|
}
|
|
|
|
|
2022-07-31 10:02:29 +02:00
|
|
|
unsigned char* RenderD7::NFontApi::GetGlyphBitmap(char glyph)
|
2022-07-31 01:23:39 +02:00
|
|
|
{
|
2022-07-31 10:02:29 +02:00
|
|
|
//stbtt_GetGlyphBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1);
|
|
|
|
stbtt_GetCodepointBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1);
|
2022-07-31 01:23:39 +02:00
|
|
|
w = x1-x0;
|
|
|
|
h = y1-y0;
|
|
|
|
|
2022-07-31 10:02:29 +02:00
|
|
|
unsigned char* bitmap;
|
|
|
|
bitmap = stbtt_GetCodepointBitmap(&font, scale, scale, glyph, &w, &h, 0, 0);
|
2022-07-31 01:23:39 +02:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2022-07-31 10:02:29 +02:00
|
|
|
int RenderD7::NFontApi::GetGlyphHeight(char glyph)
|
2022-07-31 01:23:39 +02:00
|
|
|
{
|
2022-07-31 10:02:29 +02:00
|
|
|
stbtt_GetCodepointBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1);
|
2022-07-31 01:23:39 +02:00
|
|
|
w = x1-x0;
|
|
|
|
h = y1-y0;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2022-07-31 10:02:29 +02:00
|
|
|
int RenderD7::NFontApi::GetGlyphWidth(char glyph)
|
2022-07-31 01:23:39 +02:00
|
|
|
{
|
2022-07-31 10:02:29 +02:00
|
|
|
stbtt_GetCodepointBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1);
|
2022-07-31 01:23:39 +02:00
|
|
|
w = x1-x0;
|
|
|
|
h = y1-y0;
|
|
|
|
return w;
|
2022-07-31 10:02:29 +02:00
|
|
|
}
|