add patch
[framework/osp/uifw.git] / src / graphics / FGrp_FontRsrcManager.cpp
index faa0849..6a0ea05 100644 (file)
@@ -2,14 +2,14 @@
 // Open Service Platform
 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
 //
-// Licensed under the Flora License, Version 1.0 (the License);
+// Licensed under the Apache License, Version 2.0 (the License);
 // you may not use this file except in compliance with the License.
 // You may obtain a copy of the License at
 //
-//     http://floralicense.org/license/
+//     http://www.apache.org/licenses/LICENSE-2.0/
 //
 // Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
+// distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
@@ -72,6 +72,7 @@ const int _DEFAULT_FONT_FACE = 0;
 const char* _DEFAULT_SYSTEM_FONT = "DefaultSystemFont";
 const char* _DEFAULT_SYSTEM_BOLD_FONT = "DefaultSystemBoldFont";
 const _Util::FixedPoint26_6 _SYSTEM_DEFAULT_FONT_SIZE(16);
+const int _BLACK_LIST_MAX_COUNT = 1000;
 
 class _FontNull
        : public Tizen::Graphics::_IFont
@@ -160,12 +161,12 @@ protected:
 
        virtual void Destroy(void) {}
 
-       virtual bool FindCache(unsigned long character, _Util::FixedPoint26_6 size, int style, Glyph** pOut)
+       virtual bool FindCache(unsigned long glyphIndex, _Util::FixedPoint26_6 size, int style, Glyph** pOut)
        {
                return false;
        }
 
-       virtual bool AddCache(unsigned long character, _Util::FixedPoint26_6 size, int style, Glyph* pGlyph)
+       virtual bool AddCache(unsigned long glyphIndex, _Util::FixedPoint26_6 size, int style, Glyph* pGlyph)
        {
                return false;
        }
@@ -176,6 +177,7 @@ protected:
        }
 }; // _FontNull
 
+#ifdef USE_HASHMAP_FOR_FONT
 template<class T>
 class _FontHashCodeProvider
        : public IHashCodeProviderT <T>
@@ -203,7 +205,6 @@ public:
        }
 }; // _FontComparer
 
-#ifdef USE_HASHMAP_FOR_FONT
 template<class T>
 class _FontHashCodeProviderEx
        : public IHashCodeProviderT <T>
@@ -211,7 +212,26 @@ class _FontHashCodeProviderEx
 public:
        virtual int GetHashCode(const T& obj) const
        {
-               return reinterpret_cast<int>(obj.c_str());
+               const wchar_t* pText = obj.c_str();
+               const wchar_t* pTextEnd = pText - 1;
+
+               while (*(++pTextEnd))
+                       ;
+
+               int stride = ((pTextEnd - pText + 1) << 16) / 9;
+               int ix16 = 0;
+
+               int ret1 = (pText[(ix16 += stride) >> 16] & 0xFF);
+               ret1 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 8);
+               ret1 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 16);
+               ret1 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 24);
+
+               int ret2 = (pText[(ix16 += stride) >> 16] & 0xFF);
+               ret2 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 8);
+               ret2 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 16);
+               ret2 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 24);
+
+               return ret1 ^ ret2;
        }
 }; // _FontHashCodeProviderEx
 
@@ -222,11 +242,35 @@ class _FontComparerEx
 public:
        virtual result Compare(const T& obj1, const T& obj2, int& cmp) const
        {
-               cmp = wcscmp((wchar_t*)obj1.c_str(), (wchar_t*)obj1.c_str());
+               cmp = wcscmp((wchar_t*)obj1.c_str(), (wchar_t*)obj2.c_str());
 
                return E_SUCCESS;
        }
 }; // _FontComparerEx
+
+#else
+
+class _FindValueByString
+{
+public:
+       _FindValueByString(const _Util::WString& key)
+               : __key(key)
+       {
+       }
+
+       bool operator()(const std::pair<_Util::WString, Tizen::Graphics::_FontRsrcManager::SharedFontResource> pairValue)
+       {
+               return (__key == pairValue.first);
+       }
+
+       bool operator()(const std::pair<_Util::WString, _Util::WString> pairValue)
+       {
+               return (__key == pairValue.first);
+       }
+
+       const _Util::WString& __key;
+}; // _FindValueByString
+
 #endif
 }
 
@@ -238,20 +282,18 @@ _FontRsrcManager* _FontRsrcManager::__pTheInstance = null;
 _FontRsrcManager::_FontRsrcManager()
        : __isAppFontListInitialized(false)
 {
-       static _FontHashCodeProvider<String> fontHashCodeProvider;
-       static _FontComparer<String> fontComparer;
-
+#ifdef USE_HASHMAP_FOR_FONT
        // are you worry about failing to construct?
        // but, HashMapT only can give E_SUCCESS/E_INVALID_ARG/E_OUT_OF_MEMORY.
        // it means, if you ha2ve already verified input param, don't need to concern yourself.
        // even though you have E_OUT_OF_MEMORY, it's already out of our control,
        // and surely the other member function can give a error message.
-#ifdef USE_HASHMAP_FOR_FONT
+
        static _FontHashCodeProviderEx<_Util::WString> fontHashCodeProviderEx;
        static _FontComparerEx<_Util::WString> fontComparerEx;
        __fontRsrcMap.Construct(0, 0, fontHashCodeProviderEx, fontComparerEx);
+       __appFontMap.Construct(0, 0, fontHashCodeProviderEx, fontComparerEx);
 #endif
-       __appFontMap.Construct(0, 0, fontHashCodeProvider, fontComparer);
 
        __defaultSystemFontPath.Clear();
        __defaultSystemBoldFontPath.Clear();
@@ -274,12 +316,12 @@ _FontRsrcManager::~_FontRsrcManager()
 {
 #ifdef USE_HASHMAP_FOR_FONT
        __fontRsrcMap.RemoveAll();
+       __appFontMap.RemoveAll();
 #else
        __fontRsrcMap.clear();
+       __appFontMap.clear();
 #endif
 
-       __appFontMap.RemoveAll();
-
        delete __pDefaultSystemFont;
        delete __pFontMemoryManager;
 
@@ -507,7 +549,6 @@ _FontRsrcManager::AddFont(const _Util::WString& fontPath, SharedFontResource& ou
        return E_SUCCESS;
 }
 
-
 bool
 _FontRsrcManager::SearchFont(const _Util::WString& fontName)
 {
@@ -516,6 +557,31 @@ _FontRsrcManager::SearchFont(const _Util::WString& fontName)
        return (__SearchFont(fontName, sharedFont) == true);
 }
 
+void
+_FontRsrcManager::AddBlackList(const wchar_t unicode)
+{
+       if (__blackListUnicodeSet.size() > _BLACK_LIST_MAX_COUNT)
+       {
+               this->ClearBlackList();
+       }
+
+       __blackListUnicodeSet.insert(unicode);
+}
+
+bool
+_FontRsrcManager::SearchBlackList(const wchar_t unicode) const
+{
+       BlackListSetT::const_iterator blackListIterator = __blackListUnicodeSet.find(unicode);
+
+       return (blackListIterator != __blackListUnicodeSet.end());
+}
+
+void
+_FontRsrcManager::ClearBlackList()
+{
+       __blackListUnicodeSet.clear();
+}
+
 result
 _FontRsrcManager::GetFont(const byte* pFontData, int fontDataSize, int style, _Util::FixedPoint26_6 size, SharedFontResource& out)
 {
@@ -693,7 +759,7 @@ _FontRsrcManager::GetSystemFont(const Tizen::Base::String& fontName, int style,
        FcFontSet *pFontSet = null;
        FcResult res = FcResultNoMatch;
        FcChar8* pPath = null;
-       const char* fcStyle = (style & FONT_STYLE_BOLD) ? "Bold" : "Regular";
+       const int fcWeight = (style & FONT_STYLE_BOLD) ? FC_WEIGHT_BOLD : FC_WEIGHT_REGULAR;
        result r = E_SYSTEM;
 
        ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontName);
@@ -706,7 +772,7 @@ _FontRsrcManager::GetSystemFont(const Tizen::Base::String& fontName, int style,
        //FcBool rtn = FcInitReinitialize();
        //SysTryCatch(NID_GRP, rtn != FcFalse, , r, "[E_SYSTEM] Failed to init fontconfig");
 
-       pFontPattern = FcPatternBuild(NULL, FC_STYLE, FcTypeString, (FcChar8*)fcStyle, NULL);
+       pFontPattern = FcPatternBuild(NULL, FC_WEIGHT, FcTypeInteger, fcWeight, NULL);
        SysTryCatch(NID_GRP, pFontPattern, , r, "[E_SYSTEM] Failed to FcPatternBuild()");
 
        FcPatternAddString(pFontPattern, FC_FAMILY, (FcChar8*)utfString->GetPointer());
@@ -864,11 +930,12 @@ _FontRsrcManager::__SearchFont(const _Util::WString& fontPath, SharedFontResourc
 #ifdef USE_HASHMAP_FOR_FONT
        return (__fontRsrcMap.GetValue(fontPath, out) == E_SUCCESS);
 #else
-       FontMapT::const_iterator fontIterator = __fontRsrcMap.find(fontPath);
+       FontMapT::const_iterator fontIterator = std::find_if(__fontRsrcMap.begin(), __fontRsrcMap.end(), _FindValueByString(fontPath));
 
        if (fontIterator != __fontRsrcMap.end())
        {
                out = fontIterator->second;
+
                return true;
        }
 
@@ -887,7 +954,7 @@ _FontRsrcManager::__AddFont(const _Util::WString& key, const SharedFontResource&
 
        SysTryReturn(NID_GRP, r == E_SUCCESS || r == E_OBJ_ALREADY_EXIST, false, false, "[%s] Failed to add font into resource pool", GetErrorMessage(r));
 #else
-       __fontRsrcMap[key] = font;
+       __fontRsrcMap.push_back(std::make_pair(key, font));
 #endif
 
        return true;
@@ -964,7 +1031,14 @@ _FontRsrcManager::__RemoveUnusedFont(void)
 #ifdef USE_HASHMAP_FOR_FONT
                                __fontRsrcMap.Remove(key);
 #else
-                               __fontRsrcMap.erase(key);
+                               for (FontMapT::iterator fontMapIter = __fontRsrcMap.begin(); fontMapIter != __fontRsrcMap.end(); ++fontMapIter)
+                               {
+                                       if (fontMapIter->first == key)
+                                       {
+                                               __fontRsrcMap.erase(fontMapIter);
+                                               break;
+                                       }
+                               }
 #endif
                        }
                }
@@ -1032,6 +1106,7 @@ _FontRsrcManager::__ReloadFont(String fontName, bool isBold)
 #endif
 
                pFont->CleanCache();
+               this->ClearBlackList();
        }
 
        return true;
@@ -1098,7 +1173,8 @@ _FontRsrcManager::__GetFontPath(bool isBold)
 
        FcFontSet* pFontSet = null;
        FcPattern* pFontPattern = null;
-       const char* fcStyle = isBold ? "Bold" : "Regular";
+       const int fcWeight = isBold ? FC_WEIGHT_BOLD : FC_WEIGHT_REGULAR;
+
        // initialize fontconfig library
        //FcBool rtn = FcInitReinitialize();
        //SysTryCatch(NID_GRP, rtn != FcFalse, , E_SYSTEM, "[E_SYSTEM] Failed to init fontconfig");
@@ -1107,8 +1183,8 @@ _FontRsrcManager::__GetFontPath(bool isBold)
        pFontPattern = FcPatternCreate();
        SysTryCatch(NID_GRP, pFontPattern, , E_SYSTEM, "[E_SYSTEM] Failed to create font pattern");
 
+       FcPatternAddInteger(pFontPattern, FC_WEIGHT, fcWeight);
        FcPatternAddString(pFontPattern, FC_FAMILY, (FcChar8*)"Tizen");
-       FcPatternAddString(pFontPattern, FC_STYLE, (FcChar8*)fcStyle);
        FcConfigSubstitute(0, pFontPattern, FcMatchPattern);
        FcDefaultSubstitute(pFontPattern);
 
@@ -1266,18 +1342,12 @@ _FontRsrcManager::RemoveMemory(unsigned long requestedSize)
 #endif
 }
 
-Tizen::Base::String
-_FontRsrcManager::FindAppFontName(const Tizen::Base::String& fontName)
+_Util::WString
+_FontRsrcManager::FindAppFontName(const _Util::WString& fontName)
 {
-       String fontPath(L"");
-
-       if (__isAppFontListInitialized)
-       {
-               __appFontMap.GetValue(fontName, fontPath);
+       _Util::WString fontPath(L"");
 
-               return fontPath;
-       }
-       else
+       if (!__isAppFontListInitialized)
        {
                String dirName;
                Directory dir;
@@ -1323,7 +1393,13 @@ _FontRsrcManager::FindAppFontName(const Tizen::Base::String& fontName)
 
                                faceName = _Font::GetFaceName(name);
 
-                               __appFontMap.Add(faceName, name);
+                               _Util::WString faceNameTemp(faceName.GetPointer());
+                               _Util::WString nameTemp(name.GetPointer());
+#ifdef USE_HASHMAP_FOR_FONT
+                               __appFontMap.Add(faceNameTemp, nameTemp);
+#else
+                               __appFontMap.push_back(std::make_pair(faceNameTemp, nameTemp));
+#endif
                        }
                }
 
@@ -1331,11 +1407,20 @@ _FontRsrcManager::FindAppFontName(const Tizen::Base::String& fontName)
                pDirEnum = null;
 
                __isAppFontListInitialized = true;
+       }
 
-               __appFontMap.GetValue(fontName, fontPath);
+#ifdef USE_HASHMAP_FOR_FONT
+       __appFontMap.GetValue(fontName, fontPath);
+#else
+       AppFontMapT::const_iterator appFontIterator = std::find_if(__appFontMap.begin(), __appFontMap.end(), _FindValueByString(fontName));
 
-               return fontPath;
+       if (appFontIterator != __appFontMap.end())
+       {
+               fontPath = appFontIterator->second;
        }
+#endif
+
+       return fontPath;
 }
 
-}} // Tizen::Graphics
+}} // Tizen::Graphics
\ No newline at end of file