2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FGrp_FontRsrcManager.cpp
20 * @brief This is the cpp file for _FontRsrcManager class.
27 #include <unique_ptr.h>
30 #include <fontconfig/fontconfig.h>
33 #include <FBaseColIHashCodeProviderT.h>
34 #include <FBaseUtilStringUtil.h>
36 #include <FGrpFontCommon.h>
37 #include <FIoDirectory.h>
38 #include <FBaseUtilStringTokenizer.h>
40 #include <FBaseSysLog.h>
42 #include "FGrp_Font.h"
43 #include "FGrp_FontRsrcManager.h"
44 #include "FGrp_FontMemoryManager.h"
45 #include "FGrp_FontFt2.h"
47 #define USE_REMOVE_ALL_CACHE
50 #include "util/FGrp_UtilTemplate.h"
53 #define IF_NOT_INITIALIZED(code) if (_pImpl == null) \
58 using namespace Tizen::App;
59 using namespace Tizen::Io;
60 using namespace Tizen::Base;
61 using namespace Tizen::Base::Collection;
62 using namespace Tizen::Base::Utility;
63 using namespace Tizen::Graphics;
64 using namespace Tizen::System;
70 const int _DEFAULT_FONT_ENGINE = 0;
71 const int _DEFAULT_FONT_FACE = 0;
72 const char* _DEFAULT_SYSTEM_FONT = "DefaultSystemFont";
73 const char* _DEFAULT_SYSTEM_BOLD_FONT = "DefaultSystemBoldFont";
74 const _Util::FixedPoint26_6 _SYSTEM_DEFAULT_FONT_SIZE(16);
77 : public Tizen::Graphics::_IFont
80 virtual bool SetAttrib(const Attrib& fontAttrib)
85 virtual bool GetAttrib(Attrib& fontAttrib) const
90 virtual bool GetKerning(unsigned long character1, unsigned long character2, long& xVector, long& yVector) const
95 virtual bool LoadGlyph(unsigned long character, Glyph** pFontGlyphData)
100 virtual bool UnloadGlyph(Glyph** pFontGlyphData)
105 virtual unsigned long CheckGlyph(unsigned long character)
110 virtual bool GetGlyphList(const _Util::String& text, Tizen::Base::Collection::IListT<_IFont::Glyph *>& out, int script)
115 virtual bool GetFontSizeProperty(SizeProperty& sizeProperty) const
120 virtual bool GetFontProperty(Property& property) const
125 virtual FontMapT* GetFallbackMap(void)
130 virtual bool IsEmoji(void) const
136 virtual bool Create(const void* pBuffer, long bufSize, long face = 0)
141 virtual bool Create(const char* pFilePath, long face = 0)
146 virtual bool CreateStatic(const void* pBuffer, long bufSize, long face = 0)
151 virtual bool Reload(const void* pBuffer, long bufSize, long face = 0)
156 virtual bool Reload(const char* filePath, long face = 0)
161 virtual void Destroy(void) {}
163 virtual bool FindCache(unsigned long character, _Util::FixedPoint26_6 size, int style, Glyph** pOut)
168 virtual bool AddCache(unsigned long character, _Util::FixedPoint26_6 size, int style, Glyph* pGlyph)
173 virtual bool CleanCache(void)
179 #ifdef USE_HASHMAP_FOR_FONT
181 class _FontHashCodeProvider
182 : public IHashCodeProviderT <T>
185 virtual int GetHashCode(const T& obj) const
187 return obj.GetHashCode();
189 }; // _FontHashCodeProvider
193 : public IComparerT <T>
196 virtual result Compare(const T& obj1, const T& obj2, int& cmp) const
198 String& objString1 = static_cast <String&>(const_cast <T&>(obj1));
199 String& objString2 = static_cast <String&>(const_cast <T&>(obj2));
201 cmp = objString1.CompareTo(objString2);
208 class _FontHashCodeProviderEx
209 : public IHashCodeProviderT <T>
212 virtual int GetHashCode(const T& obj) const
214 const wchar_t* pText = obj.c_str();
215 const wchar_t* pTextEnd = pText - 1;
217 while (*(++pTextEnd))
220 int stride = ((pTextEnd - pText + 1) << 16) / 9;
223 int ret1 = (pText[(ix16 += stride) >> 16] & 0xFF);
224 ret1 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 8);
225 ret1 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 16);
226 ret1 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 24);
228 int ret2 = (pText[(ix16 += stride) >> 16] & 0xFF);
229 ret2 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 8);
230 ret2 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 16);
231 ret2 |= ((pText[(ix16 += stride) >> 16] & 0xFF) << 24);
235 }; // _FontHashCodeProviderEx
238 class _FontComparerEx
239 : public IComparerT <T>
242 virtual result Compare(const T& obj1, const T& obj2, int& cmp) const
244 cmp = wcscmp((wchar_t*)obj1.c_str(), (wchar_t*)obj2.c_str());
248 }; // _FontComparerEx
253 namespace Tizen { namespace Graphics
256 _FontRsrcManager* _FontRsrcManager::__pTheInstance = null;
258 _FontRsrcManager::_FontRsrcManager()
259 : __isAppFontListInitialized(false)
261 #ifdef USE_HASHMAP_FOR_FONT
262 // are you worry about failing to construct?
263 // but, HashMapT only can give E_SUCCESS/E_INVALID_ARG/E_OUT_OF_MEMORY.
264 // it means, if you ha2ve already verified input param, don't need to concern yourself.
265 // even though you have E_OUT_OF_MEMORY, it's already out of our control,
266 // and surely the other member function can give a error message.
268 static _FontHashCodeProviderEx<_Util::WString> fontHashCodeProviderEx;
269 static _FontComparerEx<_Util::WString> fontComparerEx;
270 __fontRsrcMap.Construct(0, 0, fontHashCodeProviderEx, fontComparerEx);
271 __appFontMap.Construct(0, 0, fontHashCodeProviderEx, fontComparerEx);
275 __defaultSystemFontPath.Clear();
276 __defaultSystemBoldFontPath.Clear();
277 __pDefaultSystemFont = null;
281 const unsigned long cacheMemorySize = 1 * 1024 * 1024;
282 std::unique_ptr<_FontMemoryManager> pMemoryMgr(new (std::nothrow) _FontMemoryManager(_FontMemoryManager::TYPE_SIZE, cacheMemorySize));
284 if (pMemoryMgr != null && pMemoryMgr->IsValid())
286 __pFontMemoryManager = pMemoryMgr.release();
289 SysAssert(__pFontMemoryManager != null);
292 _FontRsrcManager::~_FontRsrcManager()
294 #ifdef USE_HASHMAP_FOR_FONT
295 __fontRsrcMap.RemoveAll();
296 __appFontMap.RemoveAll();
298 __fontRsrcMap.clear();
299 __appFontMap.clear();
302 delete __pDefaultSystemFont;
303 delete __pFontMemoryManager;
309 _FontRsrcManager::GetFont(const Tizen::Base::String& fontPath, int style, _Util::FixedPoint26_6 size, SharedFontResource& out)
311 _Util::WString fontPathTemp(fontPath.GetPointer());
313 SharedFontResource sharedFont;
314 _IFont* pFont = null;
318 //--------------------------------------------------------------------------
319 // validation check of input param
321 // in case input fontPath is indicating folder,
322 // we should return 'E_UNSUPPORTED_FORMAT' as bads 2.0 did.
323 //--------------------------------------------------------------------------
326 result r = File::GetAttributes(fontPath, attr);
329 return E_FILE_NOT_FOUND;
331 else if (attr.IsDirectory())
333 return E_UNSUPPORTED_FORMAT;
337 //--------------------------------------------------------------------------
340 // if we already have specified resoruce,
341 // return it with increasing reference count
342 //--------------------------------------------------------------------------
343 rtn = __SearchFont(fontPathTemp, sharedFont);
352 //--------------------------------------------------------------------------
356 //--------------------------------------------------------------------------
359 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontPath);
364 std::auto_ptr<ByteBuffer> pathString(pBuffer);
366 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, (char*)pathString->GetPointer(), _DEFAULT_FONT_FACE, &pFont);
372 result r = file.Construct(fontPath, L"rb");
373 SysTryReturnResult(NID_GRP, r == E_SUCCESS, E_SYSTEM, "Failed to open file");
375 r = file.Seek(FILESEEKPOSITION_END, 0);
376 SysTryReturnResult(NID_GRP, r == E_SUCCESS, E_SYSTEM, "Failed to seek file");
378 fileLength = file.Tell();
379 SysTryReturnResult(NID_GRP, fileLength > 0, E_SYSTEM, "The length of the file is not positive");
381 r = file.Seek(FILESEEKPOSITION_BEGIN, 0);
382 SysTryReturnResult(NID_GRP, r == E_SUCCESS, E_SYSTEM, "Failed to seek file");
384 std::unique_ptr<unsigned char[]> buffer(new (std::nothrow) unsigned char[fileLength]);
385 SysTryReturnResult(NID_GRP, buffer, E_OUT_OF_MEMORY, "Failed to create font instance");
387 readLength = file.Read(buffer.get(), fileLength);
388 SysTryReturnResult(NID_GRP, readLength == fileLength, E_SYSTEM, "Failed to read file");
391 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, buffer.get(), fileLength, _DEFAULT_FONT_FACE, &pFont);
393 std::auto_ptr<_IFont> autoPtrFont(pFont);
394 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont instance");
396 // make shared resource
397 sharedFont.reset(autoPtrFont.release());
399 _IFont::Attrib fontAttrib;
400 rtn = pFont->GetAttrib(fontAttrib);
401 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
403 fontAttrib.size = size;
404 fontAttrib.style = 0;
405 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
406 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
408 rtn = pFont->SetAttrib(fontAttrib);
409 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
412 //--------------------------------------------------------------------------
415 // add newly created font resource to rsrc manager
416 //--------------------------------------------------------------------------
417 rtn = __AddFont(fontPathTemp, sharedFont);
418 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resource into the shared container.");
423 //--------------------------------------------------------------------------
426 // remove unused font
427 //--------------------------------------------------------------------------
428 __RemoveUnusedFont();
434 _FontRsrcManager::GetTempFont(const Tizen::Base::String& fontPath, int style, _Util::FixedPoint26_6 size, SharedFontResource& out)
436 _Util::WString fontPathTemp(fontPath.GetPointer());
438 SharedFontResource sharedFont;
439 _IFont* pFont = null;
440 //_FontCache* pFontCache = null;
442 //unsigned char* pBuffer = null;
444 //--------------------------------------------------------------------------
445 // validation check of input param
447 // in case input fontPath is indicating folder,
448 // we should return 'E_UNSUPPORTED_FORMAT' as bads 2.0 did.
449 //--------------------------------------------------------------------------
452 result r = File::GetAttributes(fontPath, attr);
455 return E_FILE_NOT_FOUND;
457 else if (attr.IsDirectory())
459 return E_UNSUPPORTED_FORMAT;
463 //--------------------------------------------------------------------------
466 // if we already have specified resoruce,
467 // return it with increasing reference count
468 //--------------------------------------------------------------------------
469 rtn = __SearchFont(fontPathTemp, sharedFont);
478 //--------------------------------------------------------------------------
482 //--------------------------------------------------------------------------
484 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontPath);
489 std::auto_ptr<ByteBuffer> pathString(pBuffer);
491 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, (char*)pathString->GetPointer(), _DEFAULT_FONT_FACE, &pFont);
493 std::auto_ptr<_IFont> autoPtrFont(pFont);
494 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont instance");
496 // make shared resource
497 sharedFont.reset(autoPtrFont.release());
499 _IFont::Attrib fontAttrib;
500 rtn = pFont->GetAttrib(fontAttrib);
501 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
503 fontAttrib.size = size;
504 fontAttrib.style = 0;
505 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
506 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
508 rtn = pFont->SetAttrib(fontAttrib);
509 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
519 _FontRsrcManager::AddFont(const _Util::WString& fontPath, SharedFontResource& out)
523 rtn = __AddFont(fontPath, out);
524 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resource into the shared container.");
531 _FontRsrcManager::SearchFont(const _Util::WString& fontName)
533 SharedFontResource sharedFont;
535 return (__SearchFont(fontName, sharedFont) == true);
539 _FontRsrcManager::GetFont(const byte* pFontData, int fontDataSize, int style, _Util::FixedPoint26_6 size, SharedFontResource& out)
541 SharedFontResource sharedFont;
542 _IFont* pFont = null;
544 //--------------------------------------------------------------------------
547 // create new font with user resource.
548 //--------------------------------------------------------------------------
549 bool rtn = __CreateFont(_DEFAULT_FONT_ENGINE, pFontData, fontDataSize, _DEFAULT_FONT_FACE, &pFont);
550 std::auto_ptr<_IFont> autoPtrFont(pFont);
551 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont");
553 // make shared resource
554 sharedFont.reset(autoPtrFont.release());
556 _IFont::Attrib fontAttrib;
557 rtn = pFont->GetAttrib(fontAttrib);
558 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
560 fontAttrib.size = size;
561 fontAttrib.style = 0;
562 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
563 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
565 rtn = pFont->SetAttrib(fontAttrib);
566 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
568 //--------------------------------------------------------------------------
571 // add newly created font resource to rsrc manager
572 //--------------------------------------------------------------------------
573 static long long idx = 0;
575 String fontName(L"UserMemoryFont");
576 fontName.Append(idx++);
578 _Util::WString fontNameTemp(fontName.GetPointer());
580 rtn = __AddFont(fontNameTemp, sharedFont);
581 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resrouce into the shared container.");
586 //--------------------------------------------------------------------------
589 // remove unused font
590 //--------------------------------------------------------------------------
591 __RemoveUnusedFont();
597 _FontRsrcManager::GetFont(int style, _Util::FixedPoint26_6 size, SharedFontResource& out)
599 SharedFontResource sharedFont;
600 bool isBold = (style & FONT_STYLE_BOLD) ? true : false;
602 String systemFontName = isBold ? _DEFAULT_SYSTEM_BOLD_FONT : _DEFAULT_SYSTEM_FONT;
604 _Util::WString systemFontNameTemp(systemFontName.GetPointer());
606 _IFont* pFont = null;
609 //--------------------------------------------------------------------------
612 // if we already have specified resoruce,
613 // return it with increasing reference count
614 //--------------------------------------------------------------------------
615 rtn = __SearchFont(systemFontNameTemp, sharedFont);
624 //--------------------------------------------------------------------------
627 // create new font with default font resource.
628 //--------------------------------------------------------------------------
630 isBold ? __defaultSystemBoldFontPath.Clear() : __defaultSystemFontPath.Clear();
632 const String& defaultSystemFontPath = __GetDefaultSystemFontPath(isBold);
633 SysTryReturnResult(NID_GRP, defaultSystemFontPath.IsEmpty() == false, E_SYSTEM, "Failed to get default system font path");
637 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(defaultSystemFontPath);
642 std::auto_ptr<ByteBuffer> pathString(pBuffer);
644 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, (char*)pathString->GetPointer(), _DEFAULT_FONT_FACE, &pFont);
649 result r = file.Construct(defaultSystemFontPath, L"rb");
650 SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to open file", GetErrorMessage(r));
652 r = file.Seek(FILESEEKPOSITION_END, 0);
653 SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to seek file", GetErrorMessage(r));
655 fileLength = file.Tell();
656 SysTryReturn(NID_GRP, fileLength > 0, r, r, "[%s] The length of the file is not positive", GetErrorMessage(r));
658 r = file.Seek(FILESEEKPOSITION_BEGIN, 0);
659 SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to seek file", GetErrorMessage(r));
661 std::unique_ptr<unsigned char[]> autoPtrBuffer(new (std::nothrow) unsigned char[fileLength]);
662 SysTryReturn(NID_GRP, autoPtrBuffer, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to create buffer of the file length");
664 file.Read(autoPtrBuffer.get(), fileLength);
666 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, autoPtrBuffer.get(), fileLength, _DEFAULT_FONT_FACE, &pFont);
668 std::auto_ptr<_IFont> autoPtrFont(pFont);
669 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont instance");
671 // make shared resource
672 sharedFont.reset(autoPtrFont.release());
674 _IFont::Attrib fontAttrib;
675 rtn = pFont->GetAttrib(fontAttrib);
676 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
678 fontAttrib.size = size;
679 fontAttrib.style = 0;
680 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
681 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
683 rtn = pFont->SetAttrib(fontAttrib);
684 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
687 //--------------------------------------------------------------------------
690 // add newly created font resource to rsrc manager
691 //--------------------------------------------------------------------------
692 rtn = __AddFont(systemFontNameTemp, sharedFont);
693 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resource into the shared container");
698 //--------------------------------------------------------------------------
701 // remove unused font
702 //--------------------------------------------------------------------------
703 __RemoveUnusedFont();
709 _FontRsrcManager::GetSystemFont(const Tizen::Base::String& fontName, int style, _Util::FixedPoint26_6 size, SharedFontResource& out)
711 FcPattern *pFontPattern = null;
712 FcFontSet *pFontSet = null;
713 FcResult res = FcResultNoMatch;
714 FcChar8* pPath = null;
715 const char* fcStyle = (style & FONT_STYLE_BOLD) ? "Bold" : "Regular";
718 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontName);
723 std::auto_ptr<ByteBuffer> utfString(pBuffer);
725 //FcBool rtn = FcInitReinitialize();
726 //SysTryCatch(NID_GRP, rtn != FcFalse, , r, "[E_SYSTEM] Failed to init fontconfig");
728 pFontPattern = FcPatternBuild(NULL, FC_STYLE, FcTypeString, (FcChar8*)fcStyle, NULL);
729 SysTryCatch(NID_GRP, pFontPattern, , r, "[E_SYSTEM] Failed to FcPatternBuild()");
731 FcPatternAddString(pFontPattern, FC_FAMILY, (FcChar8*)utfString->GetPointer());
732 FcConfigSubstitute(NULL, pFontPattern, FcMatchPattern);
733 FcDefaultSubstitute(pFontPattern);
735 pFontSet = FcFontSort(NULL, pFontPattern, FcTrue, NULL, &res);
736 SysTryCatch(NID_GRP, pFontSet, , r, "[E_SYSTEM] Failed to FcFontSort()");
738 res = FcPatternGetString(pFontSet->fonts[0], FC_FILE, 0, &pPath);
739 SysTryCatch(NID_GRP, res == FcResultMatch, , r, "[E_SYSTEM] Failed to get path of system font");
741 r = this->GetFont(String((char*)pPath), style, size, out);
742 SysTryCatch(NID_GRP, r == E_SUCCESS, , r, "[%s] Failed to get font", GetErrorMessage(r));
745 FcPatternDestroy(pFontPattern);
746 FcFontSetDestroy(pFontSet);
754 FcFontSetDestroy(pFontSet);
759 FcPatternDestroy(pFontPattern);
768 _FontRsrcManager::__InitFontRsrcManager(void)
770 static _FontRsrcManager instance;
771 __pTheInstance = &instance;
775 _FontRsrcManager::GetInstance(void)
777 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
781 pthread_once(&once_block, __InitFontRsrcManager);
784 return (*__pTheInstance);
788 _FontRsrcManager::__CreateFont(int fontEngine, const void* pBuffer, long bufSize, long face, _IFont** ppOut)
791 SysTryReturn(NID_GRP, ppOut, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
793 std::auto_ptr<_IFont> autoPtrFont;
798 autoPtrFont.reset(new (std::nothrow) _FontFt2);
802 autoPtrFont.reset(new (std::nothrow) _FontNull);
806 SysTryReturn(NID_GRP, autoPtrFont.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
808 bool rtn = autoPtrFont->Create(pBuffer, bufSize, face);
809 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to create font resource");
812 *ppOut = autoPtrFont.release();
818 _FontRsrcManager::__CreateFont(int fontEngine, const char* filePath, const long face, _IFont** ppOut)
821 SysTryReturn(NID_GRP, ppOut, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
823 std::auto_ptr<_IFont> autoPtrFont;
828 autoPtrFont.reset(new (std::nothrow) _FontFt2);
832 autoPtrFont.reset(new (std::nothrow) _FontNull);
836 SysTryReturn(NID_GRP, autoPtrFont.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
838 bool rtn = autoPtrFont->Create(filePath, face);
839 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to create font resource");
842 *ppOut = autoPtrFont.release();
848 _FontRsrcManager::__CreateStaticFont(int fontEngine, const void* pBuffer, long bufSize, long face, _IFont** ppOut)
851 SysTryReturn(NID_GRP, ppOut, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
853 std::auto_ptr<_IFont> autoPtrFont;
858 autoPtrFont.reset(new (std::nothrow) _FontFt2);
862 autoPtrFont.reset(new (std::nothrow) _FontNull);
866 SysTryReturn(NID_GRP, autoPtrFont.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
868 bool rtn = autoPtrFont->CreateStatic(pBuffer, bufSize, face);
869 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to create font resource");
872 *ppOut = autoPtrFont.release();
878 _FontRsrcManager::__SearchFont(const _Util::WString& fontPath, SharedFontResource& out) const
881 SysTryReturn(NID_GRP, out.get() == null, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
883 #ifdef USE_HASHMAP_FOR_FONT
884 return (__fontRsrcMap.GetValue(fontPath, out) == E_SUCCESS);
886 FontMapT::const_iterator fontIterator = __fontRsrcMap.find(fontPath);
888 if (fontIterator != __fontRsrcMap.end())
890 out = fontIterator->second;
899 _FontRsrcManager::__AddFont(const _Util::WString& key, const SharedFontResource& font)
902 SysTryReturn(NID_GRP, font.get() != null, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input param is given");
904 #ifdef USE_HASHMAP_FOR_FONT
905 result r = __fontRsrcMap.Add(key, font);
907 SysTryReturn(NID_GRP, r == E_SUCCESS || r == E_OBJ_ALREADY_EXIST, false, false, "[%s] Failed to add font into resource pool", GetErrorMessage(r));
909 __fontRsrcMap[key] = font;
916 _FontRsrcManager::__RemoveUnusedFont(void)
918 ArrayListT<_Util::WString> keys;
919 String systemFontName(_DEFAULT_SYSTEM_FONT);
920 String systemBoldFontName(_DEFAULT_SYSTEM_BOLD_FONT);
922 _Util::WString systemFontNameTemp(systemFontName.GetPointer());
923 _Util::WString systemBoldFontNameTemp(systemBoldFontName.GetPointer());
925 // find out unused font
927 #ifdef USE_HASHMAP_FOR_FONT
929 std::auto_ptr<IMapEnumeratorT<_Util::WString, SharedFontResource> > enumerator(__fontRsrcMap.GetMapEnumeratorN());
930 result r = GetLastResult();
931 SysTryReturnVoidResult(NID_GRP, enumerator.get() != null, r, "[%s] Propagating.", GetErrorMessage(r));
933 while (enumerator->MoveNext() == E_SUCCESS)
935 SharedFontResource font;
936 r = enumerator->GetValue(font);
938 if (r == E_SUCCESS && font.use_count() == 2)
940 _Util::WString key(L"");
941 r = enumerator->GetKey(key);
943 if (r == E_SUCCESS && (key != systemFontNameTemp && key != systemBoldFontNameTemp))
952 for (FontMapT::iterator fontIterator = __fontRsrcMap.begin(); fontIterator != __fontRsrcMap.end(); ++fontIterator)
954 SharedFontResource font(fontIterator->second);
956 if (font != null && font.use_count() == 2)
958 _Util::WString key(fontIterator->first);
960 if (key != systemFontNameTemp && key != systemBoldFontNameTemp)
970 if (keys.GetCount() > 0)
972 std::auto_ptr<IEnumeratorT<_Util::WString> > enumerator(keys.GetEnumeratorN());
973 result r = GetLastResult();
974 SysTryReturnVoidResult(NID_GRP, enumerator.get() != null, r, "[%s] Propagating.", GetErrorMessage(r));
976 while (enumerator->MoveNext() == E_SUCCESS)
978 _Util::WString key(L"");
979 r = enumerator->GetCurrent(key);
983 #ifdef USE_HASHMAP_FOR_FONT
984 __fontRsrcMap.Remove(key);
986 __fontRsrcMap.erase(key);
996 _FontRsrcManager::__ReloadFont(String fontName, bool isBold)
998 SharedFontResource sharedFont;
999 _Util::WString systemFontName(fontName.GetPointer());
1001 _IFont* pFont = null;
1004 // Check if there is default system font in font resource manager
1005 // If there isn't default system font, we can't reload font resource
1006 rtn = __SearchFont(systemFontName, sharedFont);
1007 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to search default system font");
1009 // Get default system font interface instance
1010 pFont = sharedFont.get();
1012 // Reload system font resource in font interface
1015 isBold ? __defaultSystemBoldFontPath.Clear() : __defaultSystemFontPath.Clear();
1016 const String& fontPath = __GetDefaultSystemFontPath(isBold);
1018 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontPath);
1019 if (pBuffer == null)
1023 std::auto_ptr<ByteBuffer> pathString(pBuffer);
1025 rtn = pFont->Reload((char*)pathString->GetPointer(), _DEFAULT_FONT_FACE);
1026 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to reload font");
1031 isBold ? __defaultSystemBoldFontPath.Clear() : __defaultSystemFontPath.Clear();
1033 result r = file.Construct(__GetDefaultSystemFontPath(isBold), L"rb");
1034 SysTryReturn(NID_GRP, r == E_SUCCESS, false, r, "[%s] Failed to open file", GetErrorMessage(r));
1036 r = file.Seek(FILESEEKPOSITION_END, 0);
1037 SysTryReturn(NID_GRP, r == E_SUCCESS, false, r, "[%s] Failed to seek file", GetErrorMessage(r));
1039 fileLength = file.Tell();
1040 SysTryReturn(NID_GRP, fileLength > 0, false, r, "[%s] The length of the file is not positive", GetErrorMessage(r));
1042 r = file.Seek(FILESEEKPOSITION_BEGIN, 0);
1043 SysTryReturn(NID_GRP, r == E_SUCCESS, false, r, "[%s] Failed to seek file", GetErrorMessage(r));
1045 std::unique_ptr<unsigned char[]> autoPtrBuffer(new (std::nothrow) unsigned char[fileLength]);
1046 SysTryReturn(NID_GRP, autoPtrBuffer, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to create buffer of the file length");
1048 file.Read(autoPtrBuffer.get(), fileLength);
1049 rtn = pFont->Reload(autoPtrBuffer.get(), fileLength, _DEFAULT_FONT_FACE);
1050 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to reload font");
1053 pFont->CleanCache();
1060 _FontRsrcManager::ReloadDefaultSystemFont(void)
1062 // reinitialize fontconfig library
1063 FcInitReinitialize();
1065 String systemFontName;
1067 systemFontName = _DEFAULT_SYSTEM_FONT;
1068 __ReloadFont(systemFontName, false);
1070 systemFontName = _DEFAULT_SYSTEM_BOLD_FONT;
1071 __ReloadFont(systemFontName, true);
1077 _FontRsrcManager::GetMemoryManager(void)
1079 return *__pFontMemoryManager;
1084 _FontRsrcManager::GetDefaultSystemFont(void)
1086 if (__pDefaultSystemFont == null)
1088 std::auto_ptr<_Font> font(new (std::nothrow) _Font);
1092 result r = font->Construct(FONT_STYLE_PLAIN, _SYSTEM_DEFAULT_FONT_SIZE);
1096 __pDefaultSystemFont = font.release();
1101 SysAssert(__pDefaultSystemFont != null);
1103 return *__pDefaultSystemFont;
1108 _FontRsrcManager::__GetMaxFontEngine(void) const
1114 _FontRsrcManager::__GetFontPath(bool isBold)
1116 String fontPath(L"");
1118 FcFontSet* pFontSet = null;
1119 FcPattern* pFontPattern = null;
1120 const char* fcStyle = isBold ? "Bold" : "Regular";
1121 // initialize fontconfig library
1122 //FcBool rtn = FcInitReinitialize();
1123 //SysTryCatch(NID_GRP, rtn != FcFalse, , E_SYSTEM, "[E_SYSTEM] Failed to init fontconfig");
1126 pFontPattern = FcPatternCreate();
1127 SysTryCatch(NID_GRP, pFontPattern, , E_SYSTEM, "[E_SYSTEM] Failed to create font pattern");
1129 FcPatternAddString(pFontPattern, FC_FAMILY, (FcChar8*)"Tizen");
1130 FcPatternAddString(pFontPattern, FC_STYLE, (FcChar8*)fcStyle);
1131 FcConfigSubstitute(0, pFontPattern, FcMatchPattern);
1132 FcDefaultSubstitute(pFontPattern);
1135 pFontSet = FcFontSetCreate();
1136 SysTryCatch(NID_GRP, pFontSet, , E_SYSTEM, "[E_SYSTEM] Failed to create fontset");
1139 FcPattern* pMatchedPattern = null;
1140 FcResult result = FcResultNoMatch;
1141 pMatchedPattern = FcFontMatch(NULL, pFontPattern, &result);
1143 if (/*result == FcResultMatch && */pMatchedPattern)
1145 FcFontSetAdd(pFontSet, pMatchedPattern);
1148 // check if we have a system font info
1149 SysTryCatch(NID_GRP, pFontSet->nfont >= 1, , E_SYSTEM, "[E_SYSTEM] Default system count is %d", pFontSet->nfont);
1151 FcChar8* pPath = null;
1152 result = FcPatternGetString(pFontSet->fonts[0], FC_FILE, 0, &pPath);
1153 SysTryCatch(NID_GRP, result == FcResultMatch, , E_SYSTEM, "[E_SYSTEM] Failed to create fontset");
1155 fontPath.Append(String((char*)pPath));
1159 FcPatternDestroy(pFontPattern);
1160 FcFontSetDestroy(pFontSet);
1166 if (pFontSet != null)
1168 FcFontSetDestroy(pFontSet);
1171 if (pFontPattern != null)
1173 FcPatternDestroy(pFontPattern);
1180 _FontRsrcManager::__GetDefaultSystemFontPath(bool isBold)
1182 String& defaultPath = (isBold) ? __defaultSystemBoldFontPath : __defaultSystemFontPath;
1184 if (!defaultPath.IsEmpty())
1190 String path = __GetFontPath(isBold);
1196 path = __GetFontPath(false);
1197 // [ykahn] Is 'path' always not NULL ?
1200 __defaultSystemBoldFontPath.Clear();
1201 __defaultSystemBoldFontPath.Append(path);
1203 return __defaultSystemBoldFontPath;
1207 __defaultSystemFontPath.Clear();
1208 __defaultSystemFontPath.Append(path);
1210 return __defaultSystemFontPath;
1216 _FontRsrcManager::RemoveMemory(unsigned long requestedSize)
1218 #ifdef USE_REMOVE_ALL_CACHE
1220 #ifdef USE_HASHMAP_FOR_FONT
1221 result r = E_SUCCESS;
1222 std::auto_ptr<IMapEnumeratorT<_Util::WString, SharedFontResource > > enumerator(__fontRsrcMap.GetMapEnumeratorN());
1224 while (enumerator->MoveNext() == E_SUCCESS)
1226 SharedFontResource fontRsrc;
1227 r = enumerator->GetValue(fontRsrc);
1229 fontRsrc.get()->CleanCache();
1234 for (FontMapT::iterator fontIterator = __fontRsrcMap.begin(); fontIterator != __fontRsrcMap.end(); ++fontIterator)
1236 SharedFontResource fontRsrc(fontIterator->second);
1238 fontRsrc.get()->CleanCache();
1245 unsigned long remainedMemory = 0;
1246 result r = E_SUCCESS;
1247 bool isComplete = false;
1249 ArrayListT<String> keys;
1250 String systemFontName(_DEFAULT_SYSTEM_FONT);
1251 _Util::WString systemFontNameTemp(systemFontName.GetPointer());
1253 std::auto_ptr<IMapEnumeratorT<_Util::WString, SharedFontResource > > enumerator(__fontRsrcMap.GetMapEnumeratorN());
1255 while (enumerator->MoveNext() == E_SUCCESS)
1263 r = enumerator->GetKey(key);
1264 if (r == E_SUCCESS/* && key != systemFontNameTemp*/)
1266 SharedFontResource fontRsrc;
1267 r = enumerator->GetValue(fontRsrc);
1269 while (!fontRsrc.get()->second->IsEmpty())
1271 fontRsrc.get()->second->RemoveFirst();
1273 remainedMemory = __pFontMemoryManager->GetRemainedMemory();
1275 if (remainedMemory > requestedSize)
1289 _FontRsrcManager::FindAppFontName(const _Util::WString& fontName)
1291 _Util::WString fontPath(L"");
1293 if (!__isAppFontListInitialized)
1297 DirEnumerator* pDirEnum;
1299 //dirName = L"/usr/share/app_font";
1300 dirName = Tizen::App::App::GetInstance()->GetAppResourcePath() + L"font";
1302 // Open the directory
1303 if (IsFailed(dir.Construct(dirName)))
1308 // Reads all the directory entries
1309 pDirEnum = dir.ReadN();
1316 // Loops through all the directory entries
1317 while (pDirEnum->MoveNext() == E_SUCCESS)
1319 SharedFontResource sharedFont;
1323 DirEntry entry = pDirEnum->GetCurrentDirEntry();
1324 Tizen::Base::Utility::StringTokenizer formatTok(entry.GetName(), L".");
1326 while (formatTok.GetTokenCount())
1328 formatTok.GetNextToken(token);
1331 if (entry.IsDirectory() == false)
1333 name.Append(dirName);
1335 name.Append(entry.GetName());
1337 faceName = _Font::GetFaceName(name);
1339 _Util::WString faceNameTemp(faceName.GetPointer());
1340 _Util::WString nameTemp(name.GetPointer());
1341 #ifdef USE_HASHMAP_FOR_FONT
1342 __appFontMap.Add(faceNameTemp, nameTemp);
1344 __appFontMap[faceNameTemp] = nameTemp;
1352 __isAppFontListInitialized = true;
1355 #ifdef USE_HASHMAP_FOR_FONT
1356 __appFontMap.GetValue(fontName, fontPath);
1358 AppFontMapT::const_iterator appFontIterator = __appFontMap.find(fontName);
1360 if (appFontIterator != __appFontMap.end())
1362 fontPath = appFontIterator->second;
1369 }} // Tizen::Graphics