4960f856ee0120a97313edb8f3e19b5e25214c75
[platform/core/uifw/dali-core.git] / dali / internal / event / images / emoji-factory.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/event/images/emoji-factory.h>
19
20 // EXTERNAL INCLUDES
21 #include <sstream>
22 #include <climits>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/platform-abstraction.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace
35 {
36 const std::string U( "u" );       ///<
37 const std::string PNG( ".png" );  ///< Used to build the emoji file name.
38 const std::size_t PNG_SIZE( 4u ); ///<
39 } // namespace
40
41 EmojiFactory::EmojiFactory()
42 : mColorGlyphs(),
43   mMinEmoji( std::numeric_limits<uint32_t>::max() )
44 {
45   Internal::ThreadLocalStorage& tls = Internal::ThreadLocalStorage::Get();
46
47   // Get the color glyphs from the directory.
48   Integration::PlatformAbstraction& platform = tls.GetPlatformAbstraction();
49
50   std::vector<std::string> fileNames;
51   platform.GetFileNamesFromDirectory( DALI_EMOTICON_DIR, fileNames );
52
53   for( std::vector<std::string>::const_iterator it = fileNames.begin(), endIt = fileNames.end(); it != endIt; ++it )
54   {
55     const std::string& fileName( *it );
56     const std::size_t fileNameSize = fileName.size();
57
58     if( ( fileNameSize < PNG_SIZE + 1u ) ||
59         ( U != fileName.substr( 0u, 1u ) ) ||
60         ( PNG != fileName.substr( fileNameSize - PNG_SIZE, PNG_SIZE ) ) )
61     {
62       // Invalid emoji file name.
63       break;
64     }
65
66     uint32_t character = 0u;
67
68     std::istringstream( fileName.substr( 1u, fileNameSize - PNG_SIZE - 1u ) ) >> std::hex >> character;
69
70     if( 0u != character )
71     {
72       if( character < mMinEmoji )
73       {
74         mMinEmoji = character;
75       }
76
77       mColorGlyphs[character] = fileName;
78     }
79   }
80 }
81
82 EmojiFactory::~EmojiFactory()
83 {
84 }
85
86 bool EmojiFactory::IsEmoji( const uint32_t character ) const
87 {
88   return ( character >= mMinEmoji ) &&
89          ( mColorGlyphs.find( character ) != mColorGlyphs.end() );
90 }
91
92 std::string EmojiFactory::GetEmojiFileNameFromCharacter( const uint32_t character ) const
93 {
94   std::string fileName;
95
96   std::map<uint32_t,std::string>::const_iterator it = mColorGlyphs.find( character );
97
98   if( it != mColorGlyphs.end() )
99   {
100     fileName = it->second;
101   }
102
103   return fileName;
104 }
105
106 } // namespace Internal
107
108 } // namespace Dali