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 "shaping-impl.h"
22 #include <singleton-service-impl.h>
23 #include <dali/devel-api/text-abstraction/font-client.h>
24 #include <dali/devel-api/text-abstraction/glyph-info.h>
25 #include <dali/integration-api/debug.h>
28 #include <harfbuzz/hb.h>
29 #include <harfbuzz/hb-ft.h>
36 namespace TextAbstraction
42 const char* DEFAULT_LANGUAGE = "en";
43 const unsigned int DEFAULT_LANGUAGE_LENGTH = 2u;
44 const float FROM_266 = 1.0f / 64.0f;
46 const hb_script_t SCRIPT_TO_HARFBUZZ[] =
82 HB_SCRIPT_UNKNOWN, // EMOJI
86 struct Shaping::Plugin
89 : mFreeTypeLibrary( NULL ),
99 FT_Done_FreeType( mFreeTypeLibrary );
104 int error = FT_Init_FreeType( &mFreeTypeLibrary );
105 if( FT_Err_Ok != error )
107 DALI_LOG_ERROR( "FreeType Init error: %d\n", error );
111 Length Shape( const Character* const text,
112 Length numberOfCharacters,
116 // Clear previoursly shaped texts.
119 mCharacterMap.Clear();
123 // Reserve some space to avoid reallocations.
124 const Length numberOfGlyphs = static_cast<Length>( 1.3f * static_cast<float>( numberOfCharacters ) );
125 mIndices.Reserve( numberOfGlyphs );
126 mAdvance.Reserve( numberOfGlyphs );
127 mCharacterMap.Reserve( numberOfGlyphs );
128 mOffset.Reserve( 2u * numberOfGlyphs );
130 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
132 // Get the font's path file name from the font Id.
133 FontDescription fontDescription;
134 fontClient.GetDescription( fontId, fontDescription );
136 // Create a FreeType font's face.
138 FT_Error retVal = FT_New_Face( mFreeTypeLibrary, fontDescription.path.c_str(), 0u, &face );
139 if( FT_Err_Ok != retVal )
141 DALI_LOG_ERROR( "Failed to open face: %s\n", fontDescription.path.c_str() );
145 unsigned int horizontalDpi = 0u;
146 unsigned int verticalDpi = 0u;
147 fontClient.GetDpi( horizontalDpi, verticalDpi );
149 FT_Set_Char_Size( face,
151 fontClient.GetPointSize( fontId ),
155 /* Get our harfbuzz font struct */
156 hb_font_t* harfBuzzFont;
157 harfBuzzFont = hb_ft_font_create( face, NULL );
159 /* Create a buffer for harfbuzz to use */
160 hb_buffer_t* harfBuzzBuffer = hb_buffer_create();
162 const bool rtlDirection = IsRightToLeftScript( script );
163 hb_buffer_set_direction( harfBuzzBuffer,
164 rtlDirection ? HB_DIRECTION_RTL : HB_DIRECTION_LTR ); /* or LTR */
166 hb_buffer_set_script( harfBuzzBuffer,
167 SCRIPT_TO_HARFBUZZ[ script ] ); /* see hb-unicode.h */
169 hb_buffer_set_language( harfBuzzBuffer,
170 hb_language_from_string( DEFAULT_LANGUAGE,
171 DEFAULT_LANGUAGE_LENGTH ) );
173 /* Layout the text */
174 hb_buffer_add_utf32( harfBuzzBuffer, text, numberOfCharacters, 0u, numberOfCharacters );
176 hb_shape( harfBuzzFont, harfBuzzBuffer, NULL, 0u );
179 unsigned int glyphCount;
180 hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( harfBuzzBuffer, &glyphCount );
181 hb_glyph_position_t *glyphPositions = hb_buffer_get_glyph_positions( harfBuzzBuffer, &glyphCount );
182 const GlyphIndex lastGlyphIndex = glyphCount - 1u;
183 for( GlyphIndex i = 0u; i < glyphCount; )
187 // If the direction is right to left, Harfbuzz retrieves the glyphs in the visual order.
188 // The glyphs are needed in the logical order to layout the text in lines.
189 // Do not change the order of the glyphs if they belong to the same cluster.
190 GlyphIndex rtlIndex = lastGlyphIndex - i;
192 unsigned int cluster = glyphInfo[rtlIndex].cluster;
193 unsigned int previousCluster = cluster;
194 Length numberOfGlyphsInCluster = 0u;
196 while( ( cluster == previousCluster ) )
198 ++numberOfGlyphsInCluster;
199 previousCluster = cluster;
205 cluster = glyphInfo[rtlIndex].cluster;
213 rtlIndex = lastGlyphIndex - ( i + ( numberOfGlyphsInCluster - 1u ) );
215 for( GlyphIndex j = 0u; j < numberOfGlyphsInCluster; ++j )
217 const GlyphIndex index = rtlIndex + j;
219 mIndices.PushBack( glyphInfo[index].codepoint );
220 mAdvance.PushBack( floor( glyphPositions[index].x_advance * FROM_266 ) );
221 mCharacterMap.PushBack( glyphInfo[index].cluster );
222 mOffset.PushBack( floor( glyphPositions[index].x_offset * FROM_266 ) );
223 mOffset.PushBack( floor( glyphPositions[index].y_offset * FROM_266 ) );
226 i += numberOfGlyphsInCluster;
230 mIndices.PushBack( glyphInfo[i].codepoint );
231 mAdvance.PushBack( floor( glyphPositions[i].x_advance * FROM_266 ) );
232 mCharacterMap.PushBack( glyphInfo[i].cluster );
233 mOffset.PushBack( floor( glyphPositions[i].x_offset * FROM_266 ) );
234 mOffset.PushBack( floor( glyphPositions[i].y_offset * FROM_266 ) );
241 hb_buffer_destroy( harfBuzzBuffer );
242 hb_font_destroy( harfBuzzFont );
243 FT_Done_Face( face );
245 return mIndices.Count();
248 void GetGlyphs( GlyphInfo* glyphInfo,
249 CharacterIndex* glyphToCharacterMap )
251 Vector<CharacterIndex>::ConstIterator indicesIt = mIndices.Begin();
252 Vector<float>::ConstIterator advanceIt = mAdvance.Begin();
253 Vector<float>::ConstIterator offsetIt = mOffset.Begin();
254 Vector<CharacterIndex>::ConstIterator characterMapIt = mCharacterMap.Begin();
256 for( GlyphIndex index = 0u, size = mIndices.Count(); index < size; ++index )
258 GlyphInfo& glyph = *( glyphInfo + index );
259 CharacterIndex& glyphToCharacter = *( glyphToCharacterMap + index );
261 glyph.fontId = mFontId;
262 glyph.index = *( indicesIt + index );
263 glyph.advance = *( advanceIt + index );
265 const GlyphIndex offsetIndex = 2u * index;
266 glyph.xBearing = *( offsetIt + offsetIndex );
267 glyph.yBearing = *( offsetIt + offsetIndex + 1u );
269 glyphToCharacter = *( characterMapIt + index );
273 FT_Library mFreeTypeLibrary;
275 Vector<CharacterIndex> mIndices;
276 Vector<float> mAdvance;
277 Vector<float> mOffset;
278 Vector<CharacterIndex> mCharacterMap;
292 TextAbstraction::Shaping Shaping::Get()
294 TextAbstraction::Shaping shapingHandle;
296 SingletonService service( SingletonService::Get() );
299 // Check whether the singleton is already created
300 Dali::BaseHandle handle = service.GetSingleton( typeid( TextAbstraction::Shaping ) );
303 // If so, downcast the handle
304 Shaping* impl = dynamic_cast< Internal::Shaping* >( handle.GetObjectPtr() );
305 shapingHandle = TextAbstraction::Shaping( impl );
307 else // create and register the object
309 shapingHandle = TextAbstraction::Shaping( new Shaping );
310 service.Register( typeid( shapingHandle ), shapingHandle );
314 return shapingHandle;
317 Length Shaping::Shape( const Character* const text,
318 Length numberOfCharacters,
324 return mPlugin->Shape( text,
330 void Shaping::GetGlyphs( GlyphInfo* glyphInfo,
331 CharacterIndex* glyphToCharacterMap )
335 mPlugin->GetGlyphs( glyphInfo,
336 glyphToCharacterMap );
339 void Shaping::CreatePlugin()
343 mPlugin = new Plugin();
344 mPlugin->Initialize();
348 } // namespace Internal
350 } // namespace TextAbstraction