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