diff --git a/Source/Editor/Windows/Assets/FontWindow.cs b/Source/Editor/Windows/Assets/FontWindow.cs index 0f0bc1630..71f8d800d 100644 --- a/Source/Editor/Windows/Assets/FontWindow.cs +++ b/Source/Editor/Windows/Assets/FontWindow.cs @@ -141,7 +141,6 @@ namespace FlaxEditor.Windows.Assets if (assetOptions != options) { Asset.Options = options; - Asset.Invalidate(); } } diff --git a/Source/Engine/Render2D/Font.cpp b/Source/Engine/Render2D/Font.cpp index ed3093f35..10b4b116f 100644 --- a/Source/Engine/Render2D/Font.cpp +++ b/Source/Engine/Render2D/Font.cpp @@ -112,6 +112,21 @@ void Font::CacheText(const StringView& text) } } +void Font::Invalidate() +{ + ScopeLock lock(_asset->Locker); + + FlushFaceSize(); + const FT_Face face = _asset->GetFTFace(); + ASSERT(face != nullptr); + _height = Convert26Dot6ToRoundedPixel(FT_MulFix(face->height, face->size->metrics.y_scale)); + _hasKerning = FT_HAS_KERNING(face) != 0; + _ascender = Convert26Dot6ToRoundedPixel(face->size->metrics.ascender); + _descender = Convert26Dot6ToRoundedPixel(face->size->metrics.descender); + _lineGap = _height - _ascender + _descender; + _kerningTable.Clear(); +} + void Font::ProcessText(const StringView& text, Array>& outputLines, const TextLayoutOptions& layout) { int32 textLength = text.Length(); @@ -463,8 +478,7 @@ void Font::FlushFaceSize() const { // Set the character size const FT_Face face = _asset->GetFTFace(); - FontOptions options = _asset->GetOptions(); - float size = options.RasterMode == FontRasterMode::MSDF ? options.MSDFSize : _size; + float size = _asset->GetOptions().RasterMode == FontRasterMode::MSDF ? _asset->GetOptions().MSDFSize : _size; const FT_Error error = FT_Set_Char_Size(face, 0, ConvertPixelTo26Dot6(size * FontManager::FontScale), DefaultDPI, DefaultDPI); if (error) { diff --git a/Source/Engine/Render2D/Font.h b/Source/Engine/Render2D/Font.h index f176bb3f3..ace21e64a 100644 --- a/Source/Engine/Render2D/Font.h +++ b/Source/Engine/Render2D/Font.h @@ -228,6 +228,11 @@ public: /// The text witch characters to cache. API_FUNCTION() void CacheText(const StringView& text); + /// + /// Refresh cached metrics. Can be used after changing font asset options. + /// + API_FUNCTION() void Invalidate(); + public: /// /// Processes text to get cached lines for rendering. diff --git a/Source/Engine/Render2D/FontAsset.cpp b/Source/Engine/Render2D/FontAsset.cpp index c16bd56b4..39fe89207 100644 --- a/Source/Engine/Render2D/FontAsset.cpp +++ b/Source/Engine/Render2D/FontAsset.cpp @@ -101,6 +101,26 @@ FontFlags FontAsset::GetStyle() const void FontAsset::SetOptions(const FontOptions& value) { _options = value; + Invalidate(); + + if (_virtualBold) + { + auto options = _options; + options.Flags |= FontFlags::Bold; + _virtualBold->SetOptions(options); + } + if (_virtualItalic) + { + auto options = _options; + options.Flags |= FontFlags::Italic; + _virtualItalic->SetOptions(options); + } + if (_virtualMSDF) + { + auto options = _options; + options.RasterMode = FontRasterMode::MSDF; + _virtualMSDF->SetOptions(options); + } } Font* FontAsset::CreateFont(float size) @@ -218,6 +238,10 @@ void FontAsset::Invalidate() FontManager::Invalidate(entry.Value); _characterCache.Clear(); + + // Refresh cached metrics of all fonts created from this asset + for (auto font : _fonts) + font->Invalidate(); } uint64 FontAsset::GetMemoryUsage() const