[NUI] Apply Sp and Dp type converter for text point size.
authorEverLEEst(SangHyeon Lee) <sh10233.lee@samsung.com>
Wed, 5 Jan 2022 12:31:57 +0000 (21:31 +0900)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Wed, 19 Jan 2022 05:29:33 +0000 (14:29 +0900)
src/Tizen.NUI/src/public/BaseComponents/TextEditor.cs
src/Tizen.NUI/src/public/BaseComponents/TextField.cs
src/Tizen.NUI/src/public/BaseComponents/TextLabel.cs
src/Tizen.NUI/src/public/Utility/GraphicsTypeExtensions.cs
src/Tizen.NUI/src/public/Utility/PointTypeConverter.cs
src/Tizen.NUI/src/public/XamlBinding/PointSizeTypeConverter.cs [new file with mode: 0755]

index 74d284f..ae8085c 100755 (executable)
@@ -22,6 +22,7 @@ using System;
 using System.Globalization;
 using System.ComponentModel;
 using Tizen.NUI.Text;
+using Tizen.NUI.Binding;
 
 namespace Tizen.NUI.BaseComponents
 {
@@ -294,6 +295,7 @@ namespace Tizen.NUI.BaseComponents
         /// The size of font in points.<br />
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
+        [Binding.TypeConverter(typeof(PointSizeTypeConverter))]
         public float PointSize
         {
             get
@@ -985,6 +987,7 @@ namespace Tizen.NUI.BaseComponents
         /// The font's size of the new input text in points.<br />
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
+        [Binding.TypeConverter(typeof(PointSizeTypeConverter))]
         public float InputPointSize
         {
             get
index f9dd39d..712fa96 100755 (executable)
@@ -361,6 +361,7 @@ namespace Tizen.NUI.BaseComponents
         /// The size of font in points.<br />
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
+        [Binding.TypeConverter(typeof(PointSizeTypeConverter))]
         public float PointSize
         {
             get
@@ -1286,6 +1287,7 @@ namespace Tizen.NUI.BaseComponents
         /// The font's size of the new input text in points.<br />
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
+        [Binding.TypeConverter(typeof(PointSizeTypeConverter))]
         public float InputPointSize
         {
             get
index 776cbcf..c70f890 100755 (executable)
@@ -22,6 +22,7 @@ using System;
 using System.Globalization;
 using System.ComponentModel;
 using Tizen.NUI.Text;
+using Tizen.NUI.Binding;
 
 namespace Tizen.NUI.BaseComponents
 {
@@ -323,6 +324,7 @@ namespace Tizen.NUI.BaseComponents
         /// The size of font in points.<br />
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
+        [Binding.TypeConverter(typeof(PointSizeTypeConverter))]
         public float PointSize
         {
             get
index b09319e..c47d29e 100644 (file)
@@ -1,4 +1,21 @@
-using System.ComponentModel;
+/*
+ * Copyright(c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+using System.ComponentModel;
 
 namespace Tizen.NUI
 {
@@ -511,5 +528,29 @@ namespace Tizen.NUI
         {
             return GraphicsTypeManager.Instance.Point.ConvertPointToDp(pt);
         }
+
+        /// <summary>
+        /// Converter float font sp size to point size.
+        /// </summary>
+        /// <param name="sp">The float sp unit value to be converted point unit.</param>
+        /// <returns>The float point unit value.</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static float SpToPt(this float sp)
+        {
+            return GraphicsTypeManager.Instance.Point.ConvertSpToPoint(sp);
+        }
+
+        /// <summary>
+        /// Converter float font point size to sp size.
+        /// </summary>
+        /// <param name="pt">The float point unit value to be converted sp unit.</param>
+        /// <returns>The float dp unit value.</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static float PtToSp(this float pt)
+        {
+            return GraphicsTypeManager.Instance.Point.ConvertPointToSp(pt);
+        }
     }
 }
index 9da10b2..17f3137 100755 (executable)
@@ -81,6 +81,41 @@ namespace Tizen.NUI
         }
 
         /// <summary>
+        /// Converts script to pixel.
+        /// </summary>
+        /// <returns>Pixel value that is converted from input string</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public float ConvertScriptToPoint(string scriptValue)
+        {
+            float convertedValue = 0;
+            if (scriptValue != null)
+            {
+                if (scriptValue.EndsWith("sp"))
+                {
+                    convertedValue = ConvertSpToPoint(float.Parse(scriptValue.Substring(0, scriptValue.LastIndexOf("sp")), CultureInfo.InvariantCulture));
+                }
+                else if (scriptValue.EndsWith("dp"))
+                {
+                    convertedValue = ConvertDpToPoint(float.Parse(scriptValue.Substring(0, scriptValue.LastIndexOf("dp")), CultureInfo.InvariantCulture));
+                }
+                else if (scriptValue.EndsWith("pt"))
+                {
+                    convertedValue = float.Parse(scriptValue.Substring(0, scriptValue.LastIndexOf("px")), CultureInfo.InvariantCulture);
+                }
+                else
+                {
+                    if (!float.TryParse(scriptValue, NumberStyles.Any, CultureInfo.InvariantCulture, out convertedValue))
+                    {
+                        NUILog.Error("Cannot convert the script {scriptValue}\n");
+                        convertedValue = 0;
+                    }
+                }
+            }
+            return convertedValue;
+        }
+
+        /// <summary>
         /// Converts point type to pixel.
         /// </summary>
         /// <returns>Pixel value that is converted by the the display matric</returns>
@@ -103,9 +138,9 @@ namespace Tizen.NUI
         }
 
         /// <summary>
-        /// Converts point type to pixel.
+        /// Converts dp type to point type.
         /// </summary>
-        /// <returns>Pixel value that is converted by the the display matric</returns>
+        /// <returns>Point value that is converted from dp.</returns>
         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
         public float ConvertDpToPoint(float value)
@@ -114,14 +149,40 @@ namespace Tizen.NUI
         }
 
         /// <summary>
-        /// Converts pixel to point type.
+        /// Converts point type to dp type.
         /// </summary>
-        /// <returns>An converted value from pixel</returns>
+        /// <returns>Dp value that is converted from point.</returns>
         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
         public float ConvertPointToDp(float value)
         {
             return value * ((float)GraphicsTypeManager.Instance.BaselineDpi / (float)pointDpi);
         }
+
+        /// <summary>
+        /// Converts sp type to point type.
+        /// </summary>
+        /// <returns>Point value that is converted from dp.</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public float ConvertSpToPoint(float value)
+        {
+            float scale = GraphicsTypeManager.Instance.ScalingFactor;
+            if (scale <= 0) scale = 1;
+            return value * ((float)pointDpi / (float)GraphicsTypeManager.Instance.BaselineDpi) * scale;
+        }
+
+        /// <summary>
+        /// Converts point type to sp type.
+        /// </summary>
+        /// <returns>Sp value that is converted from point.</returns>
+        /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public float ConvertPointToSp(float value)
+        {
+            float scale = GraphicsTypeManager.Instance.ScalingFactor;
+            if (scale <= 0) scale = 1;
+            return value * ((float)GraphicsTypeManager.Instance.BaselineDpi / (float)pointDpi) / scale;
+        }
     }
 }
diff --git a/src/Tizen.NUI/src/public/XamlBinding/PointSizeTypeConverter.cs b/src/Tizen.NUI/src/public/XamlBinding/PointSizeTypeConverter.cs
new file mode 100755 (executable)
index 0000000..cc0a1c2
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright(c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+using System;
+using System.Linq;
+using System.Reflection;
+using System.Globalization;
+
+using Tizen.NUI;
+using Tizen.NUI.Xaml;
+using System.ComponentModel;
+
+namespace Tizen.NUI.Binding
+{
+    /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    [ProvideCompiledAttribute("Tizen.NUI.Xaml.Core.XamlC.PointSizeTypeConverter")]
+    public class PointSizeTypeConverter : TypeConverter
+    {
+        /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public override object ConvertFromInvariantString(string value)
+        {
+            if(!string.IsNullOrEmpty(value))
+            {
+                return (int)GraphicsTypeManager.Instance.Point.ConvertScriptToPoint(value.Trim());
+            }
+
+            throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(int)}");
+        }
+
+        /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public override string ConvertToString(object value)
+        {
+            if (value != null)
+            {
+                int integer = (int)value;
+                return integer.ToString();
+            }
+            return "";
+        }
+    }
+}