0f4a11482678c284476a545c78e948becfab6adb
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / 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/internal/text/shaper.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/text-abstraction/script.h>
23 #include <dali/devel-api/text-abstraction/shaping.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/font-run.h>
27 #include <dali-toolkit/internal/text/logical-model-impl.h>
28 #include <dali-toolkit/internal/text/script-run.h>
29 #include <dali-toolkit/internal/text/visual-model-impl.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>& glyphToCharacterMap,
52                 Vector<Length>& charactersPerGlyph,
53                 Vector<GlyphIndex>& newParagraphGlyphs )
54 {
55   const Length numberOfCharacters = text.Count();
56
57   if( 0u == numberOfCharacters )
58   {
59     // Nothing to do if there are no characters.
60     return;
61   }
62
63 #ifdef DEBUG_ENABLED
64   const Length numberOfFontRuns = fonts.Count();
65   const Length numberOfScriptRuns = scripts.Count();
66 #endif
67
68   DALI_ASSERT_DEBUG( ( 0u != numberOfFontRuns ) &&
69                      ( numberOfCharacters == fonts[numberOfFontRuns - 1u].characterRun.characterIndex + fonts[numberOfFontRuns - 1u].characterRun.numberOfCharacters ) &&
70                      "Toolkit::Text::ShapeText. All characters must have a font set." );
71
72   DALI_ASSERT_DEBUG( ( 0u != numberOfScriptRuns ) &&
73                      ( numberOfCharacters == scripts[numberOfScriptRuns - 1u].characterRun.characterIndex + scripts[numberOfScriptRuns - 1u].characterRun.numberOfCharacters ) &&
74                      "Toolkit::Text::ShapeText. All characters must have a script set." );
75
76   // The text needs to be split in chunks of consecutive characters.
77   // Each chunk must contain characters with the same font id and script set.
78   // A chunk of consecutive characters must not contain a LINE_MUST_BREAK, if there is one a new chunk has to be created.
79
80   TextAbstraction::Shaping shaping = TextAbstraction::Shaping::Get();
81
82   // To shape the text a font and an script is needed.
83   Vector<FontRun>::ConstIterator fontRunIt = fonts.Begin();
84   Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
85
86   // Index to the the next one to be shaped. Is pointing the character after the last one it was shaped.
87   CharacterIndex previousIndex = 0u;
88
89   // The current font id and script used to shape the text.
90   FontId currentFontId = 0u;
91   Script currentScript = TextAbstraction::UNKNOWN;
92
93   // Reserve some space to allocate the glyphs and the glyph to character map.
94   // There is no way to know the number of glyphs before shaping the text.
95   // To avoid reallocations it's reserved space for a slightly biger number of glyphs than the number of characters.
96
97   Length numberOfGlyphsReserved = static_cast<Length>( numberOfCharacters * 1.3f );
98   glyphs.Resize( numberOfGlyphsReserved );
99   glyphToCharacterMap.Resize( numberOfGlyphsReserved );
100
101   // The actual number of glyphs.
102   Length totalNumberOfGlyphs = 0u;
103
104   const Character* const textBuffer = text.Begin();
105   const LineBreakInfo* const lineBreakInfoBuffer = lineBreakInfo.Begin();
106   GlyphInfo* glyphsBuffer = glyphs.Begin();
107   CharacterIndex* glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
108
109   // Traverse the characters and shape the text.
110   for( previousIndex = 0; previousIndex < numberOfCharacters; )
111   {
112     // Get the font id and the script.
113     const FontRun& fontRun = *fontRunIt;
114     const ScriptRun& scriptRun = *scriptRunIt;
115
116     currentFontId = fontRun.fontId;
117     currentScript = scriptRun.script;
118
119     // Get the min index to the last character of both runs.
120     CharacterIndex currentIndex = min( fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters,
121                                        scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters );
122
123     // Check if there is a line must break.
124     bool mustBreak = false;
125
126     // Check if the current index is a new paragraph character.
127     // A new paragraph character is going to be shaped in order to not to mess the conversion tables.
128     // However, the metrics need to be changed in order to not to draw a square.
129     bool isNewParagraph = false;
130
131     for( CharacterIndex index = previousIndex; index < currentIndex; ++index )
132     {
133       mustBreak = TextAbstraction::LINE_MUST_BREAK == *( lineBreakInfoBuffer + index );
134       if( mustBreak )
135       {
136         isNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + index ) );
137         currentIndex = index + 1u;
138         break;
139       }
140     }
141
142     // Shape the text for the current chunk.
143     const Length numberOfGlyphs = shaping.Shape( textBuffer + previousIndex,
144                                                  ( currentIndex - previousIndex ), // The number of characters to shape.
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       glyphToCharacterMap.Resize( numberOfGlyphsReserved );
157
158       // Iterators are not valid anymore, set them again.
159       glyphsBuffer = glyphs.Begin();
160       glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
161     }
162
163     // Retrieve the glyphs and the glyph to character conversion map.
164     shaping.GetGlyphs( glyphsBuffer + glyphIndex,
165                        glyphToCharacterMapBuffer + glyphIndex );
166
167     if( isNewParagraph )
168     {
169       // Add the index of the new paragraph glyph to a vector.
170       // Their metrics will be updated in a following step.
171       newParagraphGlyphs.PushBack( totalNumberOfGlyphs - 1u );
172     }
173
174     // Update indices.
175     if( 0u != glyphIndex )
176     {
177       for( Length index = glyphIndex; index < glyphIndex + numberOfGlyphs; ++index )
178       {
179         CharacterIndex& characterIndex = *( glyphToCharacterMapBuffer + index );
180         characterIndex += previousIndex;
181       }
182     }
183
184     // Update the iterators to get the next font or script run.
185     if( currentIndex == fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters )
186     {
187       ++fontRunIt;
188     }
189     if( currentIndex == scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters )
190     {
191       ++scriptRunIt;
192     }
193
194     // Update the previous index.
195     previousIndex = currentIndex;
196   }
197
198   // Add the number of characters per glyph.
199   charactersPerGlyph.Reserve( totalNumberOfGlyphs );
200   previousIndex = 0u;
201   for( Length index = 1u; index < totalNumberOfGlyphs; ++index )
202   {
203     const CharacterIndex characterIndex = *( glyphToCharacterMapBuffer + index );
204
205     charactersPerGlyph.PushBack( characterIndex - previousIndex );
206
207     previousIndex = characterIndex;
208   }
209
210   charactersPerGlyph.PushBack( numberOfCharacters - previousIndex );
211
212   // Resize the vectors to set the right number of items.
213   glyphs.Resize( totalNumberOfGlyphs );
214   glyphToCharacterMap.Resize( totalNumberOfGlyphs );
215 }
216
217 } // namespace Text
218
219 } // namespace Toolkit
220
221 } // namespace Dali