Merge "Multi-language support interface" into new_text
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / text / shaper.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 // CLASS HEADER
19 #include <dali-toolkit/public-api/text/shaper.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/text-abstraction/shaping.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/text/font-run.h>
26 #include <dali-toolkit/public-api/text/logical-model.h>
27 #include <dali-toolkit/public-api/text/script.h>
28 #include <dali-toolkit/public-api/text/script-run.h>
29 #include <dali-toolkit/public-api/text/visual-model.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Text
38 {
39
40 CharacterIndex min( CharacterIndex index0,
41                     CharacterIndex index1 )
42 {
43   return ( index0 < index1 ) ? index0 : index1;
44 }
45
46 void ShapeText( const Vector<Character>& text,
47                 const Vector<LineBreakInfo>& lineBreakInfo,
48                 const Vector<ScriptRun>& scripts,
49                 const Vector<FontRun>& fonts,
50                 Vector<GlyphInfo>& glyphs,
51                 Vector<CharacterIndex>& characterIndices,
52                 Vector<Length>& charactersPerGlyph )
53 {
54   const Length numberOfCharacters = text.Count();
55
56   if( 0u == numberOfCharacters )
57   {
58     // Nothing to do if there are no characters.
59     return;
60   }
61
62 #ifdef DEBUG_ENABLED
63   const Length numberOfFontRuns = fonts.Count();
64   const Length numberOfScriptRuns = scripts.Count();
65 #endif
66
67   DALI_ASSERT_DEBUG( ( 0u != numberOfFontRuns ) &&
68                      ( numberOfCharacters == fonts[numberOfFontRuns - 1u].characterRun.characterIndex + fonts[numberOfFontRuns - 1u].characterRun.numberOfCharacters ) &&
69                      "Toolkit::Text::ShapeText. All characters must have a font set." );
70
71   DALI_ASSERT_DEBUG( ( 0u != numberOfScriptRuns ) &&
72                      ( numberOfCharacters == scripts[numberOfScriptRuns - 1u].characterRun.characterIndex + scripts[numberOfScriptRuns - 1u].characterRun.numberOfCharacters ) &&
73                      "Toolkit::Text::ShapeText. All characters must have a script set." );
74
75   // The text needs to be split in chunks of consecutive characters.
76   // Each chunk must contain characters with the same font id and script set.
77   // A chunk of consecutive characters must not contain a LINE_MUST_BREAK, if there is one a new chunk have to be created.
78
79   TextAbstraction::Shaping shaping = TextAbstraction::Shaping::Get();
80
81   // To shape the text a font and an script is needed.
82   Vector<FontRun>::ConstIterator fontRunIt = fonts.Begin();
83   Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
84
85   // Index to the the next one to be shaped. Is pointing the character after the last one it was shaped.
86   CharacterIndex previousIndex = 0u;
87
88   // The current font id and script used to shape the text.
89   FontId currentFontId = 0u;
90   Script currentScript = TextAbstraction::UNKNOWN;
91
92   // Reserve some space to allocate the glyphs and the glyph to character map.
93   // There is no way to know the number of glyphs before shaping the text.
94   // To avoid reallocations it's reserved space for a slightly biger number of glyphs than the number of characters.
95
96   Length numberOfGlyphsReserved = static_cast<Length>( numberOfCharacters * 1.3f );
97   glyphs.Resize( numberOfGlyphsReserved );
98   charactersPerGlyph.Resize( numberOfGlyphsReserved );
99
100   // The actual number of glyphs.
101   Length totalNumberOfGlyphs = 0u;
102
103   const Character* textBuffer = text.Begin();
104   const LineBreakInfo* lineBreakInfoBuffer = lineBreakInfo.Begin();
105   GlyphInfo* glyphsBuffer = glyphs.Begin();
106   Length* charactersPerGlyphBuffer = charactersPerGlyph.Begin();
107
108   // Traverse the characters and shape the text.
109   for( previousIndex = 0; previousIndex < numberOfCharacters; )
110   {
111     // Get the font id and the script.
112     const FontRun& fontRun = *fontRunIt;
113     const ScriptRun& scriptRun = *scriptRunIt;
114
115     currentFontId = fontRun.fontId;
116     currentScript = scriptRun.script;
117
118     // Get the min index to the last character of both runs.
119     CharacterIndex currentIndex = min( fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters,
120                                        scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters );
121
122     // Check if there is a line must break.
123     bool mustBreak = false;
124     for( CharacterIndex index = previousIndex; index < currentIndex; ++index )
125     {
126       mustBreak = TextAbstraction::LINE_MUST_BREAK == *( lineBreakInfoBuffer + index );
127       if( mustBreak )
128       {
129         currentIndex = index;
130         break;
131       }
132     }
133
134     // Check if the current index is a white space. Do not want to shape a \n.
135     // The last character is always a must-break even if it's not a \n.
136     Length numberOfCharactersToShape = currentIndex - previousIndex;
137     if( mustBreak && !IsWhiteSpace( *( textBuffer + currentIndex ) ) )
138     {
139       ++numberOfCharactersToShape;
140     }
141
142     // Shape the text for the current chunk.
143     const Length numberOfGlyphs = shaping.Shape( textBuffer + previousIndex,
144                                                  numberOfCharactersToShape,
145                                                  currentFontId,
146                                                  currentScript );
147
148     const Length glyphIndex = totalNumberOfGlyphs;
149     totalNumberOfGlyphs += numberOfGlyphs;
150
151     if( totalNumberOfGlyphs > numberOfGlyphsReserved )
152     {
153       // Resize the vectors to get enough space.
154       numberOfGlyphsReserved = static_cast<Length>( totalNumberOfGlyphs * 1.3f );
155       glyphs.Resize( numberOfGlyphsReserved );
156       charactersPerGlyph.Resize( numberOfGlyphsReserved );
157
158       // Iterators are not valid anymore, set them again.
159       glyphsBuffer = glyphs.Begin();
160       charactersPerGlyphBuffer = charactersPerGlyph.Begin();
161     }
162
163     // Retrieve the glyphs and the glyph to character conversion map.
164     shaping.GetGlyphs( glyphsBuffer + glyphIndex,
165                        charactersPerGlyphBuffer + glyphIndex );
166
167     // Update the iterators to get the next font or script run.
168     if( currentIndex == fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters )
169     {
170       ++fontRunIt;
171     }
172     if( currentIndex == scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters )
173     {
174       ++scriptRunIt;
175     }
176
177     // Update the previous index. Jumps the \n if needed.
178     previousIndex = mustBreak ? currentIndex + 1u : currentIndex;
179   }
180
181   characterIndices.Reserve( totalNumberOfGlyphs );
182   CharacterIndex characterIndex = 0u;
183   characterIndices.PushBack( characterIndex );
184   for( Length index = 0u, length = totalNumberOfGlyphs - 1u; index < length; ++index )
185   {
186     characterIndex += *( charactersPerGlyphBuffer + index );
187     characterIndices.PushBack( characterIndex );
188   }
189
190   // Resize the vectors to set the right number of items.
191   glyphs.Resize( totalNumberOfGlyphs );
192   // characterIndices.Resize( totalNumberOfGlyphs );
193   charactersPerGlyph.Resize( totalNumberOfGlyphs );
194 }
195
196 void ShapeText( const LogicalModel& logicalModel,
197                 VisualModel& visualModel,
198                 CharacterIndex characterIndex,
199                 Length numberOfCharactersToRemove,
200                 Length numberOfCharactersToInsert )
201 {
202 }
203
204 } // namespace Text
205
206 } // namespace Toolkit
207
208 } // namespace Dali