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)
180 class _FontHashCodeProvider
181 : public IHashCodeProviderT <T>
184 virtual int GetHashCode(const T& obj) const
186 return obj.GetHashCode();
188 }; // _FontHashCodeProvider
192 : public IComparerT <T>
195 virtual result Compare(const T& obj1, const T& obj2, int& cmp) const
197 String& objString1 = static_cast <String&>(const_cast <T&>(obj1));
198 String& objString2 = static_cast <String&>(const_cast <T&>(obj2));
200 cmp = objString1.CompareTo(objString2);
206 #ifdef USE_HASHMAP_FOR_FONT
208 class _FontHashCodeProviderEx
209 : public IHashCodeProviderT <T>
212 virtual int GetHashCode(const T& obj) const
214 return reinterpret_cast<int>(obj.c_str());
216 }; // _FontHashCodeProviderEx
219 class _FontComparerEx
220 : public IComparerT <T>
223 virtual result Compare(const T& obj1, const T& obj2, int& cmp) const
225 cmp = wcscmp((wchar_t*)obj1.c_str(), (wchar_t*)obj1.c_str());
229 }; // _FontComparerEx
233 namespace Tizen { namespace Graphics
236 _FontRsrcManager* _FontRsrcManager::__pTheInstance = null;
238 _FontRsrcManager::_FontRsrcManager()
239 : __isAppFontListInitialized(false)
241 static _FontHashCodeProvider<String> fontHashCodeProvider;
242 static _FontComparer<String> fontComparer;
244 // are you worry about failing to construct?
245 // but, HashMapT only can give E_SUCCESS/E_INVALID_ARG/E_OUT_OF_MEMORY.
246 // it means, if you ha2ve already verified input param, don't need to concern yourself.
247 // even though you have E_OUT_OF_MEMORY, it's already out of our control,
248 // and surely the other member function can give a error message.
249 #ifdef USE_HASHMAP_FOR_FONT
250 static _FontHashCodeProviderEx<_Util::WString> fontHashCodeProviderEx;
251 static _FontComparerEx<_Util::WString> fontComparerEx;
252 __fontRsrcMap.Construct(0, 0, fontHashCodeProviderEx, fontComparerEx);
254 __appFontMap.Construct(0, 0, fontHashCodeProvider, fontComparer);
256 __defaultSystemFontPath.Clear();
257 __defaultSystemBoldFontPath.Clear();
258 __pDefaultSystemFont = null;
262 const unsigned long cacheMemorySize = 1 * 1024 * 1024;
263 std::unique_ptr<_FontMemoryManager> pMemoryMgr(new (std::nothrow) _FontMemoryManager(_FontMemoryManager::TYPE_SIZE, cacheMemorySize));
265 if (pMemoryMgr != null && pMemoryMgr->IsValid())
267 __pFontMemoryManager = pMemoryMgr.release();
270 SysAssert(__pFontMemoryManager != null);
273 _FontRsrcManager::~_FontRsrcManager()
275 #ifdef USE_HASHMAP_FOR_FONT
276 __fontRsrcMap.RemoveAll();
278 __fontRsrcMap.clear();
281 __appFontMap.RemoveAll();
283 delete __pDefaultSystemFont;
284 delete __pFontMemoryManager;
290 _FontRsrcManager::GetFont(const Tizen::Base::String& fontPath, int style, _Util::FixedPoint26_6 size, SharedFontResource& out)
292 _Util::WString fontPathTemp(fontPath.GetPointer());
294 SharedFontResource sharedFont;
295 _IFont* pFont = null;
299 //--------------------------------------------------------------------------
300 // validation check of input param
302 // in case input fontPath is indicating folder,
303 // we should return 'E_UNSUPPORTED_FORMAT' as bads 2.0 did.
304 //--------------------------------------------------------------------------
307 result r = File::GetAttributes(fontPath, attr);
310 return E_FILE_NOT_FOUND;
312 else if (attr.IsDirectory())
314 return E_UNSUPPORTED_FORMAT;
318 //--------------------------------------------------------------------------
321 // if we already have specified resoruce,
322 // return it with increasing reference count
323 //--------------------------------------------------------------------------
324 rtn = __SearchFont(fontPathTemp, sharedFont);
333 //--------------------------------------------------------------------------
337 //--------------------------------------------------------------------------
340 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontPath);
345 std::auto_ptr<ByteBuffer> pathString(pBuffer);
347 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, (char*)pathString->GetPointer(), _DEFAULT_FONT_FACE, &pFont);
353 result r = file.Construct(fontPath, L"rb");
354 SysTryReturnResult(NID_GRP, r == E_SUCCESS, E_SYSTEM, "Failed to open file");
356 r = file.Seek(FILESEEKPOSITION_END, 0);
357 SysTryReturnResult(NID_GRP, r == E_SUCCESS, E_SYSTEM, "Failed to seek file");
359 fileLength = file.Tell();
360 SysTryReturnResult(NID_GRP, fileLength > 0, E_SYSTEM, "The length of the file is not positive");
362 r = file.Seek(FILESEEKPOSITION_BEGIN, 0);
363 SysTryReturnResult(NID_GRP, r == E_SUCCESS, E_SYSTEM, "Failed to seek file");
365 std::unique_ptr<unsigned char[]> buffer(new (std::nothrow) unsigned char[fileLength]);
366 SysTryReturnResult(NID_GRP, buffer, E_OUT_OF_MEMORY, "Failed to create font instance");
368 readLength = file.Read(buffer.get(), fileLength);
369 SysTryReturnResult(NID_GRP, readLength == fileLength, E_SYSTEM, "Failed to read file");
372 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, buffer.get(), fileLength, _DEFAULT_FONT_FACE, &pFont);
374 std::auto_ptr<_IFont> autoPtrFont(pFont);
375 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont instance");
377 // make shared resource
378 sharedFont.reset(autoPtrFont.release());
380 _IFont::Attrib fontAttrib;
381 rtn = pFont->GetAttrib(fontAttrib);
382 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
384 fontAttrib.size = size;
385 fontAttrib.style = 0;
386 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
387 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
389 rtn = pFont->SetAttrib(fontAttrib);
390 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
393 //--------------------------------------------------------------------------
396 // add newly created font resource to rsrc manager
397 //--------------------------------------------------------------------------
398 rtn = __AddFont(fontPathTemp, sharedFont);
399 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resource into the shared container.");
404 //--------------------------------------------------------------------------
407 // remove unused font
408 //--------------------------------------------------------------------------
409 __RemoveUnusedFont();
415 _FontRsrcManager::GetTempFont(const Tizen::Base::String& fontPath, int style, _Util::FixedPoint26_6 size, SharedFontResource& out)
417 _Util::WString fontPathTemp(fontPath.GetPointer());
419 SharedFontResource sharedFont;
420 _IFont* pFont = null;
421 //_FontCache* pFontCache = null;
423 //unsigned char* pBuffer = null;
425 //--------------------------------------------------------------------------
426 // validation check of input param
428 // in case input fontPath is indicating folder,
429 // we should return 'E_UNSUPPORTED_FORMAT' as bads 2.0 did.
430 //--------------------------------------------------------------------------
433 result r = File::GetAttributes(fontPath, attr);
436 return E_FILE_NOT_FOUND;
438 else if (attr.IsDirectory())
440 return E_UNSUPPORTED_FORMAT;
444 //--------------------------------------------------------------------------
447 // if we already have specified resoruce,
448 // return it with increasing reference count
449 //--------------------------------------------------------------------------
450 rtn = __SearchFont(fontPathTemp, sharedFont);
459 //--------------------------------------------------------------------------
463 //--------------------------------------------------------------------------
465 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontPath);
470 std::auto_ptr<ByteBuffer> pathString(pBuffer);
472 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, (char*)pathString->GetPointer(), _DEFAULT_FONT_FACE, &pFont);
474 std::auto_ptr<_IFont> autoPtrFont(pFont);
475 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont instance");
477 // make shared resource
478 sharedFont.reset(autoPtrFont.release());
480 _IFont::Attrib fontAttrib;
481 rtn = pFont->GetAttrib(fontAttrib);
482 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
484 fontAttrib.size = size;
485 fontAttrib.style = 0;
486 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
487 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
489 rtn = pFont->SetAttrib(fontAttrib);
490 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
500 _FontRsrcManager::AddFont(const _Util::WString& fontPath, SharedFontResource& out)
504 rtn = __AddFont(fontPath, out);
505 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resource into the shared container.");
512 _FontRsrcManager::SearchFont(const _Util::WString& fontName)
514 SharedFontResource sharedFont;
516 return (__SearchFont(fontName, sharedFont) == true);
520 _FontRsrcManager::GetFont(const byte* pFontData, int fontDataSize, int style, _Util::FixedPoint26_6 size, SharedFontResource& out)
522 SharedFontResource sharedFont;
523 _IFont* pFont = null;
525 //--------------------------------------------------------------------------
528 // create new font with user resource.
529 //--------------------------------------------------------------------------
530 bool rtn = __CreateFont(_DEFAULT_FONT_ENGINE, pFontData, fontDataSize, _DEFAULT_FONT_FACE, &pFont);
531 std::auto_ptr<_IFont> autoPtrFont(pFont);
532 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont");
534 // make shared resource
535 sharedFont.reset(autoPtrFont.release());
537 _IFont::Attrib fontAttrib;
538 rtn = pFont->GetAttrib(fontAttrib);
539 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
541 fontAttrib.size = size;
542 fontAttrib.style = 0;
543 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
544 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
546 rtn = pFont->SetAttrib(fontAttrib);
547 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
549 //--------------------------------------------------------------------------
552 // add newly created font resource to rsrc manager
553 //--------------------------------------------------------------------------
554 static long long idx = 0;
556 String fontName(L"UserMemoryFont");
557 fontName.Append(idx++);
559 _Util::WString fontNameTemp(fontName.GetPointer());
561 rtn = __AddFont(fontNameTemp, sharedFont);
562 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resrouce into the shared container.");
567 //--------------------------------------------------------------------------
570 // remove unused font
571 //--------------------------------------------------------------------------
572 __RemoveUnusedFont();
578 _FontRsrcManager::GetFont(int style, _Util::FixedPoint26_6 size, SharedFontResource& out)
580 SharedFontResource sharedFont;
581 bool isBold = (style & FONT_STYLE_BOLD) ? true : false;
583 String systemFontName = isBold ? _DEFAULT_SYSTEM_BOLD_FONT : _DEFAULT_SYSTEM_FONT;
585 _Util::WString systemFontNameTemp(systemFontName.GetPointer());
587 _IFont* pFont = null;
590 //--------------------------------------------------------------------------
593 // if we already have specified resoruce,
594 // return it with increasing reference count
595 //--------------------------------------------------------------------------
596 rtn = __SearchFont(systemFontNameTemp, sharedFont);
605 //--------------------------------------------------------------------------
608 // create new font with default font resource.
609 //--------------------------------------------------------------------------
611 isBold ? __defaultSystemBoldFontPath.Clear() : __defaultSystemFontPath.Clear();
613 const String& defaultSystemFontPath = __GetDefaultSystemFontPath(isBold);
614 SysTryReturnResult(NID_GRP, defaultSystemFontPath.IsEmpty() == false, E_SYSTEM, "Failed to get default system font path");
618 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(defaultSystemFontPath);
623 std::auto_ptr<ByteBuffer> pathString(pBuffer);
625 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, (char*)pathString->GetPointer(), _DEFAULT_FONT_FACE, &pFont);
630 result r = file.Construct(defaultSystemFontPath, L"rb");
631 SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to open file", GetErrorMessage(r));
633 r = file.Seek(FILESEEKPOSITION_END, 0);
634 SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to seek file", GetErrorMessage(r));
636 fileLength = file.Tell();
637 SysTryReturn(NID_GRP, fileLength > 0, r, r, "[%s] The length of the file is not positive", GetErrorMessage(r));
639 r = file.Seek(FILESEEKPOSITION_BEGIN, 0);
640 SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to seek file", GetErrorMessage(r));
642 std::unique_ptr<unsigned char[]> autoPtrBuffer(new (std::nothrow) unsigned char[fileLength]);
643 SysTryReturn(NID_GRP, autoPtrBuffer, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to create buffer of the file length");
645 file.Read(autoPtrBuffer.get(), fileLength);
647 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, autoPtrBuffer.get(), fileLength, _DEFAULT_FONT_FACE, &pFont);
649 std::auto_ptr<_IFont> autoPtrFont(pFont);
650 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont instance");
652 // make shared resource
653 sharedFont.reset(autoPtrFont.release());
655 _IFont::Attrib fontAttrib;
656 rtn = pFont->GetAttrib(fontAttrib);
657 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
659 fontAttrib.size = size;
660 fontAttrib.style = 0;
661 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
662 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
664 rtn = pFont->SetAttrib(fontAttrib);
665 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
668 //--------------------------------------------------------------------------
671 // add newly created font resource to rsrc manager
672 //--------------------------------------------------------------------------
673 rtn = __AddFont(systemFontNameTemp, sharedFont);
674 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resource into the shared container");
679 //--------------------------------------------------------------------------
682 // remove unused font
683 //--------------------------------------------------------------------------
684 __RemoveUnusedFont();
690 _FontRsrcManager::GetSystemFont(const Tizen::Base::String& fontName, int style, _Util::FixedPoint26_6 size, SharedFontResource& out)
692 FcPattern *pFontPattern = null;
693 FcFontSet *pFontSet = null;
694 FcResult res = FcResultNoMatch;
695 FcChar8* pPath = null;
696 const char* fcStyle = (style & FONT_STYLE_BOLD) ? "Bold" : "Regular";
699 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontName);
704 std::auto_ptr<ByteBuffer> utfString(pBuffer);
706 //FcBool rtn = FcInitReinitialize();
707 //SysTryCatch(NID_GRP, rtn != FcFalse, , r, "[E_SYSTEM] Failed to init fontconfig");
709 pFontPattern = FcPatternBuild(NULL, FC_STYLE, FcTypeString, (FcChar8*)fcStyle, NULL);
710 SysTryCatch(NID_GRP, pFontPattern, , r, "[E_SYSTEM] Failed to FcPatternBuild()");
712 FcPatternAddString(pFontPattern, FC_FAMILY, (FcChar8*)utfString->GetPointer());
713 FcConfigSubstitute(NULL, pFontPattern, FcMatchPattern);
714 FcDefaultSubstitute(pFontPattern);
716 pFontSet = FcFontSort(NULL, pFontPattern, FcTrue, NULL, &res);
717 SysTryCatch(NID_GRP, pFontSet, , r, "[E_SYSTEM] Failed to FcFontSort()");
719 res = FcPatternGetString(pFontSet->fonts[0], FC_FILE, 0, &pPath);
720 SysTryCatch(NID_GRP, res == FcResultMatch, , r, "[E_SYSTEM] Failed to get path of system font");
722 r = this->GetFont(String((char*)pPath), style, size, out);
723 SysTryCatch(NID_GRP, r == E_SUCCESS, , r, "[%s] Failed to get font", GetErrorMessage(r));
726 FcPatternDestroy(pFontPattern);
727 FcFontSetDestroy(pFontSet);
735 FcFontSetDestroy(pFontSet);
740 FcPatternDestroy(pFontPattern);
749 _FontRsrcManager::__InitFontRsrcManager(void)
751 static _FontRsrcManager instance;
752 __pTheInstance = &instance;
756 _FontRsrcManager::GetInstance(void)
758 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
762 pthread_once(&once_block, __InitFontRsrcManager);
765 return (*__pTheInstance);
769 _FontRsrcManager::__CreateFont(int fontEngine, const void* pBuffer, long bufSize, long face, _IFont** ppOut)
772 SysTryReturn(NID_GRP, ppOut, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
774 std::auto_ptr<_IFont> autoPtrFont;
779 autoPtrFont.reset(new (std::nothrow) _FontFt2);
783 autoPtrFont.reset(new (std::nothrow) _FontNull);
787 SysTryReturn(NID_GRP, autoPtrFont.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
789 bool rtn = autoPtrFont->Create(pBuffer, bufSize, face);
790 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to create font resource");
793 *ppOut = autoPtrFont.release();
799 _FontRsrcManager::__CreateFont(int fontEngine, const char* filePath, const long face, _IFont** ppOut)
802 SysTryReturn(NID_GRP, ppOut, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
804 std::auto_ptr<_IFont> autoPtrFont;
809 autoPtrFont.reset(new (std::nothrow) _FontFt2);
813 autoPtrFont.reset(new (std::nothrow) _FontNull);
817 SysTryReturn(NID_GRP, autoPtrFont.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
819 bool rtn = autoPtrFont->Create(filePath, face);
820 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to create font resource");
823 *ppOut = autoPtrFont.release();
829 _FontRsrcManager::__CreateStaticFont(int fontEngine, const void* pBuffer, long bufSize, long face, _IFont** ppOut)
832 SysTryReturn(NID_GRP, ppOut, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
834 std::auto_ptr<_IFont> autoPtrFont;
839 autoPtrFont.reset(new (std::nothrow) _FontFt2);
843 autoPtrFont.reset(new (std::nothrow) _FontNull);
847 SysTryReturn(NID_GRP, autoPtrFont.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
849 bool rtn = autoPtrFont->CreateStatic(pBuffer, bufSize, face);
850 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to create font resource");
853 *ppOut = autoPtrFont.release();
859 _FontRsrcManager::__SearchFont(const _Util::WString& fontPath, SharedFontResource& out) const
862 SysTryReturn(NID_GRP, out.get() == null, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
864 #ifdef USE_HASHMAP_FOR_FONT
865 return (__fontRsrcMap.GetValue(fontPath, out) == E_SUCCESS);
867 FontMapT::const_iterator fontIterator = __fontRsrcMap.find(fontPath);
869 if (fontIterator != __fontRsrcMap.end())
871 out = fontIterator->second;
880 _FontRsrcManager::__AddFont(const _Util::WString& key, const SharedFontResource& font)
883 SysTryReturn(NID_GRP, font.get() != null, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input param is given");
885 #ifdef USE_HASHMAP_FOR_FONT
886 result r = __fontRsrcMap.Add(key, font);
888 SysTryReturn(NID_GRP, r == E_SUCCESS || r == E_OBJ_ALREADY_EXIST, false, false, "[%s] Failed to add font into resource pool", GetErrorMessage(r));
890 __fontRsrcMap[key] = font;
897 _FontRsrcManager::__RemoveUnusedFont(void)
899 ArrayListT<_Util::WString> keys;
900 String systemFontName(_DEFAULT_SYSTEM_FONT);
901 String systemBoldFontName(_DEFAULT_SYSTEM_BOLD_FONT);
903 _Util::WString systemFontNameTemp(systemFontName.GetPointer());
904 _Util::WString systemBoldFontNameTemp(systemBoldFontName.GetPointer());
906 // find out unused font
908 #ifdef USE_HASHMAP_FOR_FONT
910 std::auto_ptr<IMapEnumeratorT<_Util::WString, SharedFontResource> > enumerator(__fontRsrcMap.GetMapEnumeratorN());
911 result r = GetLastResult();
912 SysTryReturnVoidResult(NID_GRP, enumerator.get() != null, r, "[%s] Propagating.", GetErrorMessage(r));
914 while (enumerator->MoveNext() == E_SUCCESS)
916 SharedFontResource font;
917 r = enumerator->GetValue(font);
919 if (r == E_SUCCESS && font.use_count() == 2)
921 _Util::WString key(L"");
922 r = enumerator->GetKey(key);
924 if (r == E_SUCCESS && (key != systemFontNameTemp && key != systemBoldFontNameTemp))
933 for (FontMapT::iterator fontIterator = __fontRsrcMap.begin(); fontIterator != __fontRsrcMap.end(); ++fontIterator)
935 SharedFontResource font(fontIterator->second);
937 if (font != null && font.use_count() == 2)
939 _Util::WString key(fontIterator->first);
941 if (key != systemFontNameTemp && key != systemBoldFontNameTemp)
951 if (keys.GetCount() > 0)
953 std::auto_ptr<IEnumeratorT<_Util::WString> > enumerator(keys.GetEnumeratorN());
954 result r = GetLastResult();
955 SysTryReturnVoidResult(NID_GRP, enumerator.get() != null, r, "[%s] Propagating.", GetErrorMessage(r));
957 while (enumerator->MoveNext() == E_SUCCESS)
959 _Util::WString key(L"");
960 r = enumerator->GetCurrent(key);
964 #ifdef USE_HASHMAP_FOR_FONT
965 __fontRsrcMap.Remove(key);
967 __fontRsrcMap.erase(key);
977 _FontRsrcManager::__ReloadFont(String fontName, bool isBold)
979 SharedFontResource sharedFont;
980 _Util::WString systemFontName(fontName.GetPointer());
982 _IFont* pFont = null;
985 // Check if there is default system font in font resource manager
986 // If there isn't default system font, we can't reload font resource
987 rtn = __SearchFont(systemFontName, sharedFont);
988 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to search default system font");
990 // Get default system font interface instance
991 pFont = sharedFont.get();
993 // Reload system font resource in font interface
996 isBold ? __defaultSystemBoldFontPath.Clear() : __defaultSystemFontPath.Clear();
997 const String& fontPath = __GetDefaultSystemFontPath(isBold);
999 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontPath);
1000 if (pBuffer == null)
1004 std::auto_ptr<ByteBuffer> pathString(pBuffer);
1006 rtn = pFont->Reload((char*)pathString->GetPointer(), _DEFAULT_FONT_FACE);
1007 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to reload font");
1012 isBold ? __defaultSystemBoldFontPath.Clear() : __defaultSystemFontPath.Clear();
1014 result r = file.Construct(__GetDefaultSystemFontPath(isBold), L"rb");
1015 SysTryReturn(NID_GRP, r == E_SUCCESS, false, r, "[%s] Failed to open file", GetErrorMessage(r));
1017 r = file.Seek(FILESEEKPOSITION_END, 0);
1018 SysTryReturn(NID_GRP, r == E_SUCCESS, false, r, "[%s] Failed to seek file", GetErrorMessage(r));
1020 fileLength = file.Tell();
1021 SysTryReturn(NID_GRP, fileLength > 0, false, r, "[%s] The length of the file is not positive", GetErrorMessage(r));
1023 r = file.Seek(FILESEEKPOSITION_BEGIN, 0);
1024 SysTryReturn(NID_GRP, r == E_SUCCESS, false, r, "[%s] Failed to seek file", GetErrorMessage(r));
1026 std::unique_ptr<unsigned char[]> autoPtrBuffer(new (std::nothrow) unsigned char[fileLength]);
1027 SysTryReturn(NID_GRP, autoPtrBuffer, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to create buffer of the file length");
1029 file.Read(autoPtrBuffer.get(), fileLength);
1030 rtn = pFont->Reload(autoPtrBuffer.get(), fileLength, _DEFAULT_FONT_FACE);
1031 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to reload font");
1034 pFont->CleanCache();
1041 _FontRsrcManager::ReloadDefaultSystemFont(void)
1043 // reinitialize fontconfig library
1044 FcInitReinitialize();
1046 String systemFontName;
1048 systemFontName = _DEFAULT_SYSTEM_FONT;
1049 __ReloadFont(systemFontName, false);
1051 systemFontName = _DEFAULT_SYSTEM_BOLD_FONT;
1052 __ReloadFont(systemFontName, true);
1058 _FontRsrcManager::GetMemoryManager(void)
1060 return *__pFontMemoryManager;
1065 _FontRsrcManager::GetDefaultSystemFont(void)
1067 if (__pDefaultSystemFont == null)
1069 std::auto_ptr<_Font> font(new (std::nothrow) _Font);
1073 result r = font->Construct(FONT_STYLE_PLAIN, _SYSTEM_DEFAULT_FONT_SIZE);
1077 __pDefaultSystemFont = font.release();
1082 SysAssert(__pDefaultSystemFont != null);
1084 return *__pDefaultSystemFont;
1089 _FontRsrcManager::__GetMaxFontEngine(void) const
1095 _FontRsrcManager::__GetFontPath(bool isBold)
1097 String fontPath(L"");
1099 FcFontSet* pFontSet = null;
1100 FcPattern* pFontPattern = null;
1101 const char* fcStyle = isBold ? "Bold" : "Regular";
1102 // initialize fontconfig library
1103 //FcBool rtn = FcInitReinitialize();
1104 //SysTryCatch(NID_GRP, rtn != FcFalse, , E_SYSTEM, "[E_SYSTEM] Failed to init fontconfig");
1107 pFontPattern = FcPatternCreate();
1108 SysTryCatch(NID_GRP, pFontPattern, , E_SYSTEM, "[E_SYSTEM] Failed to create font pattern");
1110 FcPatternAddString(pFontPattern, FC_FAMILY, (FcChar8*)"Tizen");
1111 FcPatternAddString(pFontPattern, FC_STYLE, (FcChar8*)fcStyle);
1112 FcConfigSubstitute(0, pFontPattern, FcMatchPattern);
1113 FcDefaultSubstitute(pFontPattern);
1116 pFontSet = FcFontSetCreate();
1117 SysTryCatch(NID_GRP, pFontSet, , E_SYSTEM, "[E_SYSTEM] Failed to create fontset");
1120 FcPattern* pMatchedPattern = null;
1121 FcResult result = FcResultNoMatch;
1122 pMatchedPattern = FcFontMatch(NULL, pFontPattern, &result);
1124 if (/*result == FcResultMatch && */pMatchedPattern)
1126 FcFontSetAdd(pFontSet, pMatchedPattern);
1129 // check if we have a system font info
1130 SysTryCatch(NID_GRP, pFontSet->nfont >= 1, , E_SYSTEM, "[E_SYSTEM] Default system count is %d", pFontSet->nfont);
1132 FcChar8* pPath = null;
1133 result = FcPatternGetString(pFontSet->fonts[0], FC_FILE, 0, &pPath);
1134 SysTryCatch(NID_GRP, result == FcResultMatch, , E_SYSTEM, "[E_SYSTEM] Failed to create fontset");
1136 fontPath.Append(String((char*)pPath));
1140 FcPatternDestroy(pFontPattern);
1141 FcFontSetDestroy(pFontSet);
1147 if (pFontSet != null)
1149 FcFontSetDestroy(pFontSet);
1152 if (pFontPattern != null)
1154 FcPatternDestroy(pFontPattern);
1161 _FontRsrcManager::__GetDefaultSystemFontPath(bool isBold)
1163 String& defaultPath = (isBold) ? __defaultSystemBoldFontPath : __defaultSystemFontPath;
1165 if (!defaultPath.IsEmpty())
1171 String path = __GetFontPath(isBold);
1177 path = __GetFontPath(false);
1178 // [ykahn] Is 'path' always not NULL ?
1181 __defaultSystemBoldFontPath.Clear();
1182 __defaultSystemBoldFontPath.Append(path);
1184 return __defaultSystemBoldFontPath;
1188 __defaultSystemFontPath.Clear();
1189 __defaultSystemFontPath.Append(path);
1191 return __defaultSystemFontPath;
1197 _FontRsrcManager::RemoveMemory(unsigned long requestedSize)
1199 #ifdef USE_REMOVE_ALL_CACHE
1201 #ifdef USE_HASHMAP_FOR_FONT
1202 result r = E_SUCCESS;
1203 std::auto_ptr<IMapEnumeratorT<_Util::WString, SharedFontResource > > enumerator(__fontRsrcMap.GetMapEnumeratorN());
1205 while (enumerator->MoveNext() == E_SUCCESS)
1207 SharedFontResource fontRsrc;
1208 r = enumerator->GetValue(fontRsrc);
1210 fontRsrc.get()->CleanCache();
1215 for (FontMapT::iterator fontIterator = __fontRsrcMap.begin(); fontIterator != __fontRsrcMap.end(); ++fontIterator)
1217 SharedFontResource fontRsrc(fontIterator->second);
1219 fontRsrc.get()->CleanCache();
1226 unsigned long remainedMemory = 0;
1227 result r = E_SUCCESS;
1228 bool isComplete = false;
1230 ArrayListT<String> keys;
1231 String systemFontName(_DEFAULT_SYSTEM_FONT);
1232 _Util::WString systemFontNameTemp(systemFontName.GetPointer());
1234 std::auto_ptr<IMapEnumeratorT<_Util::WString, SharedFontResource > > enumerator(__fontRsrcMap.GetMapEnumeratorN());
1236 while (enumerator->MoveNext() == E_SUCCESS)
1244 r = enumerator->GetKey(key);
1245 if (r == E_SUCCESS/* && key != systemFontNameTemp*/)
1247 SharedFontResource fontRsrc;
1248 r = enumerator->GetValue(fontRsrc);
1250 while (!fontRsrc.get()->second->IsEmpty())
1252 fontRsrc.get()->second->RemoveFirst();
1254 remainedMemory = __pFontMemoryManager->GetRemainedMemory();
1256 if (remainedMemory > requestedSize)
1270 _FontRsrcManager::FindAppFontName(const Tizen::Base::String& fontName)
1272 String fontPath(L"");
1274 if (__isAppFontListInitialized)
1276 __appFontMap.GetValue(fontName, fontPath);
1284 DirEnumerator* pDirEnum;
1286 //dirName = L"/usr/share/app_font";
1287 dirName = Tizen::App::App::GetInstance()->GetAppResourcePath() + L"font";
1289 // Open the directory
1290 if (IsFailed(dir.Construct(dirName)))
1295 // Reads all the directory entries
1296 pDirEnum = dir.ReadN();
1303 // Loops through all the directory entries
1304 while (pDirEnum->MoveNext() == E_SUCCESS)
1306 SharedFontResource sharedFont;
1310 DirEntry entry = pDirEnum->GetCurrentDirEntry();
1311 Tizen::Base::Utility::StringTokenizer formatTok(entry.GetName(), L".");
1313 while (formatTok.GetTokenCount())
1315 formatTok.GetNextToken(token);
1318 if (entry.IsDirectory() == false)
1320 name.Append(dirName);
1322 name.Append(entry.GetName());
1324 faceName = _Font::GetFaceName(name);
1326 __appFontMap.Add(faceName, name);
1333 __isAppFontListInitialized = true;
1335 __appFontMap.GetValue(fontName, fontPath);
1341 }} // Tizen::Graphics