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 std::string STYLE_KEY("style");
37 const std::string WEIGHT_KEY("weight");
38 const std::string WIDTH_KEY("width");
39 const std::string SLANT_KEY("slant");
40 const std::string FAMILY_KEY("family");
41 const std::string TYPE_KEY("type");
43 const std::string SYSTEM_TOKEN("system");
47 void SetFontFamilyProperty(ControllerPtr controller, const Property::Value& value)
51 const std::string fontFamilyValue = value.Get<std::string>();
53 if(fontFamilyValue.empty())
55 // Resets the default's font family name.
56 controller->SetDefaultFontFamily("");
61 ParsePropertyString(fontFamilyValue, map);
65 // There is no map. The font has been passed as a font's family name with no format.
66 controller->SetDefaultFontFamily(fontFamilyValue);
71 Property::Value* familyValue = map.Find(FAMILY_KEY);
73 std::string fontFamilyName;
74 if(NULL != familyValue)
76 fontFamilyName = familyValue->Get<std::string>();
80 Property::Value* typeValue = map.Find(TYPE_KEY);
85 typeStr = typeValue->Get<std::string>();
88 if(TokenComparison(SYSTEM_TOKEN, typeStr.c_str(), typeStr.size()))
90 controller->UpdateAfterFontChange(fontFamilyName);
94 controller->SetDefaultFontFamily(fontFamilyName);
100 void SetFontStyleProperty(ControllerPtr controller, const Property::Value& value, FontStyle::Type type)
105 if(Property::STRING == value.GetType())
107 const std::string& fontStyleProperties = value.Get<std::string>();
109 ParsePropertyString(fontStyleProperties, map);
110 controller->FontStyleSetByString(true);
114 map = value.Get<Property::Map>();
115 controller->FontStyleSetByString(false);
121 Property::Value* weightValue = map.Find(WEIGHT_KEY);
123 FontWeight weight = TextAbstraction::FontWeight::NONE;
124 const bool weightDefined = weightValue != NULL;
127 const std::string weightStr = weightValue->Get<std::string>();
129 Scripting::GetEnumeration<FontWeight>(weightStr.c_str(),
130 FONT_WEIGHT_STRING_TABLE,
131 FONT_WEIGHT_STRING_TABLE_COUNT,
136 Property::Value* widthValue = map.Find(WIDTH_KEY);
138 FontWidth width = TextAbstraction::FontWidth::NONE;
139 const bool widthDefined = widthValue != NULL;
142 const std::string widthStr = widthValue->Get<std::string>();
144 Scripting::GetEnumeration<FontWidth>(widthStr.c_str(),
145 FONT_WIDTH_STRING_TABLE,
146 FONT_WIDTH_STRING_TABLE_COUNT,
151 Property::Value* slantValue = map.Find(SLANT_KEY);
153 FontSlant slant = TextAbstraction::FontSlant::NONE;
154 const bool slantDefined = slantValue != NULL;
157 const std::string slantStr = slantValue->Get<std::string>();
159 Scripting::GetEnumeration<FontSlant>(slantStr.c_str(),
160 FONT_SLANT_STRING_TABLE,
161 FONT_SLANT_STRING_TABLE_COUNT,
167 case FontStyle::DEFAULT:
169 // Sets the default font's style values.
171 (weightDefined && (controller->GetDefaultFontWeight() != weight)))
173 controller->SetDefaultFontWeight(weight);
177 (widthDefined && (controller->GetDefaultFontWidth() != width)))
179 controller->SetDefaultFontWidth(width);
183 (slantDefined && (controller->GetDefaultFontSlant() != slant)))
185 controller->SetDefaultFontSlant(slant);
189 case FontStyle::INPUT:
191 // Sets the input font's style values.
193 (weightDefined && (controller->GetInputFontWeight() != weight)))
195 controller->SetInputFontWeight(weight);
199 (widthDefined && (controller->GetInputFontWidth() != width)))
201 controller->SetInputFontWidth(width);
205 (slantDefined && (controller->GetInputFontSlant() != slant)))
207 controller->SetInputFontSlant(slant);
211 case FontStyle::PLACEHOLDER:
213 // Sets the placeholder text font's style values.
215 (weightDefined && (controller->GetPlaceholderTextFontWeight() != weight)))
217 controller->SetPlaceholderTextFontWeight(weight);
221 (widthDefined && (controller->GetPlaceholderTextFontWidth() != width)))
223 controller->SetPlaceholderTextFontWidth(width);
227 (slantDefined && (controller->GetPlaceholderTextFontSlant() != slant)))
229 controller->SetPlaceholderTextFontSlant(slant);
239 case FontStyle::DEFAULT:
241 controller->SetDefaultFontWeight(TextAbstraction::FontWeight::NONE);
242 controller->SetDefaultFontWidth(TextAbstraction::FontWidth::NONE);
243 controller->SetDefaultFontSlant(TextAbstraction::FontSlant::NONE);
246 case FontStyle::INPUT:
248 controller->SetInputFontWeight(TextAbstraction::FontWeight::NONE);
249 controller->SetInputFontWidth(TextAbstraction::FontWidth::NONE);
250 controller->SetInputFontSlant(TextAbstraction::FontSlant::NONE);
253 case FontStyle::PLACEHOLDER:
255 controller->SetPlaceholderTextFontWeight(TextAbstraction::FontWeight::NONE);
256 controller->SetPlaceholderTextFontWidth(TextAbstraction::FontWidth::NONE);
257 controller->SetPlaceholderTextFontSlant(TextAbstraction::FontSlant::NONE);
265 void GetFontStyleProperty(ControllerPtr controller, Property::Value& value, FontStyle::Type type)
269 const bool isSetbyString = controller->IsFontStyleSetByString();
271 bool weightDefined = false;
272 bool widthDefined = false;
273 bool slantDefined = false;
274 FontWeight weight = TextAbstraction::FontWeight::NONE;
275 FontWidth width = TextAbstraction::FontWidth::NONE;
276 FontSlant slant = TextAbstraction::FontSlant::NONE;
280 case FontStyle::DEFAULT:
282 weightDefined = controller->IsDefaultFontWeightDefined();
283 widthDefined = controller->IsDefaultFontWidthDefined();
284 slantDefined = controller->IsDefaultFontSlantDefined();
288 weight = controller->GetDefaultFontWeight();
293 width = controller->GetDefaultFontWidth();
298 slant = controller->GetDefaultFontSlant();
302 case FontStyle::INPUT:
304 weightDefined = controller->IsInputFontWeightDefined();
305 widthDefined = controller->IsInputFontWidthDefined();
306 slantDefined = controller->IsInputFontSlantDefined();
310 weight = controller->GetInputFontWeight();
315 width = controller->GetInputFontWidth();
320 slant = controller->GetInputFontSlant();
324 case FontStyle::PLACEHOLDER:
326 // The type is FontStyle::PLACEHOLDER
327 weightDefined = controller->IsPlaceholderTextFontWeightDefined();
328 widthDefined = controller->IsPlaceholderTextFontWidthDefined();
329 slantDefined = controller->IsPlaceholderTextFontSlantDefined();
333 weight = controller->GetPlaceholderTextFontWeight();
338 width = controller->GetPlaceholderTextFontWidth();
343 slant = controller->GetPlaceholderTextFontSlant();
355 if(TextAbstraction::FontWeight::NONE != weight)
357 const std::string weightStr(GetEnumerationName(weight,
358 FONT_WEIGHT_STRING_TABLE,
359 FONT_WEIGHT_STRING_TABLE_COUNT));
361 map.Insert(WEIGHT_KEY, weightStr);
367 if(TextAbstraction::FontWidth::NONE != width)
369 const std::string widthStr(GetEnumerationName(width,
370 FONT_WIDTH_STRING_TABLE,
371 FONT_WIDTH_STRING_TABLE_COUNT));
373 map.Insert(WIDTH_KEY, widthStr);
379 if(TextAbstraction::FontSlant::NONE != slant)
381 const std::string slantStr(GetEnumerationName(slant,
382 FONT_SLANT_STRING_TABLE,
383 FONT_SLANT_STRING_TABLE_COUNT));
385 map.Insert(SLANT_KEY, slantStr);
393 std::string fontStyleProperties = "{";
397 if(TextAbstraction::FontWeight::NONE != weight)
399 const std::string weightStr(GetEnumerationName(weight,
400 FONT_WEIGHT_STRING_TABLE,
401 FONT_WEIGHT_STRING_TABLE_COUNT));
403 fontStyleProperties += "\"weight\":\"" + weightStr + "\",";
409 if(TextAbstraction::FontWidth::NONE != width)
411 const std::string widthStr(GetEnumerationName(width,
412 FONT_WIDTH_STRING_TABLE,
413 FONT_WIDTH_STRING_TABLE_COUNT));
414 fontStyleProperties += "\"width\":\"" + widthStr + "\",";
420 if(TextAbstraction::FontSlant::NONE != slant)
422 const std::string slantStr(GetEnumerationName(slant,
423 FONT_SLANT_STRING_TABLE,
424 FONT_SLANT_STRING_TABLE_COUNT));
426 fontStyleProperties += "\"slant\":\"" + slantStr + "\"";
430 // If last character is comma, it will be removed.
431 if((*fontStyleProperties.rbegin()) == ',')
433 fontStyleProperties = fontStyleProperties.substr(0, fontStyleProperties.size() - 1);
435 fontStyleProperties += "}";
437 value = fontStyleProperties;
442 FontWeight StringToWeight(const char* const weightStr)
444 FontWeight weight = TextAbstraction::FontWeight::NORMAL;
445 Scripting::GetEnumeration<FontWeight>(weightStr,
446 FONT_WEIGHT_STRING_TABLE,
447 FONT_WEIGHT_STRING_TABLE_COUNT,
453 FontWidth StringToWidth(const char* const widthStr)
455 FontWidth width = TextAbstraction::FontWidth::NORMAL;
456 Scripting::GetEnumeration<FontWidth>(widthStr,
457 FONT_WIDTH_STRING_TABLE,
458 FONT_WIDTH_STRING_TABLE_COUNT,
464 FontSlant StringToSlant(const char* const slantStr)
466 FontSlant slant = TextAbstraction::FontSlant::NORMAL;
467 Scripting::GetEnumeration<FontSlant>(slantStr,
468 FONT_SLANT_STRING_TABLE,
469 FONT_SLANT_STRING_TABLE_COUNT,
477 } // namespace Toolkit