| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "../../../include/fxge/fx_ge.h" | 9 #include "../../../include/fxge/fx_ge.h" |
| 10 #include "../../../include/fxge/fx_freetype.h" | 10 #include "../../../include/fxge/fx_freetype.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 return -1; | 73 return -1; |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 m_RefCount--; | 76 m_RefCount--; |
| 77 if (m_RefCount) { | 77 if (m_RefCount) { |
| 78 return m_RefCount; | 78 return m_RefCount; |
| 79 } | 79 } |
| 80 delete this; | 80 delete this; |
| 81 return 0; | 81 return 0; |
| 82 } | 82 } |
| 83 |
| 83 CFX_FontMgr::CFX_FontMgr() : m_FTLibrary(nullptr) { | 84 CFX_FontMgr::CFX_FontMgr() : m_FTLibrary(nullptr) { |
| 84 m_pBuiltinMapper = new CFX_FontMapper(this); | 85 m_pBuiltinMapper.reset(new CFX_FontMapper(this)); |
| 85 FXSYS_memset(m_ExternalFonts, 0, sizeof m_ExternalFonts); | |
| 86 } | 86 } |
| 87 |
| 87 CFX_FontMgr::~CFX_FontMgr() { | 88 CFX_FontMgr::~CFX_FontMgr() { |
| 88 delete m_pBuiltinMapper; | 89 for (const auto& pair : m_FaceMap) |
| 89 FreeCache(); | 90 delete pair.second; |
| 90 if (m_FTLibrary) { | 91 |
| 91 FXFT_Done_FreeType(m_FTLibrary); | 92 // |m_pBuiltinMapper| references |m_FTLibrary|, so it has to be destroyed |
| 92 } | 93 // first. |
| 94 m_pBuiltinMapper.reset(); |
| 95 FXFT_Done_FreeType(m_FTLibrary); |
| 93 } | 96 } |
| 97 |
| 94 void CFX_FontMgr::InitFTLibrary() { | 98 void CFX_FontMgr::InitFTLibrary() { |
| 95 if (m_FTLibrary == NULL) { | 99 if (m_FTLibrary) |
| 96 FXFT_Init_FreeType(&m_FTLibrary); | 100 return; |
| 97 } | 101 FXFT_Init_FreeType(&m_FTLibrary); |
| 98 } | 102 } |
| 99 void CFX_FontMgr::FreeCache() { | 103 |
| 100 for (const auto& pair : m_FaceMap) { | |
| 101 delete pair.second; | |
| 102 } | |
| 103 m_FaceMap.clear(); | |
| 104 } | |
| 105 void CFX_FontMgr::SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo) { | 104 void CFX_FontMgr::SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo) { |
| 106 m_pBuiltinMapper->SetSystemFontInfo(pFontInfo); | 105 m_pBuiltinMapper->SetSystemFontInfo(pFontInfo); |
| 107 } | 106 } |
| 107 |
| 108 FXFT_Face CFX_FontMgr::FindSubstFont(const CFX_ByteString& face_name, | 108 FXFT_Face CFX_FontMgr::FindSubstFont(const CFX_ByteString& face_name, |
| 109 FX_BOOL bTrueType, | 109 FX_BOOL bTrueType, |
| 110 FX_DWORD flags, | 110 FX_DWORD flags, |
| 111 int weight, | 111 int weight, |
| 112 int italic_angle, | 112 int italic_angle, |
| 113 int CharsetCP, | 113 int CharsetCP, |
| 114 CFX_SubstFont* pSubstFont) { | 114 CFX_SubstFont* pSubstFont) { |
| 115 if (!m_FTLibrary) { | 115 InitFTLibrary(); |
| 116 FXFT_Init_FreeType(&m_FTLibrary); | |
| 117 } | |
| 118 return m_pBuiltinMapper->FindSubstFont(face_name, bTrueType, flags, weight, | 116 return m_pBuiltinMapper->FindSubstFont(face_name, bTrueType, flags, weight, |
| 119 italic_angle, CharsetCP, pSubstFont); | 117 italic_angle, CharsetCP, pSubstFont); |
| 120 } | 118 } |
| 119 |
| 121 FXFT_Face CFX_FontMgr::GetCachedFace(const CFX_ByteString& face_name, | 120 FXFT_Face CFX_FontMgr::GetCachedFace(const CFX_ByteString& face_name, |
| 122 int weight, | 121 int weight, |
| 123 FX_BOOL bItalic, | 122 FX_BOOL bItalic, |
| 124 uint8_t*& pFontData) { | 123 uint8_t*& pFontData) { |
| 125 auto it = m_FaceMap.find(KeyNameFromFace(face_name, weight, bItalic)); | 124 auto it = m_FaceMap.find(KeyNameFromFace(face_name, weight, bItalic)); |
| 126 if (it == m_FaceMap.end()) | 125 if (it == m_FaceMap.end()) |
| 127 return nullptr; | 126 return nullptr; |
| 128 | 127 |
| 129 CTTFontDesc* pFontDesc = it->second; | 128 CTTFontDesc* pFontDesc = it->second; |
| 130 pFontData = pFontDesc->m_pFontData; | 129 pFontData = pFontDesc->m_pFontData; |
| 131 pFontDesc->m_RefCount++; | 130 pFontDesc->m_RefCount++; |
| 132 return pFontDesc->m_SingleFace.m_pFace; | 131 return pFontDesc->m_SingleFace.m_pFace; |
| 133 } | 132 } |
| 134 FXFT_Face CFX_FontMgr::AddCachedFace(const CFX_ByteString& face_name, | 133 FXFT_Face CFX_FontMgr::AddCachedFace(const CFX_ByteString& face_name, |
| 135 int weight, | 134 int weight, |
| 136 FX_BOOL bItalic, | 135 FX_BOOL bItalic, |
| 137 uint8_t* pData, | 136 uint8_t* pData, |
| 138 FX_DWORD size, | 137 FX_DWORD size, |
| 139 int face_index) { | 138 int face_index) { |
| 140 CTTFontDesc* pFontDesc = new CTTFontDesc; | 139 CTTFontDesc* pFontDesc = new CTTFontDesc; |
| 141 pFontDesc->m_Type = 1; | 140 pFontDesc->m_Type = 1; |
| 142 pFontDesc->m_SingleFace.m_pFace = NULL; | 141 pFontDesc->m_SingleFace.m_pFace = NULL; |
| 143 pFontDesc->m_SingleFace.m_bBold = weight; | 142 pFontDesc->m_SingleFace.m_bBold = weight; |
| 144 pFontDesc->m_SingleFace.m_bItalic = bItalic; | 143 pFontDesc->m_SingleFace.m_bItalic = bItalic; |
| 145 pFontDesc->m_pFontData = pData; | 144 pFontDesc->m_pFontData = pData; |
| 146 pFontDesc->m_RefCount = 1; | 145 pFontDesc->m_RefCount = 1; |
| 147 FXFT_Library library; | 146 |
| 148 if (m_FTLibrary == NULL) { | 147 InitFTLibrary(); |
| 149 FXFT_Init_FreeType(&m_FTLibrary); | 148 FXFT_Library library = m_FTLibrary; |
| 150 } | |
| 151 library = m_FTLibrary; | |
| 152 int ret = FXFT_New_Memory_Face(library, pData, size, face_index, | 149 int ret = FXFT_New_Memory_Face(library, pData, size, face_index, |
| 153 &pFontDesc->m_SingleFace.m_pFace); | 150 &pFontDesc->m_SingleFace.m_pFace); |
| 154 if (ret) { | 151 if (ret) { |
| 155 delete pFontDesc; | 152 delete pFontDesc; |
| 156 return NULL; | 153 return NULL; |
| 157 } | 154 } |
| 158 ret = FXFT_Set_Pixel_Sizes(pFontDesc->m_SingleFace.m_pFace, 64, 64); | 155 ret = FXFT_Set_Pixel_Sizes(pFontDesc->m_SingleFace.m_pFace, 64, 64); |
| 159 if (ret) { | 156 if (ret) { |
| 160 delete pFontDesc; | 157 delete pFontDesc; |
| 161 return NULL; | 158 return NULL; |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 for (int i = 0; i < 16; i++) { | 325 for (int i = 0; i < 16; i++) { |
| 329 pFontDesc->m_TTCFace.m_pFaces[i] = NULL; | 326 pFontDesc->m_TTCFace.m_pFaces[i] = NULL; |
| 330 } | 327 } |
| 331 pFontDesc->m_RefCount++; | 328 pFontDesc->m_RefCount++; |
| 332 m_FaceMap[KeyNameFromSize(ttc_size, checksum)] = pFontDesc; | 329 m_FaceMap[KeyNameFromSize(ttc_size, checksum)] = pFontDesc; |
| 333 int face_index = GetTTCIndex(pFontDesc->m_pFontData, ttc_size, font_offset); | 330 int face_index = GetTTCIndex(pFontDesc->m_pFontData, ttc_size, font_offset); |
| 334 pFontDesc->m_TTCFace.m_pFaces[face_index] = | 331 pFontDesc->m_TTCFace.m_pFaces[face_index] = |
| 335 GetFixedFace(pFontDesc->m_pFontData, ttc_size, face_index); | 332 GetFixedFace(pFontDesc->m_pFontData, ttc_size, face_index); |
| 336 return pFontDesc->m_TTCFace.m_pFaces[face_index]; | 333 return pFontDesc->m_TTCFace.m_pFaces[face_index]; |
| 337 } | 334 } |
| 335 |
| 338 FXFT_Face CFX_FontMgr::GetFixedFace(const uint8_t* pData, | 336 FXFT_Face CFX_FontMgr::GetFixedFace(const uint8_t* pData, |
| 339 FX_DWORD size, | 337 FX_DWORD size, |
| 340 int face_index) { | 338 int face_index) { |
| 341 FXFT_Library library; | 339 InitFTLibrary(); |
| 342 if (m_FTLibrary == NULL) { | 340 FXFT_Library library = m_FTLibrary; |
| 343 FXFT_Init_FreeType(&m_FTLibrary); | 341 FXFT_Face face = nullptr; |
| 344 } | 342 if (FXFT_New_Memory_Face(library, pData, size, face_index, &face)) |
| 345 library = m_FTLibrary; | 343 return nullptr; |
| 346 FXFT_Face face = NULL; | 344 return FXFT_Set_Pixel_Sizes(face, 64, 64) ? nullptr : face; |
| 347 int ret = FXFT_New_Memory_Face(library, pData, size, face_index, &face); | |
| 348 if (ret) { | |
| 349 return NULL; | |
| 350 } | |
| 351 ret = FXFT_Set_Pixel_Sizes(face, 64, 64); | |
| 352 if (ret) { | |
| 353 return NULL; | |
| 354 } | |
| 355 return face; | |
| 356 } | 345 } |
| 346 |
| 357 FXFT_Face CFX_FontMgr::GetFileFace(const FX_CHAR* filename, int face_index) { | 347 FXFT_Face CFX_FontMgr::GetFileFace(const FX_CHAR* filename, int face_index) { |
| 358 FXFT_Library library; | 348 InitFTLibrary(); |
| 359 if (m_FTLibrary == NULL) { | 349 FXFT_Library library = m_FTLibrary; |
| 360 FXFT_Init_FreeType(&m_FTLibrary); | 350 FXFT_Face face = nullptr; |
| 361 } | 351 if (FXFT_New_Face(library, filename, face_index, &face)) |
| 362 library = m_FTLibrary; | 352 return nullptr; |
| 363 FXFT_Face face = NULL; | 353 return FXFT_Set_Pixel_Sizes(face, 64, 64) ? nullptr : face; |
| 364 int ret = FXFT_New_Face(library, filename, face_index, &face); | |
| 365 if (ret) { | |
| 366 return NULL; | |
| 367 } | |
| 368 ret = FXFT_Set_Pixel_Sizes(face, 64, 64); | |
| 369 if (ret) { | |
| 370 return NULL; | |
| 371 } | |
| 372 return face; | |
| 373 } | 354 } |
| 355 |
| 374 void CFX_FontMgr::ReleaseFace(FXFT_Face face) { | 356 void CFX_FontMgr::ReleaseFace(FXFT_Face face) { |
| 375 if (!face) { | 357 if (!face) { |
| 376 return; | 358 return; |
| 377 } | 359 } |
| 378 FX_BOOL bNeedFaceDone = TRUE; | 360 FX_BOOL bNeedFaceDone = TRUE; |
| 379 auto it = m_FaceMap.begin(); | 361 auto it = m_FaceMap.begin(); |
| 380 while (it != m_FaceMap.end()) { | 362 while (it != m_FaceMap.end()) { |
| 381 auto temp = it++; | 363 auto temp = it++; |
| 382 int nRet = temp->second->ReleaseFace(face); | 364 int nRet = temp->second->ReleaseFace(face); |
| 383 if (nRet == 0) { | 365 if (nRet == 0) { |
| (...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1059 if ((nStyle & 0x3) == 1) { | 1041 if ((nStyle & 0x3) == 1) { |
| 1060 iBaseFont += 1; | 1042 iBaseFont += 1; |
| 1061 } | 1043 } |
| 1062 if ((nStyle & 0x3) == 2) { | 1044 if ((nStyle & 0x3) == 2) { |
| 1063 iBaseFont += 3; | 1045 iBaseFont += 3; |
| 1064 } | 1046 } |
| 1065 if ((nStyle & 0x3) == 3) { | 1047 if ((nStyle & 0x3) == 3) { |
| 1066 iBaseFont += 2; | 1048 iBaseFont += 2; |
| 1067 } | 1049 } |
| 1068 } | 1050 } |
| 1069 if (m_pFontMgr->m_ExternalFonts[iBaseFont].m_pFontData) { | 1051 family = g_Base14FontNames[iBaseFont]; |
| 1070 if (m_FoxitFaces[iBaseFont]) { | |
| 1071 return m_FoxitFaces[iBaseFont]; | |
| 1072 } | |
| 1073 m_FoxitFaces[iBaseFont] = m_pFontMgr->GetFixedFace( | |
| 1074 m_pFontMgr->m_ExternalFonts[iBaseFont].m_pFontData, | |
| 1075 m_pFontMgr->m_ExternalFonts[iBaseFont].m_dwSize, 0); | |
| 1076 if (m_FoxitFaces[iBaseFont]) { | |
| 1077 return m_FoxitFaces[iBaseFont]; | |
| 1078 } | |
| 1079 } else { | |
| 1080 family = g_Base14FontNames[iBaseFont]; | |
| 1081 } | |
| 1082 pSubstFont->m_SubstFlags |= FXFONT_SUBST_STANDARD; | 1052 pSubstFont->m_SubstFlags |= FXFONT_SUBST_STANDARD; |
| 1083 } | 1053 } |
| 1084 } else { | 1054 } else { |
| 1085 if (flags & FXFONT_ITALIC) { | 1055 if (flags & FXFONT_ITALIC) { |
| 1086 bItalic = TRUE; | 1056 bItalic = TRUE; |
| 1087 } | 1057 } |
| 1088 } | 1058 } |
| 1089 iExact = !match.IsEmpty(); | 1059 iExact = !match.IsEmpty(); |
| 1090 void* hFont = m_pFontInfo->MapFont(weight, bItalic, Charset, PitchFamily, | 1060 void* hFont = m_pFontInfo->MapFont(weight, bItalic, Charset, PitchFamily, |
| 1091 family, iExact); | 1061 family, iExact); |
| (...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1663 int PDF_GetStandardFontName(CFX_ByteString* name) { | 1633 int PDF_GetStandardFontName(CFX_ByteString* name) { |
| 1664 AltFontName* found = static_cast<AltFontName*>( | 1634 AltFontName* found = static_cast<AltFontName*>( |
| 1665 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames), | 1635 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames), |
| 1666 sizeof(AltFontName), compareString)); | 1636 sizeof(AltFontName), compareString)); |
| 1667 if (!found) | 1637 if (!found) |
| 1668 return -1; | 1638 return -1; |
| 1669 | 1639 |
| 1670 *name = g_Base14FontNames[found->m_Index]; | 1640 *name = g_Base14FontNames[found->m_Index]; |
| 1671 return found->m_Index; | 1641 return found->m_Index; |
| 1672 } | 1642 } |
| OLD | NEW |