diff options
author | chai <chaifix@163.com> | 2018-10-14 09:47:53 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-14 09:47:53 +0800 |
commit | d94f4a50e1e967c5d225720002c439107124bc13 (patch) | |
tree | ae902c9a62c84c28826966abab1aaf07a59e3d34 | |
parent | 05e500a0d6df6fdc30e00ca3bd487380d64f6ada (diff) |
*修改文字渲染
30 files changed, 836 insertions, 469 deletions
diff --git a/build/.vs/libjin/v14/.suo b/build/.vs/libjin/v14/.suo Binary files differindex fd9eecb..9c02e56 100644 --- a/build/.vs/libjin/v14/.suo +++ b/build/.vs/libjin/v14/.suo diff --git a/build/Debug/05Font.exe b/build/Debug/05Font.exe Binary files differindex 7b9484a..c04ef8c 100644 --- a/build/Debug/05Font.exe +++ b/build/Debug/05Font.exe 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 @@ <ClCompile Include="..\..\libjin\Graphics\Canvas.cpp" /> <ClCompile Include="..\..\libjin\Graphics\Color.cpp" /> <ClCompile Include="..\..\libjin\Graphics\Drawable.cpp" /> - <ClCompile Include="..\..\libjin\Graphics\TexFont.cpp" /> - <ClCompile Include="..\..\libjin\Graphics\TTF.cpp" /> + <ClCompile Include="..\..\libjin\Graphics\Font\Decoder.cpp" /> + <ClCompile Include="..\..\libjin\Graphics\Font\TexFont.cpp" /> + <ClCompile Include="..\..\libjin\Graphics\Font\Text.cpp" /> + <ClCompile Include="..\..\libjin\Graphics\Font\TTF.cpp" /> + <ClCompile Include="..\..\libjin\Graphics\Image.cpp" /> + <ClCompile Include="..\..\libjin\Graphics\Particles\Particle.cpp" /> + <ClCompile Include="..\..\libjin\Graphics\Sprite.cpp" /> <ClCompile Include="..\..\libjin\Graphics\Mesh.cpp" /> <ClCompile Include="..\..\libjin\Graphics\OpenGL.cpp" /> <ClCompile Include="..\..\libjin\Graphics\Shader.cpp" /> <ClCompile Include="..\..\libjin\Graphics\Shapes.cpp" /> <ClCompile Include="..\..\libjin\Graphics\Texture.cpp" /> - <ClCompile Include="..\..\libjin\Graphics\Unicode.cpp" /> <ClCompile Include="..\..\libjin\Graphics\Window.cpp" /> <ClCompile Include="..\..\libjin\Input\Event.cpp" /> <ClCompile Include="..\..\libjin\Input\Joypad.cpp" /> @@ -87,12 +91,18 @@ <ClInclude Include="..\..\libjin\Graphics\Canvas.h" /> <ClInclude Include="..\..\libjin\Graphics\Color.h" /> <ClInclude Include="..\..\libjin\Graphics\Drawable.h" /> - <ClInclude Include="..\..\libjin\Graphics\TTF.h" /> - <ClInclude Include="..\..\libjin\Graphics\Font.h" /> + <ClInclude Include="..\..\libjin\Graphics\Font\Decoder.h" /> + <ClInclude Include="..\..\libjin\Graphics\Font\Font.h" /> + <ClInclude Include="..\..\libjin\Graphics\Font\Page.h" /> + <ClInclude Include="..\..\libjin\Graphics\Font\TexFont.h" /> + <ClInclude Include="..\..\libjin\Graphics\Font\Text.h" /> + <ClInclude Include="..\..\libjin\Graphics\Font\TTF.h" /> + <ClInclude Include="..\..\libjin\Graphics\Image.h" /> + <ClInclude Include="..\..\libjin\Graphics\Particles\Particle.h" /> + <ClInclude Include="..\..\libjin\Graphics\Sprite.h" /> <ClInclude Include="..\..\libjin\Graphics\Graphics.h" /> <ClInclude Include="..\..\libjin\Graphics\Mesh.h" /> <ClInclude Include="..\..\libjin\Graphics\OpenGL.h" /> - <ClInclude Include="..\..\libjin\Graphics\Page.h" /> <ClInclude Include="..\..\libjin\Graphics\Shader.h" /> <ClInclude Include="..\..\libjin\Graphics\Shaders\base.shader.h" /> <ClInclude Include="..\..\libjin\Graphics\Shaders\default.shader.h" /> @@ -100,8 +110,6 @@ <ClInclude Include="..\..\libjin\Graphics\Shaders\texture.shader.h" /> <ClInclude Include="..\..\libjin\Graphics\Shapes.h" /> <ClInclude Include="..\..\libjin\Graphics\Texture.h" /> - <ClInclude Include="..\..\libjin\Graphics\TexFont.h" /> - <ClInclude Include="..\..\libjin\Graphics\Unicode.h" /> <ClInclude Include="..\..\libjin\Graphics\Window.h" /> <ClInclude Include="..\..\libjin\Input\Event.h" /> <ClInclude Include="..\..\libjin\Input\Input.h" /> 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 @@ <Filter Include="Graphics\Shaders"> <UniqueIdentifier>{35881367-9a40-4391-9cc2-26d2ee869b8a}</UniqueIdentifier> </Filter> + <Filter Include="Graphics\Font"> + <UniqueIdentifier>{dfda69e2-693e-4ab1-945f-478c251cc67a}</UniqueIdentifier> + </Filter> + <Filter Include="Graphics\Particles"> + <UniqueIdentifier>{4c70f035-3e66-4de8-8d0f-95919d42863c}</UniqueIdentifier> + </Filter> </ItemGroup> <ItemGroup> <ClCompile Include="..\..\libjin\3rdparty\GLee\GLee.c"> @@ -165,14 +171,26 @@ <ClCompile Include="..\..\libjin\Graphics\OpenGL.cpp"> <Filter>Graphics</Filter> </ClCompile> - <ClCompile Include="..\..\libjin\Graphics\TTF.cpp"> + <ClCompile Include="..\..\libjin\Graphics\Image.cpp"> <Filter>Graphics</Filter> </ClCompile> - <ClCompile Include="..\..\libjin\Graphics\TexFont.cpp"> + <ClCompile Include="..\..\libjin\Graphics\Sprite.cpp"> <Filter>Graphics</Filter> </ClCompile> - <ClCompile Include="..\..\libjin\Graphics\Unicode.cpp"> - <Filter>Graphics</Filter> + <ClCompile Include="..\..\libjin\Graphics\Font\TexFont.cpp"> + <Filter>Graphics\Font</Filter> + </ClCompile> + <ClCompile Include="..\..\libjin\Graphics\Font\TTF.cpp"> + <Filter>Graphics\Font</Filter> + </ClCompile> + <ClCompile Include="..\..\libjin\Graphics\Particles\Particle.cpp"> + <Filter>Graphics\Particles</Filter> + </ClCompile> + <ClCompile Include="..\..\libjin\Graphics\Font\Decoder.cpp"> + <Filter>Graphics\Font</Filter> + </ClCompile> + <ClCompile Include="..\..\libjin\Graphics\Font\Text.cpp"> + <Filter>Graphics\Font</Filter> </ClCompile> </ItemGroup> <ItemGroup> @@ -358,20 +376,32 @@ <ClInclude Include="..\..\libjin\Graphics\Shaders\texture.shader.h"> <Filter>Graphics\Shaders</Filter> </ClInclude> - <ClInclude Include="..\..\libjin\Graphics\Page.h"> + <ClInclude Include="..\..\libjin\Graphics\Image.h"> <Filter>Graphics</Filter> </ClInclude> - <ClInclude Include="..\..\libjin\Graphics\TTF.h"> + <ClInclude Include="..\..\libjin\Graphics\Sprite.h"> <Filter>Graphics</Filter> </ClInclude> - <ClInclude Include="..\..\libjin\Graphics\Font.h"> - <Filter>Graphics</Filter> + <ClInclude Include="..\..\libjin\Graphics\Font\Font.h"> + <Filter>Graphics\Font</Filter> </ClInclude> - <ClInclude Include="..\..\libjin\Graphics\TexFont.h"> - <Filter>Graphics</Filter> + <ClInclude Include="..\..\libjin\Graphics\Font\Page.h"> + <Filter>Graphics\Font</Filter> </ClInclude> - <ClInclude Include="..\..\libjin\Graphics\Unicode.h"> - <Filter>Graphics</Filter> + <ClInclude Include="..\..\libjin\Graphics\Font\TexFont.h"> + <Filter>Graphics\Font</Filter> + </ClInclude> + <ClInclude Include="..\..\libjin\Graphics\Font\TTF.h"> + <Filter>Graphics\Font</Filter> + </ClInclude> + <ClInclude Include="..\..\libjin\Graphics\Particles\Particle.h"> + <Filter>Graphics\Particles</Filter> + </ClInclude> + <ClInclude Include="..\..\libjin\Graphics\Font\Decoder.h"> + <Filter>Graphics\Font</Filter> + </ClInclude> + <ClInclude Include="..\..\libjin\Graphics\Font\Text.h"> + <Filter>Graphics\Font</Filter> </ClInclude> </ItemGroup> <ItemGroup> 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 <vector> -#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 <stdlib.h> +#include <string.h> +#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 <vector> + +namespace jin +{ +namespace graphics +{ +namespace unicode +{ + + typedef unsigned int Codepoint; + + typedef std::vector<Codepoint> 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 <vector> +#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/Page.h b/libjin/Graphics/Font/Page.h index b878a74..6e7cbdf 100644 --- a/libjin/Graphics/Page.h +++ b/libjin/Graphics/Font/Page.h @@ -1,6 +1,6 @@ #ifndef __LIBJIN_PAGE_H #define __LIBJIN_PAGE_H -#include "../math/Vector2.hpp" +#include "../../math/Vector2.hpp" #include "Font.h" namespace jin diff --git a/libjin/Graphics/TTF.cpp b/libjin/Graphics/Font/TTF.cpp index 296e660..dac6733 100644 --- a/libjin/Graphics/TTF.cpp +++ b/libjin/Graphics/Font/TTF.cpp @@ -1,19 +1,25 @@ -#include "../jin_configuration.h" +#include "../../jin_configuration.h" #if LIBJIN_MODULES_RENDER -#include "OpenGL.h" -#include "ttf.h" #include <stdio.h> -#include "color.h" -#include "Shader.h" + +#include "../../Common/Array.hpp" +#include "../OpenGL.h" +#include "../Color.h" +#include "../Shader.h" +#include "TTF.h" #include "Page.h" -#include "../Common/Array.hpp" + +#define STB_TRUETYPE_IMPLEMENTATION +#include "../../3rdparty/stb/stb_truetype.h" namespace jin { namespace graphics { + using namespace unicode; + /////////////////////////////////////////////////////////////////////////////////////////////// // TTFData /////////////////////////////////////////////////////////////////////////////////////////////// @@ -59,7 +65,7 @@ namespace graphics * | | bitmap | | * +--|--------|--+ baseline * | +--------+ | - * +--------------+ decent + * +--|-----------+ decent * | | * leftSideBearing | * advanceWidth @@ -124,7 +130,7 @@ namespace graphics // TTF /////////////////////////////////////////////////////////////////////////////////////////////// - #include "Shaders/font.shader.h" + #include "../Shaders/font.shader.h" using namespace std; using namespace jin::math; @@ -205,11 +211,6 @@ namespace graphics TTF::~TTF() { - map<unsigned int, TTFGlyph*>::iterator it = glyphs.begin(); - for (; it != glyphs.end(); ++it) - { - delete it->second; - } } GLuint TTF::createAtlas() @@ -241,12 +242,47 @@ namespace graphics 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); + 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; + } - Page* TTF::typeset(Unicode::Iterator& itor, int lineheight, int spacing) +#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; @@ -257,41 +293,23 @@ glyphvertices.push_back(vertex); GlyphVertex vertex; Vector2<int> p(0, 0); int i = 0; - Codepoint c; - for (; *itor != NULL; ++itor) + for (; itor != itor.end(); ++itor) { - c = *itor; + Codepoint c = *itor; if (c == 0x0D) continue; - /* new line */ if (c == 0x0A) { + /* new line */ 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); - + glyphlize(c); p.x += glyph->width + spacing; - i += 4; } - itor = itor.begin(); + itor.toBegin(); getTextBox(itor, &page->size.w, &page->size.h, lineheight, spacing); return page; } @@ -311,38 +329,54 @@ glyphvertices.push_back(vertex); { if (c == 0x0D) continue; - /* new line */ if (c == 0x0A) { + /* new line */ 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); - + 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<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist; + vector<GlyphVertex>& glyphvertices = page->glyphvertices; + int texture = -1; + TTFGlyph* glyph = nullptr; + GlyphVertex vertex; + Vector2<int> 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(); @@ -447,24 +481,81 @@ glyphvertices.push_back(vertex); ttf->popTTFsize(); } - int TTF::getTextWidth(Unicode::Iterator& text, int spacing) + 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& text, int lineheight) + 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& text, int* w, int* h, int lineheight, int spacing) + 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) + 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(); @@ -489,31 +580,26 @@ glyphvertices.push_back(vertex); 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<unsigned int, TTFGlyph*>(character, glyph)); - + 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<unsigned int, TTFGlyph>(character, glyph)); cursor.x += adw; - return glyph; + return glyphs[character]; } - TTF::TTFGlyph* TTF::findGlyph(unsigned int character) + TTF::TTFGlyph& TTF::findGlyph(unsigned int character) { - map<unsigned int, TTFGlyph*>::iterator it = glyphs.find(character); + map<unsigned int, TTFGlyph>::iterator it = glyphs.find(character); if (it != glyphs.end()) - { return it->second; - } else - { - TTFGlyph* glyph = bakeGlyph(character); - return glyph; - } + return bakeGlyph(character); } } // graphics diff --git a/libjin/Graphics/TTF.h b/libjin/Graphics/Font/TTF.h index 0f445a2..83fe25b 100644 --- a/libjin/Graphics/TTF.h +++ b/libjin/Graphics/Font/TTF.h @@ -1,22 +1,33 @@ #ifndef __LIBJINTTF_H #define __LIBJIN_TTF_H -#include "../jin_configuration.h" +#include "../../jin_configuration.h" #if LIBJIN_MODULES_RENDER #include <vector> #include <map> -#include "drawable.h" -#include "../math/quad.h" + +#include "../../3rdparty/stb/stb_truetype.h" +#include "../../math/quad.h" +#include "../Color.h" +#include "../drawable.h" + #include "Page.h" #include "Font.h" -#include "Color.h" -#include "../3rdparty/stb/stb_truetype.h" +#include "Text.h" namespace jin { namespace graphics { + /** + * TTFData + * |- TTF + * |- TTF + * . + * . + * . + */ class TTFData { public: @@ -29,6 +40,7 @@ namespace graphics 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); @@ -52,14 +64,15 @@ namespace graphics 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; + 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; - Page* typeset(Unicode::Iterator& itor, int lineheight, int spacing = 0) override; -#if defined(ttf_debug) - void drawAtlas(); -#endif + 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: @@ -81,27 +94,28 @@ namespace graphics static const int TEXTURE_WIDTHS[TEXTURE_SIZE_LEVELS_COUNT]; static const int TEXTURE_HEIGHTS[TEXTURE_SIZE_LEVELS_COUNT]; - TTF(TTFData* ttf, Codepoint ttfSize); + TTF(TTFData* ttf, unicode::Codepoint ttfSize); void estimateSize(); GLuint createAtlas(); - TTFGlyph* bakeGlyph(Codepoint character); - TTFGlyph* findGlyph(Codepoint character); + TTFGlyph& bakeGlyph(unicode::Codepoint character); + TTFGlyph& findGlyph(unicode::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 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<GLuint> atlases; - /* map unicode codepoint to glyph */ - std::map<Codepoint, TTFGlyph*> glyphs; + std::map<unicode::Codepoint, TTFGlyph> glyphs; TTFData* ttf; const unsigned int ttfsize; int baseline; diff --git a/libjin/Graphics/TexFont.cpp b/libjin/Graphics/Font/TexFont.cpp index a000b1c..9168f97 100644 --- a/libjin/Graphics/TexFont.cpp +++ b/libjin/Graphics/Font/TexFont.cpp @@ -4,6 +4,9 @@ namespace jin { namespace graphics { + + using namespace unicode; + /* * +--------------------+ * | top | @@ -17,16 +20,16 @@ namespace graphics */ /* create texture font from tilemap */ - TexFont * TexFont::createTexFont(const Bitmap* bitmap, const Sentence& codepoints, int cellw, int cellh, const math::Vector4<int>& spacing) + TexFont * TexFont::createTexFont(const Bitmap* bitmap, const Sentence& codepoints, int cellw, int cellh) { - TexFont* tf = new TexFont(bitmap, codepoints, cellw, cellh, spacing); + 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, const math::Vector4<int>& spacing) + TexFont* TexFont::createTexFont(const Bitmap* bitmap, const Sentence& codepoints, Color mask, int cellh) { - TexFont* tf = new TexFont(bitmap, codepoints, mask, cellh, spacing); + TexFont* tf = new TexFont(bitmap, codepoints, mask, cellh); return tf; } @@ -34,11 +37,21 @@ namespace graphics { } - Page* TexFont::typeset(const Sentence& text, int lineheight, int spacing = 0) + 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) { @@ -49,14 +62,24 @@ namespace graphics } - TexFont::TexFont(const Bitmap* bitmap, const Sentence& codepoints, int cellw, int cellh, const math::Vector4<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, const math::Vector4<int>& spacing) + TexFont::TexFont(const Bitmap* bitmap, const Sentence& codepoints, Color mask, int cellh) : Drawable(bitmap) { 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 <map> +#include <vector> + +#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<unicode::Codepoint, TexGlyph> 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/Particles/Particle.cpp b/libjin/Graphics/Particles/Particle.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/libjin/Graphics/Particles/Particle.cpp diff --git a/libjin/Graphics/Particles/Particle.h b/libjin/Graphics/Particles/Particle.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/libjin/Graphics/Particles/Particle.h 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 --- /dev/null +++ b/libjin/Graphics/Sprite.cpp 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/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 <map> -#include <vector> - -#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<int>& spacing = math::Vector4<int>()); - /* create texture font from seperated glyphs */ - static TexFont* createTexFont(const Bitmap* bitmap, const unicode::Sentence& codepoints, Color mask, int cellh, const math::Vector4<int>& spacing = math::Vector4<int>()); - - ~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<int>& spacing); - TexFont(const Bitmap* bitmap, const unicode::Sentence& codepoints, Color mask, int cellh, const math::Vector4<int>& spacing); - - std::map<unicode::Codepoint, TexGlyph> 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 <stdlib.h> -#include <string.h> -#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 <vector> - -namespace jin -{ -namespace graphics -{ -namespace unicode -{ - - typedef unsigned int Codepoint; - - typedef std::vector<Codepoint> 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(); |