[NUI] refactoring GraphicsType classes and Dp. Add new Type Sp
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Utility / PointTypeConverter.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System.ComponentModel;
19 using System.Globalization;
20
21 namespace Tizen.NUI
22 {
23     /// <summary>
24     /// Default PointTypeConverter class to convert point types.
25     /// </summary>
26     /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public sealed class PointTypeConverter : GraphicsTypeConverter
29     {
30         private volatile static PointTypeConverter ptTypeConverter;
31         private const ushort pointDpi = 72;
32
33         /// <summary>
34         /// An unique Singleton Instance of PointTypeConverter.
35         /// </summary>
36         /// <value>Singleton instance of PointTypeConverter</value>
37         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
38         [EditorBrowsable(EditorBrowsableState.Never)]
39         public static PointTypeConverter Instance
40         {
41             get
42             {
43                 if (ptTypeConverter == null)
44                 {
45                     ptTypeConverter = new PointTypeConverter();
46                 }
47
48                 return ptTypeConverter;
49             }
50         }
51
52         /// <summary>
53         /// Converts script to pixel.
54         /// </summary>
55         /// <returns>Pixel value that is converted from input string</returns>
56         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
57         [EditorBrowsable(EditorBrowsableState.Never)]
58         public override float ConvertScriptToPixel(string scriptValue)
59         {
60             float convertedValue = 0;
61             if (scriptValue != null)
62             {
63                 if (scriptValue.EndsWith("pt"))
64                 {
65                     convertedValue = ConvertToPixel(float.Parse(scriptValue.Substring(0, scriptValue.LastIndexOf("pt")), CultureInfo.InvariantCulture));
66                 }
67                 else if (scriptValue.EndsWith("px"))
68                 {
69                     convertedValue = float.Parse(scriptValue.Substring(0, scriptValue.LastIndexOf("px")), CultureInfo.InvariantCulture);
70                 }
71                 else
72                 {
73                     if (!float.TryParse(scriptValue, NumberStyles.Any, CultureInfo.InvariantCulture, out convertedValue))
74                     {
75                         NUILog.Error("Cannot convert the script {scriptValue}\n");
76                         convertedValue = 0;
77                     }
78                 }
79             }
80             return convertedValue;
81         }
82
83         /// <summary>
84         /// Converts point type to pixel.
85         /// </summary>
86         /// <returns>Pixel value that is converted by the the display matric</returns>
87         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
88         [EditorBrowsable(EditorBrowsableState.Never)]
89         public override float ConvertToPixel(float value)
90         {
91             return value * (GraphicsTypeManager.Instance.ScaledDpi / (float)pointDpi);
92         }
93
94         /// <summary>
95         /// Converts pixel to point type.
96         /// </summary>
97         /// <returns>An converted value from pixel</returns>
98         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
99         [EditorBrowsable(EditorBrowsableState.Never)]
100         public override float ConvertFromPixel(float value)
101         {
102             return value * ((float)pointDpi / (float)GraphicsTypeManager.Instance.ScaledDpi);
103         }
104
105         /// <summary>
106         /// Converts point type to pixel.
107         /// </summary>
108         /// <returns>Pixel value that is converted by the the display matric</returns>
109         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
110         [EditorBrowsable(EditorBrowsableState.Never)]
111         public float ConvertDpToPoint(float value)
112         {
113             return value * ((float)pointDpi / (float)GraphicsTypeManager.Instance.BaselineDpi);
114         }
115
116         /// <summary>
117         /// Converts pixel to point type.
118         /// </summary>
119         /// <returns>An converted value from pixel</returns>
120         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
121         [EditorBrowsable(EditorBrowsableState.Never)]
122         public float ConvertPointToDp(float value)
123         {
124             return value * ((float)GraphicsTypeManager.Instance.BaselineDpi / (float)pointDpi);
125         }
126     }
127 }