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>
26 #include "font-client-impl.h"
29 #include <harfbuzz/hb.h>
30 #include <harfbuzz/hb-ft.h>
35 namespace TextAbstraction
41 const char* const DEFAULT_LANGUAGE = "en";
42 const unsigned int DEFAULT_LANGUAGE_LENGTH = 2u;
43 const float FROM_266 = 1.0f / 64.0f;
45 const hb_script_t SCRIPT_TO_HARFBUZZ[] =
49 HB_SCRIPT_COMMON, // ASCII_DIGITS
50 HB_SCRIPT_COMMON, // ASCII_PS
52 HB_SCRIPT_COMMON, // C1_CONTROLS
53 HB_SCRIPT_COMMON, // C1_PS
54 HB_SCRIPT_COMMON, // C1_MATH
55 HB_SCRIPT_COMMON, // SML_P
56 HB_SCRIPT_COMMON, // PHONETIC_U
57 HB_SCRIPT_COMMON, // PHONETIC_SS
58 HB_SCRIPT_COMMON, // NUMERIC_SS
59 HB_SCRIPT_COMMON, // LETTER_LIKE
60 HB_SCRIPT_COMMON, // NUMBER_FORMS
61 HB_SCRIPT_COMMON, // FRACTIONS_NF
62 HB_SCRIPT_COMMON, // NON_LATIN_LED
63 HB_SCRIPT_COMMON, // HWFW_S
102 HB_SCRIPT_MEETEI_MAYEK,
104 HB_SCRIPT_UNKNOWN, // EMOJI
105 HB_SCRIPT_UNKNOWN, // SYMBOLS1
106 HB_SCRIPT_UNKNOWN, // SYMBOLS2
107 HB_SCRIPT_UNKNOWN, // SYMBOLS3
108 HB_SCRIPT_UNKNOWN, // SYMBOLS4
109 HB_SCRIPT_UNKNOWN, // SYMBOLS5
113 struct Shaping::Plugin
127 Length Shape( const Character* const text,
128 Length numberOfCharacters,
132 // Clear previoursly shaped texts.
135 mCharacterMap.Clear();
139 // Reserve some space to avoid reallocations.
140 const Length numberOfGlyphs = static_cast<Length>( 1.3f * static_cast<float>( numberOfCharacters ) );
141 mIndices.Reserve( numberOfGlyphs );
142 mAdvance.Reserve( numberOfGlyphs );
143 mCharacterMap.Reserve( numberOfGlyphs );
144 mOffset.Reserve( 2u * numberOfGlyphs );
146 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
147 TextAbstraction::Internal::FontClient& fontClientImpl = TextAbstraction::GetImplementation( fontClient );
149 // Create a FreeType font's face.
152 face = fontClientImpl.GetFreetypeFace( fontId );
153 if( nullptr == face )
155 // Nothing to do if the face is null.
159 /* Get our harfbuzz font struct */
160 hb_font_t* harfBuzzFont;
161 harfBuzzFont = hb_ft_font_create( face, NULL );
163 /* Create a buffer for harfbuzz to use */
164 hb_buffer_t* harfBuzzBuffer = hb_buffer_create();
166 const bool rtlDirection = IsRightToLeftScript( script );
167 hb_buffer_set_direction( harfBuzzBuffer,
168 rtlDirection ? HB_DIRECTION_RTL : HB_DIRECTION_LTR ); /* or LTR */
170 hb_buffer_set_script( harfBuzzBuffer,
171 SCRIPT_TO_HARFBUZZ[ script ] ); /* see hb-unicode.h */
174 char* currentLocale = setlocale(LC_MESSAGES,NULL);
176 std::istringstream stringStream( currentLocale );
177 std::string localeString;
178 std::getline(stringStream, localeString, '_');
179 hb_buffer_set_language( harfBuzzBuffer, hb_language_from_string( localeString.c_str(), localeString.size() ) );
181 /* Layout the text */
182 hb_buffer_add_utf32( harfBuzzBuffer, text, numberOfCharacters, 0u, numberOfCharacters );
184 hb_shape( harfBuzzFont, harfBuzzBuffer, NULL, 0u );
187 unsigned int glyphCount;
188 hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( harfBuzzBuffer, &glyphCount );
189 hb_glyph_position_t *glyphPositions = hb_buffer_get_glyph_positions( harfBuzzBuffer, &glyphCount );
190 const GlyphIndex lastGlyphIndex = glyphCount - 1u;
191 for( GlyphIndex i = 0u; i < glyphCount; )
195 // If the direction is right to left, Harfbuzz retrieves the glyphs in the visual order.
196 // The glyphs are needed in the logical order to layout the text in lines.
197 // Do not change the order of the glyphs if they belong to the same cluster.
198 GlyphIndex rtlIndex = lastGlyphIndex - i;
200 unsigned int cluster = glyphInfo[rtlIndex].cluster;
201 unsigned int previousCluster = cluster;
202 Length numberOfGlyphsInCluster = 0u;
204 while( ( cluster == previousCluster ) )
206 ++numberOfGlyphsInCluster;
207 previousCluster = cluster;
213 cluster = glyphInfo[rtlIndex].cluster;
221 rtlIndex = lastGlyphIndex - ( i + ( numberOfGlyphsInCluster - 1u ) );
223 for( GlyphIndex j = 0u; j < numberOfGlyphsInCluster; ++j )
225 const GlyphIndex index = rtlIndex + j;
227 mIndices.PushBack( glyphInfo[index].codepoint );
228 mAdvance.PushBack( floor( glyphPositions[index].x_advance * FROM_266 ) );
229 mCharacterMap.PushBack( glyphInfo[index].cluster );
230 mOffset.PushBack( floor( glyphPositions[index].x_offset * FROM_266 ) );
231 mOffset.PushBack( floor( glyphPositions[index].y_offset * FROM_266 ) );
234 i += numberOfGlyphsInCluster;
238 mIndices.PushBack( glyphInfo[i].codepoint );
239 mAdvance.PushBack( floor( glyphPositions[i].x_advance * FROM_266 ) );
240 mCharacterMap.PushBack( glyphInfo[i].cluster );
241 mOffset.PushBack( floor( glyphPositions[i].x_offset * FROM_266 ) );
242 mOffset.PushBack( floor( glyphPositions[i].y_offset * FROM_266 ) );
249 hb_buffer_destroy( harfBuzzBuffer );
250 hb_font_destroy( harfBuzzFont );
252 return mIndices.Count();
255 void GetGlyphs( GlyphInfo* glyphInfo,
256 CharacterIndex* glyphToCharacterMap )
258 Vector<CharacterIndex>::ConstIterator indicesIt = mIndices.Begin();
259 Vector<float>::ConstIterator advanceIt = mAdvance.Begin();
260 Vector<float>::ConstIterator offsetIt = mOffset.Begin();
261 Vector<CharacterIndex>::ConstIterator characterMapIt = mCharacterMap.Begin();
263 for( GlyphIndex index = 0u, size = mIndices.Count(); index < size; ++index )
265 GlyphInfo& glyph = *( glyphInfo + index );
266 CharacterIndex& glyphToCharacter = *( glyphToCharacterMap + index );
268 glyph.fontId = mFontId;
269 glyph.index = *( indicesIt + index );
270 glyph.advance = *( advanceIt + index );
272 const GlyphIndex offsetIndex = 2u * index;
273 glyph.xBearing = *( offsetIt + offsetIndex );
274 glyph.yBearing = *( offsetIt + offsetIndex + 1u );
276 glyphToCharacter = *( characterMapIt + index );
280 Vector<CharacterIndex> mIndices;
281 Vector<float> mAdvance;
282 Vector<float> mOffset;
283 Vector<CharacterIndex> mCharacterMap;
297 TextAbstraction::Shaping Shaping::Get()
299 TextAbstraction::Shaping shapingHandle;
301 SingletonService service( SingletonService::Get() );
304 // Check whether the singleton is already created
305 Dali::BaseHandle handle = service.GetSingleton( typeid( TextAbstraction::Shaping ) );
308 // If so, downcast the handle
309 Shaping* impl = dynamic_cast< Internal::Shaping* >( handle.GetObjectPtr() );
310 shapingHandle = TextAbstraction::Shaping( impl );
312 else // create and register the object
314 shapingHandle = TextAbstraction::Shaping( new Shaping );
315 service.Register( typeid( shapingHandle ), shapingHandle );
319 return shapingHandle;
322 Length Shaping::Shape( const Character* const text,
323 Length numberOfCharacters,
329 return mPlugin->Shape( text,
335 void Shaping::GetGlyphs( GlyphInfo* glyphInfo,
336 CharacterIndex* glyphToCharacterMap )
340 mPlugin->GetGlyphs( glyphInfo,
341 glyphToCharacterMap );
344 void Shaping::CreatePlugin()
348 mPlugin = new Plugin();
352 } // namespace Internal
354 } // namespace TextAbstraction