diff options
author | chai <chaifix@163.com> | 2021-11-03 13:08:39 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-11-03 13:08:39 +0800 |
commit | 5cd2e2299f98065fa06c192df4dfb2c014b2b253 (patch) | |
tree | 9878536091d2ef6657a79026dcc7b45a8eb091dd | |
parent | c10e0d92f46e5eaf25a69e1fafe5f4dbd8eaab9d (diff) |
*font
-rw-r--r-- | Data/Scripts/Editor/AssetBrowser.lua | 3 | ||||
-rw-r--r-- | Runtime/GUI/Font.cpp | 65 | ||||
-rw-r--r-- | Runtime/GUI/Font.h | 2 | ||||
-rw-r--r-- | Runtime/GUI/UITextMesh.cpp | 2 |
4 files changed, 55 insertions, 17 deletions
diff --git a/Data/Scripts/Editor/AssetBrowser.lua b/Data/Scripts/Editor/AssetBrowser.lua index 0c2d03f..8d8efa6 100644 --- a/Data/Scripts/Editor/AssetBrowser.lua +++ b/Data/Scripts/Editor/AssetBrowser.lua @@ -38,8 +38,7 @@ AssetBrowser.OnGUI = function(self) Engine.Rendering.SetVector2("gamelab_ui_position", {0, 0})
--Engine.Rendering.SetTexture("gamelab_main_tex", tex)
--Engine.Rendering.DrawUIQuad({0, 0, 10, 20})
- -- _G["default_font"]:GetCharacters("你好世界!Hello,World! Project Window Properties", 15)
- _G["default_font"]:GetCharacters("A", 17)
+ _G["default_font"]:GetCharacters("你好世界!Hello,World! Project Window Properties", 12)
Engine.Rendering.ResetUniformState()
end
diff --git a/Runtime/GUI/Font.cpp b/Runtime/GUI/Font.cpp index 614792a..d669844 100644 --- a/Runtime/GUI/Font.cpp +++ b/Runtime/GUI/Font.cpp @@ -15,6 +15,9 @@ using namespace character; static std::string s_FontError; +static std::vector<unsigned char> s_PixelBuffer; +static int s_SizePerPixel = sizeof(unsigned char); + Font::Font(std::string path, TextGeneratingSettings settings) : LuaBind::NativeClass<Font>() { @@ -109,8 +112,14 @@ const Character* Font::GetCharacter(character::Codepoint codepoint, int pixelSiz auto iter = m_Characters.find(hash); if (iter == m_Characters.end()) { - RenderCharacter(codepoint, pixelSize); - iter = m_Characters.find(hash); + if (RenderCharacter(codepoint, pixelSize)) + { + iter = m_Characters.find(hash); + } + else + { + return NULL; + } } Assert(iter != m_Characters.end()); return &iter->second; @@ -133,21 +142,48 @@ void Font::RenderCharacters(std::vector<character::Codepoint>& codepoint, int pi } } -void Font::RenderCharacter(character::Codepoint codepoint, int pixelSize) +bool Font::RenderCharacter(character::Codepoint codepoint, int pixelSize) { character::Hash hash = GetHash(codepoint, pixelSize); if (m_Characters.count(hash) != 0) - return; + return true; FT_Set_Pixel_Sizes(m_FTFace, 0, pixelSize); - if (FT_Load_Char(m_FTFace, codepoint, FT_LOAD_RENDER)) - { - return; - } + + // bug: ʱȾĽFT_PIXEL_MODE_MONO1-bits)ԹͬflagsϻDzԣֶתΪ8-bits + + //FT_Int32 flags = FT_LOAD_TARGET_NORMAL | FT_LOAD_FORCE_AUTOHINT;
+ FT_Int32 flags = FT_LOAD_RENDER;
+ if (FT_Load_Char(m_FTFace, codepoint, flags)) + return false; int w = m_FTFace->glyph->bitmap.width; int h = m_FTFace->glyph->bitmap.rows; - TextHelper::print_glyph(m_FTFace->glyph->bitmap.buffer, w, h); + const unsigned char* pixels = m_FTFace->glyph->bitmap.buffer; + if (pixels == NULL) + return false; + + s_PixelBuffer.resize(w * h); + if (m_FTFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO) + { + // 1 bit monochrome
+ int pitch = m_FTFace->glyph->bitmap.pitch;
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ int index = x + y * w;
+ s_PixelBuffer[index] = ((pixels[pitch * y + x / 8]) & (1 << (7 - x % 8))) ? 255 : 0;
+ }
+ }
+ } + else if (m_FTFace->glyph->bitmap.pixel_mode) + { + // 8 bit grayscale
+ memcpy(&s_PixelBuffer[0], pixels, s_SizePerPixel * w * h); + } + + //TextHelper::print_glyph(&s_PixelBuffer[0], w, h); GlyphAtals* atlas = RequestAtlas(pixelSize, Internal::Vector2(w, h)); Assert(atlas); @@ -156,15 +192,17 @@ void Font::RenderCharacter(character::Codepoint codepoint, int pixelSize) try { - atlas->altas->UpdateSubImage(rect, EPixelFormat::PixelFormat_R, EPixelElementType::PixelType_UNSIGNED_BYTE, m_FTFace->glyph->bitmap.buffer); + atlas->altas->UpdateSubImage(rect, EPixelFormat::PixelFormat_R, EPixelElementType::PixelType_UNSIGNED_BYTE, &s_PixelBuffer[0]); } catch (TextureException& e) { + s_PixelBuffer.clear(); std::string error = e.what(); error = "Render Character Error: " + error; log_error(e.what()); - return; + return false; } + s_PixelBuffer.clear(); Character character; character.atlas = atlas->index; @@ -173,9 +211,8 @@ void Font::RenderCharacter(character::Codepoint codepoint, int pixelSize) character.advance = m_FTFace->glyph->advance.x * 1/64.f; m_Characters.insert(std::pair<character::Hash, Character>(hash, character)); -/* - FT_Done_Face(m_FTFace); - FT_Done_FreeType(m_FTLibrary);*/ + + return true; } const GlyphAtals* Font::GetGlyphAtlas(int index) diff --git a/Runtime/GUI/Font.h b/Runtime/GUI/Font.h index e55e78f..8e55395 100644 --- a/Runtime/GUI/Font.h +++ b/Runtime/GUI/Font.h @@ -106,7 +106,7 @@ public: const GlyphAtals* GetGlyphAtlas(int index); // pre-bake - void RenderCharacter(character::Codepoint codepoint, int pixelSize); + bool RenderCharacter(character::Codepoint codepoint, int pixelSize); void RenderCharacters(character::Codepoint* codepoint, int n, int pixelSize); void RenderCharacters(std::vector<character::Codepoint>& codepoint, int pixelSize); diff --git a/Runtime/GUI/UITextMesh.cpp b/Runtime/GUI/UITextMesh.cpp index 6a10f88..32dec3b 100644 --- a/Runtime/GUI/UITextMesh.cpp +++ b/Runtime/GUI/UITextMesh.cpp @@ -71,6 +71,8 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font,int pixelSize, EText { character::Codepoint c = str.str[i]; const Character* ch = font->GetCharacter(c, pixelSize); + if (ch == NULL) + continue; unsigned int atlasIndex = ch->atlas; TextInfo info; |