From 08db40f5e15cdeca16c4980928a35e2c0f27ed85 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 11 Oct 2018 08:34:41 +0800 Subject: *update --- build/libjin/libjin.vcxproj | 4 +- build/libjin/libjin.vcxproj.filters | 12 +++--- libjin/Graphics/Font.h | 3 ++ libjin/Graphics/Graphics.h | 2 +- libjin/Graphics/TTF.cpp | 75 ++++++++++++++++++++++++++++++++++--- libjin/Graphics/TTF.h | 15 +++++--- libjin/Graphics/TexFont.h | 4 +- libjin/Graphics/Unicode.cpp | 41 ++++++++++++++++++++ libjin/Graphics/Unicode.h | 48 ++++++++++++++++++++++++ libjin/Graphics/Utf8.cpp | 68 --------------------------------- libjin/Graphics/Utf8.h | 44 ---------------------- test/05Font/main.cpp | 26 +++---------- 12 files changed, 189 insertions(+), 153 deletions(-) create mode 100644 libjin/Graphics/Unicode.cpp create mode 100644 libjin/Graphics/Unicode.h delete mode 100644 libjin/Graphics/Utf8.cpp delete mode 100644 libjin/Graphics/Utf8.h 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 @@ - + @@ -101,7 +101,7 @@ - + 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 @@ Graphics - - Graphics - Graphics Graphics + + Graphics + @@ -355,9 +355,6 @@ Graphics\Shaders - - Graphics - Graphics\Shaders @@ -373,6 +370,9 @@ Graphics + + Graphics + 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 +#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& 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(); @@ -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 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/Unicode.cpp b/libjin/Graphics/Unicode.cpp new file mode 100644 index 0000000..08fdcd0 --- /dev/null +++ b/libjin/Graphics/Unicode.cpp @@ -0,0 +1,41 @@ +#include +#include +#include "Unicode.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; + } + + Codepoint Unicode::Iterator::operator*() + { + + } + +} +} \ 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 + +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.cpp b/libjin/Graphics/Utf8.cpp deleted file mode 100644 index 1a79d43..0000000 --- a/libjin/Graphics/Utf8.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#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 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°üº¬Îı¾ÖÕ½á·ûµÄ³¤¶È */ - 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); -- cgit v1.1-26-g67d0