Fixed clipboard disappearing issue on selection
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / markup-processor-font.cpp
1 /*
2  * Copyright (c) 2016 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
33 namespace Toolkit
34 {
35
36 namespace Text
37 {
38
39 namespace
40 {
41 const std::string XHTML_FAMILY_ATTRIBUTE("family");
42 const std::string XHTML_SIZE_ATTRIBUTE("size");
43 const std::string XHTML_WEIGHT_ATTRIBUTE("weight");
44 const std::string XHTML_WIDTH_ATTRIBUTE("width");
45 const std::string XHTML_SLANT_ATTRIBUTE("slant");
46
47 const unsigned int MAX_FONT_ATTRIBUTE_SIZE = 15u; ///< The maximum length of any of the possible 'weight', 'width' or 'slant' values.
48 }
49
50 void ProcessFontTag( const Tag& tag, FontDescriptionRun& fontRun )
51 {
52   for( Vector<Attribute>::ConstIterator it = tag.attributes.Begin(),
53          endIt = tag.attributes.End();
54        it != endIt;
55        ++it )
56   {
57     const Attribute& attribute( *it );
58     if( TokenComparison( XHTML_FAMILY_ATTRIBUTE, attribute.nameBuffer, attribute.nameLength ) )
59     {
60       fontRun.familyDefined = true;
61       fontRun.familyLength = attribute.valueLength;
62       fontRun.familyName = new char[fontRun.familyLength];
63       memcpy( fontRun.familyName, attribute.valueBuffer, fontRun.familyLength );
64       // The memory is freed when the font run is removed from the logical model.
65     }
66     else if( TokenComparison( XHTML_SIZE_ATTRIBUTE, attribute.nameBuffer, attribute.nameLength ) )
67     {
68       // 64.f is used to convert from point size to 26.6 pixel format.
69       fontRun.size = static_cast<PointSize26Dot6>( StringToFloat( attribute.valueBuffer ) * 64.f );
70       fontRun.sizeDefined = true;
71     }
72     else if( TokenComparison( XHTML_WEIGHT_ATTRIBUTE, attribute.nameBuffer, attribute.nameLength ) )
73     {
74       // The StringToWeight() uses the Scripting::GetEnumeration() function which requires the input string to end with a '\0' char.
75       char value[MAX_FONT_ATTRIBUTE_SIZE+1u];
76       const Length length = attribute.valueLength > MAX_FONT_ATTRIBUTE_SIZE ? MAX_FONT_ATTRIBUTE_SIZE : attribute.valueLength;
77       memcpy( value, attribute.valueBuffer, length );
78       value[length] = 0;
79
80       fontRun.weight = StringToWeight( value );
81       fontRun.weightDefined = true;
82     }
83     else if( TokenComparison( XHTML_WIDTH_ATTRIBUTE, attribute.nameBuffer, attribute.nameLength ) )
84     {
85       // The StringToWidth() uses the Scripting::GetEnumeration() function which requires the input string to end with a '\0' char.
86       char value[MAX_FONT_ATTRIBUTE_SIZE+1u];
87       const Length length = attribute.valueLength > MAX_FONT_ATTRIBUTE_SIZE ? MAX_FONT_ATTRIBUTE_SIZE : attribute.valueLength;
88       memcpy( value, attribute.valueBuffer, length );
89       value[length] = 0;
90
91       fontRun.width = StringToWidth( value );
92       fontRun.widthDefined = true;
93     }
94     else if( TokenComparison( XHTML_SLANT_ATTRIBUTE, attribute.nameBuffer, attribute.nameLength ) )
95     {
96       // The StringToSlant() uses the Scripting::GetEnumeration() function which requires the input string to end with a '\0' char.
97       char value[MAX_FONT_ATTRIBUTE_SIZE+1u];
98       const Length length = attribute.valueLength > MAX_FONT_ATTRIBUTE_SIZE ? MAX_FONT_ATTRIBUTE_SIZE : attribute.valueLength;
99       memcpy( value, attribute.valueBuffer, length );
100       value[length] = 0;
101
102       fontRun.slant = StringToSlant( value );
103       fontRun.slantDefined = true;
104     }
105   }
106 }
107
108 } // namespace Text
109
110 } // namespace Toolkit
111
112 } // namespace Dali