From bead6e4f15cf79ed397aaf65503523d8565b1e93 Mon Sep 17 00:00:00 2001 From: Bowon Ryu Date: Thu, 1 Dec 2022 18:32:48 +0900 Subject: [PATCH] [NUI] Add GetSystemFonts in FontClient user can get a list of system fonts sample code: https://github.com/wonrst/NUI-Test/blob/main/text-font-list/font_list.cs Signed-off-by: Bowon Ryu --- .../src/internal/Interop/Interop.FontClient.cs | 2 +- .../src/public/BaseComponents/TextConstants.cs | 66 ++++++++++++++++++++++ .../src/public/BaseComponents/TextUtils.cs | 39 +++++++++++++ src/Tizen.NUI/src/public/Utility/FontClient.cs | 41 ++++++++++++-- 4 files changed, 141 insertions(+), 7 deletions(-) diff --git a/src/Tizen.NUI/src/internal/Interop/Interop.FontClient.cs b/src/Tizen.NUI/src/internal/Interop/Interop.FontClient.cs index fcf3dc8..a600a83 100755 --- a/src/Tizen.NUI/src/internal/Interop/Interop.FontClient.cs +++ b/src/Tizen.NUI/src/internal/Interop/Interop.FontClient.cs @@ -90,7 +90,7 @@ namespace Tizen.NUI public static extern void GetDefaultPlatformFontDescription(global::System.Runtime.InteropServices.HandleRef jarg1, global::System.Runtime.InteropServices.HandleRef jarg2); [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_FontClient_GetSystemFonts")] - public static extern void GetSystemFonts(global::System.Runtime.InteropServices.HandleRef jarg1, global::System.Runtime.InteropServices.HandleRef jarg2); + public static extern global::System.IntPtr GetSystemFonts(global::System.Runtime.InteropServices.HandleRef fontClient); [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_FontClient_GetDescription")] public static extern void GetDescription(global::System.Runtime.InteropServices.HandleRef jarg1, uint jarg2, global::System.Runtime.InteropServices.HandleRef jarg3); diff --git a/src/Tizen.NUI/src/public/BaseComponents/TextConstants.cs b/src/Tizen.NUI/src/public/BaseComponents/TextConstants.cs index c1603be..b141b0a 100644 --- a/src/Tizen.NUI/src/public/BaseComponents/TextConstants.cs +++ b/src/Tizen.NUI/src/public/BaseComponents/TextConstants.cs @@ -801,4 +801,70 @@ namespace Tizen.NUI.Text [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() => (LeftImageUrl, RightImageUrl).GetHashCode(); } + + /// + /// A struct to pass data of FontInfo PropertyMap. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public struct FontInfo : IEquatable + { + /// + /// The FontFamily of the font. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public string Family { get; set; } + + /// + /// The FontPath of the font. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public string Path { get; set; } + + /// + /// The FontStyle of the font. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public FontStyle Style { get; set; } + + /// + /// Determines whether the specified object is equal to the current object. + /// + /// The object to compare with the current object. + /// true if equal FontInfo, else false. + [EditorBrowsable(EditorBrowsableState.Never)] + public override bool Equals(object obj) => obj is FontInfo other && this.Equals(other); + + /// + /// Determines whether the specified object is equal to the current object. + /// + /// The FontInfo to compare with the current FontInfo. + /// true if equal FontInfo, else false. + [EditorBrowsable(EditorBrowsableState.Never)] + public bool Equals(FontInfo other) => Family == other.Family && Path == other.Path && Style == other.Style; + + /// + /// The == operator. + /// + /// FontInfo to compare + /// FontInfo to be compared + /// true if FontInfos are equal + [EditorBrowsable(EditorBrowsableState.Never)] + public static bool operator ==(FontInfo lhsFontInfo, FontInfo rhsFontInfo) => lhsFontInfo.Equals(rhsFontInfo); + + /// + /// The != operator. + /// + /// FontInfo to compare + /// FontInfo to be compared + /// true if FontInfos are not equal + [EditorBrowsable(EditorBrowsableState.Never)] + public static bool operator !=(FontInfo lhsFontInfo, FontInfo rhsFontInfo) => !lhsFontInfo.Equals(rhsFontInfo); + + /// + /// Gets the hash code of this FontInfo. + /// + /// The hash code. + [EditorBrowsable(EditorBrowsableState.Never)] + public override int GetHashCode() => (Family, Path, Style).GetHashCode(); + } } diff --git a/src/Tizen.NUI/src/public/BaseComponents/TextUtils.cs b/src/Tizen.NUI/src/public/BaseComponents/TextUtils.cs index 7988a3f..1d34cee 100755 --- a/src/Tizen.NUI/src/public/BaseComponents/TextUtils.cs +++ b/src/Tizen.NUI/src/public/BaseComponents/TextUtils.cs @@ -19,6 +19,8 @@ extern alias TizenSystemSettings; using TizenSystemSettings.Tizen.System; using System; using System.ComponentModel; +using System.Collections.Generic; +using Tizen.NUI.Text; namespace Tizen.NUI.BaseComponents { @@ -1083,6 +1085,43 @@ namespace Tizen.NUI.BaseComponents return ret; } + /// + /// This method converts a FontInfo property array to a list and returns it. + /// The FontInfo PropertyArray. + /// A list of FontInfo struct. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public static List GetFontInfoList(PropertyArray fontArray) + { + var fontList = new List(); + if (fontArray != null) + { + for (uint i = 0 ; i < fontArray.Count(); i ++) + { + using (var fontInfoMap = new PropertyMap()) + using (var propertyValue = fontArray[i]) + { + propertyValue.Get(fontInfoMap); + + if (fontInfoMap.Count() > 0) + { + var fontInfo = new FontInfo(); + fontInfo.Family = TextMapHelper.GetStringFromMap(fontInfoMap, "family", "TizenSans"); + fontInfo.Path = TextMapHelper.GetStringFromMap(fontInfoMap, "path", ""); + fontInfo.Style = new FontStyle() + { + Width = (FontWidthType)TextMapHelper.GetIntFromMap(fontInfoMap, "width", 0), + Weight = (FontWeightType)TextMapHelper.GetIntFromMap(fontInfoMap, "weight", 0), + Slant = (FontSlantType)TextMapHelper.GetIntFromMap(fontInfoMap, "slant", 0), + }; + fontList.Add(fontInfo); + } + } + } + } + return fontList; + } + #if PROFILE_TV private const float FontSizeScaleSmall = 0.8f; private const float FontSizeScaleNormal = 1.0f; diff --git a/src/Tizen.NUI/src/public/Utility/FontClient.cs b/src/Tizen.NUI/src/public/Utility/FontClient.cs index c8889da..d748e4c 100755 --- a/src/Tizen.NUI/src/public/Utility/FontClient.cs +++ b/src/Tizen.NUI/src/public/Utility/FontClient.cs @@ -15,6 +15,9 @@ * */ using System.ComponentModel; +using System.Collections.Generic; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Text; namespace Tizen.NUI { @@ -209,6 +212,38 @@ namespace Tizen.NUI return ret; } + /// + /// Retrieve the list of font info supported by the system. + /// + /// The list of FontInfo + /// + /// + /// + /// + /// The following example demonstrates how to use the GetSystemFonts method. + /// + /// var fontList = FontClient.Instance.GetSystemFonts(); + /// foreach(Tizen.NUI.Text.FontInfo fontInfo in fontList) + /// { + /// string fontFamily = fontInfo.Family; + /// string fontPath = fontInfo.Path; + /// FontWidthType fontWidth = fontInfo.Style.Width; + /// FontWeightType fontWeight = fontInfo.Style.Weight; + /// FontSlantType fontSlant = fontInfo.Style.Slant; + /// } + /// + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public List GetSystemFonts() + { + using PropertyArray fontArray = new PropertyArray(Interop.FontClient.GetSystemFonts(SwigCPtr), true); + if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); + + List fontInfoList; + fontInfoList = TextUtils.GetFontInfoList(fontArray); + return fontInfoList; + } + internal static FontClient Get() { FontClient ret = new FontClient(Interop.FontClient.Get(), true); @@ -254,12 +289,6 @@ namespace Tizen.NUI if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } - internal void GetSystemFonts(SWIGTYPE_p_std__vectorT_Dali__TextAbstraction__FontDescription_t systemFonts) - { - Interop.FontClient.GetSystemFonts(SwigCPtr, SWIGTYPE_p_std__vectorT_Dali__TextAbstraction__FontDescription_t.getCPtr(systemFonts)); - if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); - } - internal void GetDescription(uint id, FontDescription fontDescription) { Interop.FontClient.GetDescription(SwigCPtr, id, FontDescription.getCPtr(fontDescription)); -- 2.7.4