Fix textbox height

Fix build error under non-windows platforms
This commit is contained in:
ExMatics HydrogenC
2023-12-01 19:38:15 +08:00
parent 7b63c7016c
commit 95f5e31e48
8 changed files with 85 additions and 13 deletions

View File

@@ -38,8 +38,9 @@ namespace FlaxEditor.GUI
{
Depth = -1;
if (Height < Style.Current.FontMedium.Height)
Height = Style.Current.FontMedium.Height + 4;
var mediumHeight = FallbackTextUtils.GetMaxHeight(Style.Current.FontMedium);
if (Height < mediumHeight)
Height = mediumHeight + 4;
}
/// <inheritdoc />

View File

@@ -9,36 +9,55 @@ struct TextRange;
class Font;
class FontAsset;
/// <summary>
/// Defines a list of fonts that can be used as a fallback, ordered by priority.
/// </summary>
API_CLASS(Sealed, NoSpawn) class FLAXENGINE_API FallbackFonts : public ManagedScriptingObject
{
DECLARE_SCRIPTING_TYPE_NO_SPAWN(FallbackFonts);
private:
/// <summary>
/// The list of fallback fonts, ordered by priority.
/// The first element is reserved for the primary font, fallback fonts starts from the second element.
/// </summary>
Array<FontAsset*> _fontAssets;
// Cache fallback fonts of various sizes
Dictionary<float, Array<Font*>*> _cache;
public:
/// <summary>
/// Initializes a new instance of the <see cref="FallbackFonts"/> class.
/// </summary>
/// <param name="fonts">The fallback font assets.</param>
FallbackFonts(const Array<FontAsset*>& fonts);
/// <summary>
/// Initializes a new instance of the <see cref="FallbackFonts"/> class, exposed for C#.
/// </summary>
/// <param name="fonts">The fallback font assets.</param>
/// <returns>The new instance.</returns>
API_FUNCTION() FORCE_INLINE static FallbackFonts* Create(const Array<FontAsset*>& fonts) {
return New<FallbackFonts>(fonts);
}
/// <summary>
/// Get the parent assets of fallback fonts.
/// </summary>
/// <returns>The font assets.</returns>
API_PROPERTY() FORCE_INLINE Array<FontAsset*>& GetFonts() {
return _fontAssets;
}
/// <summary>
/// Set the fallback fonts.
/// </summary>
/// <param name="val">The parent assets of the new fonts.</param>
API_PROPERTY() FORCE_INLINE void SetFonts(const Array<FontAsset*>& val) {
_fontAssets = val;
}
/// <summary>
/// Combine the primary fonts with the fallback fonts to get a font list
/// Gets the fallback fonts with the given size.
/// </summary>
/// <param name="size">The size.</param>
/// <returns>The generated fonts.</returns>
API_FUNCTION() FORCE_INLINE Array<Font*>& GetFontList(float size) {
Array<Font*>* result;
if (_cache.TryGet(size, result)) {
@@ -61,6 +80,8 @@ public:
/// Gets the index of the fallback font that should be used to render the char
/// </summary>
/// <param name="c">The char.</param>
/// <param name="primaryFont">The primary font.</param>
/// <param name="missing">The number to return if none of the fonts can render.</param>
/// <returns>-1 if char can be rendered with primary font, index if it matches a fallback font. </returns>
API_FUNCTION() FORCE_INLINE int32 GetCharFallbackIndex(Char c, Font* primaryFont = nullptr, int32 missing = -1) {
if (primaryFont && primaryFont->GetAsset()->ContainsChar(c)) {
@@ -81,6 +102,10 @@ public:
return missing;
}
/// <summary>
/// Checks if every font is properly loaded.
/// </summary>
/// <returns>True if every font asset is non-null, otherwise false.</returns>
API_FUNCTION() FORCE_INLINE bool Verify() {
for (int32 i = 0; i < _fontAssets.Count(); i++)
{

View File

@@ -227,5 +227,42 @@ namespace FlaxEngine
return font.GetCharPosition(text, ref textRange, index, ref layout);
}
}
/// <summary>
/// Gets the max font height among the font and all fallback fonts of the same size.
/// </summary>
/// <param name="font">The primary font to use.</param>
/// <param name="fallbacks">The fallback fonts.</param>
/// <returns>The max height.</returns>
public static float GetMaxHeight(Font font, FallbackFonts fallbacks)
{
float height = font.Height;
var fallbackFonts = fallbacks.GetFontList(font.Size);
foreach (var item in fallbackFonts)
{
height = Mathf.Max(height, item.Height);
}
return height;
}
/// <summary>
/// Gets the max font height among the font and all fallback fonts of the same size.
/// </summary>
/// <param name="font">The primary font to use.</param>
/// <param name="useFallback">Whether to enable fallback fonts, uses <see cref="Fallbacks"/> if true.</param>
/// <returns>The max height.</returns>
public static float GetMaxHeight(Font font, bool useFallback = true)
{
if(Fallbacks != null && useFallback)
{
return GetMaxHeight(font, Fallbacks);
}
else
{
return font.Height;
}
}
}
}

View File

@@ -122,7 +122,8 @@ struct TIsPODType<FontLineCache>
};
/// <summary>
/// The font block info generated during text processing.
/// The font block info generated during text processing.
/// A block means a range of text that belongs to the same line and can be rendered with the same font.
/// </summary>
API_STRUCT(NoDefault) struct FontBlockCache
{
@@ -134,7 +135,7 @@ API_STRUCT(NoDefault) struct FontBlockCache
API_FIELD() Float2 Location;
/// <summary>
/// The height of the current block
/// The size of the current block
/// </summary>
API_FIELD() Float2 Size;
@@ -183,7 +184,7 @@ API_STRUCT(NoDefault) struct BlockedTextLineCache
API_FIELD() float MaxAscender;
/// <summary>
/// The index of the font to render with
/// The blocks that belongs to this line
/// </summary>
API_FIELD() Array<FontBlockCache> Blocks;
};

View File

@@ -199,6 +199,13 @@ bool FontAsset::Save(const StringView& path)
#endif
/// <summary>
/// Check if the font contains the glyph of a char
/// </summary>
/// <param name="c">The char to test.</param>
/// <returns>True if the font contains the glyph of the char, otherwise false.</returns>
bool FontAsset::ContainsChar(Char c) const {
return FT_Get_Char_Index(GetFTFace(), c) > 0;
}

View File

@@ -179,7 +179,7 @@ public:
/// </summary>
/// <param name="c">The char to test.</param>
/// <returns>True if the font contains the glyph of the char, otherwise false.</returns>
API_FUNCTION() FORCE_INLINE bool ContainsChar(Char c) const;
API_FUNCTION() bool ContainsChar(Char c) const;
/// <summary>
/// Invalidates all cached dynamic font atlases using this font. Can be used to reload font characters after changing font asset options.

View File

@@ -316,6 +316,7 @@ namespace FlaxEngine.GUI
color = textBlock.Style.ShadowColor;
if (!enabled)
color *= 0.6f;
// We don't need font fallbacks for rich text since the font is user-selected
Render2D.DrawText(font, _text, ref textBlock.Range, color, textBlock.Bounds.Location + textBlock.Style.ShadowOffset, textBlock.Style.CustomMaterial);
}

View File

@@ -117,7 +117,7 @@ namespace FlaxEngine.GUI
return Float2.Zero;
}
height = font.Height / DpiScale;
height = FallbackTextUtils.GetMaxHeight(font) / DpiScale;
return FallbackTextUtils.GetCharPosition(font, _text, index, ref _layout);
}
@@ -171,7 +171,7 @@ namespace FlaxEngine.GUI
{
var leftEdge = FallbackTextUtils.GetCharPosition(font, _text, SelectionLeft, ref _layout);
var rightEdge = FallbackTextUtils.GetCharPosition(font, _text, SelectionRight, ref _layout);
float fontHeight = font.Height / DpiScale;
float fontHeight = FallbackTextUtils.GetMaxHeight(font) / DpiScale;
// Draw selection background
float alpha = Mathf.Min(1.0f, Mathf.Cos(_animateTime * BackgroundSelectedFlashSpeed) * 0.5f + 1.3f);