diff --git a/Source/Editor/Content/Create/CreateFilesDialog.cs b/Source/Editor/Content/Create/CreateFilesDialog.cs index 48e4920bb..d48e878bc 100644 --- a/Source/Editor/Content/Create/CreateFilesDialog.cs +++ b/Source/Editor/Content/Create/CreateFilesDialog.cs @@ -39,7 +39,7 @@ namespace FlaxEditor.Content.Create AnchorPreset = AnchorPresets.HorizontalStretchTop, Offsets = new Margin(0, 0, 0, 40), Parent = this, - Font = new MultiFontReference(Style.Current.FontTitle) + Font = new FontReference(Style.Current.FontTitle) }; var infoLabel = new Label { diff --git a/Source/Editor/Content/Import/ImportFilesDialog.cs b/Source/Editor/Content/Import/ImportFilesDialog.cs index 5a142d0f6..967583cf6 100644 --- a/Source/Editor/Content/Import/ImportFilesDialog.cs +++ b/Source/Editor/Content/Import/ImportFilesDialog.cs @@ -60,7 +60,7 @@ namespace FlaxEditor.Content.Import AnchorPreset = AnchorPresets.HorizontalStretchTop, Offsets = new Margin(0, 0, 0, 40), Parent = this, - Font = new MultiFontReference(Style.Current.FontTitle) + Font = new FontReference(Style.Current.FontTitle) }; var infoLabel = new Label { diff --git a/Source/Editor/CustomEditors/Dedicated/RagdollEditor.cs b/Source/Editor/CustomEditors/Dedicated/RagdollEditor.cs index b6d14d81e..c4b334b3a 100644 --- a/Source/Editor/CustomEditors/Dedicated/RagdollEditor.cs +++ b/Source/Editor/CustomEditors/Dedicated/RagdollEditor.cs @@ -81,7 +81,7 @@ namespace FlaxEditor.CustomEditors.Dedicated // Title var title = new Label(2, 2, width - 4, 23.0f) { - Font = new MultiFontReference(FlaxEngine.GUI.Style.Current.FontLarge), + Font = new FontReference(FlaxEngine.GUI.Style.Current.FontLarge), Text = "Ragdoll Options", Parent = this }; diff --git a/Source/Editor/CustomEditors/Dedicated/ScriptsEditor.cs b/Source/Editor/CustomEditors/Dedicated/ScriptsEditor.cs index fd56422cb..90ae9ae54 100644 --- a/Source/Editor/CustomEditors/Dedicated/ScriptsEditor.cs +++ b/Source/Editor/CustomEditors/Dedicated/ScriptsEditor.cs @@ -43,7 +43,7 @@ namespace FlaxEditor.CustomEditors.Dedicated // Add script button var buttonText = "Add script"; - var textSize = Style.Current.FontMedium.MeasureText(buttonText); + var textSize = Render2D.MeasureText(Style.Current.FontMedium, buttonText); float addScriptButtonWidth = (textSize.X < 60.0f) ? 60.0f : textSize.X + 4; var buttonHeight = (textSize.Y < 18) ? 18 : textSize.Y + 4; _addScriptsButton = new Button diff --git a/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs b/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs index 9627edfd8..b8250918f 100644 --- a/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs +++ b/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs @@ -239,7 +239,7 @@ namespace FlaxEditor.CustomEditors.Dedicated // Title var title = new Label(2, 2, DialogWidth - 4, TitleHeight) { - Font = new MultiFontReference(style.FontLarge), + Font = new FontReference(style.FontLarge), Text = "Anchor Presets", Parent = this }; @@ -247,7 +247,7 @@ namespace FlaxEditor.CustomEditors.Dedicated // Info var info = new Label(0, title.Bottom, DialogWidth, InfoHeight) { - Font = new MultiFontReference(style.FontSmall), + Font = new FontReference(style.FontSmall), Text = "Shift: also set bounds\nControl: also set pivot", Parent = this }; @@ -423,7 +423,7 @@ namespace FlaxEditor.CustomEditors.Dedicated // Set control type button var space = layout.Space(20); var buttonText = "Set Type"; - var textSize = FlaxEngine.GUI.Style.Current.FontMedium.MeasureText(buttonText); + var textSize = Render2D.MeasureText(FlaxEngine.GUI.Style.Current.FontMedium, buttonText); float setTypeButtonWidth = (textSize.X < 60.0f) ? 60.0f : textSize.X + 4; var setTypeButton = new Button { diff --git a/Source/Editor/CustomEditors/Editors/ActorTransformEditor.cs b/Source/Editor/CustomEditors/Editors/ActorTransformEditor.cs index 0cad200fd..e84bf5914 100644 --- a/Source/Editor/CustomEditors/Editors/ActorTransformEditor.cs +++ b/Source/Editor/CustomEditors/Editors/ActorTransformEditor.cs @@ -101,7 +101,7 @@ namespace FlaxEditor.CustomEditors.Editors _linkButton.Clicked += ToggleLink; ToggleEnabled(); SetLinkStyle(); - var textSize = FlaxEngine.GUI.Style.Current.FontMedium.MeasureText(LinkedLabel.Text.Value); + var textSize = Render2D.MeasureText(FlaxEngine.GUI.Style.Current.FontMedium, LinkedLabel.Text.Value); _linkButton.LocalX += textSize.X + 10; LinkedLabel.SetupContextMenu += (label, menu, editor) => { diff --git a/Source/Editor/CustomEditors/Editors/TagEditor.cs b/Source/Editor/CustomEditors/Editors/TagEditor.cs index dbd5d124c..49ac9d937 100644 --- a/Source/Editor/CustomEditors/Editors/TagEditor.cs +++ b/Source/Editor/CustomEditors/Editors/TagEditor.cs @@ -631,7 +631,7 @@ namespace FlaxEditor.CustomEditors.Editors TooltipText = "Edit...", Parent = _label, }; - var textSize = FlaxEngine.GUI.Style.Current.FontMedium.MeasureText(buttonText); + var textSize = Render2D.MeasureText(FlaxEngine.GUI.Style.Current.FontMedium, buttonText); if (textSize.Y > button.Width) button.Width = textSize.Y + 2; diff --git a/Source/Editor/CustomEditors/LayoutElementsContainer.cs b/Source/Editor/CustomEditors/LayoutElementsContainer.cs index 1584e88de..936851b15 100644 --- a/Source/Editor/CustomEditors/LayoutElementsContainer.cs +++ b/Source/Editor/CustomEditors/LayoutElementsContainer.cs @@ -276,7 +276,7 @@ namespace FlaxEditor.CustomEditors public LabelElement Header(string text) { var element = Label(text); - element.Label.Font = new MultiFontReference(Style.Current.FontLarge); + element.Label.Font = new FontReference(Style.Current.FontLarge); return element; } @@ -284,7 +284,7 @@ namespace FlaxEditor.CustomEditors { var element = Header(header.Text); if (header.FontSize != -1) - element.Label.Font = new MultiFontReference(element.Label.Font, header.FontSize); + element.Label.Font = new FontReference(element.Label.Font.Font, header.FontSize); if (header.Color != 0) element.Label.TextColor = Color.FromRGBA(header.Color); return element; diff --git a/Source/Editor/GUI/ColumnDefinition.cs b/Source/Editor/GUI/ColumnDefinition.cs index c6e8f2889..aff1817c3 100644 --- a/Source/Editor/GUI/ColumnDefinition.cs +++ b/Source/Editor/GUI/ColumnDefinition.cs @@ -35,7 +35,7 @@ namespace FlaxEditor.GUI /// /// The title font. /// - public MultiFont TitleFont; + public Font TitleFont; /// /// The column title text color. diff --git a/Source/Editor/GUI/ComboBox.cs b/Source/Editor/GUI/ComboBox.cs index 8e6cf39a0..0417cc7e3 100644 --- a/Source/Editor/GUI/ComboBox.cs +++ b/Source/Editor/GUI/ComboBox.cs @@ -191,7 +191,7 @@ namespace FlaxEditor.GUI /// Gets or sets the font used to draw text. /// [EditorDisplay("Style"), EditorOrder(2000)] - public MultiFontReference Font { get; set; } + public FontReference Font { get; set; } /// /// Gets or sets the color of the text. @@ -273,7 +273,7 @@ namespace FlaxEditor.GUI MaximumItemsInViewCount = 20; var style = Style.Current; - Font = new MultiFontReference(style.FontMedium); + Font = new FontReference(style.FontMedium); TextColor = style.Foreground; BackgroundColor = style.BackgroundNormal; BackgroundColorHighlighted = BackgroundColor; @@ -554,7 +554,7 @@ namespace FlaxEditor.GUI var textRect = new Rectangle(margin, 0, clientRect.Width - boxSize - 2.0f * margin, clientRect.Height); Render2D.PushClip(textRect); var textColor = TextColor; - Render2D.DrawText(Font.GetMultiFont(), text, textRect, enabled ? textColor : textColor * 0.5f, TextAlignment.Near, TextAlignment.Center, TextWrapping.NoWrap, 1.0f, textScale); + Render2D.DrawText(Font.GetFont(), text, textRect, enabled ? textColor : textColor * 0.5f, TextAlignment.Near, TextAlignment.Center, TextWrapping.NoWrap, 1.0f, textScale); Render2D.PopClip(); } diff --git a/Source/Editor/GUI/ContextMenu/ContextMenuButton.cs b/Source/Editor/GUI/ContextMenu/ContextMenuButton.cs index ba3326412..3137de240 100644 --- a/Source/Editor/GUI/ContextMenu/ContextMenuButton.cs +++ b/Source/Editor/GUI/ContextMenu/ContextMenuButton.cs @@ -236,9 +236,9 @@ namespace FlaxEditor.GUI.ContextMenu float width = 20; if (style.FontMedium) { - width += style.FontMedium.MeasureText(Text).X; + width += Render2D.MeasureText(style.FontMedium, Text).X; if (!string.IsNullOrEmpty(ShortKeys)) - width += 40 + style.FontMedium.MeasureText(ShortKeys).X; + width += 40 + Render2D.MeasureText(style.FontMedium, ShortKeys).X; } return Mathf.Max(width, base.MinimumWidth); diff --git a/Source/Editor/GUI/CurveEditor.cs b/Source/Editor/GUI/CurveEditor.cs index ff14cdb24..22deec120 100644 --- a/Source/Editor/GUI/CurveEditor.cs +++ b/Source/Editor/GUI/CurveEditor.cs @@ -317,7 +317,7 @@ namespace FlaxEditor.GUI private Color _contentsColor; private Color _linesColor; private Color _labelsColor; - private MultiFont _labelsFont; + private Font _labelsFont; /// /// The keyframe UI points. diff --git a/Source/Editor/GUI/Docking/DockWindow.cs b/Source/Editor/GUI/Docking/DockWindow.cs index 374885f01..36a4c112e 100644 --- a/Source/Editor/GUI/Docking/DockWindow.cs +++ b/Source/Editor/GUI/Docking/DockWindow.cs @@ -489,7 +489,7 @@ namespace FlaxEditor.GUI.Docking { var style = Style.Current; if (style?.FontMedium != null) - _titleSize = style.FontMedium.MeasureText(_title); + _titleSize = Render2D.MeasureText(style.FontMedium, _title); } base.PerformLayoutBeforeChildren(); diff --git a/Source/Editor/GUI/MainMenu.cs b/Source/Editor/GUI/MainMenu.cs index aadfbf0a7..b313fed9f 100644 --- a/Source/Editor/GUI/MainMenu.cs +++ b/Source/Editor/GUI/MainMenu.cs @@ -76,7 +76,7 @@ namespace FlaxEditor.GUI var windowIcon = FlaxEngine.Content.LoadAsyncInternal(EditorAssets.WindowIcon); FontAsset windowIconsFont = FlaxEngine.Content.LoadAsyncInternal(EditorAssets.WindowIconsFont); - MultiFont iconFont = new MultiFontReference([windowIconsFont], 9).GetMultiFont(); + Font iconFont = windowIconsFont?.CreateFont(9); _window = mainWindow.RootWindow.Window; _window.HitTest += OnHitTest; @@ -108,7 +108,7 @@ namespace FlaxEditor.GUI _closeButton = new Button { Text = ((char)EditorAssets.SegMDL2Icons.ChromeClose).ToString(), - Font = new MultiFontReference(iconFont), + Font = new FontReference(iconFont), BackgroundColor = Color.Transparent, BorderColor = Color.Transparent, BorderColorHighlighted = Color.Transparent, @@ -124,7 +124,7 @@ namespace FlaxEditor.GUI _minimizeButton = new Button { Text = ((char)EditorAssets.SegMDL2Icons.ChromeMinimize).ToString(), - Font = new MultiFontReference(iconFont), + Font = new FontReference(iconFont), BackgroundColor = Color.Transparent, BorderColor = Color.Transparent, BorderColorHighlighted = Color.Transparent, @@ -139,7 +139,7 @@ namespace FlaxEditor.GUI _maximizeButton = new Button { Text = ((char)(_window.IsMaximized ? EditorAssets.SegMDL2Icons.ChromeRestore : EditorAssets.SegMDL2Icons.ChromeMaximize)).ToString(), - Font = new MultiFontReference(iconFont), + Font = new FontReference(iconFont), BackgroundColor = Color.Transparent, BorderColor = Color.Transparent, BorderColorHighlighted = Color.Transparent, diff --git a/Source/Editor/GUI/MainMenuButton.cs b/Source/Editor/GUI/MainMenuButton.cs index 3bc57479c..3440996aa 100644 --- a/Source/Editor/GUI/MainMenuButton.cs +++ b/Source/Editor/GUI/MainMenuButton.cs @@ -103,7 +103,7 @@ namespace FlaxEditor.GUI float width = 18; if (style.FontMedium) - width += style.FontMedium.MeasureText(Text).X; + width += Render2D.MeasureText(style.FontMedium, Text).X; Width = width; } diff --git a/Source/Editor/GUI/NavigationButton.cs b/Source/Editor/GUI/NavigationButton.cs index 6fd17332c..6face21ef 100644 --- a/Source/Editor/GUI/NavigationButton.cs +++ b/Source/Editor/GUI/NavigationButton.cs @@ -68,7 +68,7 @@ namespace FlaxEditor.GUI if (style.FontMedium) { - Width = style.FontMedium.MeasureText(Text).X + 2 * DefaultMargin; + Width = Render2D.MeasureText(style.FontMedium, Text).X + 2 * DefaultMargin; } } } diff --git a/Source/Editor/GUI/Row.cs b/Source/Editor/GUI/Row.cs index 7533dfb17..ab74cbe96 100644 --- a/Source/Editor/GUI/Row.cs +++ b/Source/Editor/GUI/Row.cs @@ -39,8 +39,8 @@ namespace FlaxEditor.GUI { Depth = -1; - if (Height < Style.Current.FontMedium.MaxHeight) - Height = Style.Current.FontMedium.MaxHeight + 4; + if (Height < Style.Current.FontMedium.Height) + Height = Style.Current.FontMedium.Height + 4; } /// diff --git a/Source/Editor/GUI/Timeline/Tracks/MemberTrack.cs b/Source/Editor/GUI/Timeline/Tracks/MemberTrack.cs index 63787df2c..b4433622e 100644 --- a/Source/Editor/GUI/Timeline/Tracks/MemberTrack.cs +++ b/Source/Editor/GUI/Timeline/Tracks/MemberTrack.cs @@ -345,7 +345,7 @@ namespace FlaxEditor.GUI.Timeline.Tracks if (_previewValue != null) { // Based on Track.Draw for track text placement - var left = _xOffset + 16 + Style.Current.FontSmall.MeasureText(Title ?? Name).X; + var left = _xOffset + 16 + Render2D.MeasureText(Style.Current.FontSmall, Title ?? Name).X; if (Icon.IsValid) left += 18; if (IsExpanded) diff --git a/Source/Editor/GUI/ToolStripButton.cs b/Source/Editor/GUI/ToolStripButton.cs index d21fd5689..cf34fd36b 100644 --- a/Source/Editor/GUI/ToolStripButton.cs +++ b/Source/Editor/GUI/ToolStripButton.cs @@ -152,7 +152,7 @@ namespace FlaxEditor.GUI if (hasSprite) width += iconSize; if (!string.IsNullOrEmpty(_text) && style.FontMedium) - width += style.FontMedium.MeasureText(_text).X + (hasSprite ? DefaultMargin : 0); + width += Render2D.MeasureText(style.FontMedium, _text).X + (hasSprite ? DefaultMargin : 0); Width = width; } diff --git a/Source/Editor/GUI/Tree/TreeNode.cs b/Source/Editor/GUI/Tree/TreeNode.cs index acb67ea8f..7b47e5eb6 100644 --- a/Source/Editor/GUI/Tree/TreeNode.cs +++ b/Source/Editor/GUI/Tree/TreeNode.cs @@ -115,7 +115,7 @@ namespace FlaxEditor.GUI.Tree /// Gets or sets the font used to render text. /// [EditorDisplay("Style"), EditorOrder(2000)] - public MultiFontReference TextFont { get; set; } + public FontReference TextFont { get; set; } /// /// Gets or sets the color of the background when tree node is selected. @@ -318,7 +318,7 @@ namespace FlaxEditor.GUI.Tree BackgroundColorSelected = style.BackgroundSelected; BackgroundColorHighlighted = style.BackgroundHighlighted; BackgroundColorSelectedUnfocused = style.LightBackground; - TextFont = new MultiFontReference(style.FontSmall); + TextFont = new FontReference(style.FontSmall); } /// @@ -573,10 +573,10 @@ namespace FlaxEditor.GUI.Tree { if (_textChanged) { - var font = TextFont.GetMultiFont(); + var font = TextFont.GetFont(); if (font) { - _textWidth = font.MeasureText(_text).X; + _textWidth = Render2D.MeasureText(font, _text).X; _textChanged = false; } } @@ -657,7 +657,7 @@ namespace FlaxEditor.GUI.Tree } // Draw text - Render2D.DrawText(TextFont.GetMultiFont(), _text, textRect, _cachedTextColor, TextAlignment.Near, TextAlignment.Center); + Render2D.DrawText(TextFont.GetFont(), _text, textRect, _cachedTextColor, TextAlignment.Near, TextAlignment.Center); // Draw drag and drop effect if (IsDragOver && _tree.DraggedOverNode == this) diff --git a/Source/Editor/Options/InterfaceOptions.cs b/Source/Editor/Options/InterfaceOptions.cs index 91eebe4eb..af432baa7 100644 --- a/Source/Editor/Options/InterfaceOptions.cs +++ b/Source/Editor/Options/InterfaceOptions.cs @@ -171,8 +171,10 @@ namespace FlaxEditor.Options get => _outputLogFont; set { - if (value == null || !value.Font) - _outputLogFont = new FontReference(ConsoleFont, 10); + if (value == null) + _outputLogFont = new FontReference(FlaxEngine.Content.LoadAsyncInternal(EditorAssets.InconsolataRegularFont), 10); + else if (!value.Font) + _outputLogFont.Font = FlaxEngine.Content.LoadAsyncInternal(EditorAssets.InconsolataRegularFont); else _outputLogFont = value; } @@ -234,30 +236,34 @@ namespace FlaxEditor.Options [EditorDisplay("Cook & Run"), EditorOrder(500)] public int NumberOfGameClientsToLaunch = 1; - private static FontAsset[] DefaultFonts => - [FlaxEngine.Content.LoadAsyncInternal(EditorAssets.PrimaryFont), - FlaxEngine.Content.LoadAsyncInternal(EditorAssets.CjkFont)]; + private static FontAsset DefaultFont => FlaxEngine.Content.LoadAsyncInternal(EditorAssets.PrimaryFont); private static FontAsset ConsoleFont => FlaxEngine.Content.LoadAsyncInternal(EditorAssets.PrimaryFont); - private MultiFontReference _titleFont = new MultiFontReference(DefaultFonts, 18); - private MultiFontReference _largeFont = new MultiFontReference(DefaultFonts, 14); - private MultiFontReference _mediumFont = new MultiFontReference(DefaultFonts, 9); - private MultiFontReference _smallFont = new MultiFontReference(DefaultFonts, 9); + private FontReference _titleFont = new FontReference(DefaultFont, 18); + private FontReference _largeFont = new FontReference(DefaultFont, 14); + private FontReference _mediumFont = new FontReference(DefaultFont, 9); + private FontReference _smallFont = new FontReference(DefaultFont, 9); private FontReference _outputLogFont = new FontReference(ConsoleFont, 10); + /// + /// The fallback fonts. + /// + public FontAsset[] Fallbacks = [FlaxEngine.Content.LoadAsyncInternal(EditorAssets.CjkFont)]; /// /// Gets or sets the title font for editor UI. /// [EditorDisplay("Fonts"), EditorOrder(600), Tooltip("The title font for editor UI.")] - public MultiFontReference TitleFont + public FontReference TitleFont { get => _titleFont; set { - if (value == null || !value.Verify()) - _titleFont = new MultiFontReference(DefaultFonts, 18); + if (value == null) + _titleFont = new FontReference(DefaultFont, 18); + else if (!value.Font) + _titleFont.Font = DefaultFont; else _titleFont = value; } @@ -267,13 +273,15 @@ namespace FlaxEditor.Options /// Gets or sets the large font for editor UI. /// [EditorDisplay("Fonts"), EditorOrder(610), Tooltip("The large font for editor UI.")] - public MultiFontReference LargeFont + public FontReference LargeFont { get => _largeFont; set { - if (value == null || !value.Verify()) - _largeFont = new MultiFontReference(DefaultFonts, 14); + if (value == null) + _largeFont = new FontReference(DefaultFont, 14); + else if (!value.Font) + _largeFont.Font = DefaultFont; else _largeFont = value; } @@ -283,13 +291,15 @@ namespace FlaxEditor.Options /// Gets or sets the medium font for editor UI. /// [EditorDisplay("Fonts"), EditorOrder(620), Tooltip("The medium font for editor UI.")] - public MultiFontReference MediumFont + public FontReference MediumFont { get => _mediumFont; set { - if (value == null || !value.Verify()) - _mediumFont = new MultiFontReference(DefaultFonts, 9); + if (value == null) + _mediumFont = new FontReference(DefaultFont, 9); + else if (!value.Font) + _mediumFont.Font = DefaultFont; else _mediumFont = value; } @@ -299,13 +309,15 @@ namespace FlaxEditor.Options /// Gets or sets the small font for editor UI. /// [EditorDisplay("Fonts"), EditorOrder(630), Tooltip("The small font for editor UI.")] - public MultiFontReference SmallFont + public FontReference SmallFont { get => _smallFont; set { - if (value == null || !value.Verify()) - _smallFont = new MultiFontReference(DefaultFonts, 9); + if (value == null) + _smallFont = new FontReference(DefaultFont, 9); + else if (!value.Font) + _smallFont.Font = DefaultFont; else _smallFont = value; } diff --git a/Source/Editor/Options/OptionsModule.cs b/Source/Editor/Options/OptionsModule.cs index d138d7d0d..b13548016 100644 --- a/Source/Editor/Options/OptionsModule.cs +++ b/Source/Editor/Options/OptionsModule.cs @@ -217,12 +217,14 @@ namespace FlaxEditor.Options if (styleName == ThemeOptions.LightDefault) { Style.Current = CreateLightStyle(); - } + } else { Style.Current = CreateDefaultStyle(); } } + + Render2D.Fallbacks = FallbackFonts.Create(Options.Interface.Fallbacks); } /// @@ -259,10 +261,10 @@ namespace FlaxEditor.Options }, // Fonts - FontTitle = options.Interface.TitleFont.GetMultiFont(), - FontLarge = options.Interface.LargeFont.GetMultiFont(), - FontMedium = options.Interface.MediumFont.GetMultiFont(), - FontSmall = options.Interface.SmallFont.GetMultiFont(), + FontTitle = options.Interface.TitleFont.GetFont(), + FontLarge = options.Interface.LargeFont.GetFont(), + FontMedium = options.Interface.MediumFont.GetFont(), + FontSmall = options.Interface.SmallFont.GetFont(), // Icons ArrowDown = Editor.Icons.ArrowDown12, @@ -312,10 +314,10 @@ namespace FlaxEditor.Options ProgressNormal = new Color(0.03f, 0.65f, 0.12f, 1f), // Fonts - FontTitle = options.Interface.TitleFont.GetMultiFont(), - FontLarge = options.Interface.LargeFont.GetMultiFont(), - FontMedium = options.Interface.MediumFont.GetMultiFont(), - FontSmall = options.Interface.SmallFont.GetMultiFont(), + FontTitle = options.Interface.TitleFont.GetFont(), + FontLarge = options.Interface.LargeFont.GetFont(), + FontMedium = options.Interface.MediumFont.GetFont(), + FontSmall = options.Interface.SmallFont.GetFont(), // Icons ArrowDown = Editor.Icons.ArrowDown12, diff --git a/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs b/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs index 621c7c25e..19a995b1e 100644 --- a/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs +++ b/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs @@ -337,7 +337,7 @@ namespace FlaxEditor.Surface.Archetypes _textRect = new Rectangle(Float2.Zero, Size); var style = Style.Current; - var titleSize = style.FontLarge.MeasureText(Title); + var titleSize = Render2D.MeasureText(style.FontLarge, Title); var width = Mathf.Max(100, titleSize.X + 50); Resize(width, 0); titleSize.X += 8.0f; @@ -1402,7 +1402,7 @@ namespace FlaxEditor.Surface.Archetypes { Title = StateTitle; var style = Style.Current; - var titleSize = style.FontLarge.MeasureText(Title); + var titleSize = Render2D.MeasureText(style.FontLarge, Title); var width = Mathf.Max(100, titleSize.X + 50); Resize(width, 0); titleSize.X += 8.0f; diff --git a/Source/Editor/Surface/Archetypes/Animation.TransitionEditor.cs b/Source/Editor/Surface/Archetypes/Animation.TransitionEditor.cs index e1fdb0a42..84c2144b2 100644 --- a/Source/Editor/Surface/Archetypes/Animation.TransitionEditor.cs +++ b/Source/Editor/Surface/Archetypes/Animation.TransitionEditor.cs @@ -39,7 +39,7 @@ namespace FlaxEditor.Surface.Archetypes // Title var title = new Label(2, 2, width - 4, 23.0f) { - Font = new MultiFontReference(Style.Current.FontLarge), + Font = new FontReference(Style.Current.FontLarge), Text = transition.SurfaceName, Parent = this }; diff --git a/Source/Editor/Surface/Archetypes/Animation.cs b/Source/Editor/Surface/Archetypes/Animation.cs index 40a3d2a63..dcf4a9689 100644 --- a/Source/Editor/Surface/Archetypes/Animation.cs +++ b/Source/Editor/Surface/Archetypes/Animation.cs @@ -77,7 +77,7 @@ namespace FlaxEditor.Surface.Archetypes Title = asset?.ShortName ?? "Animation"; var style = Style.Current; - Resize(Mathf.Max(230, style.FontLarge.MeasureText(Title).X + 30), 160); + Resize(Mathf.Max(230, Render2D.MeasureText(style.FontLarge, Title).X + 30), 160); } /// diff --git a/Source/Editor/Surface/Archetypes/BehaviorTree.cs b/Source/Editor/Surface/Archetypes/BehaviorTree.cs index cca6856ae..364dfa1ef 100644 --- a/Source/Editor/Surface/Archetypes/BehaviorTree.cs +++ b/Source/Editor/Surface/Archetypes/BehaviorTree.cs @@ -101,7 +101,7 @@ namespace FlaxEditor.Surface.Archetypes _debugRelevant = Behavior.GetNodeDebugRelevancy(instance, behavior); _debugInfo = Behavior.GetNodeDebugInfo(instance, behavior); if (!string.IsNullOrEmpty(_debugInfo)) - _debugInfoSize = Style.Current.FontSmall.MeasureText(_debugInfo); + _debugInfoSize = Render2D.MeasureText(Style.Current.FontSmall, _debugInfo); } } @@ -488,7 +488,7 @@ namespace FlaxEditor.Surface.Archetypes var height = 0.0f; var titleLabelFont = Style.Current.FontLarge; width = Mathf.Max(width, 100.0f); - width = Mathf.Max(width, titleLabelFont.MeasureText(Title).X + 30); + width = Mathf.Max(width, Render2D.MeasureText(titleLabelFont, Title).X + 30); if (_debugInfoSize.X > 0) { width = Mathf.Max(width, _debugInfoSize.X + 8.0f); diff --git a/Source/Editor/Surface/Archetypes/Function.cs b/Source/Editor/Surface/Archetypes/Function.cs index bc982a510..53950dad2 100644 --- a/Source/Editor/Surface/Archetypes/Function.cs +++ b/Source/Editor/Surface/Archetypes/Function.cs @@ -1407,7 +1407,7 @@ namespace FlaxEditor.Surface.Archetypes // Title var title = new Label(2, 2, width - 4, 23.0f) { - Font = new MultiFontReference(Style.Current.FontLarge), + Font = new FontReference(Style.Current.FontLarge), Text = "Edit function signature", Parent = this }; diff --git a/Source/Editor/Surface/AttributesEditor.cs b/Source/Editor/Surface/AttributesEditor.cs index c9e32e23a..81b11bb68 100644 --- a/Source/Editor/Surface/AttributesEditor.cs +++ b/Source/Editor/Surface/AttributesEditor.cs @@ -83,7 +83,7 @@ namespace FlaxEditor.Surface // Title var title = new Label(2, 2, width - 4, 23.0f) { - Font = new MultiFontReference(Style.Current.FontLarge), + Font = new FontReference(Style.Current.FontLarge), Text = "Edit attributes", Parent = this }; diff --git a/Source/Editor/Surface/ContextMenu/VisjectCM.cs b/Source/Editor/Surface/ContextMenu/VisjectCM.cs index 0624d44b3..5256b3cb7 100644 --- a/Source/Editor/Surface/ContextMenu/VisjectCM.cs +++ b/Source/Editor/Surface/ContextMenu/VisjectCM.cs @@ -141,7 +141,7 @@ namespace FlaxEditor.Surface.ContextMenu }; // Title bar - var titleFontReference = new MultiFontReference(Style.Current.FontLarge); + var titleFontReference = new FontReference(Style.Current.FontLarge); var titleLabel = new Label { Width = Width * 0.5f - 8f, diff --git a/Source/Editor/Surface/ContextMenu/VisjectCMItem.cs b/Source/Editor/Surface/ContextMenu/VisjectCMItem.cs index 207875a92..fd9f0c63b 100644 --- a/Source/Editor/Surface/ContextMenu/VisjectCMItem.cs +++ b/Source/Editor/Surface/ContextMenu/VisjectCMItem.cs @@ -286,7 +286,7 @@ namespace FlaxEditor.Surface.ContextMenu Render2D.DrawText(style.FontSmall, _archetype.Title, textRect, Enabled ? style.Foreground : style.ForegroundDisabled, TextAlignment.Near, TextAlignment.Center); if (_archetype.SubTitle != null) { - var titleLength = style.FontSmall.MeasureText(_archetype.Title).X; + var titleLength = Render2D.MeasureText(style.FontSmall, _archetype.Title).X; var subTitleRect = new Rectangle(textRect.X + titleLength, textRect.Y, textRect.Width - titleLength, textRect.Height); Render2D.DrawText(style.FontSmall, _archetype.SubTitle, subTitleRect, style.ForegroundDisabled, TextAlignment.Near, TextAlignment.Center); } diff --git a/Source/Editor/Surface/Elements/InputBox.cs b/Source/Editor/Surface/Elements/InputBox.cs index 2047bcd1a..2ab286bd9 100644 --- a/Source/Editor/Surface/Elements/InputBox.cs +++ b/Source/Editor/Surface/Elements/InputBox.cs @@ -1428,7 +1428,7 @@ namespace FlaxEditor.Surface.Elements if (_defaultValueEditor != null) { - _defaultValueEditor.Location = new Float2(X + Width + 8 + Style.Current.FontSmall.MeasureText(Text).X, Y); + _defaultValueEditor.Location = new Float2(X + Width + 8 + Render2D.MeasureText(Style.Current.FontSmall, Text).X, Y); } } @@ -1635,7 +1635,7 @@ namespace FlaxEditor.Surface.Elements { if (DefaultValueEditors[i].CanUse(this, ref _currentType)) { - var bounds = new Rectangle(X + Width + 8 + Style.Current.FontSmall.MeasureText(Text).X, Y, 90, Height); + var bounds = new Rectangle(X + Width + 8 + Render2D.MeasureText(Style.Current.FontSmall, Text).X, Y, 90, Height); _editor = DefaultValueEditors[i]; try { diff --git a/Source/Editor/Surface/SurfaceNode.cs b/Source/Editor/Surface/SurfaceNode.cs index b6436e542..60d776102 100644 --- a/Source/Editor/Surface/SurfaceNode.cs +++ b/Source/Editor/Surface/SurfaceNode.cs @@ -200,7 +200,7 @@ namespace FlaxEditor.Surface continue; if (child is InputBox inputBox) { - var boxWidth = boxLabelFont.MeasureText(inputBox.Text).X + 20; + var boxWidth = Render2D.MeasureText(boxLabelFont, inputBox.Text).X + 20; if (inputBox.DefaultValueEditor != null) boxWidth += inputBox.DefaultValueEditor.Width + 4; leftWidth = Mathf.Max(leftWidth, boxWidth); @@ -208,7 +208,7 @@ namespace FlaxEditor.Surface } else if (child is OutputBox outputBox) { - rightWidth = Mathf.Max(rightWidth, boxLabelFont.MeasureText(outputBox.Text).X + 20); + rightWidth = Mathf.Max(rightWidth, Render2D.MeasureText(boxLabelFont, outputBox.Text).X + 20); rightHeight = Mathf.Max(rightHeight, outputBox.Archetype.Position.Y - Constants.NodeMarginY - Constants.NodeHeaderSize + 20.0f); } else if (child is Control control) @@ -226,7 +226,7 @@ namespace FlaxEditor.Surface } } width = Mathf.Max(width, leftWidth + rightWidth + 10); - width = Mathf.Max(width, titleLabelFont.MeasureText(Title).X + 30); + width = Mathf.Max(width, Render2D.MeasureText(titleLabelFont, Title).X + 30); height = Mathf.Max(height, Mathf.Max(leftHeight, rightHeight)); Resize(width, height); } diff --git a/Source/Editor/Tools/Foliage/FoliageTab.cs b/Source/Editor/Tools/Foliage/FoliageTab.cs index 1badb8c0c..58cf6f204 100644 --- a/Source/Editor/Tools/Foliage/FoliageTab.cs +++ b/Source/Editor/Tools/Foliage/FoliageTab.cs @@ -148,7 +148,7 @@ namespace FlaxEditor.Tools.Foliage Parent = _noFoliagePanel, Enabled = false }; - var textSize = Style.Current.FontMedium.MeasureText(buttonText); + var textSize = Render2D.MeasureText(Style.Current.FontMedium, buttonText); if (_createNewFoliage.Width < textSize.X) { _createNewFoliage.LocalX -= (textSize.X - _createNewFoliage.Width) / 2; diff --git a/Source/Editor/Tools/Terrain/CarveTab.cs b/Source/Editor/Tools/Terrain/CarveTab.cs index 4ff85ca23..12c9fd148 100644 --- a/Source/Editor/Tools/Terrain/CarveTab.cs +++ b/Source/Editor/Tools/Terrain/CarveTab.cs @@ -106,7 +106,7 @@ namespace FlaxEditor.Tools.Terrain Parent = _noTerrainPanel, Enabled = false }; - var textSize = Style.Current.FontMedium.MeasureText(buttonText); + var textSize = Render2D.MeasureText(Style.Current.FontMedium, buttonText); if (_createTerrainButton.Width < textSize.X) { _createTerrainButton.LocalX -= (textSize.X - _createTerrainButton.Width) / 2; diff --git a/Source/Editor/Tools/Terrain/CreateTerrainDialog.cs b/Source/Editor/Tools/Terrain/CreateTerrainDialog.cs index cba52283d..252891d44 100644 --- a/Source/Editor/Tools/Terrain/CreateTerrainDialog.cs +++ b/Source/Editor/Tools/Terrain/CreateTerrainDialog.cs @@ -96,7 +96,7 @@ namespace FlaxEditor.Tools.Terrain AnchorPreset = AnchorPresets.HorizontalStretchTop, Offsets = new Margin(0, 0, 0, 40), Parent = this, - Font = new MultiFontReference(Style.Current.FontTitle) + Font = new FontReference(Style.Current.FontTitle) }; var infoLabel = new Label { diff --git a/Source/Editor/Utilities/TextRenderUtils.cs b/Source/Editor/Utilities/TextRenderUtils.cs new file mode 100644 index 000000000..d7b79ae81 --- /dev/null +++ b/Source/Editor/Utilities/TextRenderUtils.cs @@ -0,0 +1,108 @@ +using FlaxEngine.GUI; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlaxEngine +{ + public static class TextRenderUtils + { + /// + /// Draws a text using the global fallback defined in styles. + /// + /// The font to use. + /// The text to render. + /// The size and position of the area in which the text is drawn. + /// The text color. + /// The horizontal alignment of the text in a layout rectangle. + /// The vertical alignment of the text in a layout rectangle. + /// Describes how wrap text inside a layout rectangle. + /// The scale for distance one baseline from another. Default is 1. + /// The text drawing scale. Default is 1. + public static void DrawTextWithFallback(Font font, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f) + { + var layout = new TextLayoutOptions + { + Bounds = layoutRect, + HorizontalAlignment = horizontalAlignment, + VerticalAlignment = verticalAlignment, + TextWrapping = textWrapping, + Scale = scale, + BaseLinesGapScale = baseLinesGapScale, + }; + + + Render2D.DrawText(font, Style.Current.Fallbacks, text, color, ref layout); + } + + /// + /// Draws a text using the global fallback defined in styles. Given material must have GUI domain and a public parameter named Font (texture parameter used for a font atlas sampling). + /// + /// The font to use. + /// Custom material for font characters rendering. It must contain texture parameter named Font used to sample font texture. + /// The text to render. + /// The size and position of the area in which the text is drawn. + /// The text color. + /// The horizontal alignment of the text in a layout rectangle. + /// The vertical alignment of the text in a layout rectangle. + /// Describes how wrap text inside a layout rectangle. + /// The scale for distance one baseline from another. Default is 1. + /// The text drawing scale. Default is 1. + public static void DrawTextWithFallback(Font font, MaterialBase customMaterial, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f) + { + var layout = new TextLayoutOptions + { + Bounds = layoutRect, + HorizontalAlignment = horizontalAlignment, + VerticalAlignment = verticalAlignment, + TextWrapping = textWrapping, + Scale = scale, + BaseLinesGapScale = baseLinesGapScale, + }; + + Render2D.DrawText(font, Style.Current.Fallbacks, text, color, ref layout, customMaterial); + } + + public static Float2 MeasureTextWithFallback(Font font, string text) + { + return font.MeasureText(Style.Current.Fallbacks, text); + } + + public static Float2 MeasureTextWithFallback(Font font, string text, ref TextRange textRange) + { + return font.MeasureText(Style.Current.Fallbacks, text, ref textRange); + } + + public static Float2 MeasureTextWithFallback(Font font, string text, ref TextLayoutOptions layout) + { + return font.MeasureText(Style.Current.Fallbacks, text, ref layout); + } + + public static Float2 MeasureTextWithFallback(Font font, string text, ref TextRange textRange, ref TextLayoutOptions layout) + { + return font.MeasureText(Style.Current.Fallbacks, text, ref textRange, ref layout); + } + + public static Float2 GetCharPositionWithFallback(Font font, string text, int index) + { + return font.GetCharPosition(Style.Current.Fallbacks, text, index); + } + + public static Float2 GetCharPositionWithFallback(Font font, string text, ref TextRange textRange, int index) + { + return font.GetCharPosition(Style.Current.Fallbacks, text, ref textRange, index); + } + + public static Float2 GetCharPositionWithFallback(Font font, string text, int index, ref TextLayoutOptions layout) + { + return font.GetCharPosition(Style.Current.Fallbacks, text, index, ref layout); + } + + public static Float2 GetCharPositionWithFallback(Font font, string text, ref TextRange textRange, int index, ref TextLayoutOptions layout) + { + return font.GetCharPosition(Style.Current.Fallbacks, text, ref textRange, index, ref layout); + } + } +} diff --git a/Source/Editor/Viewport/EditorViewport.cs b/Source/Editor/Viewport/EditorViewport.cs index c49392d01..77a677c0c 100644 --- a/Source/Editor/Viewport/EditorViewport.cs +++ b/Source/Editor/Viewport/EditorViewport.cs @@ -548,9 +548,9 @@ namespace FlaxEditor.Viewport #region Camera settings widget var largestText = "Relative Panning"; - var textSize = Style.Current.FontMedium.MeasureText(largestText); + var textSize = Render2D.MeasureText(Style.Current.FontMedium, largestText); var xLocationForExtras = textSize.X + 5; - var cameraSpeedTextWidth = Style.Current.FontMedium.MeasureText("0.00").X; + var cameraSpeedTextWidth = Render2D.MeasureText(Style.Current.FontMedium, "0.00").X; // Camera Settings Widget _cameraWidget = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperRight); @@ -801,7 +801,7 @@ namespace FlaxEditor.Viewport #region View mode widget largestText = "Brightness"; - textSize = Style.Current.FontMedium.MeasureText(largestText); + textSize = Render2D.MeasureText(Style.Current.FontMedium, largestText); xLocationForExtras = textSize.X + 5; var viewMode = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperLeft); diff --git a/Source/Editor/Viewport/Widgets/ViewportWidgetButton.cs b/Source/Editor/Viewport/Widgets/ViewportWidgetButton.cs index fe73048fd..250790621 100644 --- a/Source/Editor/Viewport/Widgets/ViewportWidgetButton.cs +++ b/Source/Editor/Viewport/Widgets/ViewportWidgetButton.cs @@ -164,7 +164,7 @@ namespace FlaxEditor.Viewport.Widgets var style = Style.Current; if (style != null && style.FontMedium) - Width = CalculateButtonWidth(_forcedTextWidth > 0.0f ? _forcedTextWidth : style.FontMedium.MeasureText(_text).X, Icon.IsValid); + Width = CalculateButtonWidth(_forcedTextWidth > 0.0f ? _forcedTextWidth : Render2D.MeasureText(style.FontMedium, _text).X, Icon.IsValid); } } } diff --git a/Source/Editor/Windows/AboutDialog.cs b/Source/Editor/Windows/AboutDialog.cs index 1a81a9421..ac887dadf 100644 --- a/Source/Editor/Windows/AboutDialog.cs +++ b/Source/Editor/Windows/AboutDialog.cs @@ -41,7 +41,7 @@ namespace FlaxEditor.Windows var nameLabel = new Label(icon.Right + 10, icon.Top, 200, 34) { Text = "Flax Engine", - Font = new MultiFontReference(Style.Current.FontTitle), + Font = new FontReference(Style.Current.FontTitle), HorizontalAlignment = TextAlignment.Near, VerticalAlignment = TextAlignment.Center, Parent = this @@ -54,7 +54,7 @@ namespace FlaxEditor.Windows Parent = this }; var buttonText = "Copy version info"; - var fontSize = Style.Current.FontMedium.MeasureText(buttonText); + var fontSize = Render2D.MeasureText(Style.Current.FontMedium, buttonText); var copyVersionButton = new Button(Width - fontSize.X - 8, 6, fontSize.X + 4, 20) { Text = buttonText, diff --git a/Source/Editor/Windows/Assets/FontWindow.cs b/Source/Editor/Windows/Assets/FontWindow.cs index 2e2e14d99..bc91ca5e2 100644 --- a/Source/Editor/Windows/Assets/FontWindow.cs +++ b/Source/Editor/Windows/Assets/FontWindow.cs @@ -144,7 +144,7 @@ namespace FlaxEditor.Windows.Assets protected override void OnAssetLinked() { Asset.WaitForLoaded(); - _textPreview.Font = new MultiFontReference([Asset], 30); + _textPreview.Font = new FontReference(Asset, 30); _inputText.Text = string.Format("This is a sample text using font {0}.", Asset.FamilyName); var options = Asset.Options; _proxy.Set(ref options); diff --git a/Source/Editor/Windows/ContentWindow.Search.cs b/Source/Editor/Windows/ContentWindow.Search.cs index f29dc0bd6..a1072d158 100644 --- a/Source/Editor/Windows/ContentWindow.Search.cs +++ b/Source/Editor/Windows/ContentWindow.Search.cs @@ -57,7 +57,7 @@ namespace FlaxEditor.Windows var textRect = new Rectangle(margin, 0, clientRect.Width - boxSize - 2.0f * margin, clientRect.Height); Render2D.PushClip(textRect); var textColor = TextColor; - Render2D.DrawText(Font.GetMultiFont(), "View", textRect, enabled ? textColor : textColor * 0.5f, TextAlignment.Near, TextAlignment.Center, TextWrapping.NoWrap, 1.0f, textScale); + Render2D.DrawText(Font.GetFont(), "View", textRect, enabled ? textColor : textColor * 0.5f, TextAlignment.Near, TextAlignment.Center, TextWrapping.NoWrap, 1.0f, textScale); Render2D.PopClip(); // Arrow diff --git a/Source/Editor/Windows/PluginsWindow.cs b/Source/Editor/Windows/PluginsWindow.cs index d6c14f4e4..4dfa0ed08 100644 --- a/Source/Editor/Windows/PluginsWindow.cs +++ b/Source/Editor/Windows/PluginsWindow.cs @@ -79,7 +79,7 @@ namespace FlaxEditor.Windows HorizontalAlignment = TextAlignment.Near, AnchorPreset = AnchorPresets.HorizontalStretchTop, Text = desc.Name, - Font = new MultiFontReference(Style.Current.FontLarge), + Font = new FontReference(Style.Current.FontLarge), Parent = this, Bounds = new Rectangle(tmp1, margin, Width - tmp1 - margin, 28), }; @@ -120,9 +120,9 @@ namespace FlaxEditor.Windows url = desc.HomepageUrl; else if (!string.IsNullOrEmpty(desc.RepositoryUrl)) url = desc.RepositoryUrl; - versionLabel.Font.ForEach(x => x.Font.WaitForLoaded()); - var font = versionLabel.Font.GetMultiFont(); - var authorWidth = font.MeasureText(desc.Author).X + 8; + versionLabel.Font.Font.WaitForLoaded(); + var font = versionLabel.Font.GetFont(); + var authorWidth = Render2D.MeasureText(font, desc.Author).X + 8; var authorLabel = new ClickableLabel { HorizontalAlignment = TextAlignment.Far, @@ -392,7 +392,6 @@ namespace FlaxEditor.Windows } Editor.Log("Plugin project has been cloned."); - try { // Start git submodule clone diff --git a/Source/Editor/Windows/Profiler/Timeline.cs b/Source/Editor/Windows/Profiler/Timeline.cs index 917647f23..00365f74a 100644 --- a/Source/Editor/Windows/Profiler/Timeline.cs +++ b/Source/Editor/Windows/Profiler/Timeline.cs @@ -86,7 +86,7 @@ namespace FlaxEditor.Windows.Profiler Render2D.DrawRectangle(bounds, color * 0.5f); if (_nameLength < 0 && style.FontMedium) - _nameLength = style.FontMedium.MeasureText(_name).X; + _nameLength = Render2D.MeasureText(style.FontMedium, _name).X; if (_nameLength < bounds.Width + 4) { diff --git a/Source/Engine/Render2D/FallbackFonts.h b/Source/Engine/Render2D/FallbackFonts.h index 4b97e1c11..5861354fc 100644 --- a/Source/Engine/Render2D/FallbackFonts.h +++ b/Source/Engine/Render2D/FallbackFonts.h @@ -39,7 +39,7 @@ public: /// /// Combine the primary fonts with the fallback fonts to get a font list /// - API_PROPERTY() FORCE_INLINE Array& GetFontList(float size) { + API_FUNCTION() FORCE_INLINE Array& GetFontList(float size) { Array* result; if (_cache.TryGet(size, result)) { return *result; diff --git a/Source/Engine/Render2D/MultiFontReference.cs b/Source/Engine/Render2D/MultiFontReference.cs deleted file mode 100644 index dd3f4d179..000000000 --- a/Source/Engine/Render2D/MultiFontReference.cs +++ /dev/null @@ -1,74 +0,0 @@ - - -using System.Collections.Generic; -using System.Linq; - -namespace FlaxEngine -{ - /// - /// Reference to multiple font references - /// - public class MultiFontReference : List - { - public MultiFontReference() - { - _cachedFont = null; - } - - public MultiFontReference(IEnumerable other) - { - AddRange(other); - _cachedFont = null; - } - - public MultiFontReference(MultiFontReference other) - { - AddRange(other); - _cachedFont = other._cachedFont; - } - - public MultiFontReference(MultiFontReference other, float size) - { - AddRange(other.Select(x => new FontReference(x) { Size = size })); - _cachedFont = null; - } - - public MultiFontReference(MultiFont other) - { - AddRange(other.Fonts.Select(x => new FontReference(x))); - _cachedFont = other; - } - - public MultiFontReference(FontAsset[] assets, float size) - { - AddRange(assets.Select(x => new FontReference(x, size))); - _cachedFont = null; - } - - [EditorOrder(0), Tooltip("The font asset to use as characters source.")] - public MultiFont GetMultiFont() - { - if (_cachedFont) - return _cachedFont; - var fontList = this.Where(x => x.Font).Select(x => x.GetFont()).ToArray(); - _cachedFont = MultiFont.Create(fontList); - return _cachedFont; - } - - public bool Verify() - { - foreach (var i in this) - { - if (!i.Font) - { - return false; - } - } - - return true; - } - - [NoSerialize] - private MultiFont _cachedFont; - } -} diff --git a/Source/Engine/Render2D/Render2D.cs b/Source/Engine/Render2D/Render2D.cs index cad8380ab..b7158ccc7 100644 --- a/Source/Engine/Render2D/Render2D.cs +++ b/Source/Engine/Render2D/Render2D.cs @@ -1,11 +1,17 @@ // Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. +using FlaxEngine.GUI; using System; namespace FlaxEngine { partial class Render2D { + public static FallbackFonts Fallbacks + { + get; set; + } = null; + /// /// Pushes transformation layer. /// @@ -111,7 +117,8 @@ namespace FlaxEngine /// Describes how wrap text inside a layout rectangle. /// The scale for distance one baseline from another. Default is 1. /// The text drawing scale. Default is 1. - public static void DrawText(Font font, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f) + /// Whether to use fallback fonts for chars not renderable by the font. + public static void DrawText(Font font, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f, bool useFallback = true) { var layout = new TextLayoutOptions { @@ -122,7 +129,15 @@ namespace FlaxEngine Scale = scale, BaseLinesGapScale = baseLinesGapScale, }; - DrawText(font, text, color, ref layout); + + if (useFallback && Fallbacks != null) + { + DrawText(font, Fallbacks, text, color, ref layout); + } + else + { + DrawText(font, text, color, ref layout); + } } /// @@ -138,7 +153,8 @@ namespace FlaxEngine /// Describes how wrap text inside a layout rectangle. /// The scale for distance one baseline from another. Default is 1. /// The text drawing scale. Default is 1. - public static void DrawText(Font font, MaterialBase customMaterial, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f) + /// Whether to use fallback fonts for chars not renderable by the font. + public static void DrawText(Font font, MaterialBase customMaterial, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f, bool useFallback = true) { var layout = new TextLayoutOptions { @@ -149,63 +165,63 @@ namespace FlaxEngine Scale = scale, BaseLinesGapScale = baseLinesGapScale, }; - DrawText(font, text, color, ref layout, customMaterial); + + if (useFallback && Fallbacks != null) + { + DrawText(font, Fallbacks, text, color, ref layout, customMaterial); + } + else + { + DrawText(font, text, color, ref layout, customMaterial); + } } - /// - /// Draws a text. - /// - /// The fonts to use, ordered by priority. - /// The text to render. - /// The size and position of the area in which the text is drawn. - /// The text color. - /// The horizontal alignment of the text in a layout rectangle. - /// The vertical alignment of the text in a layout rectangle. - /// Describes how wrap text inside a layout rectangle. - /// The scale for distance one baseline from another. Default is 1. - /// The text drawing scale. Default is 1. - public static void DrawText(MultiFont fonts, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f) + public static Float2 MeasureText(Font font, string text, bool useFallback = true) { - var layout = new TextLayoutOptions + if (useFallback && Fallbacks != null) { - Bounds = layoutRect, - HorizontalAlignment = horizontalAlignment, - VerticalAlignment = verticalAlignment, - TextWrapping = textWrapping, - Scale = scale, - BaseLinesGapScale = baseLinesGapScale, - }; - - - DrawText(fonts, text, color, ref layout); + return font.MeasureText(Style.Current.Fallbacks, text); + } + else + { + return font.MeasureText(text); + } } - /// - /// Draws a text using a custom material shader. Given material must have GUI domain and a public parameter named Font (texture parameter used for a font atlas sampling). - /// - /// The fonts to use, ordered by priority. - /// Custom material for font characters rendering. It must contain texture parameter named Font used to sample font texture. - /// The text to render. - /// The size and position of the area in which the text is drawn. - /// The text color. - /// The horizontal alignment of the text in a layout rectangle. - /// The vertical alignment of the text in a layout rectangle. - /// Describes how wrap text inside a layout rectangle. - /// The scale for distance one baseline from another. Default is 1. - /// The text drawing scale. Default is 1. - public static void DrawText(MultiFont fonts, MaterialBase customMaterial, string text, Rectangle layoutRect, Color color, TextAlignment horizontalAlignment = TextAlignment.Near, TextAlignment verticalAlignment = TextAlignment.Near, TextWrapping textWrapping = TextWrapping.NoWrap, float baseLinesGapScale = 1.0f, float scale = 1.0f) + public static Float2 MeasureText(Font font, string text, ref TextRange textRange, bool useFallback = true) { - var layout = new TextLayoutOptions + if (useFallback && Fallbacks != null) { - Bounds = layoutRect, - HorizontalAlignment = horizontalAlignment, - VerticalAlignment = verticalAlignment, - TextWrapping = textWrapping, - Scale = scale, - BaseLinesGapScale = baseLinesGapScale, - }; + return font.MeasureText(Style.Current.Fallbacks, text, ref textRange); + } + else + { + return font.MeasureText(text, ref textRange); + } + } - DrawText(fonts, text, color, ref layout, customMaterial); + public static Float2 MeasureText(Font font, string text, ref TextLayoutOptions layout, bool useFallback = true) + { + if (useFallback && Fallbacks != null) + { + return font.MeasureText(Style.Current.Fallbacks, text, ref layout); + } + else + { + return font.MeasureText(text, ref layout); + } + } + + public static Float2 MeasureText(Font font, string text, ref TextRange textRange, ref TextLayoutOptions layout, bool useFallback = true) + { + if (useFallback && Fallbacks != null) + { + return font.MeasureText(Style.Current.Fallbacks, text, ref textRange, ref layout); + } + else + { + return font.MeasureText(text, ref textRange, ref layout); + } } /// diff --git a/Source/Engine/Scripting/Scripting.cs b/Source/Engine/Scripting/Scripting.cs index bc800f88a..a4cfd76b1 100644 --- a/Source/Engine/Scripting/Scripting.cs +++ b/Source/Engine/Scripting/Scripting.cs @@ -294,12 +294,12 @@ namespace FlaxEngine style.DragWindow = style.BackgroundSelected * 0.7f; // Use optionally bundled default font (matches Editor) - FontAsset[] defaultFont = [Content.LoadAsyncInternal("Editor/Fonts/Roboto-Regular"), Content.LoadAsyncInternal("Editor/Fonts/NotoSansSC-Regular")]; + FontAsset defaultFont = Content.LoadAsyncInternal("Editor/Fonts/Roboto-Regular"); - style.FontTitle = new MultiFontReference(defaultFont, 18).GetMultiFont(); - style.FontLarge = new MultiFontReference(defaultFont, 14).GetMultiFont(); - style.FontMedium = new MultiFontReference(defaultFont, 9).GetMultiFont(); - style.FontSmall = new MultiFontReference(defaultFont, 9).GetMultiFont(); + style.FontTitle = new FontReference(defaultFont, 18).GetFont(); + style.FontLarge = new FontReference(defaultFont, 14).GetFont(); + style.FontMedium = new FontReference(defaultFont, 9).GetFont(); + style.FontSmall = new FontReference(defaultFont, 9).GetFont(); Style.Current = style; } diff --git a/Source/Engine/UI/GUI/Common/Button.cs b/Source/Engine/UI/GUI/Common/Button.cs index f21a29191..756501c5e 100644 --- a/Source/Engine/UI/GUI/Common/Button.cs +++ b/Source/Engine/UI/GUI/Common/Button.cs @@ -23,7 +23,7 @@ namespace FlaxEngine.GUI /// /// The font. /// - protected MultiFontReference _font; + protected FontReference _font; /// /// The text. @@ -44,7 +44,7 @@ namespace FlaxEngine.GUI /// Gets or sets the font used to draw button text. /// [EditorDisplay("Text Style"), EditorOrder(2022), ExpandGroups] - public MultiFontReference Font + public FontReference Font { get => _font; set => _font = value; @@ -156,7 +156,7 @@ namespace FlaxEngine.GUI var style = Style.Current; if (style != null) { - _font = new MultiFontReference(style.FontMedium); + _font = new FontReference(style.FontMedium); TextColor = style.Foreground; BackgroundColor = style.BackgroundNormal; BorderColor = style.BorderNormal; @@ -262,7 +262,7 @@ namespace FlaxEngine.GUI Render2D.DrawRectangle(clientRect, borderColor, BorderThickness); // Draw text - Render2D.DrawText(_font?.GetMultiFont(), TextMaterial, _text, clientRect, textColor, TextAlignment.Center, TextAlignment.Center); + Render2D.DrawText(_font?.GetFont(), TextMaterial, _text, clientRect, textColor, TextAlignment.Center, TextAlignment.Center); } /// diff --git a/Source/Engine/UI/GUI/Common/Dropdown.cs b/Source/Engine/UI/GUI/Common/Dropdown.cs index 2681aac1b..c9fef131c 100644 --- a/Source/Engine/UI/GUI/Common/Dropdown.cs +++ b/Source/Engine/UI/GUI/Common/Dropdown.cs @@ -278,7 +278,7 @@ namespace FlaxEngine.GUI /// Gets or sets the font used to draw text. /// [EditorDisplay("Text Style"), EditorOrder(2021)] - public MultiFontReference Font { get; set; } + public FontReference Font { get; set; } /// /// Gets or sets the custom material used to render the text. It must has domain set to GUI and have a public texture parameter named Font used to sample font atlas texture with font characters data. @@ -359,7 +359,7 @@ namespace FlaxEngine.GUI : base(0, 0, 120, 18.0f) { var style = Style.Current; - Font = new MultiFontReference(style.FontMedium); + Font = new FontReference(style.FontMedium); TextColor = style.Foreground; BackgroundColor = style.BackgroundNormal; BackgroundColorHighlighted = BackgroundColor; @@ -476,7 +476,7 @@ namespace FlaxEngine.GUI var font = Font.GetFont(); for (int i = 0; i < _items.Count; i++) { - itemsWidth = Mathf.Max(itemsWidth, itemsMargin + 4 + font.MeasureText(_items[i]).X); + itemsWidth = Mathf.Max(itemsWidth, itemsMargin + 4 + Render2D.MeasureText(font, _items[i]).X); } */ var itemsWidth = Width; @@ -674,7 +674,7 @@ namespace FlaxEngine.GUI var textRect = new Rectangle(margin, 0, clientRect.Width - boxSize - 2.0f * margin, clientRect.Height); Render2D.PushClip(textRect); var textColor = TextColor; - Render2D.DrawText(Font.GetMultiFont(), FontMaterial, _items[_selectedIndex], textRect, enabled ? textColor : textColor * 0.5f, TextAlignment.Near, TextAlignment.Center); + Render2D.DrawText(Font.GetFont(), FontMaterial, _items[_selectedIndex], textRect, enabled ? textColor : textColor * 0.5f, TextAlignment.Near, TextAlignment.Center); Render2D.PopClip(); } diff --git a/Source/Engine/UI/GUI/Common/Label.cs b/Source/Engine/UI/GUI/Common/Label.cs index ccc1cc190..d7147d3ab 100644 --- a/Source/Engine/UI/GUI/Common/Label.cs +++ b/Source/Engine/UI/GUI/Common/Label.cs @@ -26,7 +26,7 @@ namespace FlaxEngine.GUI /// /// The font. /// - protected MultiFontReference _font; + protected FontReference _font; /// /// Gets or sets the text. @@ -86,7 +86,7 @@ namespace FlaxEngine.GUI /// Gets or sets the font. /// [EditorDisplay("Text Style"), EditorOrder(2024)] - public MultiFontReference Font + public FontReference Font { get => _font; set @@ -192,7 +192,7 @@ namespace FlaxEngine.GUI { AutoFocus = false; var style = Style.Current; - Font = new MultiFontReference(style.FontMedium); + Font = new FontReference(style.FontMedium); TextColor = style.Foreground; TextColorHighlighted = style.Foreground; } @@ -203,7 +203,7 @@ namespace FlaxEngine.GUI { AutoFocus = false; var style = Style.Current; - Font = new MultiFontReference(style.FontMedium); + Font = new FontReference(style.FontMedium); TextColor = style.Foreground; TextColorHighlighted = style.Foreground; } @@ -235,7 +235,7 @@ namespace FlaxEngine.GUI } } - Render2D.DrawText(_font.GetMultiFont(), Material, _text, rect, color, hAlignment, wAlignment, Wrapping, BaseLinesGapScale, scale); + Render2D.DrawText(_font.GetFont(), Material, _text, rect, color, hAlignment, wAlignment, Wrapping, BaseLinesGapScale, scale); if (ClipText) Render2D.PopClip(); @@ -246,7 +246,7 @@ namespace FlaxEngine.GUI { if (_autoWidth || _autoHeight || _autoFitText) { - var font = _font.GetMultiFont(); + var font = _font.GetFont(); if (font) { // Calculate text size @@ -256,7 +256,7 @@ namespace FlaxEngine.GUI layout.Bounds.Size.X = Width - Margin.Width; else if (_autoWidth && !_autoHeight) layout.Bounds.Size.Y = Height - Margin.Height; - _textSize = font.MeasureText(_text, ref layout); + _textSize = Render2D.MeasureText(font, _text, ref layout); _textSize.Y *= BaseLinesGapScale; // Check if size is controlled via text diff --git a/Source/Engine/UI/GUI/Common/RichTextBox.cs b/Source/Engine/UI/GUI/Common/RichTextBox.cs index f8ac7d353..514878b37 100644 --- a/Source/Engine/UI/GUI/Common/RichTextBox.cs +++ b/Source/Engine/UI/GUI/Common/RichTextBox.cs @@ -46,7 +46,7 @@ namespace FlaxEngine.GUI var style = Style.Current; _textStyle = new TextBlockStyle { - Font = new FontReference(style.FontMedium.Fonts.First()), + Font = new FontReference(style.FontMedium), Color = style.Foreground, BackgroundSelectedBrush = new SolidColorBrush(style.BackgroundSelected), }; diff --git a/Source/Engine/UI/GUI/Common/TextBox.cs b/Source/Engine/UI/GUI/Common/TextBox.cs index d9c7c1a80..a32f3a76c 100644 --- a/Source/Engine/UI/GUI/Common/TextBox.cs +++ b/Source/Engine/UI/GUI/Common/TextBox.cs @@ -1,5 +1,6 @@ // Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. +using System.Drawing; using System.Linq; namespace FlaxEngine.GUI @@ -40,7 +41,7 @@ namespace FlaxEngine.GUI /// Gets or sets the font. /// [EditorDisplay("Text Style"), EditorOrder(2024)] - public MultiFontReference Font { get; set; } + public FontReference Font { get; set; } /// /// Gets or sets the custom material used to render the text. It must has domain set to GUI and have a public texture parameter named Font used to sample font atlas texture with font characters data. @@ -90,7 +91,7 @@ namespace FlaxEngine.GUI _layout.Bounds = new Rectangle(DefaultMargin, 1, Width - 2 * DefaultMargin, Height - 2); var style = Style.Current; - Font = new MultiFontReference(style.FontMedium); + Font = new FontReference(style.FontMedium); TextColor = style.Foreground; WatermarkTextColor = style.ForegroundDisabled; SelectionColor = style.BackgroundSelected; @@ -99,33 +100,33 @@ namespace FlaxEngine.GUI /// public override Float2 GetTextSize() { - var font = Font.GetMultiFont(); + var font = Font.GetFont(); if (font == null) { return Float2.Zero; } - return font.MeasureText(_text, ref _layout); + return Render2D.MeasureText(font, _text, ref _layout); } /// public override Float2 GetCharPosition(int index, out float height) { - var font = Font.GetMultiFont(); + var font = Font.GetFont(); if (font == null) { height = Height; return Float2.Zero; } - height = font.MaxHeight / DpiScale; + height = font.Height / DpiScale; return font.GetCharPosition(_text, index, ref _layout); } /// public override int HitTestText(Float2 location) { - var font = Font.GetMultiFont(); + var font = Font.GetFont(); if (font == null) { return 0; @@ -148,7 +149,7 @@ namespace FlaxEngine.GUI // Cache data var rect = new Rectangle(Float2.Zero, Size); bool enabled = EnabledInHierarchy; - var font = Font.GetMultiFont(); + var font = Font.GetFont(); if (!font) return; @@ -172,7 +173,7 @@ namespace FlaxEngine.GUI { var leftEdge = font.GetCharPosition(_text, SelectionLeft, ref _layout); var rightEdge = font.GetCharPosition(_text, SelectionRight, ref _layout); - float fontHeight = font.MaxHeight / DpiScale; + float fontHeight = font.Height / DpiScale; // Draw selection background float alpha = Mathf.Min(1.0f, Mathf.Cos(_animateTime * BackgroundSelectedFlashSpeed) * 0.5f + 1.3f); @@ -212,11 +213,25 @@ namespace FlaxEngine.GUI var color = TextColor; if (!enabled) color *= 0.6f; - Render2D.DrawText(font, _text, color, ref _layout, TextMaterial); + if (Render2D.Fallbacks != null) + { + Render2D.DrawText(font, Render2D.Fallbacks, _text, color, ref _layout, TextMaterial); + } + else + { + Render2D.DrawText(font, _text, color, ref _layout, TextMaterial); + } } else if (!string.IsNullOrEmpty(_watermarkText) && !IsFocused) { - Render2D.DrawText(font, _watermarkText, WatermarkTextColor, ref _layout, TextMaterial); + if (Render2D.Fallbacks != null) + { + Render2D.DrawText(font, Render2D.Fallbacks, _watermarkText, WatermarkTextColor, ref _layout, TextMaterial); + } + else + { + Render2D.DrawText(font, _watermarkText, WatermarkTextColor, ref _layout, TextMaterial); + } } // Caret diff --git a/Source/Engine/UI/GUI/Panels/DropPanel.cs b/Source/Engine/UI/GUI/Panels/DropPanel.cs index 06256c9f8..ed620ab98 100644 --- a/Source/Engine/UI/GUI/Panels/DropPanel.cs +++ b/Source/Engine/UI/GUI/Panels/DropPanel.cs @@ -130,7 +130,7 @@ namespace FlaxEngine.GUI /// Gets or sets the font used to render panel header text. /// [EditorDisplay("Header Text Style"), EditorOrder(2020), ExpandGroups] - public MultiFontReference HeaderTextFont { get; set; } + public FontReference HeaderTextFont { get; set; } /// /// Gets or sets the custom material used to render the text. It must has domain set to GUI and have a public texture parameter named Font used to sample font atlas texture with font characters data. @@ -238,7 +238,7 @@ namespace FlaxEngine.GUI var style = Style.Current; HeaderColor = style.BackgroundNormal; HeaderColorMouseOver = style.BackgroundHighlighted; - HeaderTextFont = new MultiFontReference(style.FontMedium); + HeaderTextFont = new FontReference(style.FontMedium); HeaderTextColor = style.Foreground; ArrowImageOpened = new SpriteBrush(style.ArrowDown); ArrowImageClosed = new SpriteBrush(style.ArrowRight); @@ -375,7 +375,7 @@ namespace FlaxEngine.GUI textColor *= 0.6f; } - Render2D.DrawText(HeaderTextFont.GetMultiFont(), HeaderTextMaterial, HeaderText, textRect, textColor, TextAlignment.Near, TextAlignment.Center); + Render2D.DrawText(HeaderTextFont.GetFont(), HeaderTextMaterial, HeaderText, textRect, textColor, TextAlignment.Near, TextAlignment.Center); if (!_isClosed && EnableContainmentLines) { diff --git a/Source/Engine/UI/GUI/Style.cs b/Source/Engine/UI/GUI/Style.cs index a8b87fb3a..c4b51b085 100644 --- a/Source/Engine/UI/GUI/Style.cs +++ b/Source/Engine/UI/GUI/Style.cs @@ -15,61 +15,67 @@ namespace FlaxEngine.GUI public static Style Current { get; set; } [Serialize] - private MultiFontReference _fontTitle; + private FontReference _fontTitle; /// /// The font title. /// [NoSerialize] [EditorOrder(10)] - public MultiFont FontTitle + public Font FontTitle { - get => _fontTitle?.GetMultiFont(); - set => _fontTitle = new MultiFontReference(value); + get => _fontTitle?.GetFont(); + set => _fontTitle = new FontReference(value); } [Serialize] - private MultiFontReference _fontLarge; + private FontReference _fontLarge; /// /// The font large. /// [NoSerialize] [EditorOrder(20)] - public MultiFont FontLarge + public Font FontLarge { - get => _fontLarge?.GetMultiFont(); - set => _fontLarge = new MultiFontReference(value); + get => _fontLarge?.GetFont(); + set => _fontLarge = new FontReference(value); } [Serialize] - private MultiFontReference _fontMedium; + private FontReference _fontMedium; /// /// The font medium. /// [NoSerialize] [EditorOrder(30)] - public MultiFont FontMedium + public Font FontMedium { - get => _fontMedium?.GetMultiFont(); - set => _fontMedium = new MultiFontReference(value); + get => _fontMedium?.GetFont(); + set => _fontMedium = new FontReference(value); } [Serialize] - private MultiFontReference _fontSmall; + private FontReference _fontSmall; /// /// The font small. /// [NoSerialize] [EditorOrder(40)] - public MultiFont FontSmall + public Font FontSmall { - get => _fontSmall?.GetMultiFont(); - set => _fontSmall = new MultiFontReference(value); + get => _fontSmall?.GetFont(); + set => _fontSmall = new FontReference(value); } + /// + /// The fallback fonts to use if the primary font can't render the char. + /// + [EditorOrder(50)] + public FallbackFonts Fallbacks; + /// /// The background color. ///