aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-10-14 10:16:46 +0800
committerchai <chaifix@163.com>2018-10-14 10:16:46 +0800
commit3d6cf33a941234aa45bd4f8c61cadac50f75289a (patch)
tree93aeec64ec4e62f5044487468d6773144ddb3267
parentd94f4a50e1e967c5d225720002c439107124bc13 (diff)
*update
-rw-r--r--build/Debug/05Font.exebin1222144 -> 1213440 bytes
-rw-r--r--libjin/Graphics/Font/Decoder.h2
-rw-r--r--libjin/Graphics/Font/Font.h6
-rw-r--r--libjin/Graphics/Font/TTF.cpp165
-rw-r--r--libjin/Graphics/Font/TTF.h18
-rw-r--r--libjin/Graphics/Font/TexFont.cpp22
-rw-r--r--libjin/Graphics/Font/TexFont.h14
-rw-r--r--libjin/Graphics/Font/Text.cpp93
-rw-r--r--libjin/Graphics/Font/Text.h66
9 files changed, 109 insertions, 277 deletions
diff --git a/build/Debug/05Font.exe b/build/Debug/05Font.exe
index c04ef8c..32d7eaa 100644
--- a/build/Debug/05Font.exe
+++ b/build/Debug/05Font.exe
Binary files differ
diff --git a/libjin/Graphics/Font/Decoder.h b/libjin/Graphics/Font/Decoder.h
index fbfa574..dcd2e56 100644
--- a/libjin/Graphics/Font/Decoder.h
+++ b/libjin/Graphics/Font/Decoder.h
@@ -12,7 +12,7 @@ namespace unicode
typedef unsigned int Codepoint;
- typedef std::vector<Codepoint> Sentence;
+ typedef std::vector<Codepoint> Content;
enum Encode
{
diff --git a/libjin/Graphics/Font/Font.h b/libjin/Graphics/Font/Font.h
index d7232d4..1e5f884 100644
--- a/libjin/Graphics/Font/Font.h
+++ b/libjin/Graphics/Font/Font.h
@@ -17,12 +17,10 @@ namespace graphics
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 Page* typeset(const unicode::Content& text, 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::Content& text, 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;
};
diff --git a/libjin/Graphics/Font/TTF.cpp b/libjin/Graphics/Font/TTF.cpp
index dac6733..ea340e4 100644
--- a/libjin/Graphics/Font/TTF.cpp
+++ b/libjin/Graphics/Font/TTF.cpp
@@ -235,28 +235,13 @@ namespace graphics
return t;
}
- void TTF::print(const Sentence& t, int x, int y, int lineheight, int spacing)
+ void TTF::print(const Content& 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;\
@@ -282,39 +267,7 @@ namespace graphics
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<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;
- 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* TTF::typeset(const Content& text, int lineheight, int spacing)
{
Page* page = new Page();
page->font = this;
@@ -346,35 +299,7 @@ namespace graphics
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;
+ return typeset(*text, lineheight, spacing);
}
void TTF::print(const Page* page, int x, int y)
@@ -396,6 +321,11 @@ namespace graphics
}
}
+ void TTF::print(const unicode::Text& text, int x, int y, int lineheight, int spacing /* = 0 */)
+ {
+ print(*text, x, y, lineheight, spacing);
+ }
+
int TTF::getCharWidth(int c)
{
int adw, lsb;
@@ -410,7 +340,7 @@ namespace graphics
return descent;
}
- int TTF::getTextWidth(const Sentence& t, int spacing)
+ int TTF::getTextWidth(const Content& t, int spacing)
{
ttf->pushTTFsize(ttfsize);
int res = 0;
@@ -432,7 +362,7 @@ namespace graphics
return res;
}
- int TTF::getTextHeight(const Sentence& t, int lineheight)
+ int TTF::getTextHeight(const Content& t, int lineheight)
{
ttf->pushTTFsize(ttfsize);
int res = 0;
@@ -452,7 +382,7 @@ namespace graphics
return res;
}
- void TTF::getTextBox(const Sentence& text, int* w, int* h, int lineheight, int spacing)
+ void TTF::getTextBox(const Content& text, int* w, int* h, int lineheight, int spacing)
{
ttf->pushTTFsize(ttfsize);
*w = 0;
@@ -481,79 +411,6 @@ namespace graphics
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;
diff --git a/libjin/Graphics/Font/TTF.h b/libjin/Graphics/Font/TTF.h
index 83fe25b..59e8125 100644
--- a/libjin/Graphics/Font/TTF.h
+++ b/libjin/Graphics/Font/TTF.h
@@ -65,13 +65,11 @@ namespace graphics
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;
+ Page* typeset(const unicode::Content& text, 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;
+ void print(const unicode::Content& text, int x, int y, int lineheight, int spacing = 0) override;
+ void print(const Page* page, int x, int y) override;
~TTF();
@@ -104,13 +102,9 @@ namespace graphics
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 getTextWidth(const unicode::Content& text, int spacing = 0);
+ int getTextHeight(const unicode::Content& text, int lineheight);
+ void getTextBox(const unicode::Content& text, int* w, int* h, int lineheight, int spacing = 0);
int textureWidth;
int textureHeight;
diff --git a/libjin/Graphics/Font/TexFont.cpp b/libjin/Graphics/Font/TexFont.cpp
index 9168f97..990a1af 100644
--- a/libjin/Graphics/Font/TexFont.cpp
+++ b/libjin/Graphics/Font/TexFont.cpp
@@ -20,14 +20,14 @@ namespace graphics
*/
/* create texture font from tilemap */
- TexFont * TexFont::createTexFont(const Bitmap* bitmap, const Sentence& codepoints, int cellw, int cellh)
+ TexFont * TexFont::createTexFont(const Bitmap* bitmap, const Content& 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* TexFont::createTexFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh)
{
TexFont* tf = new TexFont(bitmap, codepoints, mask, cellh);
return tf;
@@ -37,7 +37,7 @@ namespace graphics
{
}
- Page* TexFont::typeset(const Sentence& text, int lineheight, int spacing)
+ Page* TexFont::typeset(const Content& text, int lineheight, int spacing)
{
return nullptr;
}
@@ -47,17 +47,12 @@ namespace graphics
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 Content& text, int x, int y, int linehgiht, int spacing)
{
}
@@ -67,19 +62,14 @@ namespace graphics
}
- 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)
+ TexFont::TexFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh)
: Drawable(bitmap)
{
int l, r, t, b;
}
- TexFont::TexFont(const Bitmap* bitmap, const Sentence& codepoints, Color mask, int cellh)
+ TexFont::TexFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh)
: Drawable(bitmap)
{
diff --git a/libjin/Graphics/Font/TexFont.h b/libjin/Graphics/Font/TexFont.h
index c2142fa..c1b2d18 100644
--- a/libjin/Graphics/Font/TexFont.h
+++ b/libjin/Graphics/Font/TexFont.h
@@ -23,28 +23,26 @@ namespace graphics
{
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::Content& 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::Content& 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;
+ Page* typeset(const unicode::Content& 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;
+ void print(const unicode::Content& text, int x, int y, int linehgiht, 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);
+ TexFont(const Bitmap* bitmap, const unicode::Content& codepoints, int cellw, int cellh);
+ TexFont(const Bitmap* bitmap, const unicode::Content& codepoints, Color mask, int cellh);
std::map<unicode::Codepoint, TexGlyph> glyphs;
diff --git a/libjin/Graphics/Font/Text.cpp b/libjin/Graphics/Font/Text.cpp
index 91774c3..3ab7f07 100644
--- a/libjin/Graphics/Font/Text.cpp
+++ b/libjin/Graphics/Font/Text.cpp
@@ -11,91 +11,99 @@ namespace unicode
// iterator
/////////////////////////////////////////////////////////////////////////////
- Iterator::Iterator(const Iterator& itor)
- : text(itor.text)
+ Text::Iterator::Iterator(const Iterator& itor)
+ : data(itor.data)
, p(itor.p)
- , decoder(itor.decoder)
+ , encode(itor.encode)
+ , length(itor.length)
{
+ switch (encode)
+ {
+ case Encode::UTF8: decoder = new Utf8(); break;
+ }
}
- Iterator::Iterator(const Text& text)
- : text(text)
- , p(text.data)
- , decoder(text.decoder)
+ Text::Iterator::Iterator(const Encode& _encode, const void* _data, unsigned int _length)
+ : data(_data)
+ , p(_data)
+ , encode(_encode)
+ , length(_length)
{
+ switch (encode)
+ {
+ case Encode::UTF8: decoder = new Utf8(); break;
+ }
}
- Iterator::~Iterator()
+ Text::Iterator::~Iterator()
{
}
- Codepoint Iterator::get()
+ Codepoint Text::Iterator::get()
{
Codepoint codepoint;
decoder->decode(p, &codepoint);
return codepoint;
}
- Codepoint Iterator::operator*()
+ Codepoint Text::Iterator::operator*()
{
return get();
}
- Iterator Iterator::begin()
+ Text::Iterator Text::Iterator::begin()
{
- Iterator itor(text);
+ Iterator itor(encode, data, length);
itor.toBegin();
return itor;
}
- Iterator Iterator::end()
+ Text::Iterator Text::Iterator::end()
{
- Iterator itor(text);
+ Iterator itor(encode, data, length);
itor.toEnd();
return itor;
}
- void Iterator::toBegin()
+ void Text::Iterator::toBegin()
{
- p = (const unsigned char*)text.data;
+ p = (const unsigned char*)data;
}
- void Iterator::toEnd()
+ void Text::Iterator::toEnd()
{
- p = (const unsigned char*)text.data + text.length;
+ p = (const unsigned char*)data + length;
}
- Iterator& Iterator::operator ++()
+ Text::Iterator& Text::Iterator::operator ++()
{
p = decoder->next(p);
return *this;
}
- Iterator Iterator::operator ++(int)
+ Text::Iterator Text::Iterator::operator ++(int)
{
p = decoder->next(p);
- Iterator itor(text);
+ Iterator itor(encode, data, length);
itor.p = p;
return itor;
}
- bool Iterator::operator !=(const Iterator& itor)
+ bool Text::Iterator::operator !=(const Iterator& itor)
{
- const Text& text2 = itor.text;
- return !(text.data == text2.data
+ return !(data == itor.data
&& p == itor.p
- && text.length == text2.length
- && text.encode == text2.encode);
+ && length == itor.length
+ && encode == itor.encode);
}
- bool Iterator::operator ==(const Iterator& itor)
+ bool Text::Iterator::operator ==(const Iterator& itor)
{
- const Text& text2 = itor.text;
- return text.data == text2.data
+ return data == itor.data
&& p == itor.p
- && text.length == text2.length
- && text.encode == text2.encode;
+ && length == itor.length
+ && encode == itor.encode;
}
/////////////////////////////////////////////////////////////////////////////
@@ -103,35 +111,26 @@ namespace unicode
/////////////////////////////////////////////////////////////////////////////
Text::Text(Encode _encode, const void* _data, unsigned int _length)
- : encode(_encode)
- , length(_length)
{
- data = new char[length];
- memcpy(data, _data, length);
- switch (encode)
+ Iterator it = Iterator(_encode, _data, _length);
+ for (; it != it.end(); ++it)
{
- case UTF8: decoder = new Utf8(); break;
+ content.push_back(*it);
}
}
Text::~Text()
{
- delete decoder;
- delete data;
}
- Iterator Text::getIterator() const
+ const Content& Text::getContent() const
{
- Iterator itor = Iterator(*this);
- return itor;
+ return content;
}
- Sentence Text::getSentence() const
+ const Content& Text::operator*() const
{
- Sentence sentence;
- for (Iterator it = getIterator(); it != it.end(); ++it)
- sentence.push_back(*it);
- return sentence;
+ return content;
}
} // unicode
diff --git a/libjin/Graphics/Font/Text.h b/libjin/Graphics/Font/Text.h
index 11897a0..c2cc64b 100644
--- a/libjin/Graphics/Font/Text.h
+++ b/libjin/Graphics/Font/Text.h
@@ -12,34 +12,6 @@ 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
{
@@ -47,17 +19,41 @@ namespace unicode
Text(Encode encode, const void* data, unsigned int length);
~Text();
- Iterator getIterator() const;
- Sentence getSentence() const;
+ const Content& getContent() const;
+ const Content& operator*() const;
private:
- friend class Iterator;
+ class Iterator
+ {
+ public:
+ Iterator(const Iterator& itor);
+ Iterator(const Encode& encode, const void* data, unsigned int length);
+ ~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 Encode encode;
- Decoder* decoder;
+ const Encode encode;
+ const Decoder* decoder;
+ const void* p;
+ const void* const data;
+ unsigned int length;
+ };
- void* data;
- const unsigned int length;
+ Content content;
};