Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue where font metrics were incorrect for monospaced fonts. #8886

Merged
merged 12 commits into from
May 13, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,8 @@ else if (channelCount==3)
throw new FontFormatException("Could not generate font preview: " + e.getMessage());
}
}

boolean is_monospaced = true;
float base_advance = include_glyph_count > 0 ? glyphs.get(0).advance : 0;
for (int i = 0; i < include_glyph_count; i++) {
Glyph glyph = glyphs.get(i);
GlyphBank.Glyph.Builder glyphBuilder = GlyphBank.Glyph.newBuilder()
Expand All @@ -745,8 +746,9 @@ else if (channelCount==3)
}

glyphBankBuilder.addGlyphs(glyphBuilder);
is_monospaced &= base_advance == glyph.advance;
AGulev marked this conversation as resolved.
Show resolved Hide resolved
}

glyphBankBuilder.setIsMonospaced(is_monospaced);
return previewImage;

}
Expand Down
14 changes: 8 additions & 6 deletions engine/engine/src/test/def-3575/def-3575.script
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function init(self)
end

function update(self, dt)
-- Values are taken from the output from label.get_text_metrics from defold version 1.2.138
-- Values are taken from the output from resource.get_text_metrics from defold version 1.2.138
-- The base settings used are:
-- font: "/builtins/fonts/vera_mo_bd.ttf"
-- font: "/builtins/fonts/vera_mo_bd.ttf" - monospaced
-- size: 14
-- antialias: 1
-- alpha: 1.0
Expand All @@ -35,9 +35,11 @@ function update(self, dt)
-- shadow_alpha: 1.0
-- shadow_blur: 2

local function test_label_metrics(label_name,test_metrics)
local function test_label_metrics(label_name, test_metrics)
-- Test that we are getting the expected value back from get_text_metrics
local metrics = label.get_text_metrics("#" .. label_name)
local comp = "#" .. label_name
local font = go.get(comp, "font")
local metrics = resource.get_text_metrics(font, label.get_text(comp))

local result = test_metrics.max_ascent == metrics.max_ascent and
test_metrics.max_descent == metrics.max_descent and
Expand All @@ -54,7 +56,7 @@ function update(self, dt)
local function test_label_bitmap()
local metrics_valid = {
max_ascent = 13,
width = 781,
width = 768,
height = 17,
max_descent = 4
}
Expand All @@ -65,7 +67,7 @@ function update(self, dt)
local function test_label_df()
local metrics_valid ={
max_ascent = 13,
width = 783,
width = 768,
height = 17,
max_descent = 4,
}
Expand Down
1 change: 1 addition & 0 deletions engine/gamesys/src/gamesys/resources/res_font_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ namespace dmGameSystem
params.m_ImageFormat = glyph_bank->m_ImageFormat;
params.m_GlyphChannels = glyph_bank->m_GlyphChannels;
params.m_GlyphData = glyph_bank->m_GlyphData.m_Data;
params.m_IsMonospaced = glyph_bank->m_IsMonospaced;

if (font_map == 0)
{
Expand Down
2 changes: 2 additions & 0 deletions engine/render/proto/render/font_ddf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ message GlyphBank
optional uint32 cache_cell_width = 14;
optional uint32 cache_cell_height = 15;
optional uint32 cache_cell_max_ascent = 16;

optional bool is_monospaced = 17 [default = false];
}

message FontMap
Expand Down
18 changes: 14 additions & 4 deletions engine/render/src/render/font_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ namespace dmRender
, m_CacheCellPadding(0)
, m_LayerMask(FACE)
, m_ImageFormat(dmRenderDDF::TYPE_BITMAP)
, m_IsMonospaced(false)
{

}
Expand Down Expand Up @@ -111,6 +112,7 @@ namespace dmRender
, m_CacheCellMaxAscent(0)
, m_CacheCellPadding(0)
, m_LayerMask(FACE)
, m_IsMonospaced(false)
{

}
Expand Down Expand Up @@ -162,6 +164,8 @@ namespace dmRender
uint32_t m_CacheCellMaxAscent;
uint8_t m_CacheCellPadding;
uint8_t m_LayerMask;
uint8_t m_IsMonospaced:1;
uint8_t :7;
};

static float GetLineTextMetrics(HFontMap font_map, float tracking, const char* text, int n, bool measure_trailing_space);
Expand Down Expand Up @@ -235,6 +239,7 @@ namespace dmRender
uint32_t cell_count = font_map->m_CacheColumns * font_map->m_CacheRows;

font_map->m_CellTempData = (uint8_t*)malloc(font_map->m_CacheCellWidth*font_map->m_CacheCellHeight*4);
font_map->m_IsMonospaced = params.m_IsMonospaced;

switch (params.m_GlyphChannels)
{
Expand Down Expand Up @@ -325,6 +330,7 @@ namespace dmRender
font_map->m_OutlineAlpha = params.m_OutlineAlpha;
font_map->m_ShadowAlpha = params.m_ShadowAlpha;
font_map->m_LayerMask = params.m_LayerMask;
font_map->m_IsMonospaced = params.m_IsMonospaced;

font_map->m_CacheWidth = params.m_CacheWidth;
font_map->m_CacheHeight = params.m_CacheHeight;
Expand Down Expand Up @@ -1206,10 +1212,14 @@ namespace dmRender
}
if (n > 0 && 0 != last)
{
uint32_t last_width = (measure_trailing_space && last->m_Character == ' ') ? (int16_t)last->m_Advance : last->m_Width;
float last_end_point = last->m_LeftBearing + last_width;
float last_right_bearing = last->m_Advance - last_end_point;
width = width - last_right_bearing - tracking;
if (!font_map->m_IsMonospaced)
{
uint32_t last_width = (measure_trailing_space && last->m_Character == ' ') ? (int16_t)last->m_Advance : last->m_Width;
float last_end_point = last->m_LeftBearing + last_width;
float last_right_bearing = last->m_Advance - last_end_point;
width = width - last_right_bearing;
}
width -= tracking;
}

return width;
Expand Down
2 changes: 2 additions & 0 deletions engine/render/src/render/font_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ namespace dmRender
uint32_t m_CacheCellMaxAscent;
uint8_t m_CacheCellPadding;
uint8_t m_LayerMask;
uint8_t m_IsMonospaced:1;
uint8_t :7;

dmRenderDDF::FontTextureFormat m_ImageFormat;
};
Expand Down