2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/text/shaper.h>
22 #include <dali/devel-api/text-abstraction/shaping.h>
33 CharacterIndex min( CharacterIndex index0,
34 CharacterIndex index1 )
36 return ( index0 < index1 ) ? index0 : index1;
39 void ShapeText( const Vector<Character>& text,
40 const Vector<LineBreakInfo>& lineBreakInfo,
41 const Vector<ScriptRun>& scripts,
42 const Vector<FontRun>& fonts,
43 CharacterIndex startCharacterIndex,
44 GlyphIndex startGlyphIndex,
45 Length numberOfCharacters,
46 Vector<GlyphInfo>& glyphs,
47 Vector<CharacterIndex>& glyphToCharacterMap,
48 Vector<Length>& charactersPerGlyph,
49 Vector<GlyphIndex>& newParagraphGlyphs )
51 if( 0u == numberOfCharacters )
53 // Nothing to do if there are no characters.
58 const Length numberOfFontRuns = fonts.Count();
59 const Length numberOfScriptRuns = scripts.Count();
60 const Length totalNumberOfCharacters = text.Count();
63 DALI_ASSERT_DEBUG( ( 0u != numberOfFontRuns ) &&
64 ( totalNumberOfCharacters == fonts[numberOfFontRuns - 1u].characterRun.characterIndex + fonts[numberOfFontRuns - 1u].characterRun.numberOfCharacters ) &&
65 "Toolkit::Text::ShapeText. All characters must have a font set." );
67 DALI_ASSERT_DEBUG( ( 0u != numberOfScriptRuns ) &&
68 ( totalNumberOfCharacters == scripts[numberOfScriptRuns - 1u].characterRun.characterIndex + scripts[numberOfScriptRuns - 1u].characterRun.numberOfCharacters ) &&
69 "Toolkit::Text::ShapeText. All characters must have a script set." );
71 // The text needs to be split in chunks of consecutive characters.
72 // Each chunk must contain characters with the same font id and script set.
73 // A chunk of consecutive characters must not contain a LINE_MUST_BREAK, if there is one a new chunk has to be created.
75 TextAbstraction::Shaping shaping = TextAbstraction::Shaping::Get();
77 // To shape the text a font and an script is needed.
79 // Get the font run containing the startCharacterIndex character.
80 Vector<FontRun>::ConstIterator fontRunIt = fonts.Begin();
81 for( Vector<FontRun>::ConstIterator endIt = fonts.End(); fontRunIt < endIt; ++fontRunIt )
83 const FontRun& run = *fontRunIt;
84 if( startCharacterIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
91 // Get the script run containing the startCharacterIndex character.
92 Vector<ScriptRun>::ConstIterator scriptRunIt = scripts.Begin();
93 for( Vector<ScriptRun>::ConstIterator endIt = scripts.End(); scriptRunIt < endIt; ++scriptRunIt )
95 const ScriptRun& run = *scriptRunIt;
96 if( startCharacterIndex < run.characterRun.characterIndex + run.characterRun.numberOfCharacters )
103 // Index to the the next one to be shaped. Is pointing the character after the last one it was shaped.
104 CharacterIndex previousIndex = 0u;
106 // The current font id and script used to shape the text.
107 FontId currentFontId = 0u;
108 Script currentScript = TextAbstraction::UNKNOWN;
110 // Reserve some space to allocate the glyphs and the glyph to character map.
111 // There is no way to know the number of glyphs before shaping the text.
112 // To avoid reallocations it's reserved space for a slightly biger number of glyphs than the number of characters.
114 const Length currentNumberOfGlyphs = glyphs.Count();
115 const Length numberOfGlyphsReserved = static_cast<Length>( numberOfCharacters * 1.3f );
116 glyphs.Resize( currentNumberOfGlyphs + numberOfGlyphsReserved );
117 glyphToCharacterMap.Resize( currentNumberOfGlyphs + numberOfGlyphsReserved );
119 // The actual number of glyphs.
120 Length totalNumberOfGlyphs = currentNumberOfGlyphs;
121 // The number of new glyphs.
122 Length numberOfNewGlyphs = 0u;
124 const Character* const textBuffer = text.Begin();
125 const LineBreakInfo* const lineBreakInfoBuffer = lineBreakInfo.Begin();
126 CharacterIndex* glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
128 Length glyphIndex = startGlyphIndex;
130 // Traverse the characters and shape the text.
131 const CharacterIndex lastCharacter = startCharacterIndex + numberOfCharacters;
132 for( previousIndex = startCharacterIndex; previousIndex < lastCharacter; )
134 // Get the font id and the script.
135 const FontRun& fontRun = *fontRunIt;
136 const ScriptRun& scriptRun = *scriptRunIt;
138 currentFontId = fontRun.fontId;
139 currentScript = scriptRun.script;
140 bool softwareItalic = fontRun.softwareItalic;
141 bool softwareBold = fontRun.softwareBold;
143 // Get the min index to the last character of both runs.
144 CharacterIndex currentIndex = min( fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters,
145 scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters );
147 // Check if there is a line must break.
148 bool mustBreak = false;
150 // Check if the current index is a new paragraph character.
151 // A new paragraph character is going to be shaped in order to not to mess the conversion tables.
152 // However, the metrics need to be changed in order to not to draw a square.
153 bool isNewParagraph = false;
155 for( CharacterIndex index = previousIndex; index < currentIndex; ++index )
157 mustBreak = TextAbstraction::LINE_MUST_BREAK == *( lineBreakInfoBuffer + index );
160 isNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + index ) );
161 currentIndex = index + 1u;
166 // Shape the text for the current chunk.
167 const Length numberOfGlyphs = shaping.Shape( textBuffer + previousIndex,
168 ( currentIndex - previousIndex ), // The number of characters to shape.
172 // Retrieve the glyphs and the glyph to character conversion map.
173 Vector<GlyphInfo> tmpGlyphs;
174 Vector<CharacterIndex> tmpGlyphToCharacterMap;
177 glyphInfo.softwareItalic = softwareItalic;
178 glyphInfo.softwareBold = softwareBold;
180 tmpGlyphs.Resize( numberOfGlyphs, glyphInfo );
181 tmpGlyphToCharacterMap.Resize( numberOfGlyphs );
182 shaping.GetGlyphs( tmpGlyphs.Begin(),
183 tmpGlyphToCharacterMap.Begin() );
185 // Update the new indices of the glyph to character map.
186 if( 0u != totalNumberOfGlyphs )
188 for( Vector<CharacterIndex>::Iterator it = tmpGlyphToCharacterMap.Begin(),
189 endIt = tmpGlyphToCharacterMap.End();
193 *it += previousIndex;
197 totalNumberOfGlyphs += numberOfGlyphs;
198 numberOfNewGlyphs += numberOfGlyphs;
200 glyphs.Insert( glyphs.Begin() + glyphIndex, tmpGlyphs.Begin(), tmpGlyphs.End() );
201 glyphToCharacterMap.Insert( glyphToCharacterMap.Begin() + glyphIndex, tmpGlyphToCharacterMap.Begin(), tmpGlyphToCharacterMap.End() );
202 glyphIndex += numberOfGlyphs;
204 // Set the buffer pointers again.
205 glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
209 // Add the index of the new paragraph glyph to a vector.
210 // Their metrics will be updated in a following step.
211 newParagraphGlyphs.PushBack( glyphIndex - 1u );
214 // Update the iterators to get the next font or script run.
215 if( currentIndex == fontRun.characterRun.characterIndex + fontRun.characterRun.numberOfCharacters )
219 if( currentIndex == scriptRun.characterRun.characterIndex + scriptRun.characterRun.numberOfCharacters )
224 // Update the previous index.
225 previousIndex = currentIndex;
229 for( Length index = startGlyphIndex + numberOfNewGlyphs; index < totalNumberOfGlyphs; ++index )
231 CharacterIndex& characterIndex = *( glyphToCharacterMapBuffer + index );
232 characterIndex += numberOfCharacters;
235 // Add the number of characters per glyph.
236 charactersPerGlyph.Reserve( totalNumberOfGlyphs );
237 Length* charactersPerGlyphBuffer = charactersPerGlyph.Begin();
239 const GlyphIndex lastGlyph = startGlyphIndex + numberOfNewGlyphs;
240 previousIndex = startCharacterIndex;
241 for( Length index = startGlyphIndex + 1u; index < lastGlyph; ++index )
243 const CharacterIndex characterIndex = *( glyphToCharacterMapBuffer + index );
245 charactersPerGlyph.Insert( charactersPerGlyphBuffer + index - 1u, characterIndex - previousIndex );
247 previousIndex = characterIndex;
249 charactersPerGlyph.Insert( charactersPerGlyphBuffer + lastGlyph - 1u, numberOfCharacters + startCharacterIndex - previousIndex );
251 // Resize the vectors to set the right number of items.
252 glyphs.Resize( totalNumberOfGlyphs );
253 glyphToCharacterMap.Resize( totalNumberOfGlyphs );
258 } // namespace Toolkit