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[] =
50 HB_SCRIPT_COMMON, // ASCII_DIGITS
51 HB_SCRIPT_COMMON, // ASCII_PS
53 HB_SCRIPT_COMMON, // C1_CONTROLS
54 HB_SCRIPT_COMMON, // C1_PS
55 HB_SCRIPT_COMMON, // C1_MATH
56 HB_SCRIPT_COMMON, // SML_P
57 HB_SCRIPT_COMMON, // PHONETIC_U
58 HB_SCRIPT_COMMON, // PHONETIC_SS
59 HB_SCRIPT_COMMON, // NUMERIC_SS
60 HB_SCRIPT_COMMON, // LETTER_LIKE
61 HB_SCRIPT_COMMON, // NUMBER_FORMS
62 HB_SCRIPT_COMMON, // FRACTIONS_NF
63 HB_SCRIPT_COMMON, // NON_LATIN_LED
64 HB_SCRIPT_COMMON, // HWFW_S
100 HB_SCRIPT_UNKNOWN, // EMOJI
101 HB_SCRIPT_UNKNOWN, // SYMBOLS1
102 HB_SCRIPT_UNKNOWN, // SYMBOLS2
103 HB_SCRIPT_UNKNOWN, // SYMBOLS3
104 HB_SCRIPT_UNKNOWN, // SYMBOLS4
105 HB_SCRIPT_UNKNOWN, // SYMBOLS5
109 struct Shaping::Plugin
112 : mFreeTypeLibrary( NULL ),
122 FT_Done_FreeType( mFreeTypeLibrary );
127 int error = FT_Init_FreeType( &mFreeTypeLibrary );
128 if( FT_Err_Ok != error )
130 DALI_LOG_ERROR( "FreeType Init error: %d\n", error );
134 Length Shape( const Character* const text,
135 Length numberOfCharacters,
139 // Clear previoursly shaped texts.
142 mCharacterMap.Clear();
146 // Reserve some space to avoid reallocations.
147 const Length numberOfGlyphs = static_cast<Length>( 1.3f * static_cast<float>( numberOfCharacters ) );
148 mIndices.Reserve( numberOfGlyphs );
149 mAdvance.Reserve( numberOfGlyphs );
150 mCharacterMap.Reserve( numberOfGlyphs );
151 mOffset.Reserve( 2u * numberOfGlyphs );
153 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
155 // Get the font's path file name from the font Id.
156 FontDescription fontDescription;
157 fontClient.GetDescription( fontId, fontDescription );
159 // Create a FreeType font's face.
161 FT_Error retVal = FT_New_Face( mFreeTypeLibrary, fontDescription.path.c_str(), 0u, &face );
162 if( FT_Err_Ok != retVal )
164 DALI_LOG_ERROR( "Failed to open face: %s\n", fontDescription.path.c_str() );
168 unsigned int horizontalDpi = 0u;
169 unsigned int verticalDpi = 0u;
170 fontClient.GetDpi( horizontalDpi, verticalDpi );
172 FT_Set_Char_Size( face,
174 fontClient.GetPointSize( fontId ),
178 /* Get our harfbuzz font struct */
179 hb_font_t* harfBuzzFont;
180 harfBuzzFont = hb_ft_font_create( face, NULL );
182 /* Create a buffer for harfbuzz to use */
183 hb_buffer_t* harfBuzzBuffer = hb_buffer_create();
185 const bool rtlDirection = IsRightToLeftScript( script );
186 hb_buffer_set_direction( harfBuzzBuffer,
187 rtlDirection ? HB_DIRECTION_RTL : HB_DIRECTION_LTR ); /* or LTR */
189 hb_buffer_set_script( harfBuzzBuffer,
190 SCRIPT_TO_HARFBUZZ[ script ] ); /* see hb-unicode.h */
192 hb_buffer_set_language( harfBuzzBuffer,
193 hb_language_from_string( DEFAULT_LANGUAGE,
194 DEFAULT_LANGUAGE_LENGTH ) );
196 /* Layout the text */
197 hb_buffer_add_utf32( harfBuzzBuffer, text, numberOfCharacters, 0u, numberOfCharacters );
199 hb_shape( harfBuzzFont, harfBuzzBuffer, NULL, 0u );
202 unsigned int glyphCount;
203 hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( harfBuzzBuffer, &glyphCount );
204 hb_glyph_position_t *glyphPositions = hb_buffer_get_glyph_positions( harfBuzzBuffer, &glyphCount );
205 const GlyphIndex lastGlyphIndex = glyphCount - 1u;
206 for( GlyphIndex i = 0u; i < glyphCount; )
210 // If the direction is right to left, Harfbuzz retrieves the glyphs in the visual order.
211 // The glyphs are needed in the logical order to layout the text in lines.
212 // Do not change the order of the glyphs if they belong to the same cluster.
213 GlyphIndex rtlIndex = lastGlyphIndex - i;
215 unsigned int cluster = glyphInfo[rtlIndex].cluster;
216 unsigned int previousCluster = cluster;
217 Length numberOfGlyphsInCluster = 0u;
219 while( ( cluster == previousCluster ) )
221 ++numberOfGlyphsInCluster;
222 previousCluster = cluster;
228 cluster = glyphInfo[rtlIndex].cluster;
236 rtlIndex = lastGlyphIndex - ( i + ( numberOfGlyphsInCluster - 1u ) );
238 for( GlyphIndex j = 0u; j < numberOfGlyphsInCluster; ++j )
240 const GlyphIndex index = rtlIndex + j;
242 mIndices.PushBack( glyphInfo[index].codepoint );
243 mAdvance.PushBack( floor( glyphPositions[index].x_advance * FROM_266 ) );
244 mCharacterMap.PushBack( glyphInfo[index].cluster );
245 mOffset.PushBack( floor( glyphPositions[index].x_offset * FROM_266 ) );
246 mOffset.PushBack( floor( glyphPositions[index].y_offset * FROM_266 ) );
249 i += numberOfGlyphsInCluster;
253 mIndices.PushBack( glyphInfo[i].codepoint );
254 mAdvance.PushBack( floor( glyphPositions[i].x_advance * FROM_266 ) );
255 mCharacterMap.PushBack( glyphInfo[i].cluster );
256 mOffset.PushBack( floor( glyphPositions[i].x_offset * FROM_266 ) );
257 mOffset.PushBack( floor( glyphPositions[i].y_offset * FROM_266 ) );
264 hb_buffer_destroy( harfBuzzBuffer );
265 hb_font_destroy( harfBuzzFont );
266 FT_Done_Face( face );
268 return mIndices.Count();
271 void GetGlyphs( GlyphInfo* glyphInfo,
272 CharacterIndex* glyphToCharacterMap )
274 Vector<CharacterIndex>::ConstIterator indicesIt = mIndices.Begin();
275 Vector<float>::ConstIterator advanceIt = mAdvance.Begin();
276 Vector<float>::ConstIterator offsetIt = mOffset.Begin();
277 Vector<CharacterIndex>::ConstIterator characterMapIt = mCharacterMap.Begin();
279 for( GlyphIndex index = 0u, size = mIndices.Count(); index < size; ++index )
281 GlyphInfo& glyph = *( glyphInfo + index );
282 CharacterIndex& glyphToCharacter = *( glyphToCharacterMap + index );
284 glyph.fontId = mFontId;
285 glyph.index = *( indicesIt + index );
286 glyph.advance = *( advanceIt + index );
288 const GlyphIndex offsetIndex = 2u * index;
289 glyph.xBearing = *( offsetIt + offsetIndex );
290 glyph.yBearing = *( offsetIt + offsetIndex + 1u );
292 glyphToCharacter = *( characterMapIt + index );
296 FT_Library mFreeTypeLibrary;
298 Vector<CharacterIndex> mIndices;
299 Vector<float> mAdvance;
300 Vector<float> mOffset;
301 Vector<CharacterIndex> mCharacterMap;
315 TextAbstraction::Shaping Shaping::Get()
317 TextAbstraction::Shaping shapingHandle;
319 SingletonService service( SingletonService::Get() );
322 // Check whether the singleton is already created
323 Dali::BaseHandle handle = service.GetSingleton( typeid( TextAbstraction::Shaping ) );
326 // If so, downcast the handle
327 Shaping* impl = dynamic_cast< Internal::Shaping* >( handle.GetObjectPtr() );
328 shapingHandle = TextAbstraction::Shaping( impl );
330 else // create and register the object
332 shapingHandle = TextAbstraction::Shaping( new Shaping );
333 service.Register( typeid( shapingHandle ), shapingHandle );
337 return shapingHandle;
340 Length Shaping::Shape( const Character* const text,
341 Length numberOfCharacters,
347 return mPlugin->Shape( text,
353 void Shaping::GetGlyphs( GlyphInfo* glyphInfo,
354 CharacterIndex* glyphToCharacterMap )
358 mPlugin->GetGlyphs( glyphInfo,
359 glyphToCharacterMap );
362 void Shaping::CreatePlugin()
366 mPlugin = new Plugin();
367 mPlugin->Initialize();
371 } // namespace Internal
373 } // namespace TextAbstraction