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/internal/text/text-abstraction/shaping-impl.h>
22 #include <dali/internal/system/common/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>
37 namespace TextAbstraction
43 const char* DEFAULT_LANGUAGE = "en";
44 const unsigned int DEFAULT_LANGUAGE_LENGTH = 2u;
45 const float FROM_266 = 1.0f / 64.0f;
47 const hb_script_t SCRIPT_TO_HARFBUZZ[] =
51 HB_SCRIPT_COMMON, // ASCII_DIGITS
52 HB_SCRIPT_COMMON, // ASCII_PS
54 HB_SCRIPT_COMMON, // C1_CONTROLS
55 HB_SCRIPT_COMMON, // C1_PS
56 HB_SCRIPT_COMMON, // C1_MATH
57 HB_SCRIPT_COMMON, // SML_P
58 HB_SCRIPT_COMMON, // PHONETIC_U
59 HB_SCRIPT_COMMON, // PHONETIC_SS
60 HB_SCRIPT_COMMON, // NUMERIC_SS
61 HB_SCRIPT_COMMON, // LETTER_LIKE
62 HB_SCRIPT_COMMON, // NUMBER_FORMS
63 HB_SCRIPT_COMMON, // FRACTIONS_NF
64 HB_SCRIPT_COMMON, // NON_LATIN_LED
65 HB_SCRIPT_COMMON, // HWFW_S
104 HB_SCRIPT_MEETEI_MAYEK,
106 HB_SCRIPT_UNKNOWN, // EMOJI
107 HB_SCRIPT_UNKNOWN, // SYMBOLS1
108 HB_SCRIPT_UNKNOWN, // SYMBOLS2
109 HB_SCRIPT_UNKNOWN, // SYMBOLS3
110 HB_SCRIPT_UNKNOWN, // SYMBOLS4
111 HB_SCRIPT_UNKNOWN, // SYMBOLS5
115 struct Shaping::Plugin
118 : mFreeTypeLibrary( NULL ),
128 FT_Done_FreeType( mFreeTypeLibrary );
133 int error = FT_Init_FreeType( &mFreeTypeLibrary );
134 if( FT_Err_Ok != error )
136 DALI_LOG_ERROR( "FreeType Init error: %d\n", error );
140 Length Shape( const Character* const text,
141 Length numberOfCharacters,
145 // Clear previoursly shaped texts.
148 mCharacterMap.Clear();
152 // Reserve some space to avoid reallocations.
153 const Length numberOfGlyphs = static_cast<Length>( 1.3f * static_cast<float>( numberOfCharacters ) );
154 mIndices.Reserve( numberOfGlyphs );
155 mAdvance.Reserve( numberOfGlyphs );
156 mCharacterMap.Reserve( numberOfGlyphs );
157 mOffset.Reserve( 2u * numberOfGlyphs );
159 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
161 // Get the font's path file name from the font Id.
162 FontDescription fontDescription;
163 fontClient.GetDescription( fontId, fontDescription );
165 // Create a FreeType font's face.
167 FT_Error retVal = FT_New_Face( mFreeTypeLibrary, fontDescription.path.c_str(), 0u, &face );
168 if( FT_Err_Ok != retVal )
170 DALI_LOG_ERROR( "Failed to open face: %s\n", fontDescription.path.c_str() );
174 unsigned int horizontalDpi = 0u;
175 unsigned int verticalDpi = 0u;
176 fontClient.GetDpi( horizontalDpi, verticalDpi );
178 FT_Set_Char_Size( face,
180 fontClient.GetPointSize( fontId ),
184 /* Get our harfbuzz font struct */
185 hb_font_t* harfBuzzFont;
186 harfBuzzFont = hb_ft_font_create( face, NULL );
188 /* Create a buffer for harfbuzz to use */
189 hb_buffer_t* harfBuzzBuffer = hb_buffer_create();
191 const bool rtlDirection = IsRightToLeftScript( script );
192 hb_buffer_set_direction( harfBuzzBuffer,
193 rtlDirection ? HB_DIRECTION_RTL : HB_DIRECTION_LTR ); /* or LTR */
195 hb_buffer_set_script( harfBuzzBuffer,
196 SCRIPT_TO_HARFBUZZ[ script ] ); /* see hb-unicode.h */
199 char* currentLocale = setlocale(LC_MESSAGES,NULL);
201 std::istringstream stringStream( currentLocale );
202 std::string localeString;
203 std::getline(stringStream, localeString, '_');
204 hb_buffer_set_language( harfBuzzBuffer, hb_language_from_string( localeString.c_str(), localeString.size() ) );
206 /* Layout the text */
207 hb_buffer_add_utf32( harfBuzzBuffer, text, numberOfCharacters, 0u, numberOfCharacters );
209 hb_shape( harfBuzzFont, harfBuzzBuffer, NULL, 0u );
212 unsigned int glyphCount;
213 hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( harfBuzzBuffer, &glyphCount );
214 hb_glyph_position_t *glyphPositions = hb_buffer_get_glyph_positions( harfBuzzBuffer, &glyphCount );
215 const GlyphIndex lastGlyphIndex = glyphCount - 1u;
216 for( GlyphIndex i = 0u; i < glyphCount; )
220 // If the direction is right to left, Harfbuzz retrieves the glyphs in the visual order.
221 // The glyphs are needed in the logical order to layout the text in lines.
222 // Do not change the order of the glyphs if they belong to the same cluster.
223 GlyphIndex rtlIndex = lastGlyphIndex - i;
225 unsigned int cluster = glyphInfo[rtlIndex].cluster;
226 unsigned int previousCluster = cluster;
227 Length numberOfGlyphsInCluster = 0u;
229 while( ( cluster == previousCluster ) )
231 ++numberOfGlyphsInCluster;
232 previousCluster = cluster;
238 cluster = glyphInfo[rtlIndex].cluster;
246 rtlIndex = lastGlyphIndex - ( i + ( numberOfGlyphsInCluster - 1u ) );
248 for( GlyphIndex j = 0u; j < numberOfGlyphsInCluster; ++j )
250 const GlyphIndex index = rtlIndex + j;
252 mIndices.PushBack( glyphInfo[index].codepoint );
253 mAdvance.PushBack( floor( glyphPositions[index].x_advance * FROM_266 ) );
254 mCharacterMap.PushBack( glyphInfo[index].cluster );
255 mOffset.PushBack( floor( glyphPositions[index].x_offset * FROM_266 ) );
256 mOffset.PushBack( floor( glyphPositions[index].y_offset * FROM_266 ) );
259 i += numberOfGlyphsInCluster;
263 mIndices.PushBack( glyphInfo[i].codepoint );
264 mAdvance.PushBack( floor( glyphPositions[i].x_advance * FROM_266 ) );
265 mCharacterMap.PushBack( glyphInfo[i].cluster );
266 mOffset.PushBack( floor( glyphPositions[i].x_offset * FROM_266 ) );
267 mOffset.PushBack( floor( glyphPositions[i].y_offset * FROM_266 ) );
274 hb_buffer_destroy( harfBuzzBuffer );
275 hb_font_destroy( harfBuzzFont );
276 FT_Done_Face( face );
278 return mIndices.Count();
281 void GetGlyphs( GlyphInfo* glyphInfo,
282 CharacterIndex* glyphToCharacterMap )
284 Vector<CharacterIndex>::ConstIterator indicesIt = mIndices.Begin();
285 Vector<float>::ConstIterator advanceIt = mAdvance.Begin();
286 Vector<float>::ConstIterator offsetIt = mOffset.Begin();
287 Vector<CharacterIndex>::ConstIterator characterMapIt = mCharacterMap.Begin();
289 for( GlyphIndex index = 0u, size = mIndices.Count(); index < size; ++index )
291 GlyphInfo& glyph = *( glyphInfo + index );
292 CharacterIndex& glyphToCharacter = *( glyphToCharacterMap + index );
294 glyph.fontId = mFontId;
295 glyph.index = *( indicesIt + index );
296 glyph.advance = *( advanceIt + index );
298 const GlyphIndex offsetIndex = 2u * index;
299 glyph.xBearing = *( offsetIt + offsetIndex );
300 glyph.yBearing = *( offsetIt + offsetIndex + 1u );
302 glyphToCharacter = *( characterMapIt + index );
306 FT_Library mFreeTypeLibrary;
308 Vector<CharacterIndex> mIndices;
309 Vector<float> mAdvance;
310 Vector<float> mOffset;
311 Vector<CharacterIndex> mCharacterMap;
325 TextAbstraction::Shaping Shaping::Get()
327 TextAbstraction::Shaping shapingHandle;
329 SingletonService service( SingletonService::Get() );
332 // Check whether the singleton is already created
333 Dali::BaseHandle handle = service.GetSingleton( typeid( TextAbstraction::Shaping ) );
336 // If so, downcast the handle
337 Shaping* impl = dynamic_cast< Internal::Shaping* >( handle.GetObjectPtr() );
338 shapingHandle = TextAbstraction::Shaping( impl );
340 else // create and register the object
342 shapingHandle = TextAbstraction::Shaping( new Shaping );
343 service.Register( typeid( shapingHandle ), shapingHandle );
347 return shapingHandle;
350 Length Shaping::Shape( const Character* const text,
351 Length numberOfCharacters,
357 return mPlugin->Shape( text,
363 void Shaping::GetGlyphs( GlyphInfo* glyphInfo,
364 CharacterIndex* glyphToCharacterMap )
368 mPlugin->GetGlyphs( glyphInfo,
369 glyphToCharacterMap );
372 void Shaping::CreatePlugin()
376 mPlugin = new Plugin();
377 mPlugin->Initialize();
381 } // namespace Internal
383 } // namespace TextAbstraction