diff options
author | chai <chaifix@163.com> | 2018-10-14 22:52:40 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-14 22:52:40 +0800 |
commit | b1bbc998960fff2169dc5a992c47d08723472f9b (patch) | |
tree | 220f3bd5de2266e248884e11161dd715d7632ef2 /src/libjin/Graphics/FontData.cpp | |
parent | fbf989f9950a38566e0fb0fc5b6a7aebc9f0fb45 (diff) |
*直接渲染字符串
Diffstat (limited to 'src/libjin/Graphics/FontData.cpp')
-rw-r--r-- | src/libjin/Graphics/FontData.cpp | 115 |
1 files changed, 0 insertions, 115 deletions
diff --git a/src/libjin/Graphics/FontData.cpp b/src/libjin/Graphics/FontData.cpp deleted file mode 100644 index 1b66b12..0000000 --- a/src/libjin/Graphics/FontData.cpp +++ /dev/null @@ -1,115 +0,0 @@ -#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 |