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