From c5ed95a52617efbff4d96348411f28dda6af52d7 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Mon, 7 Jun 2021 02:36:10 +0200 Subject: [PATCH] Readd support for kerning in DynamicFont DynamicFont kerning was removed in a9b7843dca3ea3eda9f9f890a3e2c78542ca7561. This readds kerning support and fixes it at the same time. Co-authored-by: follower --- scene/resources/dynamic_font.cpp | 16 ++++++++++++++++ scene/resources/dynamic_font.h | 1 + 2 files changed, 17 insertions(+) diff --git a/scene/resources/dynamic_font.cpp b/scene/resources/dynamic_font.cpp index 4ceacf73d8c..5b6ece612b7 100644 --- a/scene/resources/dynamic_font.cpp +++ b/scene/resources/dynamic_font.cpp @@ -236,6 +236,18 @@ const Pair DynamicFon return Pair(chr, const_cast(this)); } +float DynamicFontAtSize::_get_kerning_advance(const DynamicFontAtSize *font, CharType p_char, CharType p_next) const { + float advance = 0.0; + + if (p_next) { + FT_Vector delta; + FT_Get_Kerning(font->face, FT_Get_Char_Index(font->face, p_char), FT_Get_Char_Index(font->face, p_next), FT_KERNING_DEFAULT, &delta); + advance = (delta.x / 64.0) / oversampling; + } + + return advance; +} + Size2 DynamicFontAtSize::get_char_size(CharType p_char, CharType p_next, const Vector> &p_fallbacks) const { if (!valid) { return Size2(1, 1); @@ -244,6 +256,7 @@ Size2 DynamicFontAtSize::get_char_size(CharType p_char, CharType p_next, const V Pair char_pair_with_font = _find_char_with_font(p_char, p_fallbacks); const Character *ch = char_pair_with_font.first; + DynamicFontAtSize *font = char_pair_with_font.second; ERR_FAIL_COND_V(!ch, Size2()); Size2 ret(0, get_height()); @@ -251,6 +264,7 @@ Size2 DynamicFontAtSize::get_char_size(CharType p_char, CharType p_next, const V if (ch->found) { ret.x = ch->advance; } + ret.x += _get_kerning_advance(font, p_char, p_next); return ret; } @@ -332,6 +346,8 @@ float DynamicFontAtSize::draw_char(RID p_canvas_item, const Point2 &p_pos, CharT advance = ch->advance; } + advance += _get_kerning_advance(font, p_char, p_next); + return advance; } diff --git a/scene/resources/dynamic_font.h b/scene/resources/dynamic_font.h index 9afb12cb9ff..b09e74b729a 100644 --- a/scene/resources/dynamic_font.h +++ b/scene/resources/dynamic_font.h @@ -162,6 +162,7 @@ class DynamicFontAtSize : public Reference { const Pair _find_char_with_font(CharType p_char, const Vector> &p_fallbacks) const; Character _make_outline_char(CharType p_char); + float _get_kerning_advance(const DynamicFontAtSize *font, CharType p_char, CharType p_next) const; TexturePosition _find_texture_pos_for_glyph(int p_color_size, Image::Format p_image_format, int p_width, int p_height); Character _bitmap_to_character(FT_Bitmap bitmap, int yofs, int xofs, float advance);