aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-10-11 08:34:41 +0800
committerchai <chaifix@163.com>2018-10-11 08:34:41 +0800
commit08db40f5e15cdeca16c4980928a35e2c0f27ed85 (patch)
treea7f0133a4b5920e81e2991b58c24ab566c32a0f8
parentd6263f17643654ccee4e304a048781be38a9e08d (diff)
*update
-rw-r--r--build/libjin/libjin.vcxproj4
-rw-r--r--build/libjin/libjin.vcxproj.filters12
-rw-r--r--libjin/Graphics/Font.h3
-rw-r--r--libjin/Graphics/Graphics.h2
-rw-r--r--libjin/Graphics/TTF.cpp75
-rw-r--r--libjin/Graphics/TTF.h15
-rw-r--r--libjin/Graphics/TexFont.h4
-rw-r--r--libjin/Graphics/Unicode.cpp (renamed from libjin/Graphics/Utf8.cpp)35
-rw-r--r--libjin/Graphics/Unicode.h48
-rw-r--r--libjin/Graphics/Utf8.h44
-rw-r--r--test/05Font/main.cpp26
11 files changed, 152 insertions, 116 deletions
diff --git a/build/libjin/libjin.vcxproj b/build/libjin/libjin.vcxproj
index f186a79..f787ea4 100644
--- a/build/libjin/libjin.vcxproj
+++ b/build/libjin/libjin.vcxproj
@@ -43,7 +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\Unicode.cpp" />
<ClCompile Include="..\..\libjin\Graphics\Window.cpp" />
<ClCompile Include="..\..\libjin\Input\Event.cpp" />
<ClCompile Include="..\..\libjin\Input\Joypad.cpp" />
@@ -101,7 +101,7 @@
<ClInclude Include="..\..\libjin\Graphics\Shapes.h" />
<ClInclude Include="..\..\libjin\Graphics\Texture.h" />
<ClInclude Include="..\..\libjin\Graphics\TexFont.h" />
- <ClInclude Include="..\..\libjin\Graphics\Utf8.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 cd91c37..02b4364 100644
--- a/build/libjin/libjin.vcxproj.filters
+++ b/build/libjin/libjin.vcxproj.filters
@@ -165,15 +165,15 @@
<ClCompile Include="..\..\libjin\Graphics\OpenGL.cpp">
<Filter>Graphics</Filter>
</ClCompile>
- <ClCompile Include="..\..\libjin\Graphics\Utf8.cpp">
- <Filter>Graphics</Filter>
- </ClCompile>
<ClCompile Include="..\..\libjin\Graphics\TTF.cpp">
<Filter>Graphics</Filter>
</ClCompile>
<ClCompile Include="..\..\libjin\Graphics\TexFont.cpp">
<Filter>Graphics</Filter>
</ClCompile>
+ <ClCompile Include="..\..\libjin\Graphics\Unicode.cpp">
+ <Filter>Graphics</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\libjin\3rdparty\GLee\GLee.h">
@@ -355,9 +355,6 @@
<ClInclude Include="..\..\libjin\Graphics\Shaders\default.shader.h">
<Filter>Graphics\Shaders</Filter>
</ClInclude>
- <ClInclude Include="..\..\libjin\Graphics\Utf8.h">
- <Filter>Graphics</Filter>
- </ClInclude>
<ClInclude Include="..\..\libjin\Graphics\Shaders\texture.shader.h">
<Filter>Graphics\Shaders</Filter>
</ClInclude>
@@ -373,6 +370,9 @@
<ClInclude Include="..\..\libjin\Graphics\TexFont.h">
<Filter>Graphics</Filter>
</ClInclude>
+ <ClInclude Include="..\..\libjin\Graphics\Unicode.h">
+ <Filter>Graphics</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\libjin\README.md" />
diff --git a/libjin/Graphics/Font.h b/libjin/Graphics/Font.h
index 6b8cc73..924dbc1 100644
--- a/libjin/Graphics/Font.h
+++ b/libjin/Graphics/Font.h
@@ -1,6 +1,7 @@
#ifndef __LIBJIN_FONT_H
#define __LIBJIN_FONT_H
#include <vector>
+#include "Unicode.h"
namespace jin
{
@@ -22,6 +23,8 @@ namespace graphics
virtual void print(const Page* page, int x, int y) = 0;
virtual void print(const Sentence& text, int x, int y, int lineheight, int spacing = 0) = 0;
virtual Page* typeset(const 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;
};
diff --git a/libjin/Graphics/Graphics.h b/libjin/Graphics/Graphics.h
index 7c11122..ed96315 100644
--- a/libjin/Graphics/Graphics.h
+++ b/libjin/Graphics/Graphics.h
@@ -5,7 +5,7 @@
#include "canvas.h"
#include "color.h"
-#include "Font.h"
+#include "TTF.h"
#include "Shapes.h"
#include "texture.h"
#include "Shader.h"
diff --git a/libjin/Graphics/TTF.cpp b/libjin/Graphics/TTF.cpp
index 6833610..296e660 100644
--- a/libjin/Graphics/TTF.cpp
+++ b/libjin/Graphics/TTF.cpp
@@ -241,6 +241,61 @@ 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);
+
+ Page* TTF::typeset(Unicode::Iterator& itor, 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;
+ 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();
@@ -250,10 +305,6 @@ namespace graphics
int texture = -1;
TTFGlyph* glyph = nullptr;
GlyphVertex vertex;
-#define glyphvertices_push(_x, _y, _u, _v) \
-vertex.x = _x; vertex.y = _y;\
-vertex.u = _u; vertex.v = _v;\
-glyphvertices.push_back(vertex);
Vector2<int> p(0, 0);
int i = 0;
for (Codepoint c : text)
@@ -288,7 +339,6 @@ glyphvertices.push_back(vertex);
i += 4;
}
-#undef glyphvertices_push
getTextBox(text, &page->size.w, &page->size.h, lineheight, spacing);
return page;
}
@@ -397,6 +447,21 @@ glyphvertices.push_back(vertex);
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));
diff --git a/libjin/Graphics/TTF.h b/libjin/Graphics/TTF.h
index aa1ed31..0f445a2 100644
--- a/libjin/Graphics/TTF.h
+++ b/libjin/Graphics/TTF.h
@@ -1,4 +1,4 @@
-#ifndef __LIBJIN_TTF_H
+#ifndef __LIBJINTTF_H
#define __LIBJIN_TTF_H
#include "../jin_configuration.h"
#if LIBJIN_MODULES_RENDER
@@ -54,7 +54,9 @@ namespace graphics
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(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
@@ -88,9 +90,12 @@ namespace graphics
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(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;
diff --git a/libjin/Graphics/TexFont.h b/libjin/Graphics/TexFont.h
index d830fc2..6639636 100644
--- a/libjin/Graphics/TexFont.h
+++ b/libjin/Graphics/TexFont.h
@@ -28,7 +28,9 @@ namespace graphics
Page* typeset(const Sentence& text, int lineheight, int spacing = 0) override ;
void print(const Page* page, int x, int y) override;
- void print(const Sentence& text, int x, int y, int linehgiht, int spacing = 0) override;
+ void print(const 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;};
diff --git a/libjin/Graphics/Utf8.cpp b/libjin/Graphics/Unicode.cpp
index 1a79d43..08fdcd0 100644
--- a/libjin/Graphics/Utf8.cpp
+++ b/libjin/Graphics/Unicode.cpp
@@ -1,6 +1,6 @@
#include <stdlib.h>
#include <string.h>
-#include "Utf8.h"
+#include "Unicode.h"
namespace jin
{
@@ -32,37 +32,10 @@ namespace graphics
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 Unicode::Iterator::operator*()
+ {
- Codepoint Utf8::Iterator::get()
- {
- Codepoint c;
- _p = utf8toCodepoint(_p, &c);
- return c;
- }
+ }
}
} \ No newline at end of file
diff --git a/libjin/Graphics/Unicode.h b/libjin/Graphics/Unicode.h
new file mode 100644
index 0000000..fcf17f8
--- /dev/null
+++ b/libjin/Graphics/Unicode.h
@@ -0,0 +1,48 @@
+#ifndef __LIBJIN_UTF8_H
+#define __LIBJIN_UTF8_H
+
+#include <vector>
+
+namespace jin
+{
+namespace graphics
+{
+namespace unicode
+{
+
+ typedef unsigned int Codepoint;
+
+ enum Encode
+ {
+ UCS16, // unicode 16bits
+ UTF8, // utf-8
+ UTF16 // utf-16
+ };
+
+ class Iterator
+ {
+ public:
+ Iterator(Encode encode, const char* data);
+ ~Iterator();
+
+ Codepoint get();
+ bool next();
+ Codepoint operator *();
+ void operator ++();
+ Iterator operator !=(const Iterator& itor);
+ Iterator operator ==(const Iterator& itor);
+ Iterator begin();
+ void operator = (const Iterator& itor);
+
+ private:
+ const char* _data; // weak pointer
+ unsigned int _len;
+ const char* _p;
+
+ };
+
+}
+} // graphics
+} // jin
+
+#endif \ No newline at end of file
diff --git a/libjin/Graphics/Utf8.h b/libjin/Graphics/Utf8.h
deleted file mode 100644
index d2d11fb..0000000
--- a/libjin/Graphics/Utf8.h
+++ /dev/null
@@ -1,44 +0,0 @@
-#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 3ce8183..44aefac 100644
--- a/test/05Font/main.cpp
+++ b/test/05Font/main.cpp
@@ -8,7 +8,7 @@ using namespace jin::audio;
using namespace jin::filesystem;
Font* font = nullptr;
Canvas* canvas;
-FontData* data = nullptr;
+TTFData* data = nullptr;
Shader* shader = nullptr;
Shader* shader2 = nullptr;
Page* page = nullptr;
@@ -64,25 +64,9 @@ Color frag(Color col, Texture tex, Vertex v)
fs->mount("../Debug");
Buffer buffer;
fs->read("font.ttf", &buffer);
- data = FontData::createFontData((const unsigned char*)buffer.data, buffer.size);
- font = Font::createFont(data, 15);
- page = font->typeset(u8R"(平安時代中期の物語。紫式部著。ただし,そのすべてが紫式部の筆に成るのでは
-ないとする説もある。 54帖。寛弘 (1004~12) 頃成立か。物語は3部に分けてみ
-ることができる。第1部は,容貌,才能などすべてにすぐれた主人公光源氏が,多
-啊哈噶科膜卡して広く迎えられている。貴族社会の苦悩を摘出したところに磁瓷得
-ることができる。第1部は,容貌,才能などすべてにすぐれた主人公光源氏が,多
-くの女性と関係をもちながら,運命に導かれて栄華をきわめる姿を描く。これに対
-して第2部は苦悩の世界であって,光源氏は最愛の紫の上を失い,栄華は内側から崩
-壊する。第3部 (宇治十帖) は光源氏没後の物語で,不義によって生れた薫大将を主
-人公として,不安に満ちた暗い世界が展開される。さまざまな恋愛と運命的な人生の
-うちに,貴族社会の苦悩を摘出したところに価値があり,現代では,世界的な文学と
-して広く迎えられている。abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
-漫画自1999年开始在日本集英社旗下的少年漫画杂志《周刊少年Jump》上连载。2002年
-,由日本动画工作室Studio Pierrot根据漫画原作所改编制作的电视动画版《火影忍者
-》开始在日本东京电视台播出。2004年,漫画进而改编成电影。2006年,漩涡鸣人入选
-美国《新闻周刊》日文版于10月18日发行的特集中选出的“全世界最受尊敬的100位日本
-人”。[2]
-)", 17, 0);
+ 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;
//canvas = Canvas::createCanvas(100, 100);
//page = font->typeset("こんにちは世界!", 120, 20);
@@ -90,7 +74,7 @@ Color frag(Color col, Texture tex, Vertex v)
fs->read("img.png", &buffer);
Bitmap* bitmap = Bitmap::createBitmap(buffer.data, buffer.size);
tex = Texture::createTexture(bitmap);
- canvas = Canvas::createCanvas(page->width, page->height);
+ canvas = Canvas::createCanvas(page->size.w, page->size.h);
Canvas::bind(canvas);
glClear(GL_COLOR_BUFFER_BIT);