[NUI] Add GetSystemFonts in FontClient
authorBowon Ryu <bowon.ryu@samsung.com>
Thu, 1 Dec 2022 09:32:48 +0000 (18:32 +0900)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Tue, 6 Dec 2022 08:08:09 +0000 (17:08 +0900)
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 <bowon.ryu@samsung.com>
src/Tizen.NUI/src/internal/Interop/Interop.FontClient.cs
src/Tizen.NUI/src/public/BaseComponents/TextConstants.cs
src/Tizen.NUI/src/public/BaseComponents/TextUtils.cs
src/Tizen.NUI/src/public/Utility/FontClient.cs

index fcf3dc8..a600a83 100755 (executable)
@@ -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);
index c1603be..b141b0a 100644 (file)
@@ -801,4 +801,70 @@ namespace Tizen.NUI.Text
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override int GetHashCode() => (LeftImageUrl, RightImageUrl).GetHashCode();
     }
+
+    /// <summary>
+    /// A struct to pass data of FontInfo PropertyMap.
+    /// </summary>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public struct FontInfo : IEquatable<FontInfo>
+    {
+        /// <summary>
+        /// The FontFamily of the font.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public string Family { get; set; }
+
+        /// <summary>
+        /// The FontPath of the font.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public string Path { get; set; }
+
+        /// <summary>
+        /// The FontStyle of the font.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public FontStyle Style { get; set; }
+
+        /// <summary>
+        /// Determines whether the specified object is equal to the current object.
+        /// </summary>
+        /// <param name="obj">The object to compare with the current object.</param>
+        /// <returns>true if equal FontInfo, else false.</returns>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public override bool Equals(object obj) => obj is FontInfo other && this.Equals(other);
+
+        /// <summary>
+        /// Determines whether the specified object is equal to the current object.
+        /// </summary>
+        /// <param name="other">The FontInfo to compare with the current FontInfo.</param>
+        /// <returns>true if equal FontInfo, else false.</returns>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public bool Equals(FontInfo other) => Family == other.Family && Path == other.Path && Style == other.Style;
+
+        /// <summary>
+        /// The == operator.
+        /// </summary>
+        /// <param name="lhsFontInfo">FontInfo to compare</param>
+        /// <param name="rhsFontInfo">FontInfo to be compared</param>
+        /// <returns>true if FontInfos are equal</returns>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static bool operator ==(FontInfo lhsFontInfo, FontInfo rhsFontInfo) => lhsFontInfo.Equals(rhsFontInfo);
+
+        /// <summary>
+        /// The != operator.
+        /// </summary>
+        /// <param name="lhsFontInfo">FontInfo to compare</param>
+        /// <param name="rhsFontInfo">FontInfo to be compared</param>
+        /// <returns>true if FontInfos are not equal</returns>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static bool operator !=(FontInfo lhsFontInfo, FontInfo rhsFontInfo) => !lhsFontInfo.Equals(rhsFontInfo);
+
+        /// <summary>
+        /// Gets the hash code of this FontInfo.
+        /// </summary>
+        /// <returns>The hash code.</returns>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public override int GetHashCode() => (Family, Path, Style).GetHashCode();
+    }
 }
index 7988a3f..1d34cee 100755 (executable)
@@ -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;
         }
 
+        /// <summary>
+        /// This method converts a FontInfo property array to a list and returns it.
+        /// <param name="fontArray">The FontInfo PropertyArray.</param>
+        /// <returns> A list of FontInfo struct. </returns>
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static List<FontInfo> GetFontInfoList(PropertyArray fontArray)
+        {
+            var fontList = new List<FontInfo>();
+            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;
index c8889da..d748e4c 100755 (executable)
@@ -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;
         }
 
+        /// <summary>
+        /// Retrieve the list of font info supported by the system.
+        /// </summary>
+        /// <returns>The list of FontInfo</returns>
+        /// <remarks>
+        /// <see cref="Tizen.NUI.Text.FontInfo"/>
+        /// </remarks>
+        /// <example>
+        /// The following example demonstrates how to use the GetSystemFonts method.
+        /// <code>
+        /// 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;
+        /// }
+        /// </code>
+        /// </example>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public List<FontInfo> GetSystemFonts()
+        {
+            using PropertyArray fontArray = new PropertyArray(Interop.FontClient.GetSystemFonts(SwigCPtr), true);
+            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+
+            List<FontInfo> 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));