Replace std::string global variables with C-style string.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-font-style.cpp
1 /*
2  * Copyright (c) 2022 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 // FILE HEADER
19 #include <dali-toolkit/internal/text/text-font-style.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/text/markup-processor/markup-processor-helper-functions.h>
26 #include <dali-toolkit/internal/text/property-string-parser.h>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Text
33 {
34 namespace
35 {
36 const char* WEIGHT_KEY = "weight";
37 const char* WIDTH_KEY = "width";
38 const char* SLANT_KEY = "slant";
39 const char* FAMILY_KEY = "family";
40 const char* TYPE_KEY = "type";
41
42 const char* SYSTEM_TOKEN = "system";
43
44 } // namespace
45
46 void SetFontFamilyProperty(ControllerPtr controller, const Property::Value& value)
47 {
48   if(controller)
49   {
50     const std::string fontFamilyValue = value.Get<std::string>();
51
52     if(fontFamilyValue.empty())
53     {
54       // Resets the default's font family name.
55       controller->SetDefaultFontFamily("");
56       return;
57     }
58
59     Property::Map map;
60     ParsePropertyString(fontFamilyValue, map);
61
62     if(map.Empty())
63     {
64       // There is no map. The font has been passed as a font's family name with no format.
65       controller->SetDefaultFontFamily(fontFamilyValue);
66     }
67     else
68     {
69       /// Family key
70       Property::Value* familyValue = map.Find(FAMILY_KEY);
71
72       std::string fontFamilyName;
73       if(NULL != familyValue)
74       {
75         fontFamilyName = familyValue->Get<std::string>();
76       }
77
78       /// Type key
79       Property::Value* typeValue = map.Find(TYPE_KEY);
80
81       std::string typeStr;
82       if(NULL != typeValue)
83       {
84         typeStr = typeValue->Get<std::string>();
85       }
86
87       if(TokenComparison(SYSTEM_TOKEN, typeStr.c_str(), typeStr.size()))
88       {
89         controller->UpdateAfterFontChange(fontFamilyName);
90       }
91       else
92       {
93         controller->SetDefaultFontFamily(fontFamilyName);
94       }
95     }
96   }
97 }
98
99 void SetFontStyleProperty(ControllerPtr controller, const Property::Value& value, FontStyle::Type type)
100 {
101   if(controller)
102   {
103     Property::Map map;
104     if(Property::STRING == value.GetType())
105     {
106       const std::string& fontStyleProperties = value.Get<std::string>();
107
108       ParsePropertyString(fontStyleProperties, map);
109       controller->FontStyleSetByString(true);
110     }
111     else
112     {
113       map = value.Get<Property::Map>();
114       controller->FontStyleSetByString(false);
115     }
116
117     if(!map.Empty())
118     {
119       /// Weight key
120       Property::Value* weightValue = map.Find(WEIGHT_KEY);
121
122       FontWeight weight        = TextAbstraction::FontWeight::NONE;
123       const bool weightDefined = weightValue != NULL;
124       if(weightDefined)
125       {
126         const std::string weightStr = weightValue->Get<std::string>();
127
128         Scripting::GetEnumeration<FontWeight>(weightStr.c_str(),
129                                               FONT_WEIGHT_STRING_TABLE,
130                                               FONT_WEIGHT_STRING_TABLE_COUNT,
131                                               weight);
132       }
133
134       /// Width key
135       Property::Value* widthValue = map.Find(WIDTH_KEY);
136
137       FontWidth  width        = TextAbstraction::FontWidth::NONE;
138       const bool widthDefined = widthValue != NULL;
139       if(widthDefined)
140       {
141         const std::string widthStr = widthValue->Get<std::string>();
142
143         Scripting::GetEnumeration<FontWidth>(widthStr.c_str(),
144                                              FONT_WIDTH_STRING_TABLE,
145                                              FONT_WIDTH_STRING_TABLE_COUNT,
146                                              width);
147       }
148
149       /// Slant key
150       Property::Value* slantValue = map.Find(SLANT_KEY);
151
152       FontSlant  slant        = TextAbstraction::FontSlant::NONE;
153       const bool slantDefined = slantValue != NULL;
154       if(slantDefined)
155       {
156         const std::string slantStr = slantValue->Get<std::string>();
157
158         Scripting::GetEnumeration<FontSlant>(slantStr.c_str(),
159                                              FONT_SLANT_STRING_TABLE,
160                                              FONT_SLANT_STRING_TABLE_COUNT,
161                                              slant);
162       }
163
164       switch(type)
165       {
166         case FontStyle::DEFAULT:
167         {
168           // Sets the default font's style values.
169           if(!weightDefined ||
170              (weightDefined && (controller->GetDefaultFontWeight() != weight)))
171           {
172             controller->SetDefaultFontWeight(weight);
173           }
174
175           if(!widthDefined ||
176              (widthDefined && (controller->GetDefaultFontWidth() != width)))
177           {
178             controller->SetDefaultFontWidth(width);
179           }
180
181           if(!slantDefined ||
182              (slantDefined && (controller->GetDefaultFontSlant() != slant)))
183           {
184             controller->SetDefaultFontSlant(slant);
185           }
186           break;
187         }
188         case FontStyle::INPUT:
189         {
190           // Sets the input font's style values.
191           if(!weightDefined ||
192              (weightDefined && (controller->GetInputFontWeight() != weight)))
193           {
194             controller->SetInputFontWeight(weight);
195           }
196
197           if(!widthDefined ||
198              (widthDefined && (controller->GetInputFontWidth() != width)))
199           {
200             controller->SetInputFontWidth(width);
201           }
202
203           if(!slantDefined ||
204              (slantDefined && (controller->GetInputFontSlant() != slant)))
205           {
206             controller->SetInputFontSlant(slant);
207           }
208           break;
209         }
210         case FontStyle::PLACEHOLDER:
211         {
212           // Sets the placeholder text font's style values.
213           if(!weightDefined ||
214              (weightDefined && (controller->GetPlaceholderTextFontWeight() != weight)))
215           {
216             controller->SetPlaceholderTextFontWeight(weight);
217           }
218
219           if(!widthDefined ||
220              (widthDefined && (controller->GetPlaceholderTextFontWidth() != width)))
221           {
222             controller->SetPlaceholderTextFontWidth(width);
223           }
224
225           if(!slantDefined ||
226              (slantDefined && (controller->GetPlaceholderTextFontSlant() != slant)))
227           {
228             controller->SetPlaceholderTextFontSlant(slant);
229           }
230           break;
231         }
232       } // switch
233     }   // map not empty
234     else
235     {
236       switch(type)
237       {
238         case FontStyle::DEFAULT:
239         {
240           controller->SetDefaultFontWeight(TextAbstraction::FontWeight::NONE);
241           controller->SetDefaultFontWidth(TextAbstraction::FontWidth::NONE);
242           controller->SetDefaultFontSlant(TextAbstraction::FontSlant::NONE);
243           break;
244         }
245         case FontStyle::INPUT:
246         {
247           controller->SetInputFontWeight(TextAbstraction::FontWeight::NONE);
248           controller->SetInputFontWidth(TextAbstraction::FontWidth::NONE);
249           controller->SetInputFontSlant(TextAbstraction::FontSlant::NONE);
250           break;
251         }
252         case FontStyle::PLACEHOLDER:
253         {
254           controller->SetPlaceholderTextFontWeight(TextAbstraction::FontWeight::NONE);
255           controller->SetPlaceholderTextFontWidth(TextAbstraction::FontWidth::NONE);
256           controller->SetPlaceholderTextFontSlant(TextAbstraction::FontSlant::NONE);
257           break;
258         }
259       } // switch
260     }   // map.Empty()
261   }     // controller
262 }
263
264 void GetFontStyleProperty(ControllerPtr controller, Property::Value& value, FontStyle::Type type)
265 {
266   if(controller)
267   {
268     const bool isSetbyString = controller->IsFontStyleSetByString();
269
270     bool       weightDefined = false;
271     bool       widthDefined  = false;
272     bool       slantDefined  = false;
273     FontWeight weight        = TextAbstraction::FontWeight::NONE;
274     FontWidth  width         = TextAbstraction::FontWidth::NONE;
275     FontSlant  slant         = TextAbstraction::FontSlant::NONE;
276
277     switch(type)
278     {
279       case FontStyle::DEFAULT:
280       {
281         weightDefined = controller->IsDefaultFontWeightDefined();
282         widthDefined  = controller->IsDefaultFontWidthDefined();
283         slantDefined  = controller->IsDefaultFontSlantDefined();
284
285         if(weightDefined)
286         {
287           weight = controller->GetDefaultFontWeight();
288         }
289
290         if(widthDefined)
291         {
292           width = controller->GetDefaultFontWidth();
293         }
294
295         if(slantDefined)
296         {
297           slant = controller->GetDefaultFontSlant();
298         }
299         break;
300       }
301       case FontStyle::INPUT:
302       {
303         weightDefined = controller->IsInputFontWeightDefined();
304         widthDefined  = controller->IsInputFontWidthDefined();
305         slantDefined  = controller->IsInputFontSlantDefined();
306
307         if(weightDefined)
308         {
309           weight = controller->GetInputFontWeight();
310         }
311
312         if(widthDefined)
313         {
314           width = controller->GetInputFontWidth();
315         }
316
317         if(slantDefined)
318         {
319           slant = controller->GetInputFontSlant();
320         }
321         break;
322       }
323       case FontStyle::PLACEHOLDER:
324       {
325         // The type is FontStyle::PLACEHOLDER
326         weightDefined = controller->IsPlaceholderTextFontWeightDefined();
327         widthDefined  = controller->IsPlaceholderTextFontWidthDefined();
328         slantDefined  = controller->IsPlaceholderTextFontSlantDefined();
329
330         if(weightDefined)
331         {
332           weight = controller->GetPlaceholderTextFontWeight();
333         }
334
335         if(widthDefined)
336         {
337           width = controller->GetPlaceholderTextFontWidth();
338         }
339
340         if(slantDefined)
341         {
342           slant = controller->GetPlaceholderTextFontSlant();
343         }
344         break;
345       }
346     }
347
348     if(!isSetbyString)
349     {
350       Property::Map map;
351
352       if(weightDefined)
353       {
354         if(TextAbstraction::FontWeight::NONE != weight)
355         {
356           const std::string weightStr(GetEnumerationName(weight,
357                                                          FONT_WEIGHT_STRING_TABLE,
358                                                          FONT_WEIGHT_STRING_TABLE_COUNT));
359
360           map.Insert(WEIGHT_KEY, weightStr);
361         }
362       }
363
364       if(widthDefined)
365       {
366         if(TextAbstraction::FontWidth::NONE != width)
367         {
368           const std::string widthStr(GetEnumerationName(width,
369                                                         FONT_WIDTH_STRING_TABLE,
370                                                         FONT_WIDTH_STRING_TABLE_COUNT));
371
372           map.Insert(WIDTH_KEY, widthStr);
373         }
374       }
375
376       if(slantDefined)
377       {
378         if(TextAbstraction::FontSlant::NONE != slant)
379         {
380           const std::string slantStr(GetEnumerationName(slant,
381                                                         FONT_SLANT_STRING_TABLE,
382                                                         FONT_SLANT_STRING_TABLE_COUNT));
383
384           map.Insert(SLANT_KEY, slantStr);
385         }
386       }
387
388       value = map;
389     } // SetbyMAP
390     else
391     {
392       std::string fontStyleProperties = "{";
393
394       if(weightDefined)
395       {
396         if(TextAbstraction::FontWeight::NONE != weight)
397         {
398           const std::string weightStr(GetEnumerationName(weight,
399                                                          FONT_WEIGHT_STRING_TABLE,
400                                                          FONT_WEIGHT_STRING_TABLE_COUNT));
401
402           fontStyleProperties += "\"weight\":\"" + weightStr + "\",";
403         }
404       }
405
406       if(widthDefined)
407       {
408         if(TextAbstraction::FontWidth::NONE != width)
409         {
410           const std::string widthStr(GetEnumerationName(width,
411                                                         FONT_WIDTH_STRING_TABLE,
412                                                         FONT_WIDTH_STRING_TABLE_COUNT));
413           fontStyleProperties += "\"width\":\"" + widthStr + "\",";
414         }
415       }
416
417       if(slantDefined)
418       {
419         if(TextAbstraction::FontSlant::NONE != slant)
420         {
421           const std::string slantStr(GetEnumerationName(slant,
422                                                         FONT_SLANT_STRING_TABLE,
423                                                         FONT_SLANT_STRING_TABLE_COUNT));
424
425           fontStyleProperties += "\"slant\":\"" + slantStr + "\"";
426         }
427       }
428
429       // If last character is comma, it will be removed.
430       if((*fontStyleProperties.rbegin()) == ',')
431       {
432         fontStyleProperties = fontStyleProperties.substr(0, fontStyleProperties.size() - 1);
433       }
434       fontStyleProperties += "}";
435
436       value = fontStyleProperties;
437     } // SetbyString
438   }   // controller
439 }
440
441 FontWeight StringToWeight(const char* const weightStr)
442 {
443   FontWeight weight = TextAbstraction::FontWeight::NORMAL;
444   Scripting::GetEnumeration<FontWeight>(weightStr,
445                                         FONT_WEIGHT_STRING_TABLE,
446                                         FONT_WEIGHT_STRING_TABLE_COUNT,
447                                         weight);
448
449   return weight;
450 }
451
452 FontWidth StringToWidth(const char* const widthStr)
453 {
454   FontWidth width = TextAbstraction::FontWidth::NORMAL;
455   Scripting::GetEnumeration<FontWidth>(widthStr,
456                                        FONT_WIDTH_STRING_TABLE,
457                                        FONT_WIDTH_STRING_TABLE_COUNT,
458                                        width);
459
460   return width;
461 }
462
463 FontSlant StringToSlant(const char* const slantStr)
464 {
465   FontSlant slant = TextAbstraction::FontSlant::NORMAL;
466   Scripting::GetEnumeration<FontSlant>(slantStr,
467                                        FONT_SLANT_STRING_TABLE,
468                                        FONT_SLANT_STRING_TABLE_COUNT,
469                                        slant);
470
471   return slant;
472 }
473
474 } // namespace Text
475
476 } // namespace Toolkit
477
478 } // namespace Dali