2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.ComponentModel;
20 using System.Globalization;
26 /// GraphicsTypeConverter class to convert types.
28 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 public class GraphicsTypeConverter
33 private const float defaultDpi = 160.0f;
36 /// Converts script to px
38 /// <returns>Pixel value that is converted from input string</returns>
39 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
40 [EditorBrowsable(EditorBrowsableState.Never)]
41 public virtual float ConvertScriptToPixel(string scriptValue)
43 float convertedValue = 0;
44 if (scriptValue != null)
46 if (scriptValue.EndsWith("dp"))
48 convertedValue = ConvertToPixel(float.Parse(scriptValue.Substring(0, scriptValue.LastIndexOf("dp")), CultureInfo.InvariantCulture));
50 else if (scriptValue.EndsWith("px"))
52 convertedValue = float.Parse(scriptValue.Substring(0, scriptValue.LastIndexOf("px")), CultureInfo.InvariantCulture);
56 if (!float.TryParse(scriptValue, NumberStyles.Any, CultureInfo.InvariantCulture, out convertedValue))
58 NUILog.Error("Cannot convert the script {scriptValue}\n");
63 return convertedValue;
67 /// Converts other type to px
69 /// <returns>Pixel value that is converted by the the display matric</returns>
70 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
71 [EditorBrowsable(EditorBrowsableState.Never)]
72 public virtual float ConvertToPixel(float value)
74 return value * (NUIApplication.GetDefaultWindow().Dpi.X / defaultDpi);
78 /// Converts px to other type
80 /// <returns>An converted value from pixel</returns>
81 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
82 [EditorBrowsable(EditorBrowsableState.Never)]
83 public virtual float ConvertFromPixel(float value)
85 return value * (defaultDpi / NUIApplication.GetDefaultWindow().Dpi.X);