Rendering API clean-up
[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                             Vector<FontId>& fontIds,
35                             const TextAbstraction::FontDescription& defaultFontDescription,
36                             TextAbstraction::PointSize26Dot6 defaultPointSize,
37                             CharacterIndex startIndex,
38                             Length numberOfCharacters )
39 {
40   // Get the handle to the font client.
41   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
42
43   // Pointer to the font id buffer.
44   FontId* fontIdsBuffer = fontIds.Begin();
45
46   // Traverse all the characters.
47   for( CharacterIndex index = startIndex; index < numberOfCharacters; ++index )
48   {
49     // The default font description and font point size.
50     TextAbstraction::FontDescription fontDescription = defaultFontDescription;
51     TextAbstraction::PointSize26Dot6 fontSize = defaultPointSize;
52     bool defaultFont = true;
53
54     // Traverse all the font descriptions.
55     for( Vector<FontDescriptionRun>::ConstIterator it = fontDescriptions.Begin(),
56            endIt = fontDescriptions.End();
57          it != endIt;
58          ++it )
59     {
60       // Check whether the character's font is modified by the current font description.
61       const FontDescriptionRun& fontRun = *it;
62       if( ( index >= fontRun.characterRun.characterIndex ) &&
63           ( index < fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters ) )
64       {
65         if( fontRun.familyDefined )
66         {
67           fontDescription.family = std::string( fontRun.familyName, fontRun.familyLength );
68           defaultFont = false;
69         }
70         if( fontRun.weightDefined )
71         {
72           fontDescription.weight = fontRun.weight;
73           defaultFont = false;
74         }
75         if( fontRun.widthDefined )
76         {
77           fontDescription.width = fontRun.width;
78           defaultFont = false;
79         }
80         if( fontRun.slantDefined )
81         {
82           fontDescription.slant = fontRun.slant;
83           defaultFont = false;
84         }
85         if( fontRun.sizeDefined )
86         {
87           fontSize = fontRun.size;
88           defaultFont = false;
89         }
90       }
91     }
92
93     // Get the font id if is not the default font.
94     if( !defaultFont )
95     {
96       *( fontIdsBuffer + index - startIndex ) = fontClient.GetFontId( fontDescription, fontSize );
97     }
98   }
99 }
100
101 Script GetScript( Length index,
102                   Vector<ScriptRun>::ConstIterator& scriptRunIt,
103                   const Vector<ScriptRun>::ConstIterator& scriptRunEndIt )
104 {
105   Script script = TextAbstraction::UNKNOWN;
106
107   while( scriptRunIt != scriptRunEndIt )
108   {
109     const ScriptRun& scriptRun = *scriptRunIt;
110
111     if( index >= scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters )
112     {
113       ++scriptRunIt;
114     }
115     else if( index >= scriptRun.characterRun.characterIndex )
116     {
117       script = scriptRun.script;
118
119       if( index + 1u == scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters )
120       {
121         // All the characters of the current run have been traversed. Get the next one for the next iteration.
122         ++scriptRunIt;
123       }
124
125       break;
126     }
127   }
128
129   return script;
130 }
131
132 } // namespace Text
133
134 } // namespace Toolkit
135
136 } // namespace Dali