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