From 4428d0ac933482274d09862fd984ac8ede681f7c Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 3 Nov 2021 13:29:01 +0800 Subject: *font space --- Runtime/GUI/Font.cpp | 89 +++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 40 deletions(-) (limited to 'Runtime/GUI/Font.cpp') diff --git a/Runtime/GUI/Font.cpp b/Runtime/GUI/Font.cpp index d669844..8a5acea 100644 --- a/Runtime/GUI/Font.cpp +++ b/Runtime/GUI/Font.cpp @@ -151,63 +151,70 @@ bool Font::RenderCharacter(character::Codepoint codepoint, int pixelSize) // bug: 发现有时候渲染的结果是FT_PIXEL_MODE_MONO(1-bits),试过不同的flags组合还是不对,最后手动转换为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; + Character character; + int w = m_FTFace->glyph->bitmap.width; int h = m_FTFace->glyph->bitmap.rows; 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) + if (pixels != NULL) // 非空格 { - // 1 bit monochrome - int pitch = m_FTFace->glyph->bitmap.pitch; - for (int y = 0; y < h; ++y) - { - for (int x = 0; x < w; ++x) + 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) { - int index = x + y * w; - s_PixelBuffer[index] = ((pixels[pitch * y + x / 8]) & (1 << (7 - x % 8))) ? 255 : 0; + 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); - } + } + 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); + //TextHelper::print_glyph(&s_PixelBuffer[0], w, h); - GlyphAtals* atlas = RequestAtlas(pixelSize, Internal::Vector2(w, h)); - Assert(atlas); + GlyphAtals* atlas = RequestAtlas(pixelSize, Internal::Vector2(w, h)); + Assert(atlas); - Internal::Rect rect = GetRenderChartAndMove(atlas, Internal::Vector2(w, h)); + Internal::Rect rect = GetRenderChartAndMove(atlas, Internal::Vector2(w, h)); - try - { - atlas->altas->UpdateSubImage(rect, EPixelFormat::PixelFormat_R, EPixelElementType::PixelType_UNSIGNED_BYTE, &s_PixelBuffer[0]); - } - catch (TextureException& e) - { + try + { + 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 false; + } s_PixelBuffer.clear(); - std::string error = e.what(); - error = "Render Character Error: " + error; - log_error(e.what()); - return false; - } - s_PixelBuffer.clear(); - Character character; - character.atlas = atlas->index; - character.position = rect; - character.bearing = Internal::Vector2(m_FTFace->glyph->bitmap_left, m_FTFace->glyph->bitmap_top); + character.atlas = atlas->index; + character.position = rect; + character.bearing = Internal::Vector2(m_FTFace->glyph->bitmap_left, m_FTFace->glyph->bitmap_top); + } + else // 空格 + { + character.atlas = FONT_NOT_IN_ATLAS_PLACEHOLDER; + character.position = Rect(0,0,0,0); + character.bearing = Vector2(0, 0); + } + character.advance = m_FTFace->glyph->advance.x * 1/64.f; m_Characters.insert(std::pair(hash, character)); @@ -260,6 +267,8 @@ GlyphAtals* Font::RequestAtlas(int pixelSize, Internal::Vector2 preferSize) auto iter = m_AtlasCache.find(pixelSize); if (iter == m_AtlasCache.end() || !HasEnoughSpace(iter->second, preferSize)) { + Assert(m_Atlases.size() < FONT_NOT_IN_ATLAS_PLACEHOLDER); + Texture* tex = CreateAtlas(); GlyphAtals newAtlas = GlyphAtals(); newAtlas.altas = tex; -- cgit v1.1-26-g67d0