2 * Copyright (c) 2022 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 #include <dali-toolkit/internal/text/text-font-style.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/internal/text/markup-processor/markup-processor-helper-functions.h>
26 #include <dali-toolkit/internal/text/property-string-parser.h>
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";
42 const char* SYSTEM_TOKEN = "system";
46 void SetFontFamilyProperty(ControllerPtr controller, const Property::Value& value)
50 const std::string fontFamilyValue = value.Get<std::string>();
52 if(fontFamilyValue.empty())
54 // Resets the default's font family name.
55 controller->SetDefaultFontFamily("");
60 ParsePropertyString(fontFamilyValue, map);
64 // There is no map. The font has been passed as a font's family name with no format.
65 controller->SetDefaultFontFamily(fontFamilyValue);
70 Property::Value* familyValue = map.Find(FAMILY_KEY);
72 std::string fontFamilyName;
73 if(NULL != familyValue)
75 fontFamilyName = familyValue->Get<std::string>();
79 Property::Value* typeValue = map.Find(TYPE_KEY);
84 typeStr = typeValue->Get<std::string>();
87 if(TokenComparison(SYSTEM_TOKEN, typeStr.c_str(), typeStr.size()))
89 controller->UpdateAfterFontChange(fontFamilyName);
93 controller->SetDefaultFontFamily(fontFamilyName);
99 void SetFontStyleProperty(ControllerPtr controller, const Property::Value& value, FontStyle::Type type)
104 if(Property::STRING == value.GetType())
106 const std::string& fontStyleProperties = value.Get<std::string>();
108 ParsePropertyString(fontStyleProperties, map);
109 controller->FontStyleSetByString(true);
113 map = value.Get<Property::Map>();
114 controller->FontStyleSetByString(false);
120 Property::Value* weightValue = map.Find(WEIGHT_KEY);
122 FontWeight weight = TextAbstraction::FontWeight::NONE;
123 const bool weightDefined = weightValue != NULL;
126 const std::string weightStr = weightValue->Get<std::string>();
128 Scripting::GetEnumeration<FontWeight>(weightStr.c_str(),
129 FONT_WEIGHT_STRING_TABLE,
130 FONT_WEIGHT_STRING_TABLE_COUNT,
135 Property::Value* widthValue = map.Find(WIDTH_KEY);
137 FontWidth width = TextAbstraction::FontWidth::NONE;
138 const bool widthDefined = widthValue != NULL;
141 const std::string widthStr = widthValue->Get<std::string>();
143 Scripting::GetEnumeration<FontWidth>(widthStr.c_str(),
144 FONT_WIDTH_STRING_TABLE,
145 FONT_WIDTH_STRING_TABLE_COUNT,
150 Property::Value* slantValue = map.Find(SLANT_KEY);
152 FontSlant slant = TextAbstraction::FontSlant::NONE;
153 const bool slantDefined = slantValue != NULL;
156 const std::string slantStr = slantValue->Get<std::string>();
158 Scripting::GetEnumeration<FontSlant>(slantStr.c_str(),
159 FONT_SLANT_STRING_TABLE,
160 FONT_SLANT_STRING_TABLE_COUNT,
166 case FontStyle::DEFAULT:
168 // Sets the default font's style values.
170 (weightDefined && (controller->GetDefaultFontWeight() != weight)))
172 controller->SetDefaultFontWeight(weight);
176 (widthDefined && (controller->GetDefaultFontWidth() != width)))
178 controller->SetDefaultFontWidth(width);
182 (slantDefined && (controller->GetDefaultFontSlant() != slant)))
184 controller->SetDefaultFontSlant(slant);
188 case FontStyle::INPUT:
190 // Sets the input font's style values.
192 (weightDefined && (controller->GetInputFontWeight() != weight)))
194 controller->SetInputFontWeight(weight);
198 (widthDefined && (controller->GetInputFontWidth() != width)))
200 controller->SetInputFontWidth(width);
204 (slantDefined && (controller->GetInputFontSlant() != slant)))
206 controller->SetInputFontSlant(slant);
210 case FontStyle::PLACEHOLDER:
212 // Sets the placeholder text font's style values.
214 (weightDefined && (controller->GetPlaceholderTextFontWeight() != weight)))
216 controller->SetPlaceholderTextFontWeight(weight);
220 (widthDefined && (controller->GetPlaceholderTextFontWidth() != width)))
222 controller->SetPlaceholderTextFontWidth(width);
226 (slantDefined && (controller->GetPlaceholderTextFontSlant() != slant)))
228 controller->SetPlaceholderTextFontSlant(slant);
238 case FontStyle::DEFAULT:
240 controller->SetDefaultFontWeight(TextAbstraction::FontWeight::NONE);
241 controller->SetDefaultFontWidth(TextAbstraction::FontWidth::NONE);
242 controller->SetDefaultFontSlant(TextAbstraction::FontSlant::NONE);
245 case FontStyle::INPUT:
247 controller->SetInputFontWeight(TextAbstraction::FontWeight::NONE);
248 controller->SetInputFontWidth(TextAbstraction::FontWidth::NONE);
249 controller->SetInputFontSlant(TextAbstraction::FontSlant::NONE);
252 case FontStyle::PLACEHOLDER:
254 controller->SetPlaceholderTextFontWeight(TextAbstraction::FontWeight::NONE);
255 controller->SetPlaceholderTextFontWidth(TextAbstraction::FontWidth::NONE);
256 controller->SetPlaceholderTextFontSlant(TextAbstraction::FontSlant::NONE);
264 void GetFontStyleProperty(ControllerPtr controller, Property::Value& value, FontStyle::Type type)
268 const bool isSetbyString = controller->IsFontStyleSetByString();
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;
279 case FontStyle::DEFAULT:
281 weightDefined = controller->IsDefaultFontWeightDefined();
282 widthDefined = controller->IsDefaultFontWidthDefined();
283 slantDefined = controller->IsDefaultFontSlantDefined();
287 weight = controller->GetDefaultFontWeight();
292 width = controller->GetDefaultFontWidth();
297 slant = controller->GetDefaultFontSlant();
301 case FontStyle::INPUT:
303 weightDefined = controller->IsInputFontWeightDefined();
304 widthDefined = controller->IsInputFontWidthDefined();
305 slantDefined = controller->IsInputFontSlantDefined();
309 weight = controller->GetInputFontWeight();
314 width = controller->GetInputFontWidth();
319 slant = controller->GetInputFontSlant();
323 case FontStyle::PLACEHOLDER:
325 // The type is FontStyle::PLACEHOLDER
326 weightDefined = controller->IsPlaceholderTextFontWeightDefined();
327 widthDefined = controller->IsPlaceholderTextFontWidthDefined();
328 slantDefined = controller->IsPlaceholderTextFontSlantDefined();
332 weight = controller->GetPlaceholderTextFontWeight();
337 width = controller->GetPlaceholderTextFontWidth();
342 slant = controller->GetPlaceholderTextFontSlant();
354 if(TextAbstraction::FontWeight::NONE != weight)
356 const std::string weightStr(GetEnumerationName(weight,
357 FONT_WEIGHT_STRING_TABLE,
358 FONT_WEIGHT_STRING_TABLE_COUNT));
360 map.Insert(WEIGHT_KEY, weightStr);
366 if(TextAbstraction::FontWidth::NONE != width)
368 const std::string widthStr(GetEnumerationName(width,
369 FONT_WIDTH_STRING_TABLE,
370 FONT_WIDTH_STRING_TABLE_COUNT));
372 map.Insert(WIDTH_KEY, widthStr);
378 if(TextAbstraction::FontSlant::NONE != slant)
380 const std::string slantStr(GetEnumerationName(slant,
381 FONT_SLANT_STRING_TABLE,
382 FONT_SLANT_STRING_TABLE_COUNT));
384 map.Insert(SLANT_KEY, slantStr);
392 std::string fontStyleProperties = "{";
396 if(TextAbstraction::FontWeight::NONE != weight)
398 const std::string weightStr(GetEnumerationName(weight,
399 FONT_WEIGHT_STRING_TABLE,
400 FONT_WEIGHT_STRING_TABLE_COUNT));
402 fontStyleProperties += "\"weight\":\"" + weightStr + "\",";
408 if(TextAbstraction::FontWidth::NONE != width)
410 const std::string widthStr(GetEnumerationName(width,
411 FONT_WIDTH_STRING_TABLE,
412 FONT_WIDTH_STRING_TABLE_COUNT));
413 fontStyleProperties += "\"width\":\"" + widthStr + "\",";
419 if(TextAbstraction::FontSlant::NONE != slant)
421 const std::string slantStr(GetEnumerationName(slant,
422 FONT_SLANT_STRING_TABLE,
423 FONT_SLANT_STRING_TABLE_COUNT));
425 fontStyleProperties += "\"slant\":\"" + slantStr + "\"";
429 // If last character is comma, it will be removed.
430 if((*fontStyleProperties.rbegin()) == ',')
432 fontStyleProperties = fontStyleProperties.substr(0, fontStyleProperties.size() - 1);
434 fontStyleProperties += "}";
436 value = fontStyleProperties;
441 FontWeight StringToWeight(const char* const weightStr)
443 FontWeight weight = TextAbstraction::FontWeight::NORMAL;
444 Scripting::GetEnumeration<FontWeight>(weightStr,
445 FONT_WEIGHT_STRING_TABLE,
446 FONT_WEIGHT_STRING_TABLE_COUNT,
452 FontWidth StringToWidth(const char* const widthStr)
454 FontWidth width = TextAbstraction::FontWidth::NORMAL;
455 Scripting::GetEnumeration<FontWidth>(widthStr,
456 FONT_WIDTH_STRING_TABLE,
457 FONT_WIDTH_STRING_TABLE_COUNT,
463 FontSlant StringToSlant(const char* const slantStr)
465 FontSlant slant = TextAbstraction::FontSlant::NORMAL;
466 Scripting::GetEnumeration<FontSlant>(slantStr,
467 FONT_SLANT_STRING_TABLE,
468 FONT_SLANT_STRING_TABLE_COUNT,
476 } // namespace Toolkit