diff options
-rw-r--r-- | build/libjin/libjin.vcxproj | 2 | ||||
-rw-r--r-- | build/libjin/libjin.vcxproj.filters | 6 | ||||
-rw-r--r-- | libjin/3rdparty/ogl/OpenGL.h | 13 | ||||
-rw-r--r-- | libjin/Graphics/Canvas.cpp | 4 | ||||
-rw-r--r-- | libjin/Graphics/Drawable.cpp | 1 | ||||
-rw-r--r-- | libjin/Graphics/Drawable.h | 1 | ||||
-rw-r--r-- | libjin/Graphics/Font.cpp | 120 | ||||
-rw-r--r-- | libjin/Graphics/Font.h | 6 | ||||
-rw-r--r-- | libjin/Graphics/Shaders/base.shader.h | 19 | ||||
-rw-r--r-- | libjin/Graphics/Shaders/font.shader.h | 4 | ||||
-rw-r--r-- | libjin/Graphics/Utf8.cpp | 68 | ||||
-rw-r--r-- | libjin/Graphics/Utf8.h | 44 | ||||
-rw-r--r-- | test/05Font/main.cpp | 5 |
13 files changed, 233 insertions, 60 deletions
diff --git a/build/libjin/libjin.vcxproj b/build/libjin/libjin.vcxproj index 71bf2ce..b5026d1 100644 --- a/build/libjin/libjin.vcxproj +++ b/build/libjin/libjin.vcxproj @@ -43,6 +43,7 @@ <ClCompile Include="..\..\libjin\Graphics\Shader.cpp" /> <ClCompile Include="..\..\libjin\Graphics\Shapes.cpp" /> <ClCompile Include="..\..\libjin\Graphics\Texture.cpp" /> + <ClCompile Include="..\..\libjin\Graphics\Utf8.cpp" /> <ClCompile Include="..\..\libjin\Graphics\Window.cpp" /> <ClCompile Include="..\..\libjin\Input\Event.cpp" /> <ClCompile Include="..\..\libjin\Input\Joypad.cpp" /> @@ -97,6 +98,7 @@ <ClInclude Include="..\..\libjin\Graphics\Shaders\font.shader.h" /> <ClInclude Include="..\..\libjin\Graphics\Shapes.h" /> <ClInclude Include="..\..\libjin\Graphics\Texture.h" /> + <ClInclude Include="..\..\libjin\Graphics\Utf8.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 390c495..1ef8c0e 100644 --- a/build/libjin/libjin.vcxproj.filters +++ b/build/libjin/libjin.vcxproj.filters @@ -171,6 +171,9 @@ <ClCompile Include="..\..\libjin\Graphics\OpenGL.cpp"> <Filter>Graphics</Filter> </ClCompile> + <ClCompile Include="..\..\libjin\Graphics\Utf8.cpp"> + <Filter>Graphics</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\..\libjin\3rdparty\GLee\GLee.h"> @@ -358,6 +361,9 @@ <ClInclude Include="..\..\libjin\Graphics\Shaders\default.shader.h"> <Filter>Graphics\Shaders</Filter> </ClInclude> + <ClInclude Include="..\..\libjin\Graphics\Utf8.h"> + <Filter>Graphics</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <None Include="..\..\libjin\README.md" /> diff --git a/libjin/3rdparty/ogl/OpenGL.h b/libjin/3rdparty/ogl/OpenGL.h index 586122b..7c1c599 100644 --- a/libjin/3rdparty/ogl/OpenGL.h +++ b/libjin/3rdparty/ogl/OpenGL.h @@ -92,10 +92,21 @@ namespace ogl2d glBindFramebuffer(GL_FRAMEBUFFER, fbo); } + // Ļһ + inline void ortho(int w, float radio) + { + glOrtho(0, w, w*radio, 0, -1, 1); + } + + inline void orthox(int w, int h) + { + glOrtho(0, w, h, 0, -1, 1); + } + private: struct { GLubyte r, g, b, a; } _color; // current draw color struct { GLubyte r, g, b, a; } _precolor; // previous draw color - GLuint _texture; // current binded texture + GLuint _texture; // current binded texture }; diff --git a/libjin/Graphics/Canvas.cpp b/libjin/Graphics/Canvas.cpp index d53e775..514f73a 100644 --- a/libjin/Graphics/Canvas.cpp +++ b/libjin/Graphics/Canvas.cpp @@ -102,6 +102,8 @@ namespace graphics glBindFramebuffer(GL_FRAMEBUFFER, DEFAULT_CANVAS->fbo); + /* radio of screen and canvas sound be the same */ + /* set viewport on screen */ glViewport(0, 0, ww, wh); @@ -110,7 +112,7 @@ namespace graphics glPushMatrix(); glLoadIdentity(); /* flip bottom and top */ - glOrtho(0, ww, wh, 0, -1, 1); + gl.orthox(ww, wh); /* set (model*view) matrix */ glMatrixMode(GL_MODELVIEW); diff --git a/libjin/Graphics/Drawable.cpp b/libjin/Graphics/Drawable.cpp index 412b900..a6910ab 100644 --- a/libjin/Graphics/Drawable.cpp +++ b/libjin/Graphics/Drawable.cpp @@ -55,7 +55,6 @@ namespace graphics gl.drawArrays(GL_QUADS, 0, 4); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); - /* pop the model matrix */ glPopMatrix(); diff --git a/libjin/Graphics/Drawable.h b/libjin/Graphics/Drawable.h index 4f7d760..c77068c 100644 --- a/libjin/Graphics/Drawable.h +++ b/libjin/Graphics/Drawable.h @@ -27,6 +27,7 @@ namespace graphics static const int DRAWABLE_V_SIZE = 8; GLuint texture; + GLuint vbo; /* TODO: vertex buffer object */ /* GLuint vbo; */ jin::math::Vector2<unsigned int> size; diff --git a/libjin/Graphics/Font.cpp b/libjin/Graphics/Font.cpp index 305c735..19257db 100644 --- a/libjin/Graphics/Font.cpp +++ b/libjin/Graphics/Font.cpp @@ -165,20 +165,20 @@ namespace graphics glyphinfolist[glyphinfolist.size() - 1].count += 4; Glyph::Bbox& bbox = glyph->bbox; // 1 - vertex.x = p.x; vertex.y = p.y; - vertex.u = bbox.x; vertex.v = bbox.y; + vertex.x = p.x; vertex.y = p.y; + vertex.u = bbox.x; vertex.v = bbox.y; glyphvertices.push_back(vertex); - // 2 - vertex.x = p.x; vertex.y = p.y + glyph->height; - vertex.u = bbox.x; vertex.v = bbox.y + bbox.height; + // 2 + vertex.x = p.x; vertex.y = p.y + glyph->height; + vertex.u = bbox.x; vertex.v = bbox.y + bbox.height; glyphvertices.push_back(vertex); - // 3 - vertex.x = p.x + glyph->width; vertex.y = p.y + glyph->height; - vertex.u = bbox.x + bbox.width; vertex.v = bbox.y + bbox.height; + // 3 + vertex.x = p.x + glyph->width; vertex.y = p.y + glyph->height; + vertex.u = bbox.x + bbox.width; vertex.v = bbox.y + bbox.height; glyphvertices.push_back(vertex); - // 4 - vertex.x = p.x + glyph->width; vertex.y = p.y; - vertex.u = bbox.x + bbox.width; vertex.v = bbox.y; + // 4 + vertex.x = p.x + glyph->width; vertex.y = p.y; + vertex.u = bbox.x + bbox.width; vertex.v = bbox.y; glyphvertices.push_back(vertex); p.x += glyph->width + spacing; @@ -227,55 +227,85 @@ namespace graphics gl.disableClientState(GL_TEXTURE_COORD_ARRAY); } - //float Font::getCharWidth(int c, int last) { - // int res = 0; - // int width, lsb; - // stbtt_GetCodepointHMetrics(&fontData.font, c, &width, &lsb); - // res = width; - // if (last) { - // int kerning = stbtt_GetCodepointKernAdvance(&fontData.font, last, c); - // res += kerning; - // } - // return res * fontData.scale; - //} - - //int Font::getTextWidth(const char* text) - //{ - // float res = 0; - // int last = 0; - // const char *p = text; - // while (*p) { - // unsigned c; - // p = utf8toCodepoint(p, &c); - // res += getCharWidth(c, last); - // last = c; - // } - // return ceil(res); - //} - int Font::getCharWidth(int c) { - return 0; + int adw, lsb; + font->getHMetrics(c, &adw, &lsb); + return adw; } int Font::getCharHeight(int c) { - return 0; + return descent; } - int Font::getTextWidth(const char* text) + int Font::getTextWidth(const char* t, int spacing) { - return 0; + int res = 0; + int tmp = 0; + const char *p = t; + while (*p) { + unsigned c; + p = utf8toCodepoint(p, &c); + if (*p == 0x0D) + continue; + if (*p == 0x0A) + { + tmp = 0; + continue; + } + tmp += getCharWidth(c) + spacing; + if (tmp > res) res = tmp; + } + return res; } - int Font::getTextHeight(const char* text) + int Font::getTextHeight(const char* t, int lineheight) { - return 0; + int res = 0; + bool newline = true; + while (*t) + { + unsigned c; + t = utf8toCodepoint(t, &c); + if (*t == 0x0A) + newline = true; + else if (*t == 0x0D); + else if (newline) + { + newline = false; + res += lineheight; + } + } + return res; } - void Font::getTextBox(const char* text, int* w, int* h) + void Font::getTextBox(const char* text, int* w, int* h, int lineheight, int spacing) { - return ; + *w = 0; + *h = 0; + int tmp = 0; + const char *p = text; + bool nl = true; + while (*p) { + unsigned c; + p = utf8toCodepoint(p, &c); + if (*p == 0x0D) + continue; + if (*p == 0x0A) + { + tmp = 0; + nl = true; + continue; + } + else if(nl) + { + nl = false; + *h += lineheight; + } + tmp += getCharWidth(c) + spacing; + if (tmp > *w) *w = tmp; + } } Glyph* Font::bakeGlyph(unsigned int character) diff --git a/libjin/Graphics/Font.h b/libjin/Graphics/Font.h index 34e53c8..2a1032c 100644 --- a/libjin/Graphics/Font.h +++ b/libjin/Graphics/Font.h @@ -77,9 +77,9 @@ namespace graphics int getCharWidth(int c); int getCharHeight(int c); - int getTextWidth(const char* text); - int getTextHeight(const char* text); - void getTextBox(const char* text, int* w, int* h); + int getTextWidth(const char* text, int spacing = 0); + int getTextHeight(const char* text, int lineheight); + void getTextBox(const char* text, int* w, int* h, int lineheight, int spacing = 0); int textureWidth; int textureHeight; diff --git a/libjin/Graphics/Shaders/base.shader.h b/libjin/Graphics/Shaders/base.shader.h index 080e27e..6136267 100644 --- a/libjin/Graphics/Shaders/base.shader.h +++ b/libjin/Graphics/Shaders/base.shader.h @@ -7,7 +7,16 @@ static const char* default_texture = "_tex0_"; -static const char* base_shader = R"( +static const char* base_vertex = R"( +#version 330 core +void main() +{ + +} +)"; + +static const char* base_fragment = R"( +#version 130 core #define number float #define Texture sampler2D #define Canvas sampler2D @@ -19,16 +28,16 @@ static const char* base_shader = R"( #define Vec4 vec4 #define Number number #define Image Texture - +out vec4 color; extern Texture %s; %s void main() { - gl_FragColor = effect(gl_Color, %s, gl_TexCoord[0].xy, gl_FragCoord.xy); + color = effect(gl_Color, %s, gl_TexCoord[0].xy, gl_FragCoord.xy); } )"; -static const int SHADER_FORMAT_SIZE = strlen(base_shader) + strlen(default_texture) * 2; +static const int SHADER_FORMAT_SIZE = strlen(base_fragment) + strlen(default_texture) * 2; #define formatShader(buf, program)\ - sprintf(buf, base_shader, default_texture, program, default_texture) + sprintf(buf, base_fragment, default_texture, program, default_texture) diff --git a/libjin/Graphics/Shaders/font.shader.h b/libjin/Graphics/Shaders/font.shader.h index 10284f0..c2555a6 100644 --- a/libjin/Graphics/Shaders/font.shader.h +++ b/libjin/Graphics/Shaders/font.shader.h @@ -1,9 +1,9 @@ // shader -static const char* font_shader = R"font_shader( +static const char* font_shader = R"( Color effect(Color col, Texture tex, vec2 uv, vec2 xy) { // r Color c = Texel(tex, uv); return Vec4(col.rgb, c.a); } -)font_shader";
\ No newline at end of file +)";
\ No newline at end of file diff --git a/libjin/Graphics/Utf8.cpp b/libjin/Graphics/Utf8.cpp new file mode 100644 index 0000000..1a79d43 --- /dev/null +++ b/libjin/Graphics/Utf8.cpp @@ -0,0 +1,68 @@ +#include <stdlib.h> +#include <string.h> +#include "Utf8.h" + +namespace jin +{ +namespace graphics +{ + + /* utf8 byte string to unicode codepoint */ + static const char *utf8toCodepoint(const char *p, unsigned *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; + } + + Utf8::Utf8(const char* raw, unsigned int length) + { + _length = length; + _raw = (char*)calloc(1, length); + memcpy(_raw, raw, length); + } + + Utf8::Iterator Utf8::getIterator() + { + return Iterator(*this); + } + + Utf8::~Utf8() + { + free(_raw); + _raw = nullptr; + _length = 0; + } + + Utf8::Iterator::Iterator(const Utf8& utf8) + : _utf8(utf8) + { + _p = utf8._raw; + } + + Codepoint Utf8::Iterator::get() + { + Codepoint c; + _p = utf8toCodepoint(_p, &c); + return c; + } + +} +}
\ No newline at end of file diff --git a/libjin/Graphics/Utf8.h b/libjin/Graphics/Utf8.h new file mode 100644 index 0000000..d2d11fb --- /dev/null +++ b/libjin/Graphics/Utf8.h @@ -0,0 +1,44 @@ +#ifndef __LIBJIN_UTF8_H +#define __LIBJIN_UTF8_H + +namespace jin +{ +namespace graphics +{ + + typedef unsigned int Codepoint; + + class Utf8 + { + public: + class Iterator + { + public: + /* unicode codepoint */ + Codepoint get(); + + private: + friend class Utf8; + Iterator(const Utf8&); + + const char* _p; + const Utf8& _utf8; + }; + + /* rawıսij */ + Utf8(const char* raw, unsigned int length); + Iterator getIterator(); + + private: + friend class Utf8::Iterator; + ~Utf8(); + + char* _raw; + unsigned int _length; + + }; + +} +} + +#endif
\ No newline at end of file diff --git a/test/05Font/main.cpp b/test/05Font/main.cpp index 3c4fd46..ec82b6a 100644 --- a/test/05Font/main.cpp +++ b/test/05Font/main.cpp @@ -11,6 +11,7 @@ Canvas* canvas; FontData* data = nullptr; JSLProgram* shader = nullptr; Page* page = nullptr; + float dt; void onLoad() { @@ -27,7 +28,7 @@ Color effect(Color col, Texture tex, vec2 uv, vec2 screen) Buffer buffer; fs->read("font.ttf", &buffer); data = FontData::createFontData((const unsigned char*)buffer.data, buffer.size); - font = Font::createFont(data, 12); + font = Font::createFont(data, 15); //canvas = Canvas::createCanvas(100, 100); //page = font->typeset("こんにちは世界!", 120, 20); } @@ -77,7 +78,7 @@ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 》开始在日本东京电视台播出。2004年,漫画进而改编成电影。2006年,漩涡鸣人入选 美国《新闻周刊》日文版于10月18日发行的特集中选出的“全世界最受尊敬的100位日本 人”。[2] -)", 10, 10 + 15 * 2, 14); +)", 12, 10 + 15 * 2, 18); //font->print(u8"你好世界!", 10, 10 + 15*3); //font->render(page); glColor4f(1, 1, 1, 1); |