aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/FontData.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Graphics/FontData.cpp')
-rw-r--r--src/libjin/Graphics/FontData.cpp115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/libjin/Graphics/FontData.cpp b/src/libjin/Graphics/FontData.cpp
new file mode 100644
index 0000000..1b66b12
--- /dev/null
+++ b/src/libjin/Graphics/FontData.cpp
@@ -0,0 +1,115 @@
+#include "FontData.h"
+#define STB_TRUETYPE_IMPLEMENTATION
+#include "../3rdparty/stb/stb_truetype.h"
+#include <stdio.h>
+
+namespace jin
+{
+namespace graphics
+{
+
+ FontData* FontData::createFontData(const unsigned char* data, unsigned int size)
+ {
+ FontData* font = nullptr;
+ try
+ {
+ font = new FontData(data, size);
+ return font;
+ }
+ catch (...)
+ {
+ return nullptr;
+ }
+ }
+
+ FontData::FontData(const unsigned char* d, unsigned int s)
+ {
+ raw.size = s;
+ raw.data = (unsigned char*)malloc(s);
+ memcpy(raw.data, d, s);
+ if (!stbtt_InitFont(&info, (const unsigned char*)raw.data, 0))
+ {
+ delete raw.data;
+ throw 0;
+ }
+ /* push default fontsize */
+ pushFontsize(FONT_SIZE);
+ }
+
+ FontData::~FontData()
+ {
+ free(raw.data);
+ }
+
+ /*
+ * (0, 0)
+ * +--------------+ ascent
+ * | +--------+ |
+ * | | | |
+ * | | bitmap | |
+ * +--|--------|--+ baseline
+ * | +--------+ |
+ * +--|-----------+ decent
+ * | |
+ * leftSideBearing |
+ * |
+ * advanceWidth
+ */
+ void FontData::getVMetrics(int* baseline, int* descent)
+ {
+ float scale = scales.back();
+ int ascent;
+ stbtt_GetFontVMetrics(&info, &ascent, descent, 0);
+ *baseline = (int)(ascent*scale) + 1; // slight adjustment
+ *descent = *baseline - (int)(*descent*scale) + 1;
+ }
+
+ void FontData::getHMetrics(unsigned int codepoint, int* advanceWidth, int* leftSideBearing)
+ {
+ float scale = scales.back();
+ int adw, lsb;
+ stbtt_GetCodepointHMetrics(&info, codepoint, &adw, &lsb);
+ *advanceWidth = (int)(adw*scale);
+ *leftSideBearing = (int)(lsb*scale);
+ }
+
+ void FontData::pushFontsize(unsigned int fs)
+ {
+ float sc = stbtt_ScaleForPixelHeight(&info, fs);
+ scales.push_back(sc);
+ }
+
+ void FontData::popFontsize()
+ {
+ /* always keep default font size on the bottom of stack */
+ if(scales.size() > 1)
+ scales.pop_back();
+ }
+
+ Channel* FontData::getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const
+ {
+ float scale = scales.back();
+ Channel* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, xoff, yoff);
+ return bitmap;
+ }
+
+ Color* FontData::getCodepointBitmap(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const
+ {
+ float scale = scales.back();
+ Channel* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, xoff, yoff);
+ int w = *width, h = *height;
+ //int xo = *xoff, yo = *yoff;
+ Color* bitmap32 = new Color[w*h];
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ bitmap32[x + y * w].set(0xff, 0xff, 0xff, bitmap[x + y * w]);
+ }
+ }
+ free(bitmap);
+ return bitmap32;
+ }
+
+}
+} \ No newline at end of file