2 * Copyright(c) 2021 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;
22 namespace Tizen.NUI.BaseComponents
25 /// TextMapHelper converts PropertyMap to struct and struct to PropertyMap.
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 public static class TextMapHelper
31 /// It returns a string value according to FontWidthType.
32 /// The returned value can be used for FontStyle PropertyMap.
33 /// <param name="fontWidthType">The FontWidthType enum value.</param>
34 /// <returns> A string value for FontStyle.Width property. </returns>
36 [EditorBrowsable(EditorBrowsableState.Never)]
37 public static string GetFontWidthString(FontWidthType fontWidthType)
39 string value = GetCamelCase(fontWidthType.ToString());
40 return string.IsNullOrEmpty(value) ? "none" : value;
44 /// It returns a string value according to FontWeightType.
45 /// The returned value can be used for FontStyle PropertyMap.
46 /// <param name="fontWeightType">The FontWeightType enum value.</param>
47 /// <returns> A string value for FontStyle.Weight property. </returns>
49 [EditorBrowsable(EditorBrowsableState.Never)]
50 public static string GetFontWeightString(FontWeightType fontWeightType)
52 string value = GetCamelCase(fontWeightType.ToString());
53 return string.IsNullOrEmpty(value) ? "none" : value;
57 /// It returns a string value according to FontSlantType.
58 /// The returned value can be used for FontStyle PropertyMap.
59 /// <param name="fontSlantType">The FontSlantType enum value.</param>
60 /// <returns> A string value for FontStyle.Slant property. </returns>
62 [EditorBrowsable(EditorBrowsableState.Never)]
63 public static string GetFontSlantString(FontSlantType fontSlantType)
65 string value = GetCamelCase(fontSlantType.ToString());
66 return string.IsNullOrEmpty(value) ? "none" : value;
70 /// It returns a string value according to FontSizeType.
71 /// The returned value can be used for TextFit PropertyMap.
72 /// <param name="fontSizeType">The FontSizeType enum value.</param>
73 /// <returns> A string value for TextFit.FontSizeType property. </returns>
75 [EditorBrowsable(EditorBrowsableState.Never)]
76 public static string GetFontSizeString(FontSizeType fontSizeType)
78 string value = GetCamelCase(fontSizeType.ToString());
79 return string.IsNullOrEmpty(value) ? "pointSize" : value;
83 /// It returns a FontWidthType value according to fontWidthString.
84 /// The returned value can be used for FontStyle PropertyMap.
85 /// <param name="fontWidthString">The FontWidth string value.</param>
86 /// <returns> A FontWidthType value for FontStyle.Width property. </returns>
88 [EditorBrowsable(EditorBrowsableState.Never)]
89 public static FontWidthType GetFontWidthType(string fontWidthString)
92 if (!(Enum.TryParse(fontWidthString, true, out value) && Enum.IsDefined(typeof(FontWidthType), value)))
94 value = FontWidthType.None; // If parsing fails, set a default value.
101 /// It returns a FontWeightType value according to fontWeightString.
102 /// The returned value can be used for FontStyle PropertyMap.
103 /// <param name="fontWeightString">The FontWeight string value.</param>
104 /// <returns> A FontWeightType value for FontStyle.Weight property. </returns>
106 [EditorBrowsable(EditorBrowsableState.Never)]
107 public static FontWeightType GetFontWeightType(string fontWeightString)
109 FontWeightType value;
110 if (!(Enum.TryParse(fontWeightString, true, out value) && Enum.IsDefined(typeof(FontWeightType), value)))
112 value = FontWeightType.None; // If parsing fails, set a default value.
119 /// It returns a FontSlantType value according to fontSlantString.
120 /// The returned value can be used for FontStyle PropertyMap.
121 /// <param name="fontSlantString">The FontSlant string value.</param>
122 /// <returns> A FontSlantType value for FontStyle.Slant property. </returns>
124 [EditorBrowsable(EditorBrowsableState.Never)]
125 public static FontSlantType GetFontSlantType(string fontSlantString)
128 if (!(Enum.TryParse(fontSlantString, true, out value) && Enum.IsDefined(typeof(FontSlantType), value)))
130 value = FontSlantType.None; // If parsing fails, set a default value.
137 /// It returns a FontSizeType value according to fontSizeString.
138 /// The returned value can be used for FontStyle PropertyMap.
139 /// <param name="fontSizeString">The FontSizeType string value.</param>
140 /// <returns> A FontSizeType value for TextFit.FontSizeType property. </returns>
142 [EditorBrowsable(EditorBrowsableState.Never)]
143 public static FontSizeType GetFontSizeType(string fontSizeString)
146 if (!(Enum.TryParse(fontSizeString, true, out value) && Enum.IsDefined(typeof(FontSizeType), value)))
148 value = FontSizeType.PointSize; // If parsing fails, set a default value.
155 /// This method converts a FontStyle struct to a PropertyMap and returns it.
156 /// The returned map can be used for set FontStyle PropertyMap in the SetFontStyle method.
157 /// <param name="fontStyle">The FontStyle struct value.</param>
158 /// <returns> A PropertyMap for FontStyle property. </returns>
160 [EditorBrowsable(EditorBrowsableState.Never)]
161 public static PropertyMap GetFontStyleMap(FontStyle fontStyle)
163 var map = new PropertyMap();
164 map.Add("width", GetFontWidthString(fontStyle.Width));
165 map.Add("weight", GetFontWeightString(fontStyle.Weight));
166 map.Add("slant", GetFontSlantString(fontStyle.Slant));
172 /// This method converts a FontStyle map to a struct and returns it.
173 /// The returned struct can be returned to the user as a FontStyle in the GetFontStyle method.
174 /// <param name="map">The FontStyle PropertyMap.</param>
175 /// <returns> A FontStyle struct. </returns>
177 [EditorBrowsable(EditorBrowsableState.Never)]
178 public static FontStyle GetFontStyleStruct(PropertyMap map)
180 var fontStyle = new FontStyle();
183 var defaultValue = "none";
184 fontStyle.Width = GetFontWidthType(GetStringFromMap(map, "width", defaultValue));
185 fontStyle.Weight = GetFontWeightType(GetStringFromMap(map, "weight", defaultValue));
186 fontStyle.Slant = GetFontSlantType(GetStringFromMap(map, "slant", defaultValue));
193 /// This method converts a InputFilter struct to a PropertyMap and returns it. <br />
194 /// The returned map can be used for set InputFilter PropertyMap in the SetInputFilter method. <br />
195 /// <param name="inputFilter">The InputFilter struct value.</param>
196 /// <returns> A PropertyMap for InputFilter property. </returns>
198 [EditorBrowsable(EditorBrowsableState.Never)]
199 public static PropertyMap GetInputFilterMap(InputFilter inputFilter)
201 var defaultValue = "";
203 var map = new PropertyMap();
204 var accepted = inputFilter.Accepted == null ? defaultValue : inputFilter.Accepted;
205 var rejected = inputFilter.Rejected == null ? defaultValue : inputFilter.Rejected;
206 map.Add(0, accepted);
207 map.Add(1, rejected);
213 /// This method converts a InputFilter map to a struct and returns it. <br />
214 /// The returned struct can be returned to the user as a InputFilter in the GetInputFilter method. <br />
215 /// <param name="map">The InputFilter PropertyMap.</param>
216 /// <returns> A InputFilter struct. </returns>
218 [EditorBrowsable(EditorBrowsableState.Never)]
219 public static InputFilter GetInputFilterStruct(PropertyMap map)
221 var inputFilter = new InputFilter();
224 var defaultValue = "";
225 inputFilter.Accepted = GetStringFromMap(map, 0, defaultValue);
226 inputFilter.Rejected = GetStringFromMap(map, 1, defaultValue);
233 /// This method converts a Underline struct to a PropertyMap and returns it.
234 /// The returned map can be used for set Underline PropertyMap in the SetUnderline method.
235 /// <param name="underline">The Underline struct value.</param>
236 /// <returns> A PropertyMap for Underline property. </returns>
238 [EditorBrowsable(EditorBrowsableState.Never)]
239 public static PropertyMap GetUnderlineMap(Underline underline)
241 var map = new PropertyMap();
243 map.Add("enable", underline.Enable);
245 if (underline.Color != null)
246 map.Add("color", underline.Color);
248 if (underline.Height != null)
249 map.Add("height", (float)underline.Height);
255 /// This method converts a Underline map to a struct and returns it.
256 /// The returned struct can be returned to the user as a Underline in the GetUnderline method.
257 /// <param name="map">The Underline PropertyMap.</param>
258 /// <returns> A Underline struct. </returns>
260 [EditorBrowsable(EditorBrowsableState.Never)]
261 public static Underline GetUnderlineStruct(PropertyMap map)
263 var underline = new Underline();
266 underline.Enable = GetBoolFromMap(map, "enable", false);
267 underline.Color = GetColorFromMap(map, "color");
268 underline.Height = GetFloatFromMap(map, "height", 0.0f);
275 /// This method converts a Shadow struct to a PropertyMap and returns it.
276 /// The returned map can be used for set Shadow PropertyMap in the SetShadow method.
277 /// <param name="shadow">The Shadow struct value.</param>
278 /// <returns> A PropertyMap for Shadow property. </returns>
280 [EditorBrowsable(EditorBrowsableState.Never)]
281 public static PropertyMap GetShadowMap(Tizen.NUI.Text.Shadow shadow)
283 var map = new PropertyMap();
285 if (shadow.Offset != null)
286 map.Add("offset", shadow.Offset);
288 if (shadow.Color != null)
289 map.Add("color", shadow.Color);
291 if (shadow.BlurRadius != null)
292 map.Add("blurRadius", (float)shadow.BlurRadius);
298 /// This method converts a Shadow map to a struct and returns it.
299 /// The returned struct can be returned to the user as a Shadow in the GetShadow method.
300 /// <param name="map">The Shadow PropertyMap.</param>
301 /// <returns> A Shadow struct. </returns>
303 [EditorBrowsable(EditorBrowsableState.Never)]
304 public static Tizen.NUI.Text.Shadow GetShadowStruct(PropertyMap map)
306 var shadow = new Tizen.NUI.Text.Shadow();
309 shadow.Offset = GetVector2FromMap(map, "offset");
310 shadow.Color = GetColorFromMap(map, "color");
311 shadow.BlurRadius = GetFloatFromMap(map, "blurRadius", 0.0f);
318 /// This method converts a Outline struct to a PropertyMap and returns it.
319 /// The returned map can be used for set Outline PropertyMap in the SetOutline method.
320 /// <param name="outline">The Outline struct value.</param>
321 /// <returns> A PropertyMap for Outline property. </returns>
323 [EditorBrowsable(EditorBrowsableState.Never)]
324 public static PropertyMap GetOutlineMap(Outline outline)
326 var map = new PropertyMap();
328 if (outline.Color != null)
329 map.Add("color", outline.Color);
331 if (outline.Width != null)
332 map.Add("width", (float)outline.Width);
338 /// This method converts a Outline map to a struct and returns it.
339 /// The returned struct can be returned to the user as a Outline in the GetOutline method.
340 /// <param name="map">The Outline PropertyMap.</param>
341 /// <returns> A Outline struct. </returns>
343 [EditorBrowsable(EditorBrowsableState.Never)]
344 public static Outline GetOutlineStruct(PropertyMap map)
346 var outline = new Outline();
349 outline.Color = GetColorFromMap(map, "color");
350 outline.Width = GetFloatFromMap(map, "width", 0.0f);
357 /// This method converts a TextFit struct to a PropertyMap and returns it.
358 /// The returned map can be used for set TextFit PropertyMap in the SetTextFit method.
359 /// <param name="textFit">The TextFit struct value.</param>
360 /// <returns> A PropertyMap for TextFit property. </returns>
362 [EditorBrowsable(EditorBrowsableState.Never)]
363 public static PropertyMap GetTextFitMap(TextFit textFit)
365 var map = new PropertyMap();
366 map.Add("enable", textFit.Enable);
367 map.Add("fontSizeType", GetFontSizeString(textFit.FontSizeType));
369 if (textFit.MinSize != null)
370 map.Add("minSize", (float)textFit.MinSize);
372 if (textFit.MaxSize != null)
373 map.Add("maxSize", (float)textFit.MaxSize);
375 if (textFit.StepSize != null)
376 map.Add("stepSize", (float)textFit.StepSize);
382 /// This method converts a TextFit map to a struct and returns it.
383 /// The returned struct can be returned to the user as a TextFit in the GetTextFit method.
384 /// <param name="map">The TextFit PropertyMap.</param>
385 /// <returns> A TextFit struct. </returns>
387 [EditorBrowsable(EditorBrowsableState.Never)]
388 public static TextFit GetTextFitStruct(PropertyMap map)
390 var textFit = new TextFit();
393 var defaultValue = "PointSize";
394 textFit.Enable = GetBoolFromMap(map, "enable", false);
395 textFit.MinSize = GetFloatFromMap(map, "minSize", 0.0f);
396 textFit.MaxSize = GetFloatFromMap(map, "maxSize", 0.0f);
397 textFit.StepSize = GetFloatFromMap(map, "stepSize", 0.0f);
398 textFit.FontSize = GetFloatFromMap(map, "fontSize", 0.0f);
399 textFit.FontSizeType = GetFontSizeType(GetStringFromMap(map, "fontSizeType", defaultValue));
406 /// This method converts a Placeholder struct to a PropertyMap and returns it.
407 /// The returned map can be used for set Placeholder PropertyMap in the SetPlaceholder method.
408 /// <param name="placeholder">The Placeholder struct value.</param>
409 /// <returns> A PropertyMap for Placeholder property. </returns>
411 [EditorBrowsable(EditorBrowsableState.Never)]
412 public static PropertyMap GetPlaceholderMap(Placeholder placeholder)
414 var map = new PropertyMap();
416 if (placeholder.Text != null)
417 map.Add("text", placeholder.Text);
419 if (placeholder.TextFocused != null)
420 map.Add("textFocused", placeholder.TextFocused);
422 if (placeholder.Color != null)
423 map.Add("color", placeholder.Color);
425 if (placeholder.FontFamily != null)
426 map.Add("fontFamily", placeholder.FontFamily);
428 if (placeholder.FontStyle != null)
430 using (var fontStyleMap = GetFontStyleMap((FontStyle)placeholder.FontStyle))
431 using (var fontStyleValue = new PropertyValue(fontStyleMap))
433 map.Add("fontStyle", fontStyleValue);
437 if (placeholder.PointSize != null && placeholder.PixelSize != null)
438 map.Add("pointSize", (float)placeholder.PointSize);
440 else if (placeholder.PointSize != null)
441 map.Add("pointSize", (float)placeholder.PointSize);
443 else if (placeholder.PixelSize != null)
444 map.Add("pixelSize", (float)placeholder.PixelSize);
446 map.Add("ellipsis", placeholder.Ellipsis);
452 /// This method converts a Placeholder map to a struct and returns it.
453 /// The returned struct can be returned to the user as a Placeholder in the GetPlaceholder method.
454 /// <param name="map">The Placeholder PropertyMap.</param>
455 /// <returns> A Placeholder struct. </returns>
457 [EditorBrowsable(EditorBrowsableState.Never)]
458 public static Placeholder GetPlaceholderStruct(PropertyMap map)
460 var placeholder = new Placeholder();
463 var defaultText = "";
464 placeholder.Text = GetStringFromMap(map, 0, defaultText);
465 placeholder.TextFocused = GetStringFromMap(map, 1, defaultText);
466 placeholder.Color = GetColorFromMap(map, 2);
467 placeholder.FontFamily = GetStringFromMap(map, 3, defaultText);
468 using (var fontStyleMap = GetMapFromMap(map, 4))
470 placeholder.FontStyle = GetFontStyleStruct(fontStyleMap);
472 placeholder.PointSize = GetNullableFloatFromMap(map, 5);
473 placeholder.PixelSize = GetNullableFloatFromMap(map, 6);
474 placeholder.Ellipsis = GetBoolFromMap(map, 7, false);
481 /// This method converts a HiddenInput struct to a PropertyMap and returns it.
482 /// The returned map can be used for set HiddenInputSettings PropertyMap in the SetHiddenInput method.
483 /// <param name="hiddenInput">The HiddenInput struct value.</param>
484 /// <returns> A PropertyMap for HiddenInput property. </returns>
486 [EditorBrowsable(EditorBrowsableState.Never)]
487 public static PropertyMap GetHiddenInputMap(HiddenInput hiddenInput)
489 var map = new PropertyMap();
491 map.Add(0, (int)hiddenInput.Mode);
493 if (hiddenInput.SubstituteCharacter != null)
494 map.Add(1, Convert.ToInt32(hiddenInput.SubstituteCharacter));
496 if (hiddenInput.SubstituteCount != null)
497 map.Add(2, (int)hiddenInput.SubstituteCount);
499 if (hiddenInput.ShowLastCharacterDuration != null)
500 map.Add(3, (int)hiddenInput.ShowLastCharacterDuration);
506 /// This method converts a HiddenInputSettings map to a struct and returns it.
507 /// The returned struct can be returned to the user as a HiddenInput in the GetHiddenInput method.
508 /// <param name="map">The HiddenInput PropertyMap.</param>
509 /// <returns> A HiddenInput struct. </returns>
511 [EditorBrowsable(EditorBrowsableState.Never)]
512 public static HiddenInput GetHiddenInputStruct(PropertyMap map)
514 var hiddenInput = new HiddenInput();
517 int defaultVlaue = 0;
518 hiddenInput.Mode = (HiddenInputModeType)GetIntFromMap(map, 0, defaultVlaue);
520 int? substituteCharacter = GetNullableIntFromMap(map, 1);
521 if (substituteCharacter != null)
522 hiddenInput.SubstituteCharacter = Convert.ToChar(substituteCharacter);
524 hiddenInput.SubstituteCount = GetNullableIntFromMap(map, 2);
525 hiddenInput.ShowLastCharacterDuration = GetNullableIntFromMap(map, 3);
532 /// This method converts a fileName string to a PropertyMap and returns it.
533 /// The returned map can be used for set SelectionHandleImageLeft, SelectionHandleImageRight PropertyMap in the SetSelectionHandleImage method.
534 /// <param name="fileName">The file path string value for SelectionHandleImage.</param>
535 /// <returns> A PropertyMap for SelectionHandleImageLeft, SelectionHandleImageRight properties. </returns>
537 [EditorBrowsable(EditorBrowsableState.Never)]
538 public static PropertyMap GetFileNameMap(string fileName)
540 return new PropertyMap().Add("filename", fileName);
544 /// This method converts a SelectionHandleImageLeft, SelectionHandleImageRight map to a struct and returns it.
545 /// The returned struct can be returned to the user as a SelectionHandleImage in the GetSelectionHandleImage method.
546 /// <param name="leftImageMap">The SelectionHandleImageLeft PropertyMap.</param>
547 /// <param name="rightImageMap">The SelectionHandleImageRight PropertyMap.</param>
548 /// <returns> A SelectionHandleImage struct. </returns>
550 [EditorBrowsable(EditorBrowsableState.Never)]
551 public static SelectionHandleImage GetSelectionHandleImageStruct(PropertyMap leftImageMap, PropertyMap rightImageMap)
553 var defaultValue = "";
555 var selectionHandleImage = new SelectionHandleImage();
556 if (null != leftImageMap)
557 selectionHandleImage.LeftImageUrl = GetStringFromMap(leftImageMap, "filename", defaultValue);
559 if (null != rightImageMap)
560 selectionHandleImage.RightImageUrl = GetStringFromMap(rightImageMap, "filename", defaultValue);
562 return selectionHandleImage;
565 internal static string GetCamelCase(string pascalCase)
567 if (!string.IsNullOrEmpty(pascalCase))
569 char[] charArray = pascalCase.ToCharArray();
570 charArray[0] = Char.ToLower(charArray[0]);
571 pascalCase = new string(charArray);
577 internal static string GetStringFromMap(PropertyMap map, string key, string defaultValue)
579 string value = defaultValue;
580 using (var propertyValue = map.Find(0, key))
582 if (null != propertyValue) propertyValue.Get(out value);
587 internal static string GetStringFromMap(PropertyMap map, int key, string defaultValue)
589 string value = defaultValue;
590 using (var propertyValue = map.Find(key))
592 if (null != propertyValue) propertyValue.Get(out value);
597 internal static bool GetBoolFromMap(PropertyMap map, string key, bool defaultValue)
599 bool value = defaultValue;
600 using (var propertyValue = map.Find(0, key))
602 if (null != propertyValue) propertyValue.Get(out value);
607 internal static bool GetBoolFromMap(PropertyMap map, int key, bool defaultValue)
609 bool value = defaultValue;
610 using (var propertyValue = map.Find(key))
612 if (null != propertyValue) propertyValue.Get(out value);
617 internal static int GetIntFromMap(PropertyMap map, int key, int defaultValue)
619 int value = defaultValue;
620 using (var propertyValue = map.Find(key))
622 if (null != propertyValue) propertyValue.Get(out value);
627 internal static float GetFloatFromMap(PropertyMap map, string key, float defaultValue)
629 float value = defaultValue;
630 using (var propertyValue = map.Find(0, key))
632 if (null != propertyValue) propertyValue.Get(out value);
637 internal static Color GetColorFromMap(PropertyMap map, string key)
639 Color value = new Color();
640 using (var propertyValue = map.Find(0, key))
642 if (null != propertyValue) propertyValue.Get(value);
647 internal static Color GetColorFromMap(PropertyMap map, int key)
649 Color value = new Color();
650 using (var propertyValue = map.Find(key))
652 if (null != propertyValue) propertyValue.Get(value);
657 internal static Vector2 GetVector2FromMap(PropertyMap map, string key)
659 Vector2 value = new Vector2();
660 using (var propertyValue = map.Find(0, key))
662 if (null != propertyValue) propertyValue.Get(value);
667 internal static PropertyMap GetMapFromMap(PropertyMap map, int key)
669 PropertyMap value = new PropertyMap();
670 using (var propertyValue = map.Find(key))
672 if (null != propertyValue) propertyValue.Get(value);
677 internal static int? GetNullableIntFromMap(PropertyMap map, int key)
679 using (var propertyValue = map.Find(key))
681 if (propertyValue == null)
684 propertyValue.Get(out int value);
689 internal static float? GetNullableFloatFromMap(PropertyMap map, int key)
691 using (var propertyValue = map.Find(key))
693 if (propertyValue == null)
696 propertyValue.Get(out float value);
701 internal static bool IsValue(PropertyMap map, int key)
703 using (var propertyValue = map.Find(key))
705 if (propertyValue == null)