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