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
102 HB_SCRIPT_UNKNOWN, // EMOJI
103 HB_SCRIPT_UNKNOWN, // SYMBOLS1
104 HB_SCRIPT_UNKNOWN, // SYMBOLS2
105 HB_SCRIPT_UNKNOWN, // SYMBOLS3
106 HB_SCRIPT_UNKNOWN, // SYMBOLS4
107 HB_SCRIPT_UNKNOWN, // SYMBOLS5
111 struct Shaping::Plugin
114 : mFreeTypeLibrary( NULL ),
124 FT_Done_FreeType( mFreeTypeLibrary );
129 int error = FT_Init_FreeType( &mFreeTypeLibrary );
130 if( FT_Err_Ok != error )
132 DALI_LOG_ERROR( "FreeType Init error: %d\n", error );
136 Length Shape( const Character* const text,
137 Length numberOfCharacters,
141 // Clear previoursly shaped texts.
144 mCharacterMap.Clear();
148 // Reserve some space to avoid reallocations.
149 const Length numberOfGlyphs = static_cast<Length>( 1.3f * static_cast<float>( numberOfCharacters ) );
150 mIndices.Reserve( numberOfGlyphs );
151 mAdvance.Reserve( numberOfGlyphs );
152 mCharacterMap.Reserve( numberOfGlyphs );
153 mOffset.Reserve( 2u * numberOfGlyphs );
155 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
157 // Get the font's path file name from the font Id.
158 FontDescription fontDescription;
159 fontClient.GetDescription( fontId, fontDescription );
161 // Create a FreeType font's face.
163 FT_Error retVal = FT_New_Face( mFreeTypeLibrary, fontDescription.path.c_str(), 0u, &face );
164 if( FT_Err_Ok != retVal )
166 DALI_LOG_ERROR( "Failed to open face: %s\n", fontDescription.path.c_str() );
170 unsigned int horizontalDpi = 0u;
171 unsigned int verticalDpi = 0u;
172 fontClient.GetDpi( horizontalDpi, verticalDpi );
174 FT_Set_Char_Size( face,
176 fontClient.GetPointSize( fontId ),
180 /* Get our harfbuzz font struct */
181 hb_font_t* harfBuzzFont;
182 harfBuzzFont = hb_ft_font_create( face, NULL );
184 /* Create a buffer for harfbuzz to use */
185 hb_buffer_t* harfBuzzBuffer = hb_buffer_create();
187 const bool rtlDirection = IsRightToLeftScript( script );
188 hb_buffer_set_direction( harfBuzzBuffer,
189 rtlDirection ? HB_DIRECTION_RTL : HB_DIRECTION_LTR ); /* or LTR */
191 hb_buffer_set_script( harfBuzzBuffer,
192 SCRIPT_TO_HARFBUZZ[ script ] ); /* see hb-unicode.h */
194 hb_buffer_set_language( harfBuzzBuffer,
195 hb_language_from_string( DEFAULT_LANGUAGE,
196 DEFAULT_LANGUAGE_LENGTH ) );
198 /* Layout the text */
199 hb_buffer_add_utf32( harfBuzzBuffer, text, numberOfCharacters, 0u, numberOfCharacters );
201 hb_shape( harfBuzzFont, harfBuzzBuffer, NULL, 0u );
204 unsigned int glyphCount;
205 hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( harfBuzzBuffer, &glyphCount );
206 hb_glyph_position_t *glyphPositions = hb_buffer_get_glyph_positions( harfBuzzBuffer, &glyphCount );
207 const GlyphIndex lastGlyphIndex = glyphCount - 1u;
208 for( GlyphIndex i = 0u; i < glyphCount; )
212 // If the direction is right to left, Harfbuzz retrieves the glyphs in the visual order.
213 // The glyphs are needed in the logical order to layout the text in lines.
214 // Do not change the order of the glyphs if they belong to the same cluster.
215 GlyphIndex rtlIndex = lastGlyphIndex - i;
217 unsigned int cluster = glyphInfo[rtlIndex].cluster;
218 unsigned int previousCluster = cluster;
219 Length numberOfGlyphsInCluster = 0u;
221 while( ( cluster == previousCluster ) )
223 ++numberOfGlyphsInCluster;
224 previousCluster = cluster;
230 cluster = glyphInfo[rtlIndex].cluster;
238 rtlIndex = lastGlyphIndex - ( i + ( numberOfGlyphsInCluster - 1u ) );
240 for( GlyphIndex j = 0u; j < numberOfGlyphsInCluster; ++j )
242 const GlyphIndex index = rtlIndex + j;
244 mIndices.PushBack( glyphInfo[index].codepoint );
245 mAdvance.PushBack( floor( glyphPositions[index].x_advance * FROM_266 ) );
246 mCharacterMap.PushBack( glyphInfo[index].cluster );
247 mOffset.PushBack( floor( glyphPositions[index].x_offset * FROM_266 ) );
248 mOffset.PushBack( floor( glyphPositions[index].y_offset * FROM_266 ) );
251 i += numberOfGlyphsInCluster;
255 mIndices.PushBack( glyphInfo[i].codepoint );
256 mAdvance.PushBack( floor( glyphPositions[i].x_advance * FROM_266 ) );
257 mCharacterMap.PushBack( glyphInfo[i].cluster );
258 mOffset.PushBack( floor( glyphPositions[i].x_offset * FROM_266 ) );
259 mOffset.PushBack( floor( glyphPositions[i].y_offset * FROM_266 ) );
266 hb_buffer_destroy( harfBuzzBuffer );
267 hb_font_destroy( harfBuzzFont );
268 FT_Done_Face( face );
270 return mIndices.Count();
273 void GetGlyphs( GlyphInfo* glyphInfo,
274 CharacterIndex* glyphToCharacterMap )
276 Vector<CharacterIndex>::ConstIterator indicesIt = mIndices.Begin();
277 Vector<float>::ConstIterator advanceIt = mAdvance.Begin();
278 Vector<float>::ConstIterator offsetIt = mOffset.Begin();
279 Vector<CharacterIndex>::ConstIterator characterMapIt = mCharacterMap.Begin();
281 for( GlyphIndex index = 0u, size = mIndices.Count(); index < size; ++index )
283 GlyphInfo& glyph = *( glyphInfo + index );
284 CharacterIndex& glyphToCharacter = *( glyphToCharacterMap + index );
286 glyph.fontId = mFontId;
287 glyph.index = *( indicesIt + index );
288 glyph.advance = *( advanceIt + index );
290 const GlyphIndex offsetIndex = 2u * index;
291 glyph.xBearing = *( offsetIt + offsetIndex );
292 glyph.yBearing = *( offsetIt + offsetIndex + 1u );
294 glyphToCharacter = *( characterMapIt + index );
298 FT_Library mFreeTypeLibrary;
300 Vector<CharacterIndex> mIndices;
301 Vector<float> mAdvance;
302 Vector<float> mOffset;
303 Vector<CharacterIndex> mCharacterMap;
317 TextAbstraction::Shaping Shaping::Get()
319 TextAbstraction::Shaping shapingHandle;
321 SingletonService service( SingletonService::Get() );
324 // Check whether the singleton is already created
325 Dali::BaseHandle handle = service.GetSingleton( typeid( TextAbstraction::Shaping ) );
328 // If so, downcast the handle
329 Shaping* impl = dynamic_cast< Internal::Shaping* >( handle.GetObjectPtr() );
330 shapingHandle = TextAbstraction::Shaping( impl );
332 else // create and register the object
334 shapingHandle = TextAbstraction::Shaping( new Shaping );
335 service.Register( typeid( shapingHandle ), shapingHandle );
339 return shapingHandle;
342 Length Shaping::Shape( const Character* const text,
343 Length numberOfCharacters,
349 return mPlugin->Shape( text,
355 void Shaping::GetGlyphs( GlyphInfo* glyphInfo,
356 CharacterIndex* glyphToCharacterMap )
360 mPlugin->GetGlyphs( glyphInfo,
361 glyphToCharacterMap );
364 void Shaping::CreatePlugin()
368 mPlugin = new Plugin();
369 mPlugin->Initialize();
373 } // namespace Internal
375 } // namespace TextAbstraction