OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 |
| 7 #include "core/fxge/include/cfx_fontmgr.h" |
| 8 |
| 9 #include "core/fxge/fontdata/chromefontdata/chromefontdata.h" |
| 10 #include "core/fxge/include/cfx_fontmapper.h" |
| 11 #include "core/fxge/include/ifx_systemfontinfo.h" |
| 12 #include "core/fxge/include/fx_font.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 struct BuiltinFont { |
| 17 const uint8_t* m_pFontData; |
| 18 uint32_t m_dwSize; |
| 19 }; |
| 20 |
| 21 const BuiltinFont g_FoxitFonts[14] = { |
| 22 {g_FoxitFixedFontData, 17597}, |
| 23 {g_FoxitFixedBoldFontData, 18055}, |
| 24 {g_FoxitFixedBoldItalicFontData, 19151}, |
| 25 {g_FoxitFixedItalicFontData, 18746}, |
| 26 {g_FoxitSansFontData, 15025}, |
| 27 {g_FoxitSansBoldFontData, 16344}, |
| 28 {g_FoxitSansBoldItalicFontData, 16418}, |
| 29 {g_FoxitSansItalicFontData, 16339}, |
| 30 {g_FoxitSerifFontData, 19469}, |
| 31 {g_FoxitSerifBoldFontData, 19395}, |
| 32 {g_FoxitSerifBoldItalicFontData, 20733}, |
| 33 {g_FoxitSerifItalicFontData, 21227}, |
| 34 {g_FoxitSymbolFontData, 16729}, |
| 35 {g_FoxitDingbatsFontData, 29513}, |
| 36 }; |
| 37 |
| 38 const BuiltinFont g_MMFonts[2] = { |
| 39 {g_FoxitSerifMMFontData, 113417}, |
| 40 {g_FoxitSansMMFontData, 66919}, |
| 41 }; |
| 42 |
| 43 CFX_ByteString KeyNameFromFace(const CFX_ByteString& face_name, |
| 44 int weight, |
| 45 FX_BOOL bItalic) { |
| 46 CFX_ByteString key(face_name); |
| 47 key += ','; |
| 48 key += CFX_ByteString::FormatInteger(weight); |
| 49 key += bItalic ? 'I' : 'N'; |
| 50 return key; |
| 51 } |
| 52 |
| 53 CFX_ByteString KeyNameFromSize(int ttc_size, uint32_t checksum) { |
| 54 CFX_ByteString key; |
| 55 key.Format("%d:%d", ttc_size, checksum); |
| 56 return key; |
| 57 } |
| 58 |
| 59 int GetTTCIndex(const uint8_t* pFontData, |
| 60 uint32_t ttc_size, |
| 61 uint32_t font_offset) { |
| 62 int face_index = 0; |
| 63 const uint8_t* p = pFontData + 8; |
| 64 uint32_t nfont = GET_TT_LONG(p); |
| 65 uint32_t index; |
| 66 for (index = 0; index < nfont; index++) { |
| 67 p = pFontData + 12 + index * 4; |
| 68 if (GET_TT_LONG(p) == font_offset) |
| 69 break; |
| 70 } |
| 71 if (index >= nfont) |
| 72 face_index = 0; |
| 73 else |
| 74 face_index = index; |
| 75 return face_index; |
| 76 } |
| 77 |
| 78 } // namespace |
| 79 |
| 80 CFX_FontMgr::CFX_FontMgr() |
| 81 : m_FTLibrary(nullptr), m_FTLibrarySupportsHinting(false) { |
| 82 m_pBuiltinMapper.reset(new CFX_FontMapper(this)); |
| 83 } |
| 84 |
| 85 CFX_FontMgr::~CFX_FontMgr() { |
| 86 for (const auto& pair : m_FaceMap) |
| 87 delete pair.second; |
| 88 |
| 89 // |m_pBuiltinMapper| references |m_FTLibrary|, so it has to be destroyed |
| 90 // first. |
| 91 m_pBuiltinMapper.reset(); |
| 92 FXFT_Done_FreeType(m_FTLibrary); |
| 93 } |
| 94 |
| 95 void CFX_FontMgr::InitFTLibrary() { |
| 96 if (m_FTLibrary) |
| 97 return; |
| 98 FXFT_Init_FreeType(&m_FTLibrary); |
| 99 m_FTLibrarySupportsHinting = |
| 100 FXFT_Library_SetLcdFilter(m_FTLibrary, FT_LCD_FILTER_DEFAULT) != |
| 101 FT_Err_Unimplemented_Feature; |
| 102 } |
| 103 |
| 104 void CFX_FontMgr::SetSystemFontInfo( |
| 105 std::unique_ptr<IFX_SystemFontInfo> pFontInfo) { |
| 106 m_pBuiltinMapper->SetSystemFontInfo(std::move(pFontInfo)); |
| 107 } |
| 108 |
| 109 FXFT_Face CFX_FontMgr::FindSubstFont(const CFX_ByteString& face_name, |
| 110 FX_BOOL bTrueType, |
| 111 uint32_t flags, |
| 112 int weight, |
| 113 int italic_angle, |
| 114 int CharsetCP, |
| 115 CFX_SubstFont* pSubstFont) { |
| 116 InitFTLibrary(); |
| 117 return m_pBuiltinMapper->FindSubstFont(face_name, bTrueType, flags, weight, |
| 118 italic_angle, CharsetCP, pSubstFont); |
| 119 } |
| 120 |
| 121 FXFT_Face CFX_FontMgr::GetCachedFace(const CFX_ByteString& face_name, |
| 122 int weight, |
| 123 FX_BOOL bItalic, |
| 124 uint8_t*& pFontData) { |
| 125 auto it = m_FaceMap.find(KeyNameFromFace(face_name, weight, bItalic)); |
| 126 if (it == m_FaceMap.end()) |
| 127 return nullptr; |
| 128 |
| 129 CTTFontDesc* pFontDesc = it->second; |
| 130 pFontData = pFontDesc->m_pFontData; |
| 131 pFontDesc->m_RefCount++; |
| 132 return pFontDesc->m_SingleFace.m_pFace; |
| 133 } |
| 134 |
| 135 FXFT_Face CFX_FontMgr::AddCachedFace(const CFX_ByteString& face_name, |
| 136 int weight, |
| 137 FX_BOOL bItalic, |
| 138 uint8_t* pData, |
| 139 uint32_t size, |
| 140 int face_index) { |
| 141 CTTFontDesc* pFontDesc = new CTTFontDesc; |
| 142 pFontDesc->m_Type = 1; |
| 143 pFontDesc->m_SingleFace.m_pFace = nullptr; |
| 144 pFontDesc->m_SingleFace.m_bBold = weight; |
| 145 pFontDesc->m_SingleFace.m_bItalic = bItalic; |
| 146 pFontDesc->m_pFontData = pData; |
| 147 pFontDesc->m_RefCount = 1; |
| 148 |
| 149 InitFTLibrary(); |
| 150 FXFT_Library library = m_FTLibrary; |
| 151 int ret = FXFT_New_Memory_Face(library, pData, size, face_index, |
| 152 &pFontDesc->m_SingleFace.m_pFace); |
| 153 if (ret) { |
| 154 delete pFontDesc; |
| 155 return nullptr; |
| 156 } |
| 157 ret = FXFT_Set_Pixel_Sizes(pFontDesc->m_SingleFace.m_pFace, 64, 64); |
| 158 if (ret) { |
| 159 delete pFontDesc; |
| 160 return nullptr; |
| 161 } |
| 162 m_FaceMap[KeyNameFromFace(face_name, weight, bItalic)] = pFontDesc; |
| 163 return pFontDesc->m_SingleFace.m_pFace; |
| 164 } |
| 165 |
| 166 FXFT_Face CFX_FontMgr::GetCachedTTCFace(int ttc_size, |
| 167 uint32_t checksum, |
| 168 int font_offset, |
| 169 uint8_t*& pFontData) { |
| 170 auto it = m_FaceMap.find(KeyNameFromSize(ttc_size, checksum)); |
| 171 if (it == m_FaceMap.end()) |
| 172 return nullptr; |
| 173 |
| 174 CTTFontDesc* pFontDesc = it->second; |
| 175 pFontData = pFontDesc->m_pFontData; |
| 176 pFontDesc->m_RefCount++; |
| 177 int face_index = GetTTCIndex(pFontDesc->m_pFontData, ttc_size, font_offset); |
| 178 if (!pFontDesc->m_TTCFace.m_pFaces[face_index]) { |
| 179 pFontDesc->m_TTCFace.m_pFaces[face_index] = |
| 180 GetFixedFace(pFontDesc->m_pFontData, ttc_size, face_index); |
| 181 } |
| 182 return pFontDesc->m_TTCFace.m_pFaces[face_index]; |
| 183 } |
| 184 |
| 185 FXFT_Face CFX_FontMgr::AddCachedTTCFace(int ttc_size, |
| 186 uint32_t checksum, |
| 187 uint8_t* pData, |
| 188 uint32_t size, |
| 189 int font_offset) { |
| 190 CTTFontDesc* pFontDesc = new CTTFontDesc; |
| 191 pFontDesc->m_Type = 2; |
| 192 pFontDesc->m_pFontData = pData; |
| 193 for (int i = 0; i < 16; i++) |
| 194 pFontDesc->m_TTCFace.m_pFaces[i] = nullptr; |
| 195 pFontDesc->m_RefCount++; |
| 196 m_FaceMap[KeyNameFromSize(ttc_size, checksum)] = pFontDesc; |
| 197 int face_index = GetTTCIndex(pFontDesc->m_pFontData, ttc_size, font_offset); |
| 198 pFontDesc->m_TTCFace.m_pFaces[face_index] = |
| 199 GetFixedFace(pFontDesc->m_pFontData, ttc_size, face_index); |
| 200 return pFontDesc->m_TTCFace.m_pFaces[face_index]; |
| 201 } |
| 202 |
| 203 FXFT_Face CFX_FontMgr::GetFixedFace(const uint8_t* pData, |
| 204 uint32_t size, |
| 205 int face_index) { |
| 206 InitFTLibrary(); |
| 207 FXFT_Library library = m_FTLibrary; |
| 208 FXFT_Face face = nullptr; |
| 209 if (FXFT_New_Memory_Face(library, pData, size, face_index, &face)) |
| 210 return nullptr; |
| 211 return FXFT_Set_Pixel_Sizes(face, 64, 64) ? nullptr : face; |
| 212 } |
| 213 |
| 214 FXFT_Face CFX_FontMgr::GetFileFace(const FX_CHAR* filename, int face_index) { |
| 215 InitFTLibrary(); |
| 216 FXFT_Library library = m_FTLibrary; |
| 217 FXFT_Face face = nullptr; |
| 218 if (FXFT_New_Face(library, filename, face_index, &face)) |
| 219 return nullptr; |
| 220 return FXFT_Set_Pixel_Sizes(face, 64, 64) ? nullptr : face; |
| 221 } |
| 222 |
| 223 void CFX_FontMgr::ReleaseFace(FXFT_Face face) { |
| 224 if (!face) |
| 225 return; |
| 226 FX_BOOL bNeedFaceDone = TRUE; |
| 227 auto it = m_FaceMap.begin(); |
| 228 while (it != m_FaceMap.end()) { |
| 229 auto temp = it++; |
| 230 int nRet = temp->second->ReleaseFace(face); |
| 231 if (nRet == -1) |
| 232 continue; |
| 233 bNeedFaceDone = FALSE; |
| 234 if (nRet == 0) |
| 235 m_FaceMap.erase(temp); |
| 236 break; |
| 237 } |
| 238 if (bNeedFaceDone && !m_pBuiltinMapper->IsBuiltinFace(face)) |
| 239 FXFT_Done_Face(face); |
| 240 } |
| 241 |
| 242 bool CFX_FontMgr::GetBuiltinFont(size_t index, |
| 243 const uint8_t** pFontData, |
| 244 uint32_t* size) { |
| 245 if (index < FX_ArraySize(g_FoxitFonts)) { |
| 246 *pFontData = g_FoxitFonts[index].m_pFontData; |
| 247 *size = g_FoxitFonts[index].m_dwSize; |
| 248 return true; |
| 249 } |
| 250 index -= FX_ArraySize(g_FoxitFonts); |
| 251 if (index < FX_ArraySize(g_MMFonts)) { |
| 252 *pFontData = g_MMFonts[index].m_pFontData; |
| 253 *size = g_MMFonts[index].m_dwSize; |
| 254 return true; |
| 255 } |
| 256 return false; |
| 257 } |
OLD | NEW |