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 int _SYSTEM_DEFAULT_FONT_SIZE = 16 * 64;
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, bool isRtl, int script) const
115 virtual bool GetFontProperty(Property& property) const
120 virtual Tizen::Base::Collection::HashMapT<Tizen::Base::String, SharedFontResource>* GetFallbackMap(void)
126 virtual bool Create(const void* pBuffer, long bufSize, long face = 0)
131 virtual bool Create(const char* pFilePath, long face = 0)
136 virtual bool CreateStatic(const void* pBuffer, long bufSize, long face = 0)
141 virtual bool Reload(const void* pBuffer, long bufSize, long face = 0)
146 virtual bool Reload(const char* filePath, long face = 0)
151 virtual void Destroy(void) {}
153 virtual bool FindCache(unsigned long character, int size, int style, Glyph** pOut)
158 virtual bool AddCache(unsigned long character, int size, int style, Glyph* pGlyph)
163 virtual bool CleanCache(void)
170 class _FontHashCodeProvider
171 : public IHashCodeProviderT <T>
174 virtual int GetHashCode(const T& obj) const
176 return obj.GetHashCode();
178 }; // _FontHashCodeProvider
182 : public IComparerT <T>
185 virtual result Compare(const T& obj1, const T& obj2, int& cmp) const
187 String& objString1 = static_cast <String&>(const_cast <T&>(obj1));
188 String& objString2 = static_cast <String&>(const_cast <T&>(obj2));
190 cmp = objString1.CompareTo(objString2);
199 namespace Tizen { namespace Graphics
202 _FontRsrcManager* _FontRsrcManager::__pTheInstance = null;
204 _FontRsrcManager::_FontRsrcManager()
205 : __isAppFontListInitialized(false)
207 static _FontHashCodeProvider <String> fontHashCodeProvider;
208 static _FontComparer <String> fontComparer;
210 // are you worry about failing to construct?
211 // but, HashMapT only can give E_SUCCESS/E_INVALID_ARG/E_OUT_OF_MEMORY.
212 // it means, if you have already verified input param, don't need to concern yourself.
213 // even though you have E_OUT_OF_MEMORY, it's already out of our control,
214 // and surely the other member function can give a error message.
215 __fontRsrcMap.Construct(0, 0, fontHashCodeProvider, fontComparer);
216 __appFontMap.Construct(0, 0, fontHashCodeProvider, fontComparer);
218 __defaultSystemFontPath.Clear();
219 __defaultSystemBoldFontPath.Clear();
220 __pDefaultSystemFont = null;
224 const unsigned long cacheMemorySize = 1 * 1024 * 1024;
225 std::unique_ptr<_FontMemoryManager> pMemoryMgr(new (std::nothrow) _FontMemoryManager(_FontMemoryManager::TYPE_SIZE, cacheMemorySize));
227 if (pMemoryMgr != null && pMemoryMgr->IsValid())
229 __pFontMemoryManager = pMemoryMgr.release();
232 SysAssert(__pFontMemoryManager != null);
235 _FontRsrcManager::~_FontRsrcManager()
237 __fontRsrcMap.RemoveAll();
238 __appFontMap.RemoveAll();
240 delete __pDefaultSystemFont;
241 delete __pFontMemoryManager;
247 _FontRsrcManager::GetFont(const Tizen::Base::String& fontPath, int style, int size, SharedFontResource& out)
249 SharedFontResource sharedFont;
250 _IFont* pFont = null;
254 //--------------------------------------------------------------------------
255 // validation check of input param
257 // in case input fontPath is indicating folder,
258 // we should return 'E_UNSUPPORTED_FORMAT' as bads 2.0 did.
259 //--------------------------------------------------------------------------
262 result r = File::GetAttributes(fontPath, attr);
265 return E_FILE_NOT_FOUND;
267 else if (attr.IsDirectory())
269 return E_UNSUPPORTED_FORMAT;
273 //--------------------------------------------------------------------------
276 // if we already have specified resoruce,
277 // return it with increasing reference count
278 //--------------------------------------------------------------------------
279 rtn = __SearchFont(fontPath, sharedFont);
288 //--------------------------------------------------------------------------
292 //--------------------------------------------------------------------------
295 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontPath);
300 std::auto_ptr<ByteBuffer> pathString(pBuffer);
302 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, (char*)pathString->GetPointer(), _DEFAULT_FONT_FACE, &pFont);
308 result r = file.Construct(fontPath, L"rb");
309 SysTryReturnResult(NID_GRP, r == E_SUCCESS, E_SYSTEM, "Failed to open file");
311 r = file.Seek(FILESEEKPOSITION_END, 0);
312 SysTryReturnResult(NID_GRP, r == E_SUCCESS, E_SYSTEM, "Failed to seek file");
314 fileLength = file.Tell();
315 SysTryReturnResult(NID_GRP, fileLength > 0, E_SYSTEM, "The length of the file is not positive");
317 r = file.Seek(FILESEEKPOSITION_BEGIN, 0);
318 SysTryReturnResult(NID_GRP, r == E_SUCCESS, E_SYSTEM, "Failed to seek file");
320 std::unique_ptr<unsigned char[]> buffer(new (std::nothrow) unsigned char[fileLength]);
321 SysTryReturnResult(NID_GRP, buffer, E_OUT_OF_MEMORY, "Failed to create font instance");
323 readLength = file.Read(buffer.get(), fileLength);
324 SysTryReturnResult(NID_GRP, readLength == fileLength, E_SYSTEM, "Failed to read file");
327 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, buffer.get(), fileLength, _DEFAULT_FONT_FACE, &pFont);
329 std::auto_ptr<_IFont> autoPtrFont(pFont);
330 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont instance");
332 // make shared resource
333 sharedFont.reset(autoPtrFont.release());
335 _IFont::Attrib fontAttrib;
336 rtn = pFont->GetAttrib(fontAttrib);
337 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
339 fontAttrib.size = size;
340 fontAttrib.style = 0;
341 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
342 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
344 rtn = pFont->SetAttrib(fontAttrib);
345 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
348 //--------------------------------------------------------------------------
351 // add newly created font resource to rsrc manager
352 //--------------------------------------------------------------------------
353 rtn = __AddFont(fontPath, sharedFont);
354 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resource into the shared container.");
359 //--------------------------------------------------------------------------
362 // remove unused font
363 //--------------------------------------------------------------------------
364 __RemoveUnusedFont();
370 _FontRsrcManager::GetTempFont(const Tizen::Base::String& fontPath, int style, int size, SharedFontResource& out)
372 SharedFontResource sharedFont;
373 _IFont* pFont = null;
374 //_FontCache* pFontCache = null;
376 //unsigned char* pBuffer = null;
378 //--------------------------------------------------------------------------
379 // validation check of input param
381 // in case input fontPath is indicating folder,
382 // we should return 'E_UNSUPPORTED_FORMAT' as bads 2.0 did.
383 //--------------------------------------------------------------------------
386 result r = File::GetAttributes(fontPath, attr);
389 return E_FILE_NOT_FOUND;
391 else if (attr.IsDirectory())
393 return E_UNSUPPORTED_FORMAT;
397 //--------------------------------------------------------------------------
400 // if we already have specified resoruce,
401 // return it with increasing reference count
402 //--------------------------------------------------------------------------
403 rtn = __SearchFont(fontPath, sharedFont);
412 //--------------------------------------------------------------------------
416 //--------------------------------------------------------------------------
418 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontPath);
423 std::auto_ptr<ByteBuffer> pathString(pBuffer);
425 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, (char*)pathString->GetPointer(), _DEFAULT_FONT_FACE, &pFont);
427 std::auto_ptr<_IFont> autoPtrFont(pFont);
428 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont instance");
430 // make shared resource
431 sharedFont.reset(autoPtrFont.release());
433 _IFont::Attrib fontAttrib;
434 rtn = pFont->GetAttrib(fontAttrib);
435 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
437 fontAttrib.size = size;
438 fontAttrib.style = 0;
439 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
440 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
442 rtn = pFont->SetAttrib(fontAttrib);
443 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
453 _FontRsrcManager::AddFont(const Tizen::Base::String& fontPath, SharedFontResource& out)
457 rtn = __AddFont(fontPath, out);
458 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resource into the shared container.");
465 _FontRsrcManager::SearchFont(const Tizen::Base::String& fontName)
467 SharedFontResource sharedFont;
469 return (__SearchFont(fontName, sharedFont) == true);
473 _FontRsrcManager::GetFont(const byte* pFontData, int fontDataSize, int style, int size, SharedFontResource& out)
475 const char* pUserMemoryFont = "UserMemoryFont";
476 SharedFontResource sharedFont;
477 _IFont* pFont = null;
479 //--------------------------------------------------------------------------
482 // create new font with user resource.
483 //--------------------------------------------------------------------------
484 bool rtn = __CreateFont(_DEFAULT_FONT_ENGINE, pFontData, fontDataSize, _DEFAULT_FONT_FACE, &pFont);
485 std::auto_ptr<_IFont> autoPtrFont(pFont);
486 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont");
488 // make shared resource
489 sharedFont.reset(autoPtrFont.release());
491 _IFont::Attrib fontAttrib;
492 rtn = pFont->GetAttrib(fontAttrib);
493 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
495 fontAttrib.size = size;
496 fontAttrib.style = 0;
497 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
498 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
500 rtn = pFont->SetAttrib(fontAttrib);
501 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
503 //--------------------------------------------------------------------------
506 // add newly created font resource to rsrc manager
507 //--------------------------------------------------------------------------
508 static long long idx = 0;
509 String fontName(pUserMemoryFont);
510 fontName.Append(idx++);
511 rtn = __AddFont(fontName, sharedFont);
512 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resrouce into the shared container.");
517 //--------------------------------------------------------------------------
520 // remove unused font
521 //--------------------------------------------------------------------------
522 __RemoveUnusedFont();
528 _FontRsrcManager::GetFont(int style, int size, SharedFontResource& out)
530 SharedFontResource sharedFont;
531 bool isBold = (style & FONT_STYLE_BOLD) ? true : false;
533 String systemFontName = isBold ? _DEFAULT_SYSTEM_BOLD_FONT : _DEFAULT_SYSTEM_FONT;
535 _IFont* pFont = null;
538 //--------------------------------------------------------------------------
541 // if we already have specified resoruce,
542 // return it with increasing reference count
543 //--------------------------------------------------------------------------
544 rtn = __SearchFont(systemFontName, sharedFont);
553 //--------------------------------------------------------------------------
556 // create new font with default font resource.
557 //--------------------------------------------------------------------------
559 isBold ? __defaultSystemBoldFontPath.Clear() : __defaultSystemFontPath.Clear();
561 const String& defaultSystemFontPath = __GetDefaultSystemFontPath(isBold);
562 SysTryReturnResult(NID_GRP, defaultSystemFontPath.IsEmpty() == false, E_SYSTEM, "Failed to get default system font path");
566 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(defaultSystemFontPath);
571 std::auto_ptr<ByteBuffer> pathString(pBuffer);
573 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, (char*)pathString->GetPointer(), _DEFAULT_FONT_FACE, &pFont);
578 result r = file.Construct(defaultSystemFontPath, L"rb");
579 SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to open file", GetErrorMessage(r));
581 r = file.Seek(FILESEEKPOSITION_END, 0);
582 SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to seek file", GetErrorMessage(r));
584 fileLength = file.Tell();
585 SysTryReturn(NID_GRP, fileLength > 0, r, r, "[%s] The length of the file is not positive", GetErrorMessage(r));
587 r = file.Seek(FILESEEKPOSITION_BEGIN, 0);
588 SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to seek file", GetErrorMessage(r));
590 std::unique_ptr<unsigned char[]> autoPtrBuffer(new (std::nothrow) unsigned char[fileLength]);
591 SysTryReturn(NID_GRP, autoPtrBuffer, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to create buffer of the file length");
593 file.Read(autoPtrBuffer.get(), fileLength);
595 rtn = __CreateFont(_DEFAULT_FONT_ENGINE, autoPtrBuffer.get(), fileLength, _DEFAULT_FONT_FACE, &pFont);
597 std::auto_ptr<_IFont> autoPtrFont(pFont);
598 SysTryReturnResult(NID_GRP, rtn && autoPtrFont.get() != null, E_UNSUPPORTED_FORMAT, "Failed to create _IFont instance");
600 // make shared resource
601 sharedFont.reset(autoPtrFont.release());
603 _IFont::Attrib fontAttrib;
604 rtn = pFont->GetAttrib(fontAttrib);
605 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to get font attribute");
607 fontAttrib.size = size;
608 fontAttrib.style = 0;
609 fontAttrib.style |= (style & FONT_STYLE_BOLD) ? _IFont::STYLE_BOLD : 0;
610 fontAttrib.style |= (style & FONT_STYLE_ITALIC) ? _IFont::STYLE_ITALIC : 0;
612 rtn = pFont->SetAttrib(fontAttrib);
613 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to set font attribute");
616 //--------------------------------------------------------------------------
619 // add newly created font resource to rsrc manager
620 //--------------------------------------------------------------------------
621 rtn = __AddFont(systemFontName, sharedFont);
622 SysTryReturnResult(NID_GRP, rtn, E_SYSTEM, "Failed to add font resource into the shared container");
627 //--------------------------------------------------------------------------
630 // remove unused font
631 //--------------------------------------------------------------------------
632 __RemoveUnusedFont();
638 _FontRsrcManager::GetSystemFont(const Tizen::Base::String& fontName, int style, int size, SharedFontResource& out)
640 FcPattern *pFontPattern = null;
641 FcFontSet *pFontSet = null;
642 FcResult res = FcResultNoMatch;
643 FcChar8* pPath = null;
644 const char* fcStyle = (style & FONT_STYLE_BOLD) ? "Bold" : "Regular";
647 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontName);
652 std::auto_ptr<ByteBuffer> utfString(pBuffer);
654 FcBool rtn = FcInitReinitialize();
655 SysTryCatch(NID_GRP, rtn != FcFalse, , r, "[E_SYSTEM] Failed to init fontconfig");
657 pFontPattern = FcPatternBuild(NULL, FC_STYLE, FcTypeString, (FcChar8*)fcStyle, NULL);
658 SysTryCatch(NID_GRP, pFontPattern, , r, "[E_SYSTEM] Failed to FcPatternBuild()");
660 FcPatternAddString(pFontPattern, FC_FAMILY, (FcChar8*)utfString->GetPointer());
661 FcConfigSubstitute(NULL, pFontPattern, FcMatchPattern);
662 FcDefaultSubstitute(pFontPattern);
664 pFontSet = FcFontSort(NULL, pFontPattern, FcTrue, NULL, &res);
665 SysTryCatch(NID_GRP, pFontSet, , r, "[E_SYSTEM] Failed to FcFontSort()");
667 res = FcPatternGetString(pFontSet->fonts[0], FC_FILE, 0, &pPath);
668 SysTryCatch(NID_GRP, res == FcResultMatch, , r, "[E_SYSTEM] Failed to get path of system font");
670 r = this->GetFont(String((char*)pPath), style, size, out);
671 SysTryCatch(NID_GRP, r == E_SUCCESS, , r, "[%s] Failed to get font", GetErrorMessage(r));
674 FcPatternDestroy(pFontPattern);
675 FcFontSetDestroy(pFontSet);
683 FcFontSetDestroy(pFontSet);
688 FcPatternDestroy(pFontPattern);
697 _FontRsrcManager::__InitFontRsrcManager(void)
699 static _FontRsrcManager instance;
700 __pTheInstance = &instance;
704 _FontRsrcManager::GetInstance(void)
706 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
710 pthread_once(&once_block, __InitFontRsrcManager);
713 return (*__pTheInstance);
717 _FontRsrcManager::__CreateFont(int fontEngine, const void* pBuffer, long bufSize, long face, _IFont** ppOut)
720 SysTryReturn(NID_GRP, ppOut, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
722 std::auto_ptr<_IFont> autoPtrFont;
727 autoPtrFont.reset(new (std::nothrow) _FontFt2);
731 autoPtrFont.reset(new (std::nothrow) _FontNull);
735 SysTryReturn(NID_GRP, autoPtrFont.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
737 bool rtn = autoPtrFont->Create(pBuffer, bufSize, face);
738 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to create font resource");
741 *ppOut = autoPtrFont.release();
747 _FontRsrcManager::__CreateFont(int fontEngine, const char* filePath, const long face, _IFont** ppOut)
750 SysTryReturn(NID_GRP, ppOut, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
752 std::auto_ptr<_IFont> autoPtrFont;
757 autoPtrFont.reset(new (std::nothrow) _FontFt2);
761 autoPtrFont.reset(new (std::nothrow) _FontNull);
765 SysTryReturn(NID_GRP, autoPtrFont.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
767 bool rtn = autoPtrFont->Create(filePath, face);
768 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to create font resource");
771 *ppOut = autoPtrFont.release();
777 _FontRsrcManager::__CreateStaticFont(int fontEngine, const void* pBuffer, long bufSize, long face, _IFont** ppOut)
780 SysTryReturn(NID_GRP, ppOut, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
782 std::auto_ptr<_IFont> autoPtrFont;
787 autoPtrFont.reset(new (std::nothrow) _FontFt2);
791 autoPtrFont.reset(new (std::nothrow) _FontNull);
795 SysTryReturn(NID_GRP, autoPtrFont.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
797 bool rtn = autoPtrFont->CreateStatic(pBuffer, bufSize, face);
798 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to create font resource");
801 *ppOut = autoPtrFont.release();
807 _FontRsrcManager::__SearchFont(const String& fontPath, SharedFontResource& out) const
810 SysTryReturn(NID_GRP, out.get() == null, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid out param is given");
812 return (__fontRsrcMap.GetValue(const_cast <String&>(fontPath), out) == E_SUCCESS);
816 _FontRsrcManager::__AddFont(const String& key, const SharedFontResource& font)
819 SysTryReturn(NID_GRP, font.get() != null, false, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input param is given");
821 result r = __fontRsrcMap.Add(key, font);
822 SysTryReturn(NID_GRP, r == E_SUCCESS, false, false, "[%s] Failed to add font into resource pool", GetErrorMessage(r));
828 _FontRsrcManager::__RemoveUnusedFont(void)
830 ArrayListT<String> keys;
831 String systemFontName(_DEFAULT_SYSTEM_FONT);
832 String systemBoldFontName(_DEFAULT_SYSTEM_BOLD_FONT);
834 // find out unused font
836 std::auto_ptr<IMapEnumeratorT<String, SharedFontResource> > enumerator(__fontRsrcMap.GetMapEnumeratorN());
837 result r = GetLastResult();
838 SysTryReturnVoidResult(NID_GRP, enumerator.get() != null, r, "[%s] Propagating.", GetErrorMessage(r));
840 while (enumerator->MoveNext() == E_SUCCESS)
842 SharedFontResource font;
843 r = enumerator->GetValue(font);
845 if (r == E_SUCCESS && font.use_count() == 2)
848 r = enumerator->GetKey(key);
850 if (r == E_SUCCESS && (key != systemFontName && key != systemBoldFontName))
859 if (keys.GetCount() > 0)
861 std::auto_ptr<IEnumeratorT<String> > enumerator(keys.GetEnumeratorN());
862 result r = GetLastResult();
863 SysTryReturnVoidResult(NID_GRP, enumerator.get() != null, r, "[%s] Propagating.", GetErrorMessage(r));
865 while (enumerator->MoveNext() == E_SUCCESS)
868 r = enumerator->GetCurrent(key);
872 __fontRsrcMap.Remove(key);
881 _FontRsrcManager::__ReloadFont(String fontName, bool isBold)
883 SharedFontResource sharedFont;
884 String systemFontName(fontName);
886 _IFont* pFont = null;
889 // Check if there is default system font in font resource manager
890 // If there isn't default system font, we can't reload font resource
891 rtn = __SearchFont(systemFontName, sharedFont);
892 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to search default system font");
894 // Get default system font interface instance
895 pFont = sharedFont.get();
897 // Reload system font resource in font interface
900 isBold ? __defaultSystemBoldFontPath.Clear() : __defaultSystemFontPath.Clear();
901 const String& fontPath = __GetDefaultSystemFontPath(isBold);
903 ByteBuffer* pBuffer = StringUtil::StringToUtf8N(fontPath);
908 std::auto_ptr<ByteBuffer> pathString(pBuffer);
910 rtn = pFont->Reload((char*)pathString->GetPointer(), _DEFAULT_FONT_FACE);
911 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to reload font");
916 isBold ? __defaultSystemBoldFontPath.Clear() : __defaultSystemFontPath.Clear();
918 result r = file.Construct(__GetDefaultSystemFontPath(isBold), L"rb");
919 SysTryReturn(NID_GRP, r == E_SUCCESS, false, r, "[%s] Failed to open file", GetErrorMessage(r));
921 r = file.Seek(FILESEEKPOSITION_END, 0);
922 SysTryReturn(NID_GRP, r == E_SUCCESS, false, r, "[%s] Failed to seek file", GetErrorMessage(r));
924 fileLength = file.Tell();
925 SysTryReturn(NID_GRP, fileLength > 0, false, r, "[%s] The length of the file is not positive", GetErrorMessage(r));
927 r = file.Seek(FILESEEKPOSITION_BEGIN, 0);
928 SysTryReturn(NID_GRP, r == E_SUCCESS, false, r, "[%s] Failed to seek file", GetErrorMessage(r));
930 std::unique_ptr<unsigned char[]> autoPtrBuffer(new (std::nothrow) unsigned char[fileLength]);
931 SysTryReturn(NID_GRP, autoPtrBuffer, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to create buffer of the file length");
933 file.Read(autoPtrBuffer.get(), fileLength);
934 rtn = pFont->Reload(autoPtrBuffer.get(), fileLength, _DEFAULT_FONT_FACE);
935 SysTryReturn(NID_GRP, rtn, false, E_SYSTEM, "[E_SYSTEM] Failed to reload font");
945 _FontRsrcManager::ReloadDefaultSystemFont(void)
947 String systemFontName;
949 systemFontName = _DEFAULT_SYSTEM_FONT;
950 __ReloadFont(systemFontName, false);
952 systemFontName = _DEFAULT_SYSTEM_BOLD_FONT;
953 __ReloadFont(systemFontName, true);
959 _FontRsrcManager::GetMemoryManager(void)
961 return *__pFontMemoryManager;
966 _FontRsrcManager::GetDefaultSystemFont(void)
968 if (__pDefaultSystemFont == null)
970 std::auto_ptr<_Font> font(new (std::nothrow) _Font);
974 result r = font->Construct(FONT_STYLE_PLAIN, _SYSTEM_DEFAULT_FONT_SIZE);
978 __pDefaultSystemFont = font.release();
983 SysAssert(__pDefaultSystemFont != null);
985 return *__pDefaultSystemFont;
990 _FontRsrcManager::__GetMaxFontEngine(void) const
996 _FontRsrcManager::__GetFontPath(bool isBold)
998 String fontPath(L"");
1000 FcFontSet* pFontSet = null;
1001 FcPattern* pFontPattern = null;
1002 const char* fcStyle = isBold ? "Bold" : "Regular";
1003 // initialize fontconfig library
1004 FcBool rtn = FcInitReinitialize();
1005 SysTryCatch(NID_GRP, rtn != FcFalse, , E_SYSTEM, "[E_SYSTEM] Failed to init fontconfig");
1008 pFontPattern = FcPatternCreate();
1009 SysTryCatch(NID_GRP, pFontPattern, , E_SYSTEM, "[E_SYSTEM] Failed to create font pattern");
1011 FcPatternAddString(pFontPattern, FC_FAMILY, (FcChar8*)"Tizen");
1012 FcPatternAddString(pFontPattern, FC_STYLE, (FcChar8*)fcStyle);
1013 FcConfigSubstitute(0, pFontPattern, FcMatchPattern);
1014 FcDefaultSubstitute(pFontPattern);
1017 pFontSet = FcFontSetCreate();
1018 SysTryCatch(NID_GRP, pFontSet, , E_SYSTEM, "[E_SYSTEM] Failed to create fontset");
1021 FcPattern* pMatchedPattern = null;
1022 FcResult result = FcResultNoMatch;
1023 pMatchedPattern = FcFontMatch(NULL, pFontPattern, &result);
1025 if (/*result == FcResultMatch && */pMatchedPattern)
1027 FcFontSetAdd(pFontSet, pMatchedPattern);
1030 // check if we have a system font info
1031 SysTryCatch(NID_GRP, pFontSet->nfont >= 1, , E_SYSTEM, "[E_SYSTEM] Default system count is %d", pFontSet->nfont);
1033 FcChar8* pPath = null;
1034 result = FcPatternGetString(pFontSet->fonts[0], FC_FILE, 0, &pPath);
1035 SysTryCatch(NID_GRP, result == FcResultMatch, , E_SYSTEM, "[E_SYSTEM] Failed to create fontset");
1037 fontPath.Append(String((char*)pPath));
1041 FcPatternDestroy(pFontPattern);
1042 FcFontSetDestroy(pFontSet);
1048 if (pFontSet != null)
1050 FcFontSetDestroy(pFontSet);
1053 if (pFontPattern != null)
1055 FcPatternDestroy(pFontPattern);
1062 _FontRsrcManager::__GetDefaultSystemFontPath(bool isBold)
1064 String& defaultPath = (isBold) ? __defaultSystemBoldFontPath : __defaultSystemFontPath;
1066 if (!defaultPath.IsEmpty())
1072 String path = __GetFontPath(isBold);
1078 path = __GetFontPath(false);
1079 // [ykahn] Is 'path' always not NULL ?
1082 __defaultSystemBoldFontPath.Clear();
1083 __defaultSystemBoldFontPath.Append(path);
1085 return __defaultSystemBoldFontPath;
1089 __defaultSystemFontPath.Clear();
1090 __defaultSystemFontPath.Append(path);
1092 return __defaultSystemFontPath;
1098 _FontRsrcManager::RemoveMemory(unsigned long requestedSize)
1100 #ifdef USE_REMOVE_ALL_CACHE
1101 result r = E_SUCCESS;
1102 std::auto_ptr<IMapEnumeratorT<String, SharedFontResource > > enumerator(__fontRsrcMap.GetMapEnumeratorN());
1104 while (enumerator->MoveNext() == E_SUCCESS)
1106 SharedFontResource fontRsrc;
1107 r = enumerator->GetValue(fontRsrc);
1109 fontRsrc.get()->CleanCache();
1114 unsigned long remainedMemory = 0;
1115 result r = E_SUCCESS;
1116 bool isComplete = false;
1118 ArrayListT<String> keys;
1119 String systemFontName(_DEFAULT_SYSTEM_FONT);
1121 std::auto_ptr<IMapEnumeratorT<String, SharedFontResource > > enumerator(__fontRsrcMap.GetMapEnumeratorN());
1123 while (enumerator->MoveNext() == E_SUCCESS)
1131 r = enumerator->GetKey(key);
1132 if (r == E_SUCCESS/* && key != systemFontName*/)
1134 SharedFontResource fontRsrc;
1135 r = enumerator->GetValue(fontRsrc);
1137 while (!fontRsrc.get()->second->IsEmpty())
1139 fontRsrc.get()->second->RemoveFirst();
1141 remainedMemory = __pFontMemoryManager->GetRemainedMemory();
1143 if (remainedMemory > requestedSize)
1157 _FontRsrcManager::FindAppFontName(const Tizen::Base::String& fontName)
1159 Tizen::Base::String fontPath = L"";
1161 if (__isAppFontListInitialized)
1163 __appFontMap.GetValue(const_cast <String&>(fontName), fontPath);
1170 DirEnumerator* pDirEnum;
1172 //dirName = L"/usr/share/app_font";
1173 dirName = Tizen::App::App::GetInstance()->GetAppResourcePath() + L"font";
1175 // Open the directory
1176 if (IsFailed(dir.Construct(dirName)))
1181 // Reads all the directory entries
1182 pDirEnum = dir.ReadN();
1188 // Loops through all the directory entries
1189 while (pDirEnum->MoveNext() == E_SUCCESS)
1191 SharedFontResource sharedFont;
1195 DirEntry entry = pDirEnum->GetCurrentDirEntry();
1196 Tizen::Base::Utility::StringTokenizer formatTok(entry.GetName(), ".");
1197 while (formatTok.GetTokenCount())
1199 formatTok.GetNextToken(token);
1201 if (entry.IsDirectory() == false)
1203 name.Append(dirName);
1205 name.Append(entry.GetName());
1207 faceName = _Font::GetFaceName(name);
1209 __appFontMap.Add(faceName, name);
1216 __isAppFontListInitialized = true;
1218 __appFontMap.GetValue(const_cast <String&>(fontName), fontPath);
1223 }} // Tizen::Graphics