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