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/markup-processor/markup-processor-font.h>
22 #include <dali/public-api/common/dali-vector.h>
26 #include <dali-toolkit/internal/text/font-description-run.h>
27 #include <dali-toolkit/internal/text/markup-processor/markup-processor-attribute-helper-functions.h>
28 #include <dali-toolkit/internal/text/markup-processor/markup-processor-helper-functions.h>
29 #include <dali-toolkit/internal/text/markup-tags-and-attributes.h>
30 #include <dali-toolkit/internal/text/text-font-style.h>
40 const unsigned int MAX_FONT_ATTRIBUTE_SIZE = 15u; ///< The maximum length of any of the possible 'weight', 'width' or 'slant' values.
41 const float PIXEL_FORMAT_64_FACTOR = 64.f; ///< 64.f is used to convert from point size to 26.6 pixel format.
44 void processFontAttributeValue(char value[], const Attribute& attribute)
46 // The StringToWeight() uses the Scripting::GetEnumeration() function which requires the input string to end with a '\0' char.
47 const Length length = attribute.valueLength > MAX_FONT_ATTRIBUTE_SIZE ? MAX_FONT_ATTRIBUTE_SIZE : attribute.valueLength;
48 memcpy(value, attribute.valueBuffer, length);
52 void ProcessFontFamily(const Attribute& attribute, FontDescriptionRun& fontRun)
54 fontRun.familyDefined = true;
55 fontRun.familyLength = attribute.valueLength;
56 fontRun.familyName = new char[fontRun.familyLength];
57 memcpy(fontRun.familyName, attribute.valueBuffer, fontRun.familyLength);
58 // The memory is freed when the font run is removed from the logical model.
61 void ProcessFontSize(const Attribute& attribute, FontDescriptionRun& fontRun)
63 // 64.f is used to convert from point size to 26.6 pixel format.
64 fontRun.size = static_cast<PointSize26Dot6>(ProcessFloatAttribute(attribute) * PIXEL_FORMAT_64_FACTOR);
65 fontRun.sizeDefined = true;
68 void ProcessFontWeight(const Attribute& attribute, FontDescriptionRun& fontRun)
70 fontRun.weightDefined = ProcessEnumerationAttribute<FontWeight>(attribute, MAX_FONT_ATTRIBUTE_SIZE, &StringToWeight, fontRun.weight);
73 void ProcessFontWidth(const Attribute& attribute, FontDescriptionRun& fontRun)
75 fontRun.widthDefined = ProcessEnumerationAttribute<FontWidth>(attribute, MAX_FONT_ATTRIBUTE_SIZE, &StringToWidth, fontRun.width);
78 void ProcessFontSlant(const Attribute& attribute, FontDescriptionRun& fontRun)
80 fontRun.slantDefined = ProcessEnumerationAttribute<FontSlant>(attribute, MAX_FONT_ATTRIBUTE_SIZE, &StringToSlant, fontRun.slant);
83 void ProcessFontTag(const Tag& tag, FontDescriptionRun& fontRun)
85 for(Vector<Attribute>::ConstIterator it = tag.attributes.Begin(),
86 endIt = tag.attributes.End();
90 const Attribute& attribute(*it);
92 if(TokenComparison(MARKUP::FONT_ATTRIBUTES::FAMILY, attribute.nameBuffer, attribute.nameLength))
94 ProcessFontFamily(attribute, fontRun);
96 else if(TokenComparison(MARKUP::FONT_ATTRIBUTES::SIZE, attribute.nameBuffer, attribute.nameLength))
98 ProcessFontSize(attribute, fontRun);
100 else if(TokenComparison(MARKUP::FONT_ATTRIBUTES::WEIGHT, attribute.nameBuffer, attribute.nameLength))
102 ProcessFontWeight(attribute, fontRun);
104 else if(TokenComparison(MARKUP::FONT_ATTRIBUTES::WIDTH, attribute.nameBuffer, attribute.nameLength))
106 ProcessFontWidth(attribute, fontRun);
108 else if(TokenComparison(MARKUP::FONT_ATTRIBUTES::SLANT, attribute.nameBuffer, attribute.nameLength))
110 ProcessFontSlant(attribute, fontRun);
117 } // namespace Toolkit