Automatically dis/connect registered visuals to stage
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / multi-language-helper-functions.cpp
1 /*
2  * Copyright (c) 2015 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/multi-language-helper-functions.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/text-abstraction/font-client.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 namespace Text
31 {
32
33 void MergeFontDescriptions( const Vector<FontDescriptionRun>& fontDescriptions,
34                             const TextAbstraction::FontDescription& defaultFontDescription,
35                             TextAbstraction::PointSize26Dot6 defaultPointSize,
36                             CharacterIndex characterIndex,
37                             TextAbstraction::FontDescription& fontDescription,
38                             TextAbstraction::PointSize26Dot6& fontPointSize,
39                             bool& isDefaultFont )
40 {
41   // Initialize with the default font's point size.
42   fontPointSize = defaultPointSize;
43
44   // Initialize with the style parameters of the default font's style.
45   fontDescription = defaultFontDescription;
46
47   // Initialize as a default font.
48   isDefaultFont = true;
49
50   Length runIndex = 0u;
51
52   Length familyIndex = 0u;
53   Length weightIndex = 0u;
54   Length widthIndex = 0u;
55   Length slantIndex = 0u;
56   Length sizeIndex = 0u;
57
58   bool familyOverriden = false;
59   bool weightOverriden = false;
60   bool widthOverriden = false;
61   bool slantOverriden = false;
62   bool sizeOverriden = false;
63
64   // Traverse all the font descriptions.
65   const FontDescriptionRun* const fontDescriptionsBuffer = fontDescriptions.Begin();
66   for( Vector<FontDescriptionRun>::ConstIterator it = fontDescriptionsBuffer,
67          endIt = fontDescriptions.End();
68        it != endIt;
69        ++it, ++runIndex )
70   {
71     // Check whether the character's font is modified by the current font description.
72     const FontDescriptionRun& fontRun = *it;
73     if( ( characterIndex >= fontRun.characterRun.characterIndex ) &&
74         ( characterIndex < fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters ) )
75     {
76       if( fontRun.familyDefined )
77       {
78         isDefaultFont = false;
79         familyOverriden = true;
80         familyIndex = runIndex;
81       }
82       if( fontRun.weightDefined )
83       {
84         isDefaultFont = false;
85         weightOverriden = true;
86         weightIndex = runIndex;
87       }
88       if( fontRun.widthDefined )
89       {
90         isDefaultFont = false;
91         widthOverriden = true;
92         widthIndex = runIndex;
93       }
94       if( fontRun.slantDefined )
95       {
96         isDefaultFont = false;
97         slantOverriden = true;
98         slantIndex = runIndex;
99       }
100       if( fontRun.sizeDefined )
101       {
102         isDefaultFont = false;
103         sizeOverriden = true;
104         sizeIndex = runIndex;
105       }
106     }
107   }
108
109   // Get the font's description if is not the default font.
110   if( !isDefaultFont )
111   {
112     if( familyOverriden )
113     {
114       const FontDescriptionRun& fontRun = *( fontDescriptionsBuffer + familyIndex );
115       fontDescription.family = std::string( fontRun.familyName, fontRun.familyLength );
116     }
117
118     if( weightOverriden )
119     {
120       const FontDescriptionRun& fontRun = *( fontDescriptionsBuffer + weightIndex );
121       fontDescription.weight = fontRun.weight;
122     }
123
124     if( widthOverriden )
125     {
126       const FontDescriptionRun& fontRun = *( fontDescriptionsBuffer + widthIndex );
127       fontDescription.width = fontRun.width;
128     }
129
130     if( slantOverriden )
131     {
132       const FontDescriptionRun& fontRun = *( fontDescriptionsBuffer + slantIndex );
133       fontDescription.slant = fontRun.slant;
134     }
135
136     if( sizeOverriden )
137     {
138       const FontDescriptionRun& fontRun = *( fontDescriptionsBuffer + sizeIndex );
139       fontPointSize = fontRun.size;
140     }
141   }
142 }
143
144 Script GetScript( Length index,
145                   Vector<ScriptRun>::ConstIterator& scriptRunIt,
146                   const Vector<ScriptRun>::ConstIterator& scriptRunEndIt )
147 {
148   Script script = TextAbstraction::UNKNOWN;
149
150   while( scriptRunIt != scriptRunEndIt )
151   {
152     const ScriptRun& scriptRun = *scriptRunIt;
153
154     if( index >= scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters )
155     {
156       ++scriptRunIt;
157     }
158     else if( index >= scriptRun.characterRun.characterIndex )
159     {
160       script = scriptRun.script;
161
162       if( index + 1u == scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters )
163       {
164         // All the characters of the current run have been traversed. Get the next one for the next iteration.
165         ++scriptRunIt;
166       }
167
168       break;
169     }
170   }
171
172   return script;
173 }
174
175 } // namespace Text
176
177 } // namespace Toolkit
178
179 } // namespace Dali