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/public-api/text-abstraction/font-client.h>
24 #include <dali/public-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 unsigned int HIGH_QUALITY_PIXEL_SIZE = 200u; // Pixel size sent to FreeType2 FT_Set_Char_Size() for high quality glyphs.
43 const char* DEFAULT_LANGUAGE = "en";
44 const unsigned int DEFAULT_LANGUAGE_LENGTH = 2u;
45 const float TO_PIXELS = 64.f;
47 const hb_script_t SCRIPT_TO_HARFBUZZ[] =
70 struct Shaping::Plugin
73 : mFreeTypeLibrary( NULL ),
83 FT_Done_FreeType( mFreeTypeLibrary );
88 int error = FT_Init_FreeType( &mFreeTypeLibrary );
89 if( FT_Err_Ok != error )
91 DALI_LOG_ERROR( "FreeType Init error: %d\n", error );
95 Length Shape( const Character* const text,
96 Length numberOfCharacters,
100 // Clear previoursly shaped texts.
103 mCharacterMap.Clear();
106 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
108 // Get the font's path file name from the font Id.
109 FontDescription fontDescription;
110 fontClient.GetDescription( fontId, fontDescription );
112 // Create a FreeType font's face.
114 FT_Error retVal = FT_New_Face( mFreeTypeLibrary, fontDescription.path.c_str(), 0u, &face );
115 if( FT_Err_Ok != retVal )
117 DALI_LOG_ERROR( "Failed to open face: %s\n", fontDescription.path.c_str() );
121 FT_Set_Pixel_Sizes( face, HIGH_QUALITY_PIXEL_SIZE, HIGH_QUALITY_PIXEL_SIZE );
123 /* Get our harfbuzz font struct */
124 hb_font_t* harfBuzzFont;
125 harfBuzzFont = hb_ft_font_create( face, NULL );
127 /* Create a buffer for harfbuzz to use */
128 hb_buffer_t* harfBuzzBuffer = hb_buffer_create();
130 hb_buffer_set_direction( harfBuzzBuffer,
131 ( ARABIC == script ) ? HB_DIRECTION_RTL : HB_DIRECTION_LTR ); /* or LTR */
133 hb_buffer_set_script( harfBuzzBuffer,
134 SCRIPT_TO_HARFBUZZ[ script ] ); /* see hb-unicode.h */
136 hb_buffer_set_language( harfBuzzBuffer,
137 hb_language_from_string( DEFAULT_LANGUAGE,
138 DEFAULT_LANGUAGE_LENGTH ) );
140 /* Layout the text */
141 hb_buffer_add_utf32( harfBuzzBuffer, text, numberOfCharacters, 0u, numberOfCharacters );
143 hb_shape( harfBuzzFont, harfBuzzBuffer, NULL, 0u );
146 unsigned int glyphCount;
147 hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( harfBuzzBuffer, &glyphCount );
148 hb_glyph_position_t *glyphPositions = hb_buffer_get_glyph_positions( harfBuzzBuffer, &glyphCount );
150 for( Length i = 0u; i < glyphCount; ++i )
152 mIndices.PushBack( glyphInfo[i].codepoint );
153 mAdvance.PushBack( glyphPositions[i].x_advance / TO_PIXELS );
154 mCharacterMap.PushBack( glyphInfo[i].cluster );
158 hb_buffer_destroy( harfBuzzBuffer );
159 hb_font_destroy( harfBuzzFont );
160 FT_Done_Face( face );
162 return mIndices.Count();
165 void GetGlyphs( GlyphInfo* glyphInfo,
166 CharacterIndex* glyphToCharacterMap )
168 Vector<CharacterIndex>::ConstIterator indicesIt = mIndices.Begin();
169 Vector<float>::ConstIterator advanceIt = mAdvance.Begin();
170 Vector<CharacterIndex>::ConstIterator characterMapIt = mCharacterMap.Begin();
172 for( Length index = 0u, size = mIndices.Count(); index < size; ++index )
174 GlyphInfo& glyph = *( glyphInfo + index );
175 CharacterIndex& glyphToCharacter = *( glyphToCharacterMap + index );
177 glyph.fontId = mFontId;
178 glyph.index = *( indicesIt + index );
179 glyph.advance = *( advanceIt + index );
181 glyphToCharacter = *( characterMapIt + index );
185 FT_Library mFreeTypeLibrary;
187 Vector<CharacterIndex> mIndices;
188 Vector<float> mAdvance;
189 Vector<CharacterIndex> mCharacterMap;
203 TextAbstraction::Shaping Shaping::Get()
205 TextAbstraction::Shaping shapingHandle;
207 SingletonService service( SingletonService::Get() );
210 // Check whether the singleton is already created
211 Dali::BaseHandle handle = service.GetSingleton( typeid( TextAbstraction::Shaping ) );
214 // If so, downcast the handle
215 Shaping* impl = dynamic_cast< Internal::Shaping* >( handle.GetObjectPtr() );
216 shapingHandle = TextAbstraction::Shaping( impl );
218 else // create and register the object
220 shapingHandle = TextAbstraction::Shaping( new Shaping );
221 service.Register( typeid( shapingHandle ), shapingHandle );
225 return shapingHandle;
228 Length Shaping::Shape( const Character* const text,
229 Length numberOfCharacters,
235 return mPlugin->Shape( text,
241 void Shaping::GetGlyphs( GlyphInfo* glyphInfo,
242 CharacterIndex* glyphToCharacterMap )
246 mPlugin->GetGlyphs( glyphInfo,
247 glyphToCharacterMap );
250 void Shaping::CreatePlugin()
254 mPlugin = new Plugin();
255 mPlugin->Initialize();
259 } // namespace Internal
261 } // namespace TextAbstraction