summaryrefslogtreecommitdiff
path: root/Runtime
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-11-04 12:48:01 +0800
committerchai <chaifix@163.com>2021-11-04 12:48:01 +0800
commit94a9a28de16badb75e66a60efca3b01d31cc0fc6 (patch)
treea37e09236da2b7cda11a451d6fed4fe37afb065c /Runtime
parentb215d811a1981e20f35bb31df4e6cd2a74146193 (diff)
* text anchor
Diffstat (limited to 'Runtime')
-rw-r--r--Runtime/Debug/Log.cpp6
-rw-r--r--Runtime/GUI/Font.cpp21
-rw-r--r--Runtime/GUI/Font.h1
-rw-r--r--Runtime/GUI/UITextMesh.cpp67
-rw-r--r--Runtime/GUI/UITextMesh.h3
-rw-r--r--Runtime/Math/Math.h5
-rw-r--r--Runtime/Math/MathHelper.h2
-rw-r--r--Runtime/Math/Vector2.h34
-rw-r--r--Runtime/Scripting/GUI/Font.bind.cpp10
9 files changed, 112 insertions, 37 deletions
diff --git a/Runtime/Debug/Log.cpp b/Runtime/Debug/Log.cpp
index c44d7af..301e15a 100644
--- a/Runtime/Debug/Log.cpp
+++ b/Runtime/Debug/Log.cpp
@@ -101,7 +101,7 @@ void log_info_tag(const char* tag, const char* fmt, ...)
SetOutputColor(0);
va_list pArgs = NULL;
va_start(pArgs, fmt);
- printf("%s [Info] %s ", currentDateTime().c_str(), tag);
+ printf("%s [Info] [%s] ", currentDateTime().c_str(), tag);
vprintf(fmt, pArgs);
printf("\n");
va_end(pArgs);
@@ -114,7 +114,7 @@ void log_warning_tag(const char* tag, const char* fmt, ...)
SetOutputColor(1);
va_list pArgs = NULL;
va_start(pArgs, fmt);
- printf("%s [Warning] %s ", currentDateTime().c_str(), tag);
+ printf("%s [Warning] [%s] ", currentDateTime().c_str(), tag);
vprintf(fmt, pArgs);
printf("\n");
va_end(pArgs);
@@ -127,7 +127,7 @@ void log_error_tag(const char* tag, const char* fmt, ...)
SetOutputColor(2);
va_list pArgs = NULL;
va_start(pArgs, fmt);
- printf("%s [Error] %s ", currentDateTime().c_str(), tag);
+ printf("%s [Error] [%s] ", currentDateTime().c_str(), tag);
vprintf(fmt, pArgs);
printf("\n");
va_end(pArgs);
diff --git a/Runtime/GUI/Font.cpp b/Runtime/GUI/Font.cpp
index 1993126..4425b17 100644
--- a/Runtime/GUI/Font.cpp
+++ b/Runtime/GUI/Font.cpp
@@ -7,11 +7,14 @@
using namespace character;
-//https://unicode-table.com/en/#cjk-unified-ideographs-extension-a
-//https://learnopengl.com/In-Practice/Text-Rendering
-//https://www.zhihu.com/question/294660079
-//https://baike.baidu.com/item/%E5%9F%BA%E6%9C%AC%E5%A4%9A%E6%96%87%E7%A7%8D%E5%B9%B3%E9%9D%A2/10788078
-//https://stackoverflow.com/questions/27331819/whats-the-difference-between-a-character-a-code-point-a-glyph-and-a-grapheme
+/*
+* http://madig.github.io/freetype-web/documentation/tutorial/
+* https://unicode-table.com/en/#cjk-unified-ideographs-extension-a
+* https://learnopengl.com/In-Practice/Text-Rendering
+* https://www.zhihu.com/question/294660079
+* https://baike.baidu.com/item/%E5%9F%BA%E6%9C%AC%E5%A4%9A%E6%96%87%E7%A7%8D%E5%B9%B3%E9%9D%A2/10788078
+* https://stackoverflow.com/questions/27331819/whats-the-difference-between-a-character-a-code-point-a-glyph-and-a-grapheme
+*/
static std::string s_FontError;
@@ -161,7 +164,7 @@ bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize)
int h = m_FTFace->glyph->bitmap.rows;
const unsigned char* pixels = m_FTFace->glyph->bitmap.buffer;
- if (pixels != NULL) // 非空格
+ if (pixels != NULL) // 有些字体中space、tab没有字形的
{
s_PixelBuffer.resize(w * h);
if (m_FTFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO)
@@ -183,7 +186,7 @@ bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize)
memcpy(&s_PixelBuffer[0], pixels, s_SizePerPixel * w * h);
}
- //TextHelper::print_glyph(&s_PixelBuffer[0], w, h);
+ TextHelper::print_glyph(&s_PixelBuffer[0], w, h);
GlyphAtals* atlas = RequestAtlas(pixelSize, Internal::Vector2(w, h));
Assert(atlas);
@@ -208,7 +211,7 @@ bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize)
character.position = rect;
character.bearing = Internal::Vector2(m_FTFace->glyph->bitmap_left, m_FTFace->glyph->bitmap_top);
}
- else // 空格
+ else // space、tab
{
character.atlas = FONT_NOT_IN_ATLAS_PLACEHOLDER;
character.position = Rect(0,0,0,0);
@@ -295,7 +298,7 @@ GlyphAtals* Font::RequestAtlas(int pixelSize, Internal::Vector2 preferSize)
Texture* Font::CreateAtlas()
{
TextureSetting setting;
- setting.filterMode = ETextureFilterMode::Linear;
+ setting.filterMode = ETextureFilterMode::Nearest;
setting.wrapMode = ETextureWrapMode::Clamp;
setting.format = ETextureFormat::R8;
setting.type = ETextureType::TEX_2D;
diff --git a/Runtime/GUI/Font.h b/Runtime/GUI/Font.h
index 5fbe981..cd546af 100644
--- a/Runtime/GUI/Font.h
+++ b/Runtime/GUI/Font.h
@@ -93,6 +93,7 @@ enum EEncoding
Encoding_UTF16,
};
+// 没有字形,但是有advance的文字,比如space和tab
#define FONT_NOT_IN_ATLAS_PLACEHOLDER (INT_MAX)
// 单个字体
diff --git a/Runtime/GUI/UITextMesh.cpp b/Runtime/GUI/UITextMesh.cpp
index abb5231..94e61c4 100644
--- a/Runtime/GUI/UITextMesh.cpp
+++ b/Runtime/GUI/UITextMesh.cpp
@@ -29,6 +29,7 @@ static unsigned int s_IndicesPerText;
struct TextInfo {
const Character* ch;
float offset;
+ int line; // 从0开始
};
static unordered_map<unsigned int, vector<TextInfo>> s_TextInfos;
@@ -59,27 +60,56 @@ InitializeStaticVariables([]() {
// * 换行
// * 颜色
-UITextMesh::UITextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnchor anchor, ETextAlignment alignment)
+// lineheight 用来处理换行、锚点
+// wordwrap 自动换行
+// preferredSize 自动换行区域大小
+UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int lineHeight, ETextAnchor anchor, ETextAlignment alignment, bool wordwrap, float preferred)
{
m_Font = font;
s_TextInfos.clear();
const Vector2 atlasSize = font->GetAtlasSize();
+ Vector2 textRegion;
// 按照不同的atlas分类到s_TextInfos
float offset = 0;
+ int line = 0;
for (int i = 0; i < str.length; ++i)
{
character::Unicode c = str.str[i];
const Character* ch = font->GetCharacter(c, pixelSize);
if (ch == NULL)
continue;
+
+ if (wordwrap) // 自动换行
+ {
+ if (offset + ch->bearing.x + ch->position.width > preferred)
+ {
+ ++line;
+ offset = 0;
+ }
+ }
+
unsigned int atlasIndex = ch->atlas;
-
if (atlasIndex != FONT_NOT_IN_ATLAS_PLACEHOLDER) //非空格
{
+
+// 换行符Unix'\n', Windows'\r\n', MacOS '\r'
+#define CHECK_BREAKS() \
+ if (c == '\n' || c == '\r') \
+ { \
+ ++line; \
+ offset = 0; \
+ if (c == '\r' && ((i + 1) < str.length && str.str[i + 1] == '\n')) /*skip\n*/ \
+ ++i; \
+ continue; \
+ }
+
+ CHECK_BREAKS();
+
TextInfo info;
info.ch = ch;
info.offset = offset;
+ info.line = line;
auto list = s_TextInfos.find(atlasIndex);
if (list == s_TextInfos.end())
@@ -88,13 +118,37 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font,int pixelSize, EText
vector<TextInfo>& v = s_TextInfos[atlasIndex];
v.push_back(info);
}
+ else
+ {
+ // 有些字体换行没有字形,所以也需要在这里处理
+ CHECK_BREAKS();
+ }
offset += ch->advance;
+
+ textRegion.x = max(offset, textRegion.x);
}
+ textRegion.y = (line + 1) * lineHeight;
+
if (s_TextInfos.size() == 0)
return;
+ Vector2i textOffset;
+ Vector2 halfRegion = Vector2(textRegion.x/ 2.f, textRegion.y / 2.f);
+ switch (anchor)
+ {
+ case TextAnchor_UpperLeft: textOffset.Set(0, 0); break;
+ case TextAnchor_UpperCenter: textOffset.Set(-halfRegion.x, 0); break;
+ case TextAnchor_UpperRight: textOffset.Set(-textRegion.x, 0); break;
+ case TextAnchor_MiddleLeft: textOffset.Set(0, -halfRegion.y); break;
+ case TextAnchor_MiddleCenter: textOffset.Set(-halfRegion.x, -halfRegion.y); break;
+ case TextAnchor_MiddleRight: textOffset.Set(-textRegion.x, -halfRegion.y); break;
+ case TextAnchor_LowerLeft: textOffset.Set(0, -textRegion.y); break;
+ case TextAnchor_LowerCenter: textOffset.Set(-halfRegion.x, -textRegion.y); break;
+ case TextAnchor_LowerRight: textOffset.Set(-textRegion.x, -textRegion.y); break;
+ }
+
// 填充VBO和IBO
for (auto iter : s_TextInfos) {
unsigned int atlasIndex = iter.first; // atlas atlasIndex
@@ -113,12 +167,13 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font,int pixelSize, EText
TextInfo& text = texts[i];
int vOff = i * s_VertexPerText;
+ int lineOff = text.line * lineHeight;
// 左上角是原点
float pos[] = {
- text.offset + text.ch->bearing.x, pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-left
- text.offset + text.ch->bearing.x + text.ch->position.width, pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-right
- text.offset + text.ch->bearing.x + text.ch->position.width, pixelSize - text.ch->bearing.y, // top-right
- text.offset + text.ch->bearing.x, pixelSize - text.ch->bearing.y, // top-left
+ textOffset.x + text.offset + text.ch->bearing.x, textOffset.y + lineOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-left
+ textOffset.x + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-right
+ textOffset.x + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineOff + pixelSize - text.ch->bearing.y, // top-right
+ textOffset.x + text.offset + text.ch->bearing.x, textOffset.y + lineOff + pixelSize - text.ch->bearing.y, // top-left
};
Vector4 uvQuad = Vector4(text.ch->position.x / atlasSize.x, text.ch->position.y / atlasSize.y, text.ch->position.width / atlasSize.x, text.ch->position.height / atlasSize.y);
float uv[] = {
diff --git a/Runtime/GUI/UITextMesh.h b/Runtime/GUI/UITextMesh.h
index c88e52f..487bccd 100644
--- a/Runtime/GUI/UITextMesh.h
+++ b/Runtime/GUI/UITextMesh.h
@@ -17,7 +17,6 @@ enum ETextAnchor
TextAnchor_LowerLeft,
TextAnchor_LowerCenter,
TextAnchor_LowerRight,
- TextAnchor_DontCare
};
enum ETextAlignment {
@@ -37,7 +36,7 @@ namespace TextHelper
class UITextMesh
{
public:
- UITextMesh(const UnicodeString& str, Font* font, int pixelSize, ETextAnchor anchor, ETextAlignment alignment)/*throw TextMeshException*/;
+ UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int lineHeight, ETextAnchor anchor = TextAnchor_UpperLeft, ETextAlignment alignment = TextAlignment_Left, bool wordwrap = false, float preferred = 0)/*throw TextMeshException*/;
~UITextMesh();
diff --git a/Runtime/Math/Math.h b/Runtime/Math/Math.h
index 5e08613..80f23f7 100644
--- a/Runtime/Math/Math.h
+++ b/Runtime/Math/Math.h
@@ -8,8 +8,11 @@
#include "Rect.h"
#include "MathHelper.h"
-typedef Internal::Vector2 Vector2;
typedef Internal::Vector3 Vector3;
typedef Internal::Vector4 Vector4;
typedef Internal::Matrix44 Matrix44;
typedef Internal::Rect Rect;
+
+typedef Internal::Vector2T<int> Vector2i;
+typedef Internal::Vector2T<float> Vector2f;
+
diff --git a/Runtime/Math/MathHelper.h b/Runtime/Math/MathHelper.h
index c41eec9..ad64dcf 100644
--- a/Runtime/Math/MathHelper.h
+++ b/Runtime/Math/MathHelper.h
@@ -1,7 +1,7 @@
#pragma once
#define max(a, b)\
-(a)>(b)?(a):(b)
+((a)>(b)?(a):(b))
#define min(a, b)\
(a)<(b)?(a):(b)
diff --git a/Runtime/Math/Vector2.h b/Runtime/Math/Vector2.h
index 9d7e4e9..f13e522 100644
--- a/Runtime/Math/Vector2.h
+++ b/Runtime/Math/Vector2.h
@@ -4,47 +4,59 @@
namespace Internal
{
- struct Vector2
+ template<typename T>
+ struct Vector2T
{
- Vector2(float x = 0, float y = 0)
+ Vector2T(T x = 0, T y = 0)
{
this->x = x;
this->y = y;
}
- inline void Set(float x, float y)
+ inline void Set(T x, T y)
{
this->x = x;
this->y = y;
}
- Vector2 Clamp(float xmin, float xmax, float ymin, float ymax)
+ Vector2T Clamp(T xmin, T xmax, T ymin, T ymax)
{
- Vector2 v;
+ Vector2T v;
v.x = clamp(x, xmin, xmax);
v.y = clamp(y, ymin, ymax);
return v;
}
- float operator[](int i)
+ T operator[](int i)
{
if (i == 0) return x;
else if (i == 1) return y;
Assert(false);
}
- bool operator == (const Vector2& v) const
+ bool operator == (const Vector2T& v) const
{
return v.x == x && v.y == y;
}
- bool operator != (const Vector2& v) const
+ bool operator != (const Vector2T& v) const
{
return v.x != x || v.y != y;
}
- float x, y;
+ T x, y;
- static Vector2 zero;
- static Vector2 one;
+ static Vector2T<T> zero;
+ static Vector2T<T> one;
};
+
+ template<typename T>
+ Vector2T<T> Vector2T<T>::zero = Vector2T(0, 0);
+ template<typename T>
+ Vector2T<T> Vector2T<T>::one = Vector2T(1, 1);
+
+ typedef Internal::Vector2T<float> Vector2;
+
}
+
+typedef Internal::Vector2T<float> Vector2;
+
diff --git a/Runtime/Scripting/GUI/Font.bind.cpp b/Runtime/Scripting/GUI/Font.bind.cpp
index 7172027..e749303 100644
--- a/Runtime/Scripting/GUI/Font.bind.cpp
+++ b/Runtime/Scripting/GUI/Font.bind.cpp
@@ -88,15 +88,17 @@ LUA_BIND_IMPL_METHOD(Font, _GetCharacter)
return 1;
}
-// font:GetCharacter(str, size, encoding)
+// font:GetCharacter(str, size, wordwrap, preferredWidth, encoding)
LUA_BIND_IMPL_METHOD(Font, _GetCharacters)
{
LUA_BIND_PREPARE(L, Font);
- LUA_BIND_CHECK(L, "USN*");
+ //LUA_BIND_CHECK(L, "US");
char* buf = (char*)state.GetValue<const char*>(2, "");
int size = state.GetValue<int>(3, 12);
- int encoding = state.GetValue<int>(4, EEncoding::Encoding_UTF8);
+ bool wordwrap = state.GetValue<bool>(4, false);
+ float preferred = state.GetValue<float>(5, 0);
+ int encoding = state.GetValue<int>(6, EEncoding::Encoding_UTF8);
s_Codepoints->clear();
if (encoding == EEncoding::Encoding_UTF8)
@@ -134,7 +136,7 @@ LUA_BIND_IMPL_METHOD(Font, _GetCharacters)
WipeGLError();
- UITextMesh* tm = new UITextMesh(str, self, size, ETextAnchor::TextAnchor_MiddleLeft, ETextAlignment::TextAlignment_Left);
+ UITextMesh* tm = new UITextMesh(str, self, size, size + 3, TextAnchor_MiddleLeft, ETextAlignment::TextAlignment_Left, wordwrap, preferred);
tm->Draw();
return 0;