Merge "Italic synthesize for circular layout." into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / text / text-abstraction / shaping-impl.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS  HEADER
19 #include <dali/internal/text/text-abstraction/shaping-impl.h>
20
21 // INTERNAL INCLUDES
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"
27
28 // EXTERNAL INCLUDES
29 #include <harfbuzz/hb.h>
30 #include <harfbuzz/hb-ft.h>
31
32 namespace
33 {
34
35 #if defined(DEBUG_ENABLED)
36 Dali::Integration::Log::Filter* gLogFilter = Dali::Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_FONT_CLIENT");
37 #endif
38
39 }
40
41 namespace Dali
42 {
43
44 namespace TextAbstraction
45 {
46
47 namespace Internal
48 {
49
50 const char* const  DEFAULT_LANGUAGE = "en";
51 const unsigned int DEFAULT_LANGUAGE_LENGTH = 2u;
52 const float        FROM_266 = 1.0f / 64.0f;
53
54 const hb_script_t SCRIPT_TO_HARFBUZZ[] =
55 {
56   HB_SCRIPT_COMMON,
57
58   HB_SCRIPT_COMMON, // ASCII_DIGITS
59   HB_SCRIPT_COMMON, // ASCII_PS
60
61   HB_SCRIPT_COMMON, // C1_CONTROLS
62   HB_SCRIPT_COMMON, // C1_PS
63   HB_SCRIPT_COMMON, // C1_MATH
64   HB_SCRIPT_COMMON, // SML_P
65   HB_SCRIPT_COMMON, // PHONETIC_U
66   HB_SCRIPT_COMMON, // PHONETIC_SS
67   HB_SCRIPT_COMMON, // NUMERIC_SS
68   HB_SCRIPT_COMMON, // LETTER_LIKE
69   HB_SCRIPT_COMMON, // NUMBER_FORMS
70   HB_SCRIPT_COMMON, // FRACTIONS_NF
71   HB_SCRIPT_COMMON, // NON_LATIN_LED
72   HB_SCRIPT_COMMON, // HWFW_S
73
74   HB_SCRIPT_CYRILLIC,
75   HB_SCRIPT_GREEK,
76   HB_SCRIPT_LATIN,
77
78   HB_SCRIPT_ARABIC,
79   HB_SCRIPT_HEBREW,
80
81   HB_SCRIPT_ARMENIAN,
82   HB_SCRIPT_GEORGIAN,
83
84   HB_SCRIPT_HAN,
85   HB_SCRIPT_HANGUL,
86   HB_SCRIPT_HIRAGANA,
87   HB_SCRIPT_KATAKANA,
88   HB_SCRIPT_BOPOMOFO,
89
90   HB_SCRIPT_BENGALI,
91   HB_SCRIPT_MYANMAR,
92   HB_SCRIPT_DEVANAGARI,
93   HB_SCRIPT_GUJARATI,
94   HB_SCRIPT_GURMUKHI,
95   HB_SCRIPT_KANNADA,
96   HB_SCRIPT_MALAYALAM,
97   HB_SCRIPT_ORIYA,
98   HB_SCRIPT_SINHALA,
99   HB_SCRIPT_TAMIL,
100   HB_SCRIPT_TELUGU,
101
102   HB_SCRIPT_LAO,
103   HB_SCRIPT_THAI,
104   HB_SCRIPT_KHMER,
105   HB_SCRIPT_JAVANESE,
106   HB_SCRIPT_SUNDANESE,
107
108   HB_SCRIPT_ETHIOPIC,
109   HB_SCRIPT_OL_CHIKI,
110   HB_SCRIPT_TAGALOG,
111   HB_SCRIPT_MEETEI_MAYEK,
112
113   HB_SCRIPT_UNKNOWN, // EMOJI
114   HB_SCRIPT_UNKNOWN, // SYMBOLS1
115   HB_SCRIPT_UNKNOWN, // SYMBOLS2
116   HB_SCRIPT_UNKNOWN, // SYMBOLS3
117   HB_SCRIPT_UNKNOWN, // SYMBOLS4
118   HB_SCRIPT_UNKNOWN, // SYMBOLS5
119   HB_SCRIPT_UNKNOWN
120 };
121
122 struct Shaping::Plugin
123 {
124   Plugin()
125   : mIndices(),
126     mAdvance(),
127     mCharacterMap(),
128     mFontId( 0u )
129   {
130   }
131
132   ~Plugin()
133   {
134   }
135
136   Length Shape( const Character* const text,
137                 Length numberOfCharacters,
138                 FontId fontId,
139                 Script script )
140   {
141     // Clear previoursly shaped texts.
142     mIndices.Clear();
143     mAdvance.Clear();
144     mCharacterMap.Clear();
145     mOffset.Clear();
146     mFontId = fontId;
147
148     TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
149     TextAbstraction::Internal::FontClient& fontClientImpl = TextAbstraction::GetImplementation( fontClient );
150
151     const FontDescription::Type type = fontClientImpl.GetFontType( fontId );
152
153     switch( type )
154     {
155     case FontDescription::FACE_FONT:
156     {
157       // Reserve some space to avoid reallocations.
158       const Length numberOfGlyphs = static_cast<Length>( 1.3f * static_cast<float>( numberOfCharacters ) );
159       mIndices.Reserve( numberOfGlyphs );
160       mAdvance.Reserve( numberOfGlyphs );
161       mCharacterMap.Reserve( numberOfGlyphs );
162       mOffset.Reserve( 2u * numberOfGlyphs );
163
164       // Retrieve a FreeType font's face.
165       FT_Face face = fontClientImpl.GetFreetypeFace( fontId );
166       if( nullptr == face )
167       {
168         // Nothing to do if the face is null.
169         return 0u;
170       }
171
172       unsigned int horizontalDpi = 0u;
173       unsigned int verticalDpi = 0u;
174       fontClient.GetDpi( horizontalDpi, verticalDpi );
175
176       FT_Set_Char_Size( face,
177           0u,
178           fontClient.GetPointSize( fontId ),
179           horizontalDpi,
180           verticalDpi );
181
182       /* Get our harfbuzz font struct */
183       hb_font_t* harfBuzzFont;
184       harfBuzzFont = hb_ft_font_create( face, NULL );
185
186       /* Create a buffer for harfbuzz to use */
187       hb_buffer_t* harfBuzzBuffer = hb_buffer_create();
188
189       const bool rtlDirection = IsRightToLeftScript( script );
190       hb_buffer_set_direction( harfBuzzBuffer,
191           rtlDirection ? HB_DIRECTION_RTL : HB_DIRECTION_LTR ); /* or LTR */
192
193       hb_buffer_set_script( harfBuzzBuffer,
194           SCRIPT_TO_HARFBUZZ[ script ] ); /* see hb-unicode.h */
195
196
197       char* currentLocale = setlocale(LC_MESSAGES,NULL);
198
199       std::istringstream stringStream( currentLocale );
200       std::string localeString;
201       std::getline(stringStream, localeString, '_');
202       hb_buffer_set_language( harfBuzzBuffer, hb_language_from_string( localeString.c_str(), localeString.size() ) );
203
204       /* Layout the text */
205       hb_buffer_add_utf32( harfBuzzBuffer, text, numberOfCharacters, 0u, numberOfCharacters );
206
207       hb_shape( harfBuzzFont, harfBuzzBuffer, NULL, 0u );
208
209       /* Get glyph data */
210       unsigned int glyphCount;
211       hb_glyph_info_t* glyphInfo = hb_buffer_get_glyph_infos( harfBuzzBuffer, &glyphCount );
212       hb_glyph_position_t *glyphPositions = hb_buffer_get_glyph_positions( harfBuzzBuffer, &glyphCount );
213       const GlyphIndex lastGlyphIndex = glyphCount - 1u;
214       for( GlyphIndex i = 0u; i < glyphCount; )
215       {
216         if( rtlDirection )
217         {
218           // If the direction is right to left, Harfbuzz retrieves the glyphs in the visual order.
219           // The glyphs are needed in the logical order to layout the text in lines.
220           // Do not change the order of the glyphs if they belong to the same cluster.
221           GlyphIndex rtlIndex = lastGlyphIndex - i;
222
223           unsigned int cluster = glyphInfo[rtlIndex].cluster;
224           unsigned int previousCluster = cluster;
225           Length numberOfGlyphsInCluster = 0u;
226
227           while( ( cluster == previousCluster ) )
228           {
229             ++numberOfGlyphsInCluster;
230             previousCluster = cluster;
231
232             if( rtlIndex > 0u )
233             {
234               --rtlIndex;
235
236               cluster = glyphInfo[rtlIndex].cluster;
237             }
238             else
239             {
240               break;
241             }
242           }
243
244           rtlIndex = lastGlyphIndex - ( i + ( numberOfGlyphsInCluster - 1u ) );
245
246           for( GlyphIndex j = 0u; j < numberOfGlyphsInCluster; ++j )
247           {
248             const GlyphIndex index = rtlIndex + j;
249
250             mIndices.PushBack( glyphInfo[index].codepoint );
251             mAdvance.PushBack( floor( glyphPositions[index].x_advance * FROM_266 ) );
252             mCharacterMap.PushBack( glyphInfo[index].cluster );
253             mOffset.PushBack( floor( glyphPositions[index].x_offset * FROM_266 ) );
254             mOffset.PushBack( floor( glyphPositions[index].y_offset * FROM_266 ) );
255           }
256
257           i += numberOfGlyphsInCluster;
258         }
259         else
260         {
261           mIndices.PushBack( glyphInfo[i].codepoint );
262           mAdvance.PushBack( floor( glyphPositions[i].x_advance * FROM_266 ) );
263           mCharacterMap.PushBack( glyphInfo[i].cluster );
264           mOffset.PushBack( floor( glyphPositions[i].x_offset * FROM_266 ) );
265           mOffset.PushBack( floor( glyphPositions[i].y_offset * FROM_266 ) );
266
267           ++i;
268         }
269       }
270
271       /* Cleanup */
272       hb_buffer_destroy( harfBuzzBuffer );
273       hb_font_destroy( harfBuzzFont );
274       break;
275     }
276     case FontDescription::BITMAP_FONT:
277     {
278       // Reserve some space to avoid reallocations.
279       // The advance and offset tables can be initialized with zeros as it's not needed to get metrics from the bitmaps here.
280       mIndices.Resize( numberOfCharacters );
281       mAdvance.Resize( numberOfCharacters, 0u );
282       mCharacterMap.Reserve( numberOfCharacters );
283       mOffset.Resize( 2u * numberOfCharacters, 0.f );
284
285       // The utf32 character can be used as the glyph's index.
286       std::copy( text, text + numberOfCharacters, mIndices.Begin() );
287
288       // The glyph to character map is 1 to 1.
289       for( unsigned int index = 0u; index < numberOfCharacters; ++index )
290       {
291         mCharacterMap.PushBack( index );
292       }
293       break;
294     }
295     default:
296     {
297       DALI_LOG_INFO(gLogFilter, Debug::General, "  Invalid type of font\n");
298     }
299     }
300
301     return mIndices.Count();
302   }
303
304   void GetGlyphs( GlyphInfo* glyphInfo,
305                   CharacterIndex* glyphToCharacterMap )
306   {
307     Vector<CharacterIndex>::ConstIterator indicesIt = mIndices.Begin();
308     Vector<float>::ConstIterator advanceIt = mAdvance.Begin();
309     Vector<float>::ConstIterator offsetIt = mOffset.Begin();
310     Vector<CharacterIndex>::ConstIterator characterMapIt = mCharacterMap.Begin();
311
312     for( GlyphIndex index = 0u, size = mIndices.Count(); index < size; ++index )
313     {
314       GlyphInfo& glyph = *( glyphInfo + index );
315       CharacterIndex& glyphToCharacter = *( glyphToCharacterMap + index );
316
317       glyph.fontId = mFontId;
318       glyph.index = *( indicesIt + index );
319       glyph.advance = *( advanceIt + index );
320
321       const GlyphIndex offsetIndex = 2u * index;
322       glyph.xBearing = *( offsetIt + offsetIndex );
323       glyph.yBearing = *( offsetIt + offsetIndex + 1u );
324
325       glyphToCharacter = *( characterMapIt + index );
326     }
327   }
328
329   Vector<CharacterIndex> mIndices;
330   Vector<float>          mAdvance;
331   Vector<float>          mOffset;
332   Vector<CharacterIndex> mCharacterMap;
333   FontId                 mFontId;
334 };
335
336 Shaping::Shaping()
337 : mPlugin( NULL )
338 {
339 }
340
341 Shaping::~Shaping()
342 {
343   delete mPlugin;
344 }
345
346 TextAbstraction::Shaping Shaping::Get()
347 {
348   TextAbstraction::Shaping shapingHandle;
349
350   SingletonService service( SingletonService::Get() );
351   if( service )
352   {
353     // Check whether the singleton is already created
354     Dali::BaseHandle handle = service.GetSingleton( typeid( TextAbstraction::Shaping ) );
355     if( handle )
356     {
357       // If so, downcast the handle
358       Shaping* impl = dynamic_cast< Internal::Shaping* >( handle.GetObjectPtr() );
359       shapingHandle = TextAbstraction::Shaping( impl );
360     }
361     else // create and register the object
362     {
363       shapingHandle = TextAbstraction::Shaping( new Shaping );
364       service.Register( typeid( shapingHandle ), shapingHandle );
365     }
366   }
367
368   return shapingHandle;
369 }
370
371 Length Shaping::Shape( const Character* const text,
372                        Length numberOfCharacters,
373                        FontId fontId,
374                        Script script )
375 {
376   CreatePlugin();
377
378   return mPlugin->Shape( text,
379                          numberOfCharacters,
380                          fontId,
381                          script );
382 }
383
384 void Shaping::GetGlyphs( GlyphInfo* glyphInfo,
385                          CharacterIndex* glyphToCharacterMap )
386 {
387   CreatePlugin();
388
389   mPlugin->GetGlyphs( glyphInfo,
390                       glyphToCharacterMap );
391 }
392
393 void Shaping::CreatePlugin()
394 {
395   if( !mPlugin )
396   {
397     mPlugin = new Plugin();
398   }
399 }
400
401 } // namespace Internal
402
403 } // namespace TextAbstraction
404
405 } // namespace Dali