From 8de95df437e95c764464af7734b68245edec3e04 Mon Sep 17 00:00:00 2001 From: Jay Cho Date: Fri, 4 Nov 2022 11:58:21 +0900 Subject: [PATCH] [Tizen] Add handing Label.TextType --- .../Extensions/FormattedStringExtensions.cs | 73 +++++++++++++++++++ .../Tizen/Extensions/TextExtensions.cs | 31 ++++++-- .../net-tizen/PublicAPI.Unshipped.txt | 2 + .../src/Fonts/EmbeddedFontLoader.Tizen.cs | 12 +++ src/Core/src/Fonts/FontManager.Tizen.cs | 8 +- .../src/Platform/Tizen/MauiApplication.cs | 7 ++ 6 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 src/Controls/src/Core/Platform/Tizen/Extensions/FormattedStringExtensions.cs diff --git a/src/Controls/src/Core/Platform/Tizen/Extensions/FormattedStringExtensions.cs b/src/Controls/src/Core/Platform/Tizen/Extensions/FormattedStringExtensions.cs new file mode 100644 index 000000000000..df002b71bab9 --- /dev/null +++ b/src/Controls/src/Core/Platform/Tizen/Extensions/FormattedStringExtensions.cs @@ -0,0 +1,73 @@ +using Microsoft.Maui.Controls.Internals; +using TFormattedString = Tizen.UIExtensions.Common.FormattedString; +using TSpan = Tizen.UIExtensions.Common.Span; + +namespace Microsoft.Maui.Controls.Platform +{ + public static class FormattedStringExtensions + { + public static TFormattedString ToFormattedString(this Label label) + => ToFormattedText( + label.FormattedText, + label.TextColor, + label.RequireFontManager(), + label.ToFont(), + label.TextTransform, + label.TextDecorations); + + internal static TFormattedString ToFormattedText( + this FormattedString formattedString, + Graphics.Color defaultColor, + IFontManager fontManager, + Font? defaultFont = null, + TextTransform defaultTextTransform = TextTransform.Default, + TextDecorations defaultTextDecorations = TextDecorations.None) + { + if (formattedString == null) + return new TFormattedString(); + + var defaultFontSize = defaultFont?.Size ?? fontManager.DefaultFontSize; + var formattedText = new TFormattedString(); + + for (int i = 0; i < formattedString.Spans.Count; i++) + { + Span span = formattedString.Spans[i]; + var transform = span.TextTransform != TextTransform.Default ? span.TextTransform : defaultTextTransform; + var text = TextTransformUtilites.GetTransformedText(span.Text, transform); + + if (text == null) + continue; + + var nativeSpan = new TSpan() { Text = text }; + var textColor = span.TextColor ?? defaultColor; + + if (textColor is not null) + nativeSpan.ForegroundColor = textColor.ToPlatform(); + + if (span.BackgroundColor is not null) + nativeSpan.BackgroundColor = span.BackgroundColor.ToPlatform(); + + var font = span.ToFont(defaultFontSize); + if (font.IsDefault && defaultFont.HasValue) + font = defaultFont.Value; + + if (!font.IsDefault) + { + nativeSpan.FontSize = font.Size.ToScaledPoint(); + nativeSpan.FontFamily = fontManager.GetFontFamily(span.FontFamily); + } + + nativeSpan.LineHeight = span.LineHeight; + + var textDecorations = span.IsSet(Span.TextDecorationsProperty) + ? span.TextDecorations + : defaultTextDecorations; + if (textDecorations.HasFlag(TextDecorations.Strikethrough) || textDecorations.HasFlag(TextDecorations.Underline)) + nativeSpan.TextDecorations = span.TextDecorations.ToPlatform(); + + formattedText.Spans.Add(nativeSpan); + } + return formattedText; + } + } +} diff --git a/src/Controls/src/Core/Platform/Tizen/Extensions/TextExtensions.cs b/src/Controls/src/Core/Platform/Tizen/Extensions/TextExtensions.cs index 5287e935abdd..d11622758855 100644 --- a/src/Controls/src/Core/Platform/Tizen/Extensions/TextExtensions.cs +++ b/src/Controls/src/Core/Platform/Tizen/Extensions/TextExtensions.cs @@ -1,11 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Microsoft.Maui.Platform; -using Microsoft.Maui.Controls.Internals; +using Microsoft.Maui.Controls.Internals; +using Tizen.UIExtensions.NUI; using TEntry = Tizen.UIExtensions.NUI.Entry; using TEditor = Tizen.UIExtensions.NUI.Editor; using TLabel = Tizen.UIExtensions.NUI.Label; +using TFormattedString = Tizen.UIExtensions.Common.FormattedString; +using TSpan = Tizen.UIExtensions.Common.Span; namespace Microsoft.Maui.Controls.Platform { @@ -32,7 +31,27 @@ public static void UpdateText(this TEditor editor, InputView inputView) public static void UpdateText(this TLabel platformLabel, Label label) { - platformLabel.Text = TextTransformUtilites.GetTransformedText(label.Text, label.TextTransform); + switch (label.TextType) + { + case TextType.Text: + if (label.FormattedText != null) + platformLabel.FormattedText = label.ToFormattedString(); + else + platformLabel.Text = TextTransformUtilites.GetTransformedText(label.Text, label.TextTransform); + break; + case TextType.Html: + platformLabel.UpdateTextHtml(label); + break; + } + } + + static void UpdateTextHtml(this TLabel platformLabel, Label label) + { + var formattedText = new TFormattedString(); + var htmlSpan = new TSpan() { Text = label.Text }; + formattedText.Spans.Add(htmlSpan); + platformLabel.EnableMarkup = true; + platformLabel.Text = formattedText.ToMarkupText(); } } } diff --git a/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt index 7d1f9df1f9b6..a87f739fe2d8 100644 --- a/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt @@ -1642,6 +1642,7 @@ Microsoft.Maui.Controls.Platform.ElementChangedEventArgs.ElementChange Microsoft.Maui.Controls.Platform.ElementChangedEventArgs.NewElement.get -> TElement? Microsoft.Maui.Controls.Platform.ElementChangedEventArgs.OldElement.get -> TElement? Microsoft.Maui.Controls.Platform.FontExtensions +Microsoft.Maui.Controls.Platform.FormattedStringExtensions Microsoft.Maui.Controls.Platform.GestureHandler Microsoft.Maui.Controls.Platform.GestureHandler.Attach(Microsoft.Maui.IViewHandler! handler) -> void Microsoft.Maui.Controls.Platform.GestureHandler.Detach() -> void @@ -5888,6 +5889,7 @@ virtual Microsoft.Maui.Controls.Window.OnStopped() -> void ~static Microsoft.Maui.Controls.Platform.ButtonExtensions.UpdateText(this Tizen.UIExtensions.NUI.Button platformButton, Microsoft.Maui.Controls.Button button) -> void ~static Microsoft.Maui.Controls.Platform.CollectionViewExtensions.ToLayoutManager(this Microsoft.Maui.Controls.IItemsLayout layout, Microsoft.Maui.Controls.ItemSizingStrategy sizing = Microsoft.Maui.Controls.ItemSizingStrategy.MeasureFirstItem) -> Tizen.UIExtensions.NUI.ICollectionViewLayoutManager ~static Microsoft.Maui.Controls.Platform.FontExtensions.ToNativeFontFamily(this string self, Microsoft.Maui.IFontManager fontManager) -> string +~static Microsoft.Maui.Controls.Platform.FormattedStringExtensions.ToFormattedString(this Microsoft.Maui.Controls.Label label) -> Tizen.UIExtensions.Common.FormattedString ~static Microsoft.Maui.Controls.Platform.ImageExtensions.LoadImage(this Tizen.NUI.BaseComponents.ImageView image, Microsoft.Maui.Controls.ImageSource source) -> System.Threading.Tasks.Task ~static Microsoft.Maui.Controls.Platform.ImageExtensions.LoadImageAsync(this Tizen.NUI.BaseComponents.ImageView image, Microsoft.Maui.Controls.FileImageSource imageSource) -> System.Threading.Tasks.Task ~static Microsoft.Maui.Controls.Platform.ImageExtensions.LoadImageAsync(this Tizen.NUI.BaseComponents.ImageView image, Microsoft.Maui.Controls.StreamImageSource imageSource) -> System.Threading.Tasks.Task diff --git a/src/Core/src/Fonts/EmbeddedFontLoader.Tizen.cs b/src/Core/src/Fonts/EmbeddedFontLoader.Tizen.cs index 1f86f029b76c..ea8b0cc65f14 100644 --- a/src/Core/src/Fonts/EmbeddedFontLoader.Tizen.cs +++ b/src/Core/src/Fonts/EmbeddedFontLoader.Tizen.cs @@ -19,6 +19,13 @@ public partial class EmbeddedFontLoader : IEmbeddedFontLoader /// public string? LoadFont(EmbeddedFont font) { + var fontResourcePath = IOPath.Combine(TApplication.Current.DirectoryInfo.Resource, _fontCacheFolderName); + var fontResourceFilePath = IOPath.Combine(fontResourcePath, font.FontName!); + if (File.Exists(fontResourceFilePath)) + { + return IOPath.GetFileNameWithoutExtension(fontResourceFilePath); + } + if (FontCacheDirectory == null) { FontCacheDirectory = Directory.CreateDirectory(IOPath.Combine(TApplication.Current.DirectoryInfo.Data, _fontCacheFolderName)); @@ -28,7 +35,10 @@ public partial class EmbeddedFontLoader : IEmbeddedFontLoader var filePath = IOPath.Combine(FontCacheDirectory.FullName, font.FontName!); var name = IOPath.GetFileNameWithoutExtension(filePath); if (File.Exists(filePath)) + { return name; + } + try { using (var fileStream = File.Create(filePath)) @@ -38,6 +48,8 @@ public partial class EmbeddedFontLoader : IEmbeddedFontLoader font.ResourceStream.CopyTo(fileStream); } + Tizen.NUI.FontClient.Instance.AddCustomFontDirectory(FontCacheDirectory.FullName); + return name; } catch (Exception ex) diff --git a/src/Core/src/Fonts/FontManager.Tizen.cs b/src/Core/src/Fonts/FontManager.Tizen.cs index df6bd444b2dd..9daf1050b111 100644 --- a/src/Core/src/Fonts/FontManager.Tizen.cs +++ b/src/Core/src/Fonts/FontManager.Tizen.cs @@ -51,8 +51,7 @@ public string GetFont(Font font) if (index != -1) { string font = cleansedFont.Substring(0, index); - string style = cleansedFont.Substring(index + 1); - return $"{font}:style={style}"; + return $"{font}"; } else { @@ -79,8 +78,7 @@ string GetNativeFontFamily((string? family, float size, FontSlant slant) fontKey if (index != -1) { string font = cleansedFont.Substring(0, index); - string style = cleansedFont.Substring(index + 1); - return $"{font}:style={style}"; + return $"{font}"; } else { @@ -115,4 +113,4 @@ string GetNativeFontFamily((string? family, float size, FontSlant slant) fontKey return fontFile.PostScriptName; } } -} \ No newline at end of file +} diff --git a/src/Core/src/Platform/Tizen/MauiApplication.cs b/src/Core/src/Platform/Tizen/MauiApplication.cs index e1473d8492d9..84c5a0232a82 100644 --- a/src/Core/src/Platform/Tizen/MauiApplication.cs +++ b/src/Core/src/Platform/Tizen/MauiApplication.cs @@ -4,12 +4,16 @@ using Microsoft.Maui.LifecycleEvents; using Tizen.Applications; using Tizen.NUI; +using IOPath = System.IO.Path; +using TApplication = Tizen.Applications.Application; using NView = Tizen.NUI.BaseComponents.View; namespace Microsoft.Maui { public abstract class MauiApplication : NUIApplication, IPlatformApplication { + const string _fontCacheFolderName = "fonts"; + internal Func? _handleBackButtonPressed; IMauiContext _applicationContext = null!; @@ -28,6 +32,9 @@ protected override void OnPreCreate() FocusManager.Instance.EnableDefaultAlgorithm(true); NView.SetDefaultGrabTouchAfterLeave(true); + var fontResourcePath = IOPath.Combine(TApplication.Current.DirectoryInfo.Resource, _fontCacheFolderName); + FontClient.Instance.AddCustomFontDirectory(fontResourcePath); + var mauiApp = CreateMauiApp(); var rootContext = new MauiContext(mauiApp.Services);