From d94f4a50e1e967c5d225720002c439107124bc13 Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 14 Oct 2018 09:47:53 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E6=96=87=E5=AD=97=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/.vs/libjin/v14/.suo | Bin 142848 -> 142848 bytes build/Debug/05Font.exe | Bin 1091072 -> 1222144 bytes build/libjin.sln | 20 +- build/libjin/libjin.vcxproj | 24 +- build/libjin/libjin.vcxproj.filters | 54 ++- libjin/Graphics/Bitmap.h | 5 +- libjin/Graphics/Drawable.cpp | 2 - libjin/Graphics/Font.h | 30 -- libjin/Graphics/Font/Decoder.cpp | 78 +++++ libjin/Graphics/Font/Decoder.h | 58 ++++ libjin/Graphics/Font/Font.h | 33 ++ libjin/Graphics/Font/Page.h | 38 +++ libjin/Graphics/Font/TTF.cpp | 608 +++++++++++++++++++++++++++++++++ libjin/Graphics/Font/TTF.h | 133 ++++++++ libjin/Graphics/Font/TexFont.cpp | 89 +++++ libjin/Graphics/Font/TexFont.h | 56 +++ libjin/Graphics/Font/Text.cpp | 139 ++++++++ libjin/Graphics/Font/Text.h | 68 ++++ libjin/Graphics/Graphics.h | 4 +- libjin/Graphics/Image.cpp | 34 ++ libjin/Graphics/Image.h | 33 ++ libjin/Graphics/Page.h | 38 --- libjin/Graphics/Particles/Particle.cpp | 0 libjin/Graphics/Particles/Particle.h | 0 libjin/Graphics/Shaders/base.shader.h | 26 -- libjin/Graphics/Sprite.cpp | 0 libjin/Graphics/Sprite.h | 18 + libjin/Graphics/TTF.cpp | 522 ---------------------------- libjin/Graphics/TTF.h | 119 ------- libjin/Graphics/TexFont.cpp | 66 ---- libjin/Graphics/TexFont.h | 48 --- libjin/Graphics/Unicode.cpp | 125 ------- libjin/Graphics/Unicode.h | 85 ----- test/05Font/main.cpp | 6 +- 34 files changed, 1463 insertions(+), 1096 deletions(-) delete mode 100644 libjin/Graphics/Font.h create mode 100644 libjin/Graphics/Font/Decoder.cpp create mode 100644 libjin/Graphics/Font/Decoder.h create mode 100644 libjin/Graphics/Font/Font.h create mode 100644 libjin/Graphics/Font/Page.h create mode 100644 libjin/Graphics/Font/TTF.cpp create mode 100644 libjin/Graphics/Font/TTF.h create mode 100644 libjin/Graphics/Font/TexFont.cpp create mode 100644 libjin/Graphics/Font/TexFont.h create mode 100644 libjin/Graphics/Font/Text.cpp create mode 100644 libjin/Graphics/Font/Text.h create mode 100644 libjin/Graphics/Image.cpp create mode 100644 libjin/Graphics/Image.h delete mode 100644 libjin/Graphics/Page.h create mode 100644 libjin/Graphics/Particles/Particle.cpp create mode 100644 libjin/Graphics/Particles/Particle.h create mode 100644 libjin/Graphics/Sprite.cpp create mode 100644 libjin/Graphics/Sprite.h delete mode 100644 libjin/Graphics/TTF.cpp delete mode 100644 libjin/Graphics/TTF.h delete mode 100644 libjin/Graphics/TexFont.cpp delete mode 100644 libjin/Graphics/TexFont.h delete mode 100644 libjin/Graphics/Unicode.cpp delete mode 100644 libjin/Graphics/Unicode.h diff --git a/build/.vs/libjin/v14/.suo b/build/.vs/libjin/v14/.suo index fd9eecb..9c02e56 100644 Binary files a/build/.vs/libjin/v14/.suo and b/build/.vs/libjin/v14/.suo differ diff --git a/build/Debug/05Font.exe b/build/Debug/05Font.exe index 7b9484a..c04ef8c 100644 Binary files a/build/Debug/05Font.exe and b/build/Debug/05Font.exe differ diff --git a/build/libjin.sln b/build/libjin.sln index 59694f9..65cdd39 100644 --- a/build/libjin.sln +++ b/build/libjin.sln @@ -17,7 +17,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{D4 EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "05Font", "05Font\05Font.vcxproj", "{D1953718-E728-4A86-9CCF-8BEC1F5C5F97}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "06TextureFont", "06TextureFont\06TextureFont.vcxproj", "{A81B426C-A6C7-4861-92A9-A087872D2C53}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "06TextureFont", "06TextureFont\06TextureFont.vcxproj", "{9BE6EA25-E171-405D-86CA-AC13A7F387D1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -75,14 +75,14 @@ Global {D1953718-E728-4A86-9CCF-8BEC1F5C5F97}.Release|x64.Build.0 = Release|x64 {D1953718-E728-4A86-9CCF-8BEC1F5C5F97}.Release|x86.ActiveCfg = Release|Win32 {D1953718-E728-4A86-9CCF-8BEC1F5C5F97}.Release|x86.Build.0 = Release|Win32 - {A81B426C-A6C7-4861-92A9-A087872D2C53}.Debug|x64.ActiveCfg = Debug|x64 - {A81B426C-A6C7-4861-92A9-A087872D2C53}.Debug|x64.Build.0 = Debug|x64 - {A81B426C-A6C7-4861-92A9-A087872D2C53}.Debug|x86.ActiveCfg = Debug|Win32 - {A81B426C-A6C7-4861-92A9-A087872D2C53}.Debug|x86.Build.0 = Debug|Win32 - {A81B426C-A6C7-4861-92A9-A087872D2C53}.Release|x64.ActiveCfg = Release|x64 - {A81B426C-A6C7-4861-92A9-A087872D2C53}.Release|x64.Build.0 = Release|x64 - {A81B426C-A6C7-4861-92A9-A087872D2C53}.Release|x86.ActiveCfg = Release|Win32 - {A81B426C-A6C7-4861-92A9-A087872D2C53}.Release|x86.Build.0 = Release|Win32 + {9BE6EA25-E171-405D-86CA-AC13A7F387D1}.Debug|x64.ActiveCfg = Debug|x64 + {9BE6EA25-E171-405D-86CA-AC13A7F387D1}.Debug|x64.Build.0 = Debug|x64 + {9BE6EA25-E171-405D-86CA-AC13A7F387D1}.Debug|x86.ActiveCfg = Debug|Win32 + {9BE6EA25-E171-405D-86CA-AC13A7F387D1}.Debug|x86.Build.0 = Debug|Win32 + {9BE6EA25-E171-405D-86CA-AC13A7F387D1}.Release|x64.ActiveCfg = Release|x64 + {9BE6EA25-E171-405D-86CA-AC13A7F387D1}.Release|x64.Build.0 = Release|x64 + {9BE6EA25-E171-405D-86CA-AC13A7F387D1}.Release|x86.ActiveCfg = Release|Win32 + {9BE6EA25-E171-405D-86CA-AC13A7F387D1}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -93,7 +93,7 @@ Global {0E49D105-2032-4825-9FA1-54B1B94E3655} = {D401F737-EEF5-4EA3-8CEA-A15523AE68AD} {85071432-24B6-46D4-98D8-DAA63183093C} = {D401F737-EEF5-4EA3-8CEA-A15523AE68AD} {D1953718-E728-4A86-9CCF-8BEC1F5C5F97} = {D401F737-EEF5-4EA3-8CEA-A15523AE68AD} - {A81B426C-A6C7-4861-92A9-A087872D2C53} = {D401F737-EEF5-4EA3-8CEA-A15523AE68AD} + {9BE6EA25-E171-405D-86CA-AC13A7F387D1} = {D401F737-EEF5-4EA3-8CEA-A15523AE68AD} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {310E7948-2690-4896-9579-19E6AC4D405B} diff --git a/build/libjin/libjin.vcxproj b/build/libjin/libjin.vcxproj index f787ea4..58b1371 100644 --- a/build/libjin/libjin.vcxproj +++ b/build/libjin/libjin.vcxproj @@ -36,14 +36,18 @@ - - + + + + + + + - @@ -87,12 +91,18 @@ - - + + + + + + + + + - @@ -100,8 +110,6 @@ - - diff --git a/build/libjin/libjin.vcxproj.filters b/build/libjin/libjin.vcxproj.filters index 02b4364..f194817 100644 --- a/build/libjin/libjin.vcxproj.filters +++ b/build/libjin/libjin.vcxproj.filters @@ -61,6 +61,12 @@ {35881367-9a40-4391-9cc2-26d2ee869b8a} + + {dfda69e2-693e-4ab1-945f-478c251cc67a} + + + {4c70f035-3e66-4de8-8d0f-95919d42863c} + @@ -165,14 +171,26 @@ Graphics - + Graphics - + Graphics - - Graphics + + Graphics\Font + + + Graphics\Font + + + Graphics\Particles + + + Graphics\Font + + + Graphics\Font @@ -358,20 +376,32 @@ Graphics\Shaders - + Graphics - + Graphics - - Graphics + + Graphics\Font - - Graphics + + Graphics\Font - - Graphics + + Graphics\Font + + + Graphics\Font + + + Graphics\Particles + + + Graphics\Font + + + Graphics\Font diff --git a/libjin/Graphics/Bitmap.h b/libjin/Graphics/Bitmap.h index 5510569..85b9044 100644 --- a/libjin/Graphics/Bitmap.h +++ b/libjin/Graphics/Bitmap.h @@ -6,6 +6,7 @@ #include "../Math/Vector2.hpp" #include "../3rdparty/GLee/GLee.h" #include "Color.h" + namespace jin { namespace graphics @@ -18,7 +19,7 @@ namespace graphics static Bitmap* createBitmap(int w, int h, Color color = Color::BLACK); static Bitmap* clone(const Bitmap* bitmap); - ~Bitmap(); + virtual ~Bitmap(); /* init pixels */ void bind(Color* pixels, int w, int h); void resetPixels(const Color* pixels, int w, int h); @@ -33,7 +34,7 @@ namespace graphics inline int getWidth() const { return width; } inline int getHeight() const { return height; } - private: + protected: Bitmap(); Bitmap(int w, int h); diff --git a/libjin/Graphics/Drawable.cpp b/libjin/Graphics/Drawable.cpp index ea4561d..4be1cc2 100644 --- a/libjin/Graphics/Drawable.cpp +++ b/libjin/Graphics/Drawable.cpp @@ -44,8 +44,6 @@ namespace graphics texture_coords[4] = 1; texture_coords[5] = 1; texture_coords[6] = 1; texture_coords[7] = 0; - unsigned int w = size.w; - unsigned int h = size.h; const Color* pixels = bitmap->getPixels(); texture = gl.genTexture(); diff --git a/libjin/Graphics/Font.h b/libjin/Graphics/Font.h deleted file mode 100644 index 6c5a1fc..0000000 --- a/libjin/Graphics/Font.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef __LIBJIN_FONT_H -#define __LIBJIN_FONT_H -#include -#include "Unicode.h" - -namespace jin -{ -namespace graphics -{ - - struct Page; - - class Font - { - public: - Font() {} - virtual ~Font() {}; - - virtual void print(const Page* page, int x, int y) = 0; - virtual void print(const unicode::Sentence& text, int x, int y, int lineheight, int spacing = 0) = 0; - virtual Page* typeset(const unicode::Sentence& text, int lineheight, int spacing = 0) = 0; - virtual void print(unicode::Iterator& itor, int x, int y, int lineheight, int spacing = 0) = 0; - virtual Page* typeset(unicode::Iterator& itor, int lineheight, int spacing = 0) = 0; - - }; - -} // graphics -} // jin - -#endif \ No newline at end of file diff --git a/libjin/Graphics/Font/Decoder.cpp b/libjin/Graphics/Font/Decoder.cpp new file mode 100644 index 0000000..312e142 --- /dev/null +++ b/libjin/Graphics/Font/Decoder.cpp @@ -0,0 +1,78 @@ +#include +#include +#include "Decoder.h" + +namespace jin +{ +namespace graphics +{ +namespace unicode +{ + + /* utf8 byte string to unicode codepoint */ + static const char *utf8toCodepoint(const char *p, unsigned *res) { + return nullptr; + + } + + ///////////////////////////////////////////////////////////////////////////// + // decoders + ///////////////////////////////////////////////////////////////////////////// + + const void* Utf8::decode(const void* data, Codepoint* res) const + { + const char* p = (char*)data; + unsigned x, mask, shift; + switch (*p & 0xf0) { + case 0xf0: mask = 0x07; shift = 18; break; + case 0xe0: mask = 0x0f; shift = 12; break; + case 0xc0: + case 0xd0: mask = 0x1f; shift = 6; break; + default: + *res = *p; + return p + 1; + } + x = (*p & mask) << shift; + do { + if (*(++p) == '\0') { + *res = x; + return p; + } + shift -= 6; + x |= (*p & 0x3f) << shift; + } while (shift); + *res = x; + return p + 1; + } + + const void* Utf8::next(const void* data) const + { + const char* p = (char*)data; + unsigned x, mask, shift; + switch (*p & 0xf0) { + case 0xf0: mask = 0x07; shift = 18; break; + case 0xe0: mask = 0x0f; shift = 12; break; + case 0xc0: + case 0xd0: mask = 0x1f; shift = 6; break; + default: + return p + 1; + } + x = (*p & mask) << shift; + do { + if (*(++p) == '\0') { + return p; + } + shift -= 6; + x |= (*p & 0x3f) << shift; + } while (shift); + return p + 1; + } + + const void* Utf16::decode(const void* data, Codepoint* res) const + { + return nullptr; + } + +} // unicode +} // graphics +} // jin \ No newline at end of file diff --git a/libjin/Graphics/Font/Decoder.h b/libjin/Graphics/Font/Decoder.h new file mode 100644 index 0000000..fbfa574 --- /dev/null +++ b/libjin/Graphics/Font/Decoder.h @@ -0,0 +1,58 @@ +#ifndef __LIBJIN_UTF8_H +#define __LIBJIN_UTF8_H + +#include + +namespace jin +{ +namespace graphics +{ +namespace unicode +{ + + typedef unsigned int Codepoint; + + typedef std::vector Sentence; + + enum Encode + { + UCS16, // unicode 16bits + UCS32, // unicode 32bits + UTF8, // utf-8 + UTF16, // utf-16 + ASCII, // ASCII + }; + + class Decoder + { + public: + virtual const void* decode(const void* data, Codepoint* c) const = 0 ; + virtual const void* next(const void* data) const = 0; + }; + + class Utf8 : public Decoder + { + public: + const void* decode(const void* data, Codepoint* c) const override; + const void* next(const void* data) const override; + }; + + class Utf16 : public Decoder + { + public: + const void* decode(const void* data, Codepoint* c) const override; + const void* next(const void* data) const override; + }; + + class Ucs16 : public Decoder + { + public: + const void* decode(const void* data, Codepoint* c) const override; + const void* next(const void* data) const override; + }; + +} // unicode +} // graphics +} // jin + +#endif \ No newline at end of file diff --git a/libjin/Graphics/Font/Font.h b/libjin/Graphics/Font/Font.h new file mode 100644 index 0000000..d7232d4 --- /dev/null +++ b/libjin/Graphics/Font/Font.h @@ -0,0 +1,33 @@ +#ifndef __LIBJIN_FONT_H +#define __LIBJIN_FONT_H +#include +#include "Text.h" + +namespace jin +{ +namespace graphics +{ + + struct Page; + + class Font + { + public: + Font() {} + virtual ~Font() {}; + + virtual Page* typeset(const unicode::Text& text, int lineheight, int spacing = 0) = 0; + virtual Page* typeset(const unicode::Sentence& text, int lineheight, int spacing = 0) = 0; + virtual Page* typeset(unicode::Iterator itor, int lineheight, int spacing = 0) = 0; + + virtual void print(const Page* page, int x, int y) = 0; + virtual void print(const unicode::Sentence& text, int x, int y, int lineheight, int spacing = 0) = 0; + virtual void print(unicode::Iterator itor, int x, int y, int lineheight, int spacing = 0) = 0; + virtual void print(const unicode::Text& text, int x, int y, int lineheight, int spacing = 0) = 0; + + }; + +} // graphics +} // jin + +#endif \ No newline at end of file diff --git a/libjin/Graphics/Font/Page.h b/libjin/Graphics/Font/Page.h new file mode 100644 index 0000000..6e7cbdf --- /dev/null +++ b/libjin/Graphics/Font/Page.h @@ -0,0 +1,38 @@ +#ifndef __LIBJIN_PAGE_H +#define __LIBJIN_PAGE_H +#include "../../math/Vector2.hpp" +#include "Font.h" + +namespace jin +{ +namespace graphics +{ + + class Font; + + struct GlyphVertex + { + int x, y; // screen coordinates + float u, v; // texture uv + }; + + struct GlyphArrayDrawInfo + { + GLuint texture; // atlas + unsigned int start; // glyph vertex indecies + unsigned int count; // glyph vertex count + }; + + /* for reduce draw call */ + struct Page + { + Font* font; + std::vector glyphinfolist; + std::vector glyphvertices; + math::Vector2 size; + }; + +} +} + +#endif \ No newline at end of file diff --git a/libjin/Graphics/Font/TTF.cpp b/libjin/Graphics/Font/TTF.cpp new file mode 100644 index 0000000..dac6733 --- /dev/null +++ b/libjin/Graphics/Font/TTF.cpp @@ -0,0 +1,608 @@ +#include "../../jin_configuration.h" +#if LIBJIN_MODULES_RENDER + +#include + +#include "../../Common/Array.hpp" +#include "../OpenGL.h" +#include "../Color.h" +#include "../Shader.h" +#include "TTF.h" +#include "Page.h" + +#define STB_TRUETYPE_IMPLEMENTATION +#include "../../3rdparty/stb/stb_truetype.h" + +namespace jin +{ +namespace graphics +{ + + using namespace unicode; + + /////////////////////////////////////////////////////////////////////////////////////////////// + // TTFData + /////////////////////////////////////////////////////////////////////////////////////////////// + + TTFData* TTFData::createTTFData(const unsigned char* data, unsigned int size) + { + TTFData* ttf = nullptr; + try + { + ttf = new TTFData(data, size); + return ttf; + } + catch (...) + { + return nullptr; + } + } + + TTFData::TTFData(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 */ + pushTTFsize(FONT_SIZE); + } + + TTFData::~TTFData() + { + free(raw.data); + } + + /* + * (0, 0) + * +--------------+ ascent + * | +--------+ | + * | | | | + * | | bitmap | | + * +--|--------|--+ baseline + * | +--------+ | + * +--|-----------+ decent + * | | + * leftSideBearing | + * advanceWidth + */ + void TTFData::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 TTFData::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 TTFData::pushTTFsize(unsigned int fs) + { + float sc = stbtt_ScaleForPixelHeight(&info, fs); + scales.push_back(sc); + } + + void TTFData::popTTFsize() + { + /* always keep default ttf size on the bottom of stack */ + if (scales.size() > 1) + scales.pop_back(); + } + + Channel* TTFData::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* TTFData::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; + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // TTF + /////////////////////////////////////////////////////////////////////////////////////////////// + + #include "../Shaders/font.shader.h" + + using namespace std; + using namespace jin::math; + + const int TTF::TEXTURE_WIDTHS[] = { 128, 256, 256, 512, 512, 1024, 1024 }; + const int TTF::TEXTURE_HEIGHTS[] = { 128, 128, 256, 256, 512, 512, 1024 }; + + /* utf8 byte string to unicode codepoint */ + static const char* utf8toCodepoint(const char *p, Codepoint *res) { + unsigned x, mask, shift; + switch (*p & 0xf0) { + case 0xf0: mask = 0x07; shift = 18; break; + case 0xe0: mask = 0x0f; shift = 12; break; + case 0xc0: + case 0xd0: mask = 0x1f; shift = 6; break; + default: + *res = *p; + return p + 1; + } + x = (*p & mask) << shift; + do { + if (*(++p) == '\0') { + *res = x; + return p; + } + shift -= 6; + x |= (*p & 0x3f) << shift; + } while (shift); + *res = x; + return p + 1; + } + + /* little endian unicode */ + static const char* unicodeLittleEndian(const char* p, unsigned* res) + { + } + + /*static*/ TTF* TTF::createTTF(TTFData* fontData, unsigned int fontSzie) + { + TTF* ttf; + try + { + ttf = new TTF(fontData, fontSzie); + } + catch (...) + { + return nullptr; + } + return ttf; + } + + TTF::TTF(TTFData* f, unsigned int fontSize) + : cursor(0, 0) + , ttf(f) + , ttfsize(fontSize) + { + ttf->pushTTFsize(ttfsize); + ttf->getVMetrics(&baseline, &descent); + estimateSize(); + ttf->popTTFsize(); + /* create a default texture */ + createAtlas(); + } + + /* estimate the size of atlas texture */ + void TTF::estimateSize() + { + for (int level = 0; level <= TEXTURE_SIZE_LEVEL_MAX; ++level) + { + if (descent * (descent*0.8) * 96 <= TEXTURE_WIDTHS[level] * TEXTURE_HEIGHTS[level]) + { + textureWidth = TEXTURE_WIDTHS[level]; + textureHeight = TEXTURE_HEIGHTS[level]; + break; + } + } + } + + TTF::~TTF() + { + } + + GLuint TTF::createAtlas() + { + GLuint t; + gl.flushError(); + t = gl.genTexture(); + gl.bindTexture(t); + gl.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR); + gl.setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR); + gl.setTexParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + gl.setTexParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + gl.texImage(GL_RGBA8, textureWidth, textureHeight, GL_RGBA, GL_UNSIGNED_BYTE); + if (glGetError() != GL_NO_ERROR) + { + glDeleteTextures(1, &t); + gl.bindTexture(0); + return 0; + } + atlases.push_back(t); + gl.bindTexture(0); + return t; + } + + void TTF::print(const Sentence& t, int x, int y, int lineheight, int spacing) + { + Page* page = typeset(t, lineheight, spacing); + print(page, x, y); + delete page; + } + + void TTF::print(unicode::Iterator itor, int x, int y, int lineheight, int spacing) + { + Page* page = typeset(itor, lineheight, spacing); + print(page, x, y); + delete page; + } + + void TTF::print(const Text& text, int x, int y,int lineheight, int spacing) + { + Iterator itor(text); + Page* page = typeset(itor, lineheight, spacing); + print(page, x, y); + delete page; + } + +#define glyphvertices_push(_x, _y, _u, _v) \ + vertex.x = _x; vertex.y = _y;\ + vertex.u = _u; vertex.v = _v;\ + glyphvertices.push_back(vertex); + +#define glyphlize(c)\ + do{\ + glyph = &findGlyph(c); \ + if (texture != glyph->atlas) \ + { \ + GlyphArrayDrawInfo info; \ + info.start = i; \ + info.count = 0; \ + info.texture = glyph->atlas; \ + texture = glyph->atlas; \ + glyphinfolist.push_back(info); \ + } \ + glyphinfolist[glyphinfolist.size() - 1].count += 4; \ + TTFGlyph::Bbox& bbox = glyph->bbox; \ + glyphvertices_push(p.x, p.y, bbox.x, bbox.y); \ + glyphvertices_push(p.x, p.y + glyph->height, bbox.x, bbox.y + bbox.h); \ + glyphvertices_push(p.x + glyph->width, p.y + glyph->height, bbox.x + bbox.w, bbox.y + bbox.h); \ + glyphvertices_push(p.x + glyph->width, p.y, bbox.x + bbox.w, bbox.y); \ + }while(0) + + Page* TTF::typeset(unicode::Iterator itor, int lineheight, int spacing) + { + Page* page = new Page(); + page->font = this; + vector& glyphinfolist = page->glyphinfolist; + vector& glyphvertices = page->glyphvertices; + int texture = -1; + TTFGlyph* glyph = nullptr; + GlyphVertex vertex; + Vector2 p(0, 0); + int i = 0; + for (; itor != itor.end(); ++itor) + { + Codepoint c = *itor; + if (c == 0x0D) + continue; + if (c == 0x0A) + { + /* new line */ + p.y += lineheight; + p.x = 0; + continue; + } + glyphlize(c); + p.x += glyph->width + spacing; + i += 4; + } + itor.toBegin(); + getTextBox(itor, &page->size.w, &page->size.h, lineheight, spacing); + return page; + } + + Page* TTF::typeset(const Sentence& text, int lineheight, int spacing) + { + Page* page = new Page(); + page->font = this; + vector& glyphinfolist = page->glyphinfolist; + vector& glyphvertices = page->glyphvertices; + int texture = -1; + TTFGlyph* glyph = nullptr; + GlyphVertex vertex; + Vector2 p(0, 0); + int i = 0; + for (Codepoint c : text) + { + if (c == 0x0D) + continue; + if (c == 0x0A) + { + /* new line */ + p.y += lineheight; + p.x = 0; + continue; + } + glyphlize(c); + p.x += glyph->width + spacing; + i += 4; + } + getTextBox(text, &page->size.w, &page->size.h, lineheight, spacing); + return page; + } + + Page* TTF::typeset(const Text& text, int lineheight, int spacing) + { + Page* page = new Page(); + page->font = this; + vector& glyphinfolist = page->glyphinfolist; + vector& glyphvertices = page->glyphvertices; + int texture = -1; + TTFGlyph* glyph = nullptr; + GlyphVertex vertex; + Vector2 p(0, 0); + int i = 0; + Iterator itor(text); + for (; itor != itor.end(); ++itor) + { + Codepoint c = *itor; + if (c == 0x0D) + continue; + if (c == 0x0A) + { + /* new line */ + p.y += lineheight; + p.x = 0; + continue; + } + glyphlize(c); + p.x += glyph->width + spacing; + i += 4; + } + itor.toBegin(); + getTextBox(itor, &page->size.w, &page->size.h, lineheight, spacing); + return page; + } + + void TTF::print(const Page* page, int x, int y) + { + Shader* shader = Shader::getCurrentShader(); + const vector& glyphinfolist = page->glyphinfolist; + const vector& glyphvertices = page->glyphvertices; + gl.ModelMatrix.setTransformation(x, y, 0, 1, 1, 0, 0); + shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix); + shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix); + for (int i = 0; i < glyphinfolist.size(); ++i) + { + const GlyphArrayDrawInfo& info = glyphinfolist[i]; + shader->bindVertexPointer(2, GL_INT, sizeof(GlyphVertex), &glyphvertices[info.start].x); + shader->bindUVPointer(2, GL_FLOAT, sizeof(GlyphVertex), &glyphvertices[info.start].u); + gl.bindTexture(info.texture); + gl.drawArrays(GL_QUADS, 0, info.count); + gl.bindTexture(0); + } + } + + int TTF::getCharWidth(int c) + { + int adw, lsb; + ttf->pushTTFsize(ttfsize); + ttf->getHMetrics(c, &adw, &lsb); + ttf->popTTFsize(); + return adw; + } + + int TTF::getCharHeight(int c) + { + return descent; + } + + int TTF::getTextWidth(const Sentence& t, int spacing) + { + ttf->pushTTFsize(ttfsize); + int res = 0; + int tmp = 0; + for (Codepoint c : t) + { + if (c == 0x0D) + continue; + if (c == 0x0A) + { + tmp = 0; + continue; + } + tmp += getCharWidth(c) + spacing; + if (tmp > res) + res = tmp; + } + ttf->popTTFsize(); + return res; + } + + int TTF::getTextHeight(const Sentence& t, int lineheight) + { + ttf->pushTTFsize(ttfsize); + int res = 0; + bool newline = true; + for (Codepoint c : t) + { + if (c == 0x0A) + newline = true; + else if (c == 0x0D); + else if (newline) + { + newline = false; + res += lineheight; + } + } + ttf->popTTFsize(); + return res; + } + + void TTF::getTextBox(const Sentence& text, int* w, int* h, int lineheight, int spacing) + { + ttf->pushTTFsize(ttfsize); + *w = 0; + *h = 0; + int tmp = 0; + bool newline = true; + for (Codepoint c : text) + { + if (c == 0x0D) + continue; + if (c == 0x0A) + { + tmp = 0; + newline = true; + continue; + } + else if (newline) + { + newline = false; + *h += lineheight; + } + tmp += getCharWidth(c) + spacing; + if (tmp > *w) + *w = tmp; + } + ttf->popTTFsize(); + } + + int TTF::getTextWidth(unicode::Iterator itor, int spacing) + { + ttf->pushTTFsize(ttfsize); + int res = 0; + int tmp = 0; + for (Codepoint c = *itor; itor != itor.end(); ++itor) + { + if (c == 0x0D) + continue; + if (c == 0x0A) + { + tmp = 0; + continue; + } + tmp += getCharWidth(c) + spacing; + if (tmp > res) + res = tmp; + } + ttf->popTTFsize(); + return res; + } + + int TTF::getTextHeight(unicode::Iterator itor, int lineheight) + { + ttf->pushTTFsize(ttfsize); + int res = 0; + bool newline = true; + for (Codepoint c = *itor; itor != itor.end(); ++itor) + { + if (c == 0x0A) + newline = true; + else if (c == 0x0D); + else if (newline) + { + newline = false; + res += lineheight; + } + } + ttf->popTTFsize(); + return res; + } + + void TTF::getTextBox(unicode::Iterator itor, int* w, int* h, int lineheight, int spacing) + { + ttf->pushTTFsize(ttfsize); + *w = 0; + *h = 0; + int tmp = 0; + bool newline = true; + unicode::Codepoint c; + for (; itor != itor.end(); ++itor) + { + c = *itor; + if (c == 0x0D) + continue; + if (c == 0x0A) + { + tmp = 0; + newline = true; + continue; + } + else if (newline) + { + newline = false; + *h += lineheight; + } + tmp += getCharWidth(c) + spacing; + if (tmp > *w) + *w = tmp; + } + ttf->popTTFsize(); + } + + TTF::TTFGlyph& TTF::bakeGlyph(unsigned int character) + { + int w, h, xoff, yoff; + ttf->pushTTFsize(ttfsize); + GLuint atlas = atlases.back(); + const Color* bitmap = ttf->getCodepointBitmap(character, &w, &h, &xoff, &yoff); + int adw, lsb; + { + ttf->getHMetrics(character, &adw, &lsb); + ttf->popTTFsize(); + if (cursor.x + adw > textureWidth ) + { + cursor.x = 0; + cursor.y += descent; + if (cursor.y + descent * 2 > textureHeight) + { + /* create new atlas */ + atlas = createAtlas(); + cursor.y = 0; + } + } + gl.bindTexture(atlas); + gl.texSubImage(cursor.x + xoff, cursor.y + yoff + baseline, w, h, GL_RGBA, GL_UNSIGNED_BYTE, bitmap); + gl.bindTexture(); + delete[] bitmap; + } + TTFGlyph glyph; + glyph.atlas = atlas; + glyph.bbox.x = cursor.x / (float)textureWidth; + glyph.bbox.y = cursor.y / (float)textureHeight; + glyph.bbox.w = adw / (float)textureWidth; + glyph.bbox.h = descent / (float)textureHeight; + glyph.width = adw; + glyph.height = descent; + glyphs.insert(std::pair(character, glyph)); + cursor.x += adw; + return glyphs[character]; + } + + TTF::TTFGlyph& TTF::findGlyph(unsigned int character) + { + map::iterator it = glyphs.find(character); + if (it != glyphs.end()) + return it->second; + else + return bakeGlyph(character); + } + +} // graphics +} // jin + +#endif // LIBJIN_MODULES_RENDER \ No newline at end of file diff --git a/libjin/Graphics/Font/TTF.h b/libjin/Graphics/Font/TTF.h new file mode 100644 index 0000000..83fe25b --- /dev/null +++ b/libjin/Graphics/Font/TTF.h @@ -0,0 +1,133 @@ +#ifndef __LIBJINTTF_H +#define __LIBJIN_TTF_H +#include "../../jin_configuration.h" +#if LIBJIN_MODULES_RENDER + +#include +#include + +#include "../../3rdparty/stb/stb_truetype.h" +#include "../../math/quad.h" +#include "../Color.h" +#include "../drawable.h" + +#include "Page.h" +#include "Font.h" +#include "Text.h" + +namespace jin +{ +namespace graphics +{ + + /** + * TTFData + * |- TTF + * |- TTF + * . + * . + * . + */ + class TTFData + { + public: + static TTFData* createTTFData(const unsigned char* data, unsigned int size); + + ~TTFData(); + + void pushTTFsize(unsigned int ttfsize); + void popTTFsize(); + + Channel* getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const; + Color* getCodepointBitmap(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const; + + void getVMetrics(int* baseline, int* descent); + void getHMetrics(unsigned int codepoint, int* advanceWidth, int* leftSideBearing); + + private: + static const unsigned int FONT_SIZE = 12; + + TTFData(const unsigned char* data, unsigned int size); + + stbtt_fontinfo info; + struct + { + unsigned char* data; + unsigned int size; + } raw; + std::vector scales; + + }; + + class TTF : public Font + { + public: + static TTF* createTTF(TTFData* ttfData, unsigned int ttfSzie); + + Page* typeset(const unicode::Text& text, int lineheight, int spacing = 0) override; + Page* typeset(const unicode::Sentence& text, int lineheight, int spacing = 0) override; + Page* typeset(unicode::Iterator itor, int lineheight, int spacing = 0) override; + + void print(const unicode::Sentence& text, int x, int y, int lineheight, int spacing = 0) override; + void print(const Page* page, int x, int y) override; + void print(unicode::Iterator itor, int x, int y, int lineheight, int spacing = 0) override; + void print(const unicode::Text& text, int x, int y, int lineheight, int spacing = 0) override; + + ~TTF(); + + private: + struct TTFGlyph + { + GLuint atlas; + /* normalized coordinates */ + struct Bbox + { + float x, y; + float w, h; + } bbox; + /* glyph size in pixel */ + unsigned int width, height; + }; + + static const int TEXTURE_SIZE_LEVELS_COUNT = 7; + static const int TEXTURE_SIZE_LEVEL_MAX = TEXTURE_SIZE_LEVELS_COUNT - 1; + static const int TEXTURE_WIDTHS[TEXTURE_SIZE_LEVELS_COUNT]; + static const int TEXTURE_HEIGHTS[TEXTURE_SIZE_LEVELS_COUNT]; + + TTF(TTFData* ttf, unicode::Codepoint ttfSize); + + void estimateSize(); + GLuint createAtlas(); + TTFGlyph& bakeGlyph(unicode::Codepoint character); + TTFGlyph& findGlyph(unicode::Codepoint character); + + int getCharWidth(int c); + int getCharHeight(int c); + + int getTextWidth(const unicode::Sentence& text, int spacing = 0); + int getTextHeight(const unicode::Sentence& text, int lineheight); + void getTextBox(const unicode::Sentence& text, int* w, int* h, int lineheight, int spacing = 0); + + int getTextWidth(unicode::Iterator text, int spacing = 0); + int getTextHeight(unicode::Iterator text, int lineheight); + void getTextBox(unicode::Iterator text, int* w, int* h, int lineheight, int spacing = 0); + + int textureWidth; + int textureHeight; + std::vector atlases; + std::map glyphs; + TTFData* ttf; + const unsigned int ttfsize; + int baseline; + int descent; + + /* cursor helped render to texture */ + math::Vector2 cursor; + + }; + +} // graphics +} // jin + +#endif // LIBJIN_MODULES_RENDER +#endif // __LIBJIN_FONT_H \ No newline at end of file diff --git a/libjin/Graphics/Font/TexFont.cpp b/libjin/Graphics/Font/TexFont.cpp new file mode 100644 index 0000000..9168f97 --- /dev/null +++ b/libjin/Graphics/Font/TexFont.cpp @@ -0,0 +1,89 @@ +#include "TexFont.h" + +namespace jin +{ +namespace graphics +{ + + using namespace unicode; + + /* + * +--------------------+ + * | top | + * | l ************* r | + * | e ************* i | + * | f ****glyphs*** g | + * | t ************* h | + * | ************* t | + * | bottom | + * +--------------------+ + */ + + /* create texture font from tilemap */ + TexFont * TexFont::createTexFont(const Bitmap* bitmap, const Sentence& codepoints, int cellw, int cellh) + { + TexFont* tf = new TexFont(bitmap, codepoints, cellw, cellh); + return tf; + } + + /* create texture font from seperated glyphs */ + TexFont* TexFont::createTexFont(const Bitmap* bitmap, const Sentence& codepoints, Color mask, int cellh) + { + TexFont* tf = new TexFont(bitmap, codepoints, mask, cellh); + return tf; + } + + TexFont::~TexFont() + { + } + + Page* TexFont::typeset(const Sentence& text, int lineheight, int spacing) + { + return nullptr; + } + + Page* TexFont::typeset(const Text& text, int lineheight, int spacing) + { + return nullptr; + } + + Page* TexFont::typeset(unicode::Iterator itor, int lineheight, int spacing) + { + return nullptr; + } + + void TexFont::print(const Page* page, int x, int y) + { + + } + + void TexFont::print(const Sentence& text, int x, int y, int linehgiht, int spacing) + { + + } + + void TexFont::print(const Text& text, int x, int y, int lineheight, int spacing) + { + + } + + void TexFont::print(unicode::Iterator itor, int x, int y, int lineheight, int spacing) + { + + } + + TexFont::TexFont(const Bitmap* bitmap, const Sentence& codepoints, int cellw, int cellh) + : Drawable(bitmap) + { + int l, r, t, b; + + } + + TexFont::TexFont(const Bitmap* bitmap, const Sentence& codepoints, Color mask, int cellh) + : Drawable(bitmap) + { + + } + +} +} \ No newline at end of file diff --git a/libjin/Graphics/Font/TexFont.h b/libjin/Graphics/Font/TexFont.h new file mode 100644 index 0000000..c2142fa --- /dev/null +++ b/libjin/Graphics/Font/TexFont.h @@ -0,0 +1,56 @@ +#ifndef __LIBJIN_TEXTURE_FONT_H +#define __LIBJIN_TEXTURE_FONT_H + +#include +#include + +#include "../../Math/Vector4.hpp" +#include "../Drawable.h" +#include "../Bitmap.h" + +#include "Page.h" +#include "Font.h" +#include "Text.h" + +namespace jin +{ +namespace graphics +{ + + /* Texture font */ + class TexFont : public Font + , public Drawable + { + public: + /* create texture font from tilemap */ + static TexFont* createTexFont(const Bitmap* bitmap, const unicode::Sentence& codepoints, int cellw, int cellh); + static TexFont* createTexFont(const Bitmap* bitmap, const unicode::Text& text, int cellw, int cellh); + /* create texture font from seperated glyphs */ + static TexFont* createTexFont(const Bitmap* bitmap, const unicode::Sentence& codepoints, Color mask, int cellh); + static TexFont* createTexFont(const Bitmap* bitmap, const unicode::Text& text, Color mask, int cellh); + + ~TexFont(); + + Page* typeset(const unicode::Text& text, int lineheight, int spacing = 0) override; + Page* typeset(const unicode::Sentence& text, int lineheight, int spacing = 0) override ; + Page* typeset(unicode::Iterator itor, int lineheight, int spacing = 0) override; + + void print(const Page* page, int x, int y) override; + void print(const unicode::Sentence& text, int x, int y, int linehgiht, int spacing = 0) override; + void print(unicode::Iterator itor, int x, int y, int lineheight, int spacing = 0) override; + void print(const unicode::Text& text, int x, int y, int lineheight, int spacing = 0)override; + + private: + struct TexGlyph { unsigned short x, y, w, h;}; + + TexFont(const Bitmap* bitmap, const unicode::Sentence& codepoints, int cellw, int cellh); + TexFont(const Bitmap* bitmap, const unicode::Sentence& codepoints, Color mask, int cellh); + + std::map glyphs; + + }; + +} +} + +#endif \ No newline at end of file diff --git a/libjin/Graphics/Font/Text.cpp b/libjin/Graphics/Font/Text.cpp new file mode 100644 index 0000000..91774c3 --- /dev/null +++ b/libjin/Graphics/Font/Text.cpp @@ -0,0 +1,139 @@ +#include "Text.h" + +namespace jin +{ +namespace graphics +{ +namespace unicode +{ + + ///////////////////////////////////////////////////////////////////////////// + // iterator + ///////////////////////////////////////////////////////////////////////////// + + Iterator::Iterator(const Iterator& itor) + : text(itor.text) + , p(itor.p) + , decoder(itor.decoder) + { + } + + Iterator::Iterator(const Text& text) + : text(text) + , p(text.data) + , decoder(text.decoder) + { + } + + Iterator::~Iterator() + { + } + + Codepoint Iterator::get() + { + Codepoint codepoint; + decoder->decode(p, &codepoint); + return codepoint; + } + + Codepoint Iterator::operator*() + { + return get(); + } + + Iterator Iterator::begin() + { + Iterator itor(text); + itor.toBegin(); + return itor; + } + + Iterator Iterator::end() + { + Iterator itor(text); + itor.toEnd(); + return itor; + } + + void Iterator::toBegin() + { + p = (const unsigned char*)text.data; + } + + void Iterator::toEnd() + { + p = (const unsigned char*)text.data + text.length; + } + + Iterator& Iterator::operator ++() + { + p = decoder->next(p); + return *this; + } + + Iterator Iterator::operator ++(int) + { + + p = decoder->next(p); + Iterator itor(text); + itor.p = p; + return itor; + } + + bool Iterator::operator !=(const Iterator& itor) + { + const Text& text2 = itor.text; + return !(text.data == text2.data + && p == itor.p + && text.length == text2.length + && text.encode == text2.encode); + } + + bool Iterator::operator ==(const Iterator& itor) + { + const Text& text2 = itor.text; + return text.data == text2.data + && p == itor.p + && text.length == text2.length + && text.encode == text2.encode; + } + + ///////////////////////////////////////////////////////////////////////////// + // text + ///////////////////////////////////////////////////////////////////////////// + + Text::Text(Encode _encode, const void* _data, unsigned int _length) + : encode(_encode) + , length(_length) + { + data = new char[length]; + memcpy(data, _data, length); + switch (encode) + { + case UTF8: decoder = new Utf8(); break; + } + } + + Text::~Text() + { + delete decoder; + delete data; + } + + Iterator Text::getIterator() const + { + Iterator itor = Iterator(*this); + return itor; + } + + Sentence Text::getSentence() const + { + Sentence sentence; + for (Iterator it = getIterator(); it != it.end(); ++it) + sentence.push_back(*it); + return sentence; + } + +} // unicode +} // graphics +} // jin \ No newline at end of file diff --git a/libjin/Graphics/Font/Text.h b/libjin/Graphics/Font/Text.h new file mode 100644 index 0000000..11897a0 --- /dev/null +++ b/libjin/Graphics/Font/Text.h @@ -0,0 +1,68 @@ +#ifndef __LIBJIN_TEXT_H +#define __LIBJIN_TEXT_H + +#include "Decoder.h" + +namespace jin +{ +namespace graphics +{ +namespace unicode +{ + + class Text; + + class Iterator + { + public: + Iterator(const Iterator& itor); + Iterator(const Text& text); + ~Iterator(); + + Codepoint get(); + Iterator begin(); + Iterator end(); + void toBegin(); + void toEnd(); + Codepoint operator *(); + /* prefix ++ */ + Iterator& operator ++(); + /* postfix ++ */ + Iterator operator ++(int); + bool operator !=(const Iterator& itor); + bool operator ==(const Iterator& itor); + + private: + void operator = (const Iterator&); + + const void* p; + const Decoder* const decoder; + const Text& const text; + }; + + /* raw encoded text */ + class Text + { + public: + Text(Encode encode, const void* data, unsigned int length); + ~Text(); + + Iterator getIterator() const; + Sentence getSentence() const; + + private: + friend class Iterator; + + const Encode encode; + Decoder* decoder; + + void* data; + const unsigned int length; + + }; + +} // unicode +} // graphics +} // jin + +#endif \ No newline at end of file diff --git a/libjin/Graphics/Graphics.h b/libjin/Graphics/Graphics.h index ed96315..bca2f6f 100644 --- a/libjin/Graphics/Graphics.h +++ b/libjin/Graphics/Graphics.h @@ -5,12 +5,14 @@ #include "canvas.h" #include "color.h" -#include "TTF.h" #include "Shapes.h" #include "texture.h" #include "Shader.h" #include "window.h" #include "Bitmap.h" +#include "./Font/TTF.h" +#include "./Font/Text.h" + #endif // LIBJIN_MODULES_RENDER #endif // __LIBJIN_GRAPHICS_H \ No newline at end of file diff --git a/libjin/Graphics/Image.cpp b/libjin/Graphics/Image.cpp new file mode 100644 index 0000000..6203395 --- /dev/null +++ b/libjin/Graphics/Image.cpp @@ -0,0 +1,34 @@ +#include "../3rdparty/stb/stb_image.h" +#include "Image.h" + +namespace jin +{ +namespace graphics +{ + + /*static*/ Image* Image::createImage(const void* imgData, size_t size) + { + if (imgData == nullptr) + return nullptr; + int w, h; + void* data = stbi_load_from_memory((unsigned char *)imgData, size, &w, &h, NULL, STBI_rgb_alpha); + if (data == nullptr) + return nullptr; + Image* image = new Image(); + image->pixels = (Color*)data; + image->width = w; + image->height = h; + return image; + } + + Image::Image() + : Bitmap() + { + } + + Image::~Image() + { + } + +} +} diff --git a/libjin/Graphics/Image.h b/libjin/Graphics/Image.h new file mode 100644 index 0000000..5c426dc --- /dev/null +++ b/libjin/Graphics/Image.h @@ -0,0 +1,33 @@ +#ifndef __LIBJIN_IMAGE_H +#define __LIBJIN_IMAGE_H + +#include "Bitmap.h" + +namespace jin +{ +namespace graphics +{ + + /* just like bitmap but only from image file*/ + class Image : public Bitmap + { + public: + static Image* createImage(const void* imgData, size_t size); + ~Image(); + + private: + Image(); + + void bind(Color* pixels, int w, int h); + void resetPixels(const Color* pixels, int w, int h); + void resetPixels(const Color& pixels, int w, int h); + void setPixel(const Color& pixel, int x, int y); + void setPixels(Color pixels); + void setPixels(Color* pixels); + + }; + +} +} + +#endif \ No newline at end of file diff --git a/libjin/Graphics/Page.h b/libjin/Graphics/Page.h deleted file mode 100644 index b878a74..0000000 --- a/libjin/Graphics/Page.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef __LIBJIN_PAGE_H -#define __LIBJIN_PAGE_H -#include "../math/Vector2.hpp" -#include "Font.h" - -namespace jin -{ -namespace graphics -{ - - class Font; - - struct GlyphVertex - { - int x, y; // screen coordinates - float u, v; // texture uv - }; - - struct GlyphArrayDrawInfo - { - GLuint texture; // atlas - unsigned int start; // glyph vertex indecies - unsigned int count; // glyph vertex count - }; - - /* for reduce draw call */ - struct Page - { - Font* font; - std::vector glyphinfolist; - std::vector glyphvertices; - math::Vector2 size; - }; - -} -} - -#endif \ No newline at end of file diff --git a/libjin/Graphics/Particles/Particle.cpp b/libjin/Graphics/Particles/Particle.cpp new file mode 100644 index 0000000..e69de29 diff --git a/libjin/Graphics/Particles/Particle.h b/libjin/Graphics/Particles/Particle.h new file mode 100644 index 0000000..e69de29 diff --git a/libjin/Graphics/Shaders/base.shader.h b/libjin/Graphics/Shaders/base.shader.h index 53f8e5d..45b63cd 100644 --- a/libjin/Graphics/Shaders/base.shader.h +++ b/libjin/Graphics/Shaders/base.shader.h @@ -1,31 +1,5 @@ #ifndef __LIBJIN_BASE_SHADER_H #define __LIBJIN_BASE_SHADER_H -/* - * https://stackoverflow.com/questions/10868958/what-does-sampler2d-store - * The sampler2D is bound to a texture unit. The glUniform call binds it to texture - * unit zero. The glActiveTexture call is only needed if you are going to use multiple - * texture units (because GL_TEXTURE0 is the default anyway). -*/ -/* -#VERTEX_SHADER - -vertex vert(vertex v) -{ - return v; -} - -#END_VERTEX_SHADER - -#FRAGMENT_SHADER - -vec4 frag(vec4 color, Texture tex, vertex v) -{ - return Texel(tex, v.uv); -} - -#END_FRAGMENT_SHADER - -*/ static const char* base_shared = R"( #define Number float diff --git a/libjin/Graphics/Sprite.cpp b/libjin/Graphics/Sprite.cpp new file mode 100644 index 0000000..e69de29 diff --git a/libjin/Graphics/Sprite.h b/libjin/Graphics/Sprite.h new file mode 100644 index 0000000..acb8264 --- /dev/null +++ b/libjin/Graphics/Sprite.h @@ -0,0 +1,18 @@ +#ifndef __LIBJIN_IMAGE_H +#define __LIBJIN_IMAGE_H + +namespace jin +{ +namespace graphics +{ + + /* just like texture but with x,y */ + class Sprite + { + + }; + +} +} + +#endif \ No newline at end of file diff --git a/libjin/Graphics/TTF.cpp b/libjin/Graphics/TTF.cpp deleted file mode 100644 index 296e660..0000000 --- a/libjin/Graphics/TTF.cpp +++ /dev/null @@ -1,522 +0,0 @@ -#include "../jin_configuration.h" -#if LIBJIN_MODULES_RENDER - -#include "OpenGL.h" -#include "ttf.h" -#include -#include "color.h" -#include "Shader.h" -#include "Page.h" -#include "../Common/Array.hpp" - -namespace jin -{ -namespace graphics -{ - - /////////////////////////////////////////////////////////////////////////////////////////////// - // TTFData - /////////////////////////////////////////////////////////////////////////////////////////////// - - TTFData* TTFData::createTTFData(const unsigned char* data, unsigned int size) - { - TTFData* ttf = nullptr; - try - { - ttf = new TTFData(data, size); - return ttf; - } - catch (...) - { - return nullptr; - } - } - - TTFData::TTFData(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 */ - pushTTFsize(FONT_SIZE); - } - - TTFData::~TTFData() - { - free(raw.data); - } - - /* - * (0, 0) - * +--------------+ ascent - * | +--------+ | - * | | | | - * | | bitmap | | - * +--|--------|--+ baseline - * | +--------+ | - * +--------------+ decent - * | | - * leftSideBearing | - * advanceWidth - */ - void TTFData::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 TTFData::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 TTFData::pushTTFsize(unsigned int fs) - { - float sc = stbtt_ScaleForPixelHeight(&info, fs); - scales.push_back(sc); - } - - void TTFData::popTTFsize() - { - /* always keep default ttf size on the bottom of stack */ - if (scales.size() > 1) - scales.pop_back(); - } - - Channel* TTFData::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* TTFData::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; - } - - /////////////////////////////////////////////////////////////////////////////////////////////// - // TTF - /////////////////////////////////////////////////////////////////////////////////////////////// - - #include "Shaders/font.shader.h" - - using namespace std; - using namespace jin::math; - - const int TTF::TEXTURE_WIDTHS[] = { 128, 256, 256, 512, 512, 1024, 1024 }; - const int TTF::TEXTURE_HEIGHTS[] = { 128, 128, 256, 256, 512, 512, 1024 }; - - /* utf8 byte string to unicode codepoint */ - static const char* utf8toCodepoint(const char *p, Codepoint *res) { - unsigned x, mask, shift; - switch (*p & 0xf0) { - case 0xf0: mask = 0x07; shift = 18; break; - case 0xe0: mask = 0x0f; shift = 12; break; - case 0xc0: - case 0xd0: mask = 0x1f; shift = 6; break; - default: - *res = *p; - return p + 1; - } - x = (*p & mask) << shift; - do { - if (*(++p) == '\0') { - *res = x; - return p; - } - shift -= 6; - x |= (*p & 0x3f) << shift; - } while (shift); - *res = x; - return p + 1; - } - - /* little endian unicode */ - static const char* unicodeLittleEndian(const char* p, unsigned* res) - { - } - - /*static*/ TTF* TTF::createTTF(TTFData* fontData, unsigned int fontSzie) - { - TTF* ttf; - try - { - ttf = new TTF(fontData, fontSzie); - } - catch (...) - { - return nullptr; - } - return ttf; - } - - TTF::TTF(TTFData* f, unsigned int fontSize) - : cursor(0, 0) - , ttf(f) - , ttfsize(fontSize) - { - ttf->pushTTFsize(ttfsize); - ttf->getVMetrics(&baseline, &descent); - estimateSize(); - ttf->popTTFsize(); - /* create a default texture */ - createAtlas(); - } - - /* estimate the size of atlas texture */ - void TTF::estimateSize() - { - for (int level = 0; level <= TEXTURE_SIZE_LEVEL_MAX; ++level) - { - if (descent * (descent*0.8) * 96 <= TEXTURE_WIDTHS[level] * TEXTURE_HEIGHTS[level]) - { - textureWidth = TEXTURE_WIDTHS[level]; - textureHeight = TEXTURE_HEIGHTS[level]; - break; - } - } - } - - TTF::~TTF() - { - map::iterator it = glyphs.begin(); - for (; it != glyphs.end(); ++it) - { - delete it->second; - } - } - - GLuint TTF::createAtlas() - { - GLuint t; - gl.flushError(); - t = gl.genTexture(); - gl.bindTexture(t); - gl.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR); - gl.setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR); - gl.setTexParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - gl.setTexParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - gl.texImage(GL_RGBA8, textureWidth, textureHeight, GL_RGBA, GL_UNSIGNED_BYTE); - if (glGetError() != GL_NO_ERROR) - { - glDeleteTextures(1, &t); - gl.bindTexture(0); - return 0; - } - atlases.push_back(t); - gl.bindTexture(0); - return t; - } - - void TTF::print(const Sentence& t, int x, int y, int lineheight, int spacing) - { - Page* page = typeset(t, lineheight, spacing); - print(page, x, y); - delete page; - } - -#define glyphvertices_push(_x, _y, _u, _v) \ -vertex.x = _x; vertex.y = _y;\ -vertex.u = _u; vertex.v = _v;\ -glyphvertices.push_back(vertex); - - Page* TTF::typeset(Unicode::Iterator& itor, int lineheight, int spacing) - { - Page* page = new Page(); - page->font = this; - vector& glyphinfolist = page->glyphinfolist; - vector& glyphvertices = page->glyphvertices; - int texture = -1; - TTFGlyph* glyph = nullptr; - GlyphVertex vertex; - Vector2 p(0, 0); - int i = 0; - Codepoint c; - for (; *itor != NULL; ++itor) - { - c = *itor; - if (c == 0x0D) - continue; - /* new line */ - if (c == 0x0A) - { - p.y += lineheight; - p.x = 0; - continue; - } - glyph = findGlyph(c); - if (texture != glyph->atlas) - { - GlyphArrayDrawInfo info; - info.start = i; - info.count = 0; - info.texture = glyph->atlas; - texture = glyph->atlas; - glyphinfolist.push_back(info); - } - glyphinfolist[glyphinfolist.size() - 1].count += 4; - TTFGlyph::Bbox& bbox = glyph->bbox; - glyphvertices_push(p.x, p.y, bbox.x, bbox.y); - glyphvertices_push(p.x, p.y + glyph->height, bbox.x, bbox.y + bbox.h); - glyphvertices_push(p.x + glyph->width, p.y + glyph->height, bbox.x + bbox.w, bbox.y + bbox.h); - glyphvertices_push(p.x + glyph->width, p.y, bbox.x + bbox.w, bbox.y); - - p.x += glyph->width + spacing; - - i += 4; - } - itor = itor.begin(); - getTextBox(itor, &page->size.w, &page->size.h, lineheight, spacing); - return page; - } - - Page* TTF::typeset(const Sentence& text, int lineheight, int spacing) - { - Page* page = new Page(); - page->font = this; - vector& glyphinfolist = page->glyphinfolist; - vector& glyphvertices = page->glyphvertices; - int texture = -1; - TTFGlyph* glyph = nullptr; - GlyphVertex vertex; - Vector2 p(0, 0); - int i = 0; - for (Codepoint c : text) - { - if (c == 0x0D) - continue; - /* new line */ - if (c == 0x0A) - { - p.y += lineheight; - p.x = 0; - continue; - } - glyph = findGlyph(c); - if (texture != glyph->atlas) - { - GlyphArrayDrawInfo info; - info.start = i; - info.count = 0; - info.texture = glyph->atlas; - texture = glyph->atlas; - glyphinfolist.push_back(info); - } - glyphinfolist[glyphinfolist.size() - 1].count += 4; - TTFGlyph::Bbox& bbox = glyph->bbox; - glyphvertices_push(p.x, p.y, bbox.x, bbox.y); - glyphvertices_push(p.x, p.y + glyph->height, bbox.x, bbox.y + bbox.h); - glyphvertices_push(p.x + glyph->width, p.y + glyph->height, bbox.x + bbox.w, bbox.y + bbox.h); - glyphvertices_push(p.x + glyph->width, p.y, bbox.x + bbox.w, bbox.y); - - p.x += glyph->width + spacing; - - i += 4; - } - getTextBox(text, &page->size.w, &page->size.h, lineheight, spacing); - return page; - } - - void TTF::print(const Page* page, int x, int y) - { - Shader* shader = Shader::getCurrentShader(); - const vector& glyphinfolist = page->glyphinfolist; - const vector& glyphvertices = page->glyphvertices; - gl.ModelMatrix.setTransformation(x, y, 0, 1, 1, 0, 0); - shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix); - shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix); - for (int i = 0; i < glyphinfolist.size(); ++i) - { - const GlyphArrayDrawInfo& info = glyphinfolist[i]; - shader->bindVertexPointer(2, GL_INT, sizeof(GlyphVertex), &glyphvertices[info.start].x); - shader->bindUVPointer(2, GL_FLOAT, sizeof(GlyphVertex), &glyphvertices[info.start].u); - gl.bindTexture(info.texture); - gl.drawArrays(GL_QUADS, 0, info.count); - gl.bindTexture(0); - } - } - - int TTF::getCharWidth(int c) - { - int adw, lsb; - ttf->pushTTFsize(ttfsize); - ttf->getHMetrics(c, &adw, &lsb); - ttf->popTTFsize(); - return adw; - } - - int TTF::getCharHeight(int c) - { - return descent; - } - - int TTF::getTextWidth(const Sentence& t, int spacing) - { - ttf->pushTTFsize(ttfsize); - int res = 0; - int tmp = 0; - for (Codepoint c : t) - { - if (c == 0x0D) - continue; - if (c == 0x0A) - { - tmp = 0; - continue; - } - tmp += getCharWidth(c) + spacing; - if (tmp > res) - res = tmp; - } - ttf->popTTFsize(); - return res; - } - - int TTF::getTextHeight(const Sentence& t, int lineheight) - { - ttf->pushTTFsize(ttfsize); - int res = 0; - bool newline = true; - for (Codepoint c : t) - { - if (c == 0x0A) - newline = true; - else if (c == 0x0D); - else if (newline) - { - newline = false; - res += lineheight; - } - } - ttf->popTTFsize(); - return res; - } - - void TTF::getTextBox(const Sentence& text, int* w, int* h, int lineheight, int spacing) - { - ttf->pushTTFsize(ttfsize); - *w = 0; - *h = 0; - int tmp = 0; - bool newline = true; - for (Codepoint c : text) - { - if (c == 0x0D) - continue; - if (c == 0x0A) - { - tmp = 0; - newline = true; - continue; - } - else if (newline) - { - newline = false; - *h += lineheight; - } - tmp += getCharWidth(c) + spacing; - if (tmp > *w) - *w = tmp; - } - ttf->popTTFsize(); - } - - int TTF::getTextWidth(Unicode::Iterator& text, int spacing) - { - - } - - int TTF::getTextHeight(Unicode::Iterator& text, int lineheight) - { - - } - - void TTF::getTextBox(Unicode::Iterator& text, int* w, int* h, int lineheight, int spacing) - { - - } - - TTF::TTFGlyph* TTF::bakeGlyph(unsigned int character) - { - TTFGlyph* glyph = (TTFGlyph*)malloc(sizeof(TTFGlyph)); - int w, h, xoff, yoff; - ttf->pushTTFsize(ttfsize); - GLuint atlas = atlases.back(); - const Color* bitmap = ttf->getCodepointBitmap(character, &w, &h, &xoff, &yoff); - int adw, lsb; - { - ttf->getHMetrics(character, &adw, &lsb); - ttf->popTTFsize(); - if (cursor.x + adw > textureWidth ) - { - cursor.x = 0; - cursor.y += descent; - if (cursor.y + descent * 2 > textureHeight) - { - /* create new atlas */ - atlas = createAtlas(); - cursor.y = 0; - } - } - gl.bindTexture(atlas); - gl.texSubImage(cursor.x + xoff, cursor.y + yoff + baseline, w, h, GL_RGBA, GL_UNSIGNED_BYTE, bitmap); - gl.bindTexture(); - delete[] bitmap; - } - glyph->atlas = atlas; - glyph->bbox.x = cursor.x / (float)textureWidth; - glyph->bbox.y = cursor.y / (float)textureHeight; - glyph->bbox.w = adw / (float)textureWidth; - glyph->bbox.h = descent / (float)textureHeight; - glyph->width = adw; - glyph->height = descent; - glyphs.insert(std::pair(character, glyph)); - - cursor.x += adw; - return glyph; - } - - TTF::TTFGlyph* TTF::findGlyph(unsigned int character) - { - map::iterator it = glyphs.find(character); - if (it != glyphs.end()) - { - return it->second; - } - else - { - TTFGlyph* glyph = bakeGlyph(character); - return glyph; - } - } - -} // graphics -} // jin - -#endif // LIBJIN_MODULES_RENDER \ No newline at end of file diff --git a/libjin/Graphics/TTF.h b/libjin/Graphics/TTF.h deleted file mode 100644 index 0f445a2..0000000 --- a/libjin/Graphics/TTF.h +++ /dev/null @@ -1,119 +0,0 @@ -#ifndef __LIBJINTTF_H -#define __LIBJIN_TTF_H -#include "../jin_configuration.h" -#if LIBJIN_MODULES_RENDER - -#include -#include -#include "drawable.h" -#include "../math/quad.h" -#include "Page.h" -#include "Font.h" -#include "Color.h" -#include "../3rdparty/stb/stb_truetype.h" - -namespace jin -{ -namespace graphics -{ - - class TTFData - { - public: - static TTFData* createTTFData(const unsigned char* data, unsigned int size); - - ~TTFData(); - - void pushTTFsize(unsigned int ttfsize); - void popTTFsize(); - - Channel* getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const; - Color* getCodepointBitmap(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const; - void getVMetrics(int* baseline, int* descent); - void getHMetrics(unsigned int codepoint, int* advanceWidth, int* leftSideBearing); - - private: - static const unsigned int FONT_SIZE = 12; - - TTFData(const unsigned char* data, unsigned int size); - - stbtt_fontinfo info; - struct - { - unsigned char* data; - unsigned int size; - } raw; - std::vector scales; - - }; - - class TTF : public Font - { - public: - static TTF* createTTF(TTFData* ttfData, unsigned int ttfSzie); - - Page* typeset(const Sentence& text, int lineheight, int spacing) override; - void print(const Sentence& text, int x, int y, int lineheight, int spacing = 0) override; - void print(const Page* page, int x, int y) override; - void print(Unicode::Iterator& itor, int x, int y, int lineheight, int spacing = 0) override; - Page* typeset(Unicode::Iterator& itor, int lineheight, int spacing = 0) override; -#if defined(ttf_debug) - void drawAtlas(); -#endif - ~TTF(); - - private: - struct TTFGlyph - { - GLuint atlas; - /* normalized coordinates */ - struct Bbox - { - float x, y; - float w, h; - } bbox; - /* glyph size in pixel */ - unsigned int width, height; - }; - - static const int TEXTURE_SIZE_LEVELS_COUNT = 7; - static const int TEXTURE_SIZE_LEVEL_MAX = TEXTURE_SIZE_LEVELS_COUNT - 1; - static const int TEXTURE_WIDTHS[TEXTURE_SIZE_LEVELS_COUNT]; - static const int TEXTURE_HEIGHTS[TEXTURE_SIZE_LEVELS_COUNT]; - - TTF(TTFData* ttf, Codepoint ttfSize); - - void estimateSize(); - GLuint createAtlas(); - TTFGlyph* bakeGlyph(Codepoint character); - TTFGlyph* findGlyph(Codepoint character); - - int getCharWidth(int c); - int getCharHeight(int c); - int getTextWidth(const Sentence& text, int spacing = 0); - int getTextHeight(const Sentence& text, int lineheight); - void getTextBox(const Sentence& text, int* w, int* h, int lineheight, int spacing = 0); - int getTextWidth(Unicode::Iterator& text, int spacing = 0); - int getTextHeight(Unicode::Iterator& text, int lineheight); - void getTextBox(Unicode::Iterator& text, int* w, int* h, int lineheight, int spacing = 0); - - int textureWidth; - int textureHeight; - std::vector atlases; - /* map unicode codepoint to glyph */ - std::map glyphs; - TTFData* ttf; - const unsigned int ttfsize; - int baseline; - int descent; - - /* cursor helped render to texture */ - math::Vector2 cursor; - - }; - -} // graphics -} // jin - -#endif // LIBJIN_MODULES_RENDER -#endif // __LIBJIN_FONT_H \ No newline at end of file diff --git a/libjin/Graphics/TexFont.cpp b/libjin/Graphics/TexFont.cpp deleted file mode 100644 index a000b1c..0000000 --- a/libjin/Graphics/TexFont.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "TexFont.h" - -namespace jin -{ -namespace graphics -{ - /* - * +--------------------+ - * | top | - * | l ************* r | - * | e ************* i | - * | f ****glyphs*** g | - * | t ************* h | - * | ************* t | - * | bottom | - * +--------------------+ - */ - - /* create texture font from tilemap */ - TexFont * TexFont::createTexFont(const Bitmap* bitmap, const Sentence& codepoints, int cellw, int cellh, const math::Vector4& spacing) - { - TexFont* tf = new TexFont(bitmap, codepoints, cellw, cellh, spacing); - return tf; - } - - /* create texture font from seperated glyphs */ - TexFont* TexFont::createTexFont(const Bitmap* bitmap, const Sentence& codepoints, Color mask, int cellh, const math::Vector4& spacing) - { - TexFont* tf = new TexFont(bitmap, codepoints, mask, cellh, spacing); - return tf; - } - - TexFont::~TexFont() - { - } - - Page* TexFont::typeset(const Sentence& text, int lineheight, int spacing = 0) - { - - } - - void TexFont::print(const Page* page, int x, int y) - { - - } - - void TexFont::print(const Sentence& text, int x, int y, int linehgiht, int spacing) - { - - } - - TexFont::TexFont(const Bitmap* bitmap, const Sentence& codepoints, int cellw, int cellh, const math::Vector4& spacing) - : Drawable(bitmap) - { - int l, r, t, b; - - } - - TexFont::TexFont(const Bitmap* bitmap, const Sentence& codepoints, Color mask, int cellh, const math::Vector4& spacing) - : Drawable(bitmap) - { - - } - -} -} \ No newline at end of file diff --git a/libjin/Graphics/TexFont.h b/libjin/Graphics/TexFont.h deleted file mode 100644 index 8cc79f5..0000000 --- a/libjin/Graphics/TexFont.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef __LIBJIN_TEXTURE_FONT_H -#define __LIBJIN_TEXTURE_FONT_H - -#include -#include - -#include "Drawable.h" -#include "Page.h" -#include "Bitmap.h" -#include "Font.h" -#include "../Math/Vector4.hpp" - -namespace jin -{ -namespace graphics -{ - - class TexFont : public Font - , public Drawable - { - public: - /* create texture font from tilemap */ - static TexFont* createTexFont(const Bitmap* bitmap, const unicode::Sentence& codepoints, int cellw, int cellh, const math::Vector4& spacing = math::Vector4()); - /* create texture font from seperated glyphs */ - static TexFont* createTexFont(const Bitmap* bitmap, const unicode::Sentence& codepoints, Color mask, int cellh, const math::Vector4& spacing = math::Vector4()); - - ~TexFont(); - - Page* typeset(const unicode::Sentence& text, int lineheight, int spacing = 0) override ; - void print(const Page* page, int x, int y) override; - void print(const unicode::Sentence& text, int x, int y, int linehgiht, int spacing = 0) override; - void print(unicode::Iterator& itor, int x, int y, int lineheight, int spacing = 0) override; - Page* typeset(unicode::Iterator& itor, int lineheight, int spacing = 0) override; - - private: - struct TexGlyph { unsigned short x, y, w, h;}; - - TexFont(const Bitmap* bitmap, const unicode::Sentence& codepoints, int cellw, int cellh, const math::Vector4& spacing); - TexFont(const Bitmap* bitmap, const unicode::Sentence& codepoints, Color mask, int cellh, const math::Vector4& spacing); - - std::map glyphs; - - }; - -} -} - -#endif \ No newline at end of file diff --git a/libjin/Graphics/Unicode.cpp b/libjin/Graphics/Unicode.cpp deleted file mode 100644 index 0514636..0000000 --- a/libjin/Graphics/Unicode.cpp +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -#include "Unicode.h" - -namespace jin -{ -namespace graphics -{ -namespace unicode -{ - - /* utf8 byte string to unicode codepoint */ - static const char *utf8toCodepoint(const char *p, unsigned *res) { - - } - - ///////////////////////////////////////////////////////////////////////////// - // decoders - ///////////////////////////////////////////////////////////////////////////// - - const void* Utf8::decode(const void* data, unsigned int length, Codepoint* res) - { - const char* p = (char*)data; - unsigned x, mask, shift; - switch (*p & 0xf0) { - case 0xf0: mask = 0x07; shift = 18; break; - case 0xe0: mask = 0x0f; shift = 12; break; - case 0xc0: - case 0xd0: mask = 0x1f; shift = 6; break; - default: - *res = *p; - return p + 1; - } - x = (*p & mask) << shift; - do { - if (*(++p) == '\0') { - *res = x; - return p; - } - shift -= 6; - x |= (*p & 0x3f) << shift; - } while (shift); - *res = x; - return p + 1; - } - - const void* Utf16::decode(const void* data, unsigned int length, Codepoint* res) - { - - } - - ///////////////////////////////////////////////////////////////////////////// - // iterator - ///////////////////////////////////////////////////////////////////////////// - - Iterator::Iterator(const Iterator& itor) - : _data(itor._data) - , _length(itor._length) - , _p(itor._data) - { - } - - Iterator::Iterator(Encode encode, const void* const data, unsigned int length) - : _data(data) - { - switch (encode) - { - case Encode::UTF8: _decoder = new Utf8(); break; - } - } - - Iterator::~Iterator() - { - delete _decoder; - } - - Codepoint Iterator::get() - { - - } - - Codepoint Iterator::operator*() - { - - } - - Iterator Iterator::begin() - { - - } - - Iterator Iterator::end() - { - - } - - void Iterator::operator = (const Iterator& itor) - { - - } - - Iterator& Iterator::operator ++() - { - - return *this; - } - - Iterator Iterator::operator ++(int) - { - - } - - Iterator Iterator::operator !=(const Iterator& itor) - { - - } - - Iterator Iterator::operator ==(const Iterator& itor) - { - - } - -} -} -} \ No newline at end of file diff --git a/libjin/Graphics/Unicode.h b/libjin/Graphics/Unicode.h deleted file mode 100644 index d5ffb14..0000000 --- a/libjin/Graphics/Unicode.h +++ /dev/null @@ -1,85 +0,0 @@ -#ifndef __LIBJIN_UTF8_H -#define __LIBJIN_UTF8_H - -#include - -namespace jin -{ -namespace graphics -{ -namespace unicode -{ - - typedef unsigned int Codepoint; - - typedef std::vector Sentence; - - enum Encode - { - UCS16, // unicode 16bits - UCS32, // unicode 32bits - UTF8, // utf-8 - UTF16, // utf-16 - ASCII, // ASCII - }; - - class Decoder - { - public: - virtual const void* decode(const void* data, unsigned int length, Codepoint* c) = 0; - virtual const void* next(const void* data, unsigned int length) = 0; - }; - - class Utf8 : public Decoder - { - public: - const void* decode(const void* data, unsigned int length, Codepoint* c) override; - const void* next(const void* data, unsigned int lengthm) override; - }; - - class Utf16 : public Decoder - { - public: - const void* decode(const void* data, unsigned int length, Codepoint* c) override; - const void* next(const void* data, unsigned int lengthm) override; - }; - - class Ucs16 : public Decoder - { - public: - const void* decode(const void* data, unsigned int length, Codepoint* c) override; - const void* next(const void* data, unsigned int lengthm) override; - }; - - class Iterator - { - public: - Iterator(const Iterator& itor); - Iterator(Encode encode, const void* data, unsigned int length); - ~Iterator(); - - Codepoint get(); - Iterator begin(); - Iterator end(); - Codepoint operator *(); - /* prefix ++ */ - Iterator& operator ++(); - /* postfix ++ */ - Iterator operator ++(int); - Iterator operator !=(const Iterator& itor); - Iterator operator ==(const Iterator& itor); - void operator = (const Iterator& itor); - - private: - const Decoder* _decoder; // decoder - const void* _p; // pointer - const void* const _data; // weak pointer - unsigned int _length; // data length - - }; - -} // unicode -} // graphics -} // jin - -#endif \ No newline at end of file diff --git a/test/05Font/main.cpp b/test/05Font/main.cpp index 44aefac..6eebe04 100644 --- a/test/05Font/main.cpp +++ b/test/05Font/main.cpp @@ -66,8 +66,10 @@ Color frag(Color col, Texture tex, Vertex v) fs->read("font.ttf", &buffer); data = TTFData::createTTFData((const unsigned char*)buffer.data, buffer.size); font = TTF::createTTF(data, 15); - page = font->typeset(Unicode::Iterator(Unicode::Encode::UTF8, u8R"(测试)"), 15, 0); - delete data; + const char* str = u8R"(源氏物语げんじものがたり)"; + int l = strlen(str); + page = font->typeset(unicode::Text(unicode::Encode::UTF8, str, strlen(str)), 15, 0); + delete data; //canvas = Canvas::createCanvas(100, 100); //page = font->typeset("こんにちは世界!", 120, 20); buffer.clear(); -- cgit v1.1-26-g67d0