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 "../fontdata/chromefontdata/chromefontdata.h" | 9 #include "../fontdata/chromefontdata/chromefontdata.h" |
10 #include "core/include/fxge/fx_freetype.h" | 10 #include "core/include/fxge/fx_freetype.h" |
11 #include "core/include/fxge/fx_ge.h" | 11 #include "core/include/fxge/fx_ge.h" |
12 #include "text_int.h" | 12 #include "text_int.h" |
| 13 #include "third_party/base/stl_util.h" |
13 | 14 |
14 #define GET_TT_SHORT(w) (FX_WORD)(((w)[0] << 8) | (w)[1]) | 15 #define GET_TT_SHORT(w) (FX_WORD)(((w)[0] << 8) | (w)[1]) |
15 #define GET_TT_LONG(w) \ | 16 #define GET_TT_LONG(w) \ |
16 (FX_DWORD)(((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3]) | 17 (FX_DWORD)(((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3]) |
17 | 18 |
18 #define FX_FONT_STYLE_None 0x00 | 19 #define FX_FONT_STYLE_None 0x00 |
19 #define FX_FONT_STYLE_Bold 0x01 | 20 #define FX_FONT_STYLE_Bold 0x01 |
20 #define FX_FONT_STYLE_Italic 0x02 | 21 #define FX_FONT_STYLE_Italic 0x02 |
21 #define FX_FONT_STYLE_BoldBold 0x04 | 22 #define FX_FONT_STYLE_BoldBold 0x04 |
22 | 23 |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 } g_Codepage2CharsetTable[] = { | 202 } g_Codepage2CharsetTable[] = { |
202 {1, 0}, {2, 42}, {254, 437}, {255, 850}, {222, 874}, | 203 {1, 0}, {2, 42}, {254, 437}, {255, 850}, {222, 874}, |
203 {128, 932}, {134, 936}, {129, 949}, {136, 950}, {238, 1250}, | 204 {128, 932}, {134, 936}, {129, 949}, {136, 950}, {238, 1250}, |
204 {204, 1251}, {0, 1252}, {161, 1253}, {162, 1254}, {177, 1255}, | 205 {204, 1251}, {0, 1252}, {161, 1253}, {162, 1254}, {177, 1255}, |
205 {178, 1256}, {186, 1257}, {163, 1258}, {130, 1361}, {77, 10000}, | 206 {178, 1256}, {186, 1257}, {163, 1258}, {130, 1361}, {77, 10000}, |
206 {78, 10001}, {79, 10003}, {80, 10008}, {81, 10002}, {83, 10005}, | 207 {78, 10001}, {79, 10003}, {80, 10008}, {81, 10002}, {83, 10005}, |
207 {84, 10004}, {85, 10006}, {86, 10081}, {87, 10021}, {88, 10029}, | 208 {84, 10004}, {85, 10006}, {86, 10081}, {87, 10021}, {88, 10029}, |
208 {89, 10007}, | 209 {89, 10007}, |
209 }; | 210 }; |
210 | 211 |
| 212 const FX_DWORD kTableNAME = FXDWORD_GET_MSBFIRST("name"); |
| 213 const FX_DWORD kTableTTCF = FXDWORD_GET_MSBFIRST("ttcf"); |
| 214 |
211 int CompareFontFamilyString(const void* key, const void* element) { | 215 int CompareFontFamilyString(const void* key, const void* element) { |
212 CFX_ByteString str_key((const FX_CHAR*)key); | 216 CFX_ByteString str_key((const FX_CHAR*)key); |
213 if (str_key.Find(((AltFontFamily*)element)->m_pFontName) != -1) { | 217 if (str_key.Find(((AltFontFamily*)element)->m_pFontName) != -1) { |
214 return 0; | 218 return 0; |
215 } | 219 } |
216 return FXSYS_stricmp((const FX_CHAR*)key, | 220 return FXSYS_stricmp((const FX_CHAR*)key, |
217 ((AltFontFamily*)element)->m_pFontName); | 221 ((AltFontFamily*)element)->m_pFontName); |
218 } | 222 } |
219 | 223 |
220 int CompareString(const void* key, const void* element) { | 224 int CompareString(const void* key, const void* element) { |
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
688 GET_TT_SHORT(ptr + 2) == 0) { | 692 GET_TT_SHORT(ptr + 2) == 0) { |
689 return CFX_ByteStringC(string_ptr + GET_TT_SHORT(ptr + 10), | 693 return CFX_ByteStringC(string_ptr + GET_TT_SHORT(ptr + 10), |
690 GET_TT_SHORT(ptr + 8)); | 694 GET_TT_SHORT(ptr + 8)); |
691 } | 695 } |
692 ptr += 12; | 696 ptr += 12; |
693 } | 697 } |
694 return CFX_ByteString(); | 698 return CFX_ByteString(); |
695 } | 699 } |
696 | 700 |
697 CFX_ByteString CFX_FontMapper::GetPSNameFromTT(void* hFont) { | 701 CFX_ByteString CFX_FontMapper::GetPSNameFromTT(void* hFont) { |
698 if (m_pFontInfo == NULL) { | 702 if (!m_pFontInfo) |
699 return CFX_ByteString(); | 703 return CFX_ByteString(); |
700 } | 704 |
701 CFX_ByteString result; | 705 FX_DWORD size = m_pFontInfo->GetFontData(hFont, kTableNAME, nullptr, 0); |
702 FX_DWORD size = m_pFontInfo->GetFontData(hFont, 0x6e616d65, NULL, 0); | 706 if (!size) |
703 if (size) { | 707 return CFX_ByteString(); |
704 uint8_t* buffer = FX_Alloc(uint8_t, size); | 708 |
705 m_pFontInfo->GetFontData(hFont, 0x6e616d65, buffer, size); | 709 std::vector<uint8_t> buffer(size); |
706 result = GetNameFromTT(buffer, 6); | 710 uint8_t* buffer_ptr = pdfium::vector_as_array(&buffer); |
707 FX_Free(buffer); | 711 FX_DWORD bytes_read = |
708 } | 712 m_pFontInfo->GetFontData(hFont, kTableNAME, buffer_ptr, size); |
709 return result; | 713 return (bytes_read == size) ? GetNameFromTT(buffer_ptr, 6) : CFX_ByteString(); |
710 } | 714 } |
| 715 |
711 void CFX_FontMapper::AddInstalledFont(const CFX_ByteString& name, int charset) { | 716 void CFX_FontMapper::AddInstalledFont(const CFX_ByteString& name, int charset) { |
712 if (m_pFontInfo == NULL) { | 717 if (m_pFontInfo == NULL) { |
713 return; | 718 return; |
714 } | 719 } |
715 if (m_CharsetArray.Find((FX_DWORD)charset) == -1) { | 720 if (m_CharsetArray.Find((FX_DWORD)charset) == -1) { |
716 m_CharsetArray.Add((FX_DWORD)charset); | 721 m_CharsetArray.Add((FX_DWORD)charset); |
717 m_FaceArray.Add(name); | 722 m_FaceArray.Add(name); |
718 } | 723 } |
719 if (name == m_LastFamily) { | 724 if (name == m_LastFamily) { |
720 return; | 725 return; |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1124 } | 1129 } |
1125 int index = m_CharsetArray.Find(Charset); | 1130 int index = m_CharsetArray.Find(Charset); |
1126 if (index < 0) { | 1131 if (index < 0) { |
1127 return UseInternalSubst(pSubstFont, iBaseFont, italic_angle, old_weight, | 1132 return UseInternalSubst(pSubstFont, iBaseFont, italic_angle, old_weight, |
1128 PitchFamily); | 1133 PitchFamily); |
1129 } | 1134 } |
1130 hFont = m_pFontInfo->GetFont(m_FaceArray[index]); | 1135 hFont = m_pFontInfo->GetFont(m_FaceArray[index]); |
1131 } | 1136 } |
1132 } | 1137 } |
1133 pSubstFont->m_ExtHandle = m_pFontInfo->RetainFont(hFont); | 1138 pSubstFont->m_ExtHandle = m_pFontInfo->RetainFont(hFont); |
1134 if (hFont == NULL) { | 1139 if (!hFont) |
1135 return NULL; | 1140 return nullptr; |
1136 } | 1141 |
1137 m_pFontInfo->GetFaceName(hFont, SubstName); | 1142 m_pFontInfo->GetFaceName(hFont, SubstName); |
1138 if (Charset == FXFONT_DEFAULT_CHARSET) { | 1143 if (Charset == FXFONT_DEFAULT_CHARSET) { |
1139 m_pFontInfo->GetFontCharset(hFont, Charset); | 1144 m_pFontInfo->GetFontCharset(hFont, Charset); |
1140 } | 1145 } |
1141 FX_DWORD ttc_size = m_pFontInfo->GetFontData(hFont, 0x74746366, NULL, 0); | 1146 FX_DWORD ttc_size = m_pFontInfo->GetFontData(hFont, kTableTTCF, nullptr, 0); |
1142 FX_DWORD font_size = m_pFontInfo->GetFontData(hFont, 0, NULL, 0); | 1147 FX_DWORD font_size = m_pFontInfo->GetFontData(hFont, 0, nullptr, 0); |
1143 if (font_size == 0 && ttc_size == 0) { | 1148 if (font_size == 0 && ttc_size == 0) { |
1144 m_pFontInfo->DeleteFont(hFont); | 1149 m_pFontInfo->DeleteFont(hFont); |
1145 return NULL; | 1150 return nullptr; |
1146 } | 1151 } |
1147 FXFT_Face face = NULL; | 1152 FXFT_Face face = nullptr; |
1148 if (ttc_size) { | 1153 if (ttc_size) { |
1149 uint8_t temp[1024]; | 1154 uint8_t temp[1024]; |
1150 m_pFontInfo->GetFontData(hFont, 0x74746366, temp, 1024); | 1155 m_pFontInfo->GetFontData(hFont, kTableTTCF, temp, 1024); |
1151 FX_DWORD checksum = 0; | 1156 FX_DWORD checksum = 0; |
1152 for (int i = 0; i < 256; i++) { | 1157 for (int i = 0; i < 256; i++) { |
1153 checksum += ((FX_DWORD*)temp)[i]; | 1158 checksum += ((FX_DWORD*)temp)[i]; |
1154 } | 1159 } |
1155 uint8_t* pFontData; | 1160 uint8_t* pFontData; |
1156 face = m_pFontMgr->GetCachedTTCFace(ttc_size, checksum, | 1161 face = m_pFontMgr->GetCachedTTCFace(ttc_size, checksum, |
1157 ttc_size - font_size, pFontData); | 1162 ttc_size - font_size, pFontData); |
1158 if (face == NULL) { | 1163 if (face == NULL) { |
1159 pFontData = FX_Alloc(uint8_t, ttc_size); | 1164 pFontData = FX_Alloc(uint8_t, ttc_size); |
1160 m_pFontInfo->GetFontData(hFont, 0x74746366, pFontData, ttc_size); | 1165 m_pFontInfo->GetFontData(hFont, kTableTTCF, pFontData, ttc_size); |
1161 face = m_pFontMgr->AddCachedTTCFace(ttc_size, checksum, pFontData, | 1166 face = m_pFontMgr->AddCachedTTCFace(ttc_size, checksum, pFontData, |
1162 ttc_size, ttc_size - font_size); | 1167 ttc_size, ttc_size - font_size); |
1163 } | 1168 } |
1164 } else { | 1169 } else { |
1165 uint8_t* pFontData; | 1170 uint8_t* pFontData; |
1166 face = m_pFontMgr->GetCachedFace(SubstName, weight, bItalic, pFontData); | 1171 face = m_pFontMgr->GetCachedFace(SubstName, weight, bItalic, pFontData); |
1167 if (face == NULL) { | 1172 if (face == NULL) { |
1168 pFontData = FX_Alloc(uint8_t, font_size); | 1173 pFontData = FX_Alloc(uint8_t, font_size); |
1169 m_pFontInfo->GetFontData(hFont, 0, pFontData, font_size); | 1174 m_pFontInfo->GetFontData(hFont, 0, pFontData, font_size); |
1170 face = m_pFontMgr->AddCachedFace(SubstName, weight, bItalic, pFontData, | 1175 face = m_pFontMgr->AddCachedFace(SubstName, weight, bItalic, pFontData, |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1361 FXSYS_fseek(pFile, 0, FXSYS_SEEK_END); | 1366 FXSYS_fseek(pFile, 0, FXSYS_SEEK_END); |
1362 FX_DWORD filesize = FXSYS_ftell(pFile); | 1367 FX_DWORD filesize = FXSYS_ftell(pFile); |
1363 uint8_t buffer[16]; | 1368 uint8_t buffer[16]; |
1364 FXSYS_fseek(pFile, 0, FXSYS_SEEK_SET); | 1369 FXSYS_fseek(pFile, 0, FXSYS_SEEK_SET); |
1365 size_t readCnt = FXSYS_fread(buffer, 12, 1, pFile); | 1370 size_t readCnt = FXSYS_fread(buffer, 12, 1, pFile); |
1366 if (readCnt != 1) { | 1371 if (readCnt != 1) { |
1367 FXSYS_fclose(pFile); | 1372 FXSYS_fclose(pFile); |
1368 return; | 1373 return; |
1369 } | 1374 } |
1370 | 1375 |
1371 if (GET_TT_LONG(buffer) == 0x74746366) { | 1376 if (GET_TT_LONG(buffer) == kTableTTCF) { |
1372 FX_DWORD nFaces = GET_TT_LONG(buffer + 8); | 1377 FX_DWORD nFaces = GET_TT_LONG(buffer + 8); |
1373 if (nFaces > std::numeric_limits<FX_DWORD>::max() / 4) { | 1378 if (nFaces > std::numeric_limits<FX_DWORD>::max() / 4) { |
1374 FXSYS_fclose(pFile); | 1379 FXSYS_fclose(pFile); |
1375 return; | 1380 return; |
1376 } | 1381 } |
1377 FX_DWORD face_bytes = nFaces * 4; | 1382 FX_DWORD face_bytes = nFaces * 4; |
1378 uint8_t* offsets = FX_Alloc(uint8_t, face_bytes); | 1383 uint8_t* offsets = FX_Alloc(uint8_t, face_bytes); |
1379 readCnt = FXSYS_fread(offsets, 1, face_bytes, pFile); | 1384 readCnt = FXSYS_fread(offsets, 1, face_bytes, pFile); |
1380 if (readCnt != face_bytes) { | 1385 if (readCnt != face_bytes) { |
1381 FX_Free(offsets); | 1386 FX_Free(offsets); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1512 void* CFX_FolderFontInfo::MapFontByUnicode(FX_DWORD dwUnicode, | 1517 void* CFX_FolderFontInfo::MapFontByUnicode(FX_DWORD dwUnicode, |
1513 int weight, | 1518 int weight, |
1514 FX_BOOL bItalic, | 1519 FX_BOOL bItalic, |
1515 int pitch_family) { | 1520 int pitch_family) { |
1516 return NULL; | 1521 return NULL; |
1517 } | 1522 } |
1518 void* CFX_FolderFontInfo::GetFont(const FX_CHAR* face) { | 1523 void* CFX_FolderFontInfo::GetFont(const FX_CHAR* face) { |
1519 auto it = m_FontList.find(face); | 1524 auto it = m_FontList.find(face); |
1520 return it != m_FontList.end() ? it->second : nullptr; | 1525 return it != m_FontList.end() ? it->second : nullptr; |
1521 } | 1526 } |
| 1527 |
1522 FX_DWORD CFX_FolderFontInfo::GetFontData(void* hFont, | 1528 FX_DWORD CFX_FolderFontInfo::GetFontData(void* hFont, |
1523 FX_DWORD table, | 1529 FX_DWORD table, |
1524 uint8_t* buffer, | 1530 uint8_t* buffer, |
1525 FX_DWORD size) { | 1531 FX_DWORD size) { |
1526 if (hFont == NULL) { | 1532 if (!hFont) |
1527 return 0; | 1533 return 0; |
1528 } | 1534 |
1529 CFX_FontFaceInfo* pFont = (CFX_FontFaceInfo*)hFont; | 1535 const CFX_FontFaceInfo* pFont = static_cast<CFX_FontFaceInfo*>(hFont); |
1530 FXSYS_FILE* pFile = NULL; | |
1531 if (size > 0) { | |
1532 pFile = FXSYS_fopen(pFont->m_FilePath, "rb"); | |
1533 if (pFile == NULL) { | |
1534 return 0; | |
1535 } | |
1536 } | |
1537 FX_DWORD datasize = 0; | 1536 FX_DWORD datasize = 0; |
1538 FX_DWORD offset = 0; | 1537 FX_DWORD offset = 0; |
1539 if (table == 0) { | 1538 if (table == 0) { |
1540 datasize = pFont->m_FontOffset ? 0 : pFont->m_FileSize; | 1539 datasize = pFont->m_FontOffset ? 0 : pFont->m_FileSize; |
1541 } else if (table == 0x74746366) { | 1540 } else if (table == kTableTTCF) { |
1542 datasize = pFont->m_FontOffset ? pFont->m_FileSize : 0; | 1541 datasize = pFont->m_FontOffset ? pFont->m_FileSize : 0; |
1543 } else { | 1542 } else { |
1544 FX_DWORD nTables = pFont->m_FontTables.GetLength() / 16; | 1543 FX_DWORD nTables = pFont->m_FontTables.GetLength() / 16; |
1545 for (FX_DWORD i = 0; i < nTables; i++) { | 1544 for (FX_DWORD i = 0; i < nTables; i++) { |
1546 const uint8_t* p = (const uint8_t*)pFont->m_FontTables + i * 16; | 1545 const uint8_t* p = |
| 1546 static_cast<const uint8_t*>(pFont->m_FontTables) + i * 16; |
1547 if (GET_TT_LONG(p) == table) { | 1547 if (GET_TT_LONG(p) == table) { |
1548 offset = GET_TT_LONG(p + 8); | 1548 offset = GET_TT_LONG(p + 8); |
1549 datasize = GET_TT_LONG(p + 12); | 1549 datasize = GET_TT_LONG(p + 12); |
1550 } | 1550 } |
1551 } | 1551 } |
1552 } | 1552 } |
1553 if (datasize && size >= datasize && pFile) { | 1553 |
1554 if (FXSYS_fseek(pFile, offset, FXSYS_SEEK_SET) < 0 || | 1554 if (!datasize || size < datasize) |
1555 FXSYS_fread(buffer, datasize, 1, pFile) != 1) { | 1555 return datasize; |
1556 datasize = 0; | 1556 |
1557 } | 1557 FXSYS_FILE* pFile = FXSYS_fopen(pFont->m_FilePath, "rb"); |
| 1558 if (!pFile) |
| 1559 return 0; |
| 1560 |
| 1561 if (FXSYS_fseek(pFile, offset, FXSYS_SEEK_SET) < 0 || |
| 1562 FXSYS_fread(buffer, datasize, 1, pFile) != 1) { |
| 1563 datasize = 0; |
1558 } | 1564 } |
1559 if (pFile) { | 1565 FXSYS_fclose(pFile); |
1560 FXSYS_fclose(pFile); | |
1561 } | |
1562 return datasize; | 1566 return datasize; |
1563 } | 1567 } |
| 1568 |
1564 void CFX_FolderFontInfo::DeleteFont(void* hFont) {} | 1569 void CFX_FolderFontInfo::DeleteFont(void* hFont) {} |
1565 FX_BOOL CFX_FolderFontInfo::GetFaceName(void* hFont, CFX_ByteString& name) { | 1570 FX_BOOL CFX_FolderFontInfo::GetFaceName(void* hFont, CFX_ByteString& name) { |
1566 if (hFont == NULL) { | 1571 if (hFont == NULL) { |
1567 return FALSE; | 1572 return FALSE; |
1568 } | 1573 } |
1569 CFX_FontFaceInfo* pFont = (CFX_FontFaceInfo*)hFont; | 1574 CFX_FontFaceInfo* pFont = (CFX_FontFaceInfo*)hFont; |
1570 name = pFont->m_FaceName; | 1575 name = pFont->m_FaceName; |
1571 return TRUE; | 1576 return TRUE; |
1572 } | 1577 } |
1573 FX_BOOL CFX_FolderFontInfo::GetFontCharset(void* hFont, int& charset) { | 1578 FX_BOOL CFX_FolderFontInfo::GetFontCharset(void* hFont, int& charset) { |
1574 return FALSE; | 1579 return FALSE; |
1575 } | 1580 } |
1576 | 1581 |
1577 int PDF_GetStandardFontName(CFX_ByteString* name) { | 1582 int PDF_GetStandardFontName(CFX_ByteString* name) { |
1578 AltFontName* found = static_cast<AltFontName*>( | 1583 AltFontName* found = static_cast<AltFontName*>( |
1579 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames), | 1584 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames), |
1580 sizeof(AltFontName), CompareString)); | 1585 sizeof(AltFontName), CompareString)); |
1581 if (!found) | 1586 if (!found) |
1582 return -1; | 1587 return -1; |
1583 | 1588 |
1584 *name = g_Base14FontNames[found->m_Index]; | 1589 *name = g_Base14FontNames[found->m_Index]; |
1585 return found->m_Index; | 1590 return found->m_Index; |
1586 } | 1591 } |
OLD | NEW |