0c469b8fb0a7c981651824486e7140429f44a05d
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / markup-processor-font.cpp
1 /*
2  * Copyright (c) 2021 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/markup-processor-font.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/dali-vector.h>
23 #include <memory.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/font-description-run.h>
27 #include <dali-toolkit/internal/text/markup-processor-attribute-helper-functions.h>
28 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
29 #include <dali-toolkit/internal/text/text-font-style.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 namespace Text
36 {
37 namespace
38 {
39 const std::string XHTML_FAMILY_ATTRIBUTE("family");
40 const std::string XHTML_SIZE_ATTRIBUTE("size");
41 const std::string XHTML_WEIGHT_ATTRIBUTE("weight");
42 const std::string XHTML_WIDTH_ATTRIBUTE("width");
43 const std::string XHTML_SLANT_ATTRIBUTE("slant");
44
45 const std::string  FONT_PREFIX("font-");
46 const unsigned int FONT_PREFIX_LENGTH      = 5u;
47 const unsigned int MIN_FONT_ATTRIBUTE_SIZE = 4u;   ///< The minimum length of any of the possible 'weight', 'width' , 'slant' or 'size' values.
48 const unsigned int MAX_FONT_ATTRIBUTE_SIZE = 15u;  ///< The maximum length of any of the possible 'weight', 'width' or 'slant' values.
49 const float        PIXEL_FORMAT_64_FACTOR  = 64.f; ///< 64.f is used to convert from point size to 26.6 pixel format.
50 } // namespace
51
52 void processFontAttributeValue(char value[], const Attribute& attribute)
53 {
54   // The StringToWeight() uses the Scripting::GetEnumeration() function which requires the input string to end with a '\0' char.
55   const Length length = attribute.valueLength > MAX_FONT_ATTRIBUTE_SIZE ? MAX_FONT_ATTRIBUTE_SIZE : attribute.valueLength;
56   memcpy(value, attribute.valueBuffer, length);
57   value[length] = 0;
58 }
59
60 void ProcessFontFamily(const Attribute& attribute, FontDescriptionRun& fontRun)
61 {
62   fontRun.familyDefined = true;
63   fontRun.familyLength  = attribute.valueLength;
64   fontRun.familyName    = new char[fontRun.familyLength];
65   memcpy(fontRun.familyName, attribute.valueBuffer, fontRun.familyLength);
66   // The memory is freed when the font run is removed from the logical model.
67 }
68
69 void ProcessFontSize(const Attribute& attribute, FontDescriptionRun& fontRun)
70 {
71   // 64.f is used to convert from point size to 26.6 pixel format.
72   fontRun.size        = static_cast<PointSize26Dot6>(ProcessFloatAttribute(attribute) * PIXEL_FORMAT_64_FACTOR);
73   fontRun.sizeDefined = true;
74 }
75
76 void ProcessFontWeight(const Attribute& attribute, FontDescriptionRun& fontRun)
77 {
78   fontRun.weightDefined = ProcessEnumerationAttribute<FontWeight>(attribute, MAX_FONT_ATTRIBUTE_SIZE, &StringToWeight, fontRun.weight);
79 }
80
81 void ProcessFontWidth(const Attribute& attribute, FontDescriptionRun& fontRun)
82 {
83   fontRun.widthDefined = ProcessEnumerationAttribute<FontWidth>(attribute, MAX_FONT_ATTRIBUTE_SIZE, &StringToWidth, fontRun.width);
84 }
85
86 void ProcessFontSlant(const Attribute& attribute, FontDescriptionRun& fontRun)
87 {
88   fontRun.slantDefined = ProcessEnumerationAttribute<FontSlant>(attribute, MAX_FONT_ATTRIBUTE_SIZE, &StringToSlant, fontRun.slant);
89 }
90
91 void ProcessFontTag(const Tag& tag, FontDescriptionRun& fontRun)
92 {
93   for(Vector<Attribute>::ConstIterator it    = tag.attributes.Begin(),
94                                        endIt = tag.attributes.End();
95       it != endIt;
96       ++it)
97   {
98     const Attribute& attribute(*it);
99
100     if(TokenComparison(XHTML_FAMILY_ATTRIBUTE, attribute.nameBuffer, attribute.nameLength))
101     {
102       ProcessFontFamily(attribute, fontRun);
103     }
104     else if(TokenComparison(XHTML_SIZE_ATTRIBUTE, attribute.nameBuffer, attribute.nameLength))
105     {
106       ProcessFontSize(attribute, fontRun);
107     }
108     else if(TokenComparison(XHTML_WEIGHT_ATTRIBUTE, attribute.nameBuffer, attribute.nameLength))
109     {
110       ProcessFontWeight(attribute, fontRun);
111     }
112     else if(TokenComparison(XHTML_WIDTH_ATTRIBUTE, attribute.nameBuffer, attribute.nameLength))
113     {
114       ProcessFontWidth(attribute, fontRun);
115     }
116     else if(TokenComparison(XHTML_SLANT_ATTRIBUTE, attribute.nameBuffer, attribute.nameLength))
117     {
118       ProcessFontSlant(attribute, fontRun);
119     }
120   }
121 }
122
123 } // namespace Text
124
125 } // namespace Toolkit
126
127 } // namespace Dali