Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(583)

Side by Side Diff: core/src/fxge/ge/fx_ge_fontmap.cpp

Issue 1425023003: Fix / simplify CFX_FolderFontInfo::GetFontData() behavior. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: rebase Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/include/fxcrt/fx_system.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 400 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 } 1126 }
1122 int index = m_CharsetArray.Find(Charset); 1127 int index = m_CharsetArray.Find(Charset);
1123 if (index < 0) { 1128 if (index < 0) {
1124 return UseInternalSubst(pSubstFont, iBaseFont, italic_angle, old_weight, 1129 return UseInternalSubst(pSubstFont, iBaseFont, italic_angle, old_weight,
1125 PitchFamily); 1130 PitchFamily);
1126 } 1131 }
1127 hFont = m_pFontInfo->GetFont(m_FaceArray[index]); 1132 hFont = m_pFontInfo->GetFont(m_FaceArray[index]);
1128 } 1133 }
1129 } 1134 }
1130 pSubstFont->m_ExtHandle = m_pFontInfo->RetainFont(hFont); 1135 pSubstFont->m_ExtHandle = m_pFontInfo->RetainFont(hFont);
1131 if (hFont == NULL) { 1136 if (!hFont)
1132 return NULL; 1137 return nullptr;
1133 } 1138
1134 m_pFontInfo->GetFaceName(hFont, SubstName); 1139 m_pFontInfo->GetFaceName(hFont, SubstName);
1135 if (Charset == FXFONT_DEFAULT_CHARSET) { 1140 if (Charset == FXFONT_DEFAULT_CHARSET) {
1136 m_pFontInfo->GetFontCharset(hFont, Charset); 1141 m_pFontInfo->GetFontCharset(hFont, Charset);
1137 } 1142 }
1138 FX_DWORD ttc_size = m_pFontInfo->GetFontData(hFont, 0x74746366, NULL, 0); 1143 FX_DWORD ttc_size = m_pFontInfo->GetFontData(hFont, kTableTTCF, nullptr, 0);
1139 FX_DWORD font_size = m_pFontInfo->GetFontData(hFont, 0, NULL, 0); 1144 FX_DWORD font_size = m_pFontInfo->GetFontData(hFont, 0, nullptr, 0);
1140 if (font_size == 0 && ttc_size == 0) { 1145 if (font_size == 0 && ttc_size == 0) {
1141 m_pFontInfo->DeleteFont(hFont); 1146 m_pFontInfo->DeleteFont(hFont);
1142 return NULL; 1147 return nullptr;
1143 } 1148 }
1144 FXFT_Face face = NULL; 1149 FXFT_Face face = nullptr;
1145 if (ttc_size) { 1150 if (ttc_size) {
1146 uint8_t temp[1024]; 1151 uint8_t temp[1024];
1147 m_pFontInfo->GetFontData(hFont, 0x74746366, temp, 1024); 1152 m_pFontInfo->GetFontData(hFont, kTableTTCF, temp, 1024);
1148 FX_DWORD checksum = 0; 1153 FX_DWORD checksum = 0;
1149 for (int i = 0; i < 256; i++) { 1154 for (int i = 0; i < 256; i++) {
1150 checksum += ((FX_DWORD*)temp)[i]; 1155 checksum += ((FX_DWORD*)temp)[i];
1151 } 1156 }
1152 uint8_t* pFontData; 1157 uint8_t* pFontData;
1153 face = m_pFontMgr->GetCachedTTCFace(ttc_size, checksum, 1158 face = m_pFontMgr->GetCachedTTCFace(ttc_size, checksum,
1154 ttc_size - font_size, pFontData); 1159 ttc_size - font_size, pFontData);
1155 if (face == NULL) { 1160 if (face == NULL) {
1156 pFontData = FX_Alloc(uint8_t, ttc_size); 1161 pFontData = FX_Alloc(uint8_t, ttc_size);
1157 m_pFontInfo->GetFontData(hFont, 0x74746366, pFontData, ttc_size); 1162 m_pFontInfo->GetFontData(hFont, kTableTTCF, pFontData, ttc_size);
1158 face = m_pFontMgr->AddCachedTTCFace(ttc_size, checksum, pFontData, 1163 face = m_pFontMgr->AddCachedTTCFace(ttc_size, checksum, pFontData,
1159 ttc_size, ttc_size - font_size); 1164 ttc_size, ttc_size - font_size);
1160 } 1165 }
1161 } else { 1166 } else {
1162 uint8_t* pFontData; 1167 uint8_t* pFontData;
1163 face = m_pFontMgr->GetCachedFace(SubstName, weight, bItalic, pFontData); 1168 face = m_pFontMgr->GetCachedFace(SubstName, weight, bItalic, pFontData);
1164 if (face == NULL) { 1169 if (face == NULL) {
1165 pFontData = FX_Alloc(uint8_t, font_size); 1170 pFontData = FX_Alloc(uint8_t, font_size);
1166 m_pFontInfo->GetFontData(hFont, 0, pFontData, font_size); 1171 m_pFontInfo->GetFontData(hFont, 0, pFontData, font_size);
1167 face = m_pFontMgr->AddCachedFace(SubstName, weight, bItalic, pFontData, 1172 face = m_pFontMgr->AddCachedFace(SubstName, weight, bItalic, pFontData,
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1276 FXSYS_fseek(pFile, 0, FXSYS_SEEK_END); 1281 FXSYS_fseek(pFile, 0, FXSYS_SEEK_END);
1277 FX_DWORD filesize = FXSYS_ftell(pFile); 1282 FX_DWORD filesize = FXSYS_ftell(pFile);
1278 uint8_t buffer[16]; 1283 uint8_t buffer[16];
1279 FXSYS_fseek(pFile, 0, FXSYS_SEEK_SET); 1284 FXSYS_fseek(pFile, 0, FXSYS_SEEK_SET);
1280 size_t readCnt = FXSYS_fread(buffer, 12, 1, pFile); 1285 size_t readCnt = FXSYS_fread(buffer, 12, 1, pFile);
1281 if (readCnt != 1) { 1286 if (readCnt != 1) {
1282 FXSYS_fclose(pFile); 1287 FXSYS_fclose(pFile);
1283 return; 1288 return;
1284 } 1289 }
1285 1290
1286 if (GET_TT_LONG(buffer) == 0x74746366) { 1291 if (GET_TT_LONG(buffer) == kTableTTCF) {
1287 FX_DWORD nFaces = GET_TT_LONG(buffer + 8); 1292 FX_DWORD nFaces = GET_TT_LONG(buffer + 8);
1288 if (nFaces > std::numeric_limits<FX_DWORD>::max() / 4) { 1293 if (nFaces > std::numeric_limits<FX_DWORD>::max() / 4) {
1289 FXSYS_fclose(pFile); 1294 FXSYS_fclose(pFile);
1290 return; 1295 return;
1291 } 1296 }
1292 FX_DWORD face_bytes = nFaces * 4; 1297 FX_DWORD face_bytes = nFaces * 4;
1293 uint8_t* offsets = FX_Alloc(uint8_t, face_bytes); 1298 uint8_t* offsets = FX_Alloc(uint8_t, face_bytes);
1294 readCnt = FXSYS_fread(offsets, 1, face_bytes, pFile); 1299 readCnt = FXSYS_fread(offsets, 1, face_bytes, pFile);
1295 if (readCnt != face_bytes) { 1300 if (readCnt != face_bytes) {
1296 FX_Free(offsets); 1301 FX_Free(offsets);
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
1421 int charset, 1426 int charset,
1422 int pitch_family, 1427 int pitch_family,
1423 const FX_CHAR* family, 1428 const FX_CHAR* family,
1424 int& iExact) { 1429 int& iExact) {
1425 return NULL; 1430 return NULL;
1426 } 1431 }
1427 void* CFX_FolderFontInfo::GetFont(const FX_CHAR* face) { 1432 void* CFX_FolderFontInfo::GetFont(const FX_CHAR* face) {
1428 auto it = m_FontList.find(face); 1433 auto it = m_FontList.find(face);
1429 return it != m_FontList.end() ? it->second : nullptr; 1434 return it != m_FontList.end() ? it->second : nullptr;
1430 } 1435 }
1436
1431 FX_DWORD CFX_FolderFontInfo::GetFontData(void* hFont, 1437 FX_DWORD CFX_FolderFontInfo::GetFontData(void* hFont,
1432 FX_DWORD table, 1438 FX_DWORD table,
1433 uint8_t* buffer, 1439 uint8_t* buffer,
1434 FX_DWORD size) { 1440 FX_DWORD size) {
1435 if (hFont == NULL) { 1441 if (!hFont)
1436 return 0; 1442 return 0;
1437 } 1443
1438 CFX_FontFaceInfo* pFont = (CFX_FontFaceInfo*)hFont; 1444 const CFX_FontFaceInfo* pFont = static_cast<CFX_FontFaceInfo*>(hFont);
1439 FXSYS_FILE* pFile = NULL;
1440 if (size > 0) {
1441 pFile = FXSYS_fopen(pFont->m_FilePath, "rb");
1442 if (pFile == NULL) {
1443 return 0;
1444 }
1445 }
1446 FX_DWORD datasize = 0; 1445 FX_DWORD datasize = 0;
1447 FX_DWORD offset = 0; 1446 FX_DWORD offset = 0;
1448 if (table == 0) { 1447 if (table == 0) {
1449 datasize = pFont->m_FontOffset ? 0 : pFont->m_FileSize; 1448 datasize = pFont->m_FontOffset ? 0 : pFont->m_FileSize;
1450 } else if (table == 0x74746366) { 1449 } else if (table == kTableTTCF) {
1451 datasize = pFont->m_FontOffset ? pFont->m_FileSize : 0; 1450 datasize = pFont->m_FontOffset ? pFont->m_FileSize : 0;
1452 } else { 1451 } else {
1453 FX_DWORD nTables = pFont->m_FontTables.GetLength() / 16; 1452 FX_DWORD nTables = pFont->m_FontTables.GetLength() / 16;
1454 for (FX_DWORD i = 0; i < nTables; i++) { 1453 for (FX_DWORD i = 0; i < nTables; i++) {
1455 const uint8_t* p = (const uint8_t*)pFont->m_FontTables + i * 16; 1454 const uint8_t* p =
1455 static_cast<const uint8_t*>(pFont->m_FontTables) + i * 16;
1456 if (GET_TT_LONG(p) == table) { 1456 if (GET_TT_LONG(p) == table) {
1457 offset = GET_TT_LONG(p + 8); 1457 offset = GET_TT_LONG(p + 8);
1458 datasize = GET_TT_LONG(p + 12); 1458 datasize = GET_TT_LONG(p + 12);
1459 } 1459 }
1460 } 1460 }
1461 } 1461 }
1462 if (datasize && size >= datasize && pFile) { 1462
1463 if (FXSYS_fseek(pFile, offset, FXSYS_SEEK_SET) < 0 || 1463 if (!datasize || size < datasize)
1464 FXSYS_fread(buffer, datasize, 1, pFile) != 1) { 1464 return datasize;
1465 datasize = 0; 1465
1466 } 1466 FXSYS_FILE* pFile = FXSYS_fopen(pFont->m_FilePath, "rb");
1467 if (!pFile)
1468 return 0;
1469
1470 if (FXSYS_fseek(pFile, offset, FXSYS_SEEK_SET) < 0 ||
1471 FXSYS_fread(buffer, datasize, 1, pFile) != 1) {
1472 datasize = 0;
1467 } 1473 }
1468 if (pFile) { 1474 FXSYS_fclose(pFile);
1469 FXSYS_fclose(pFile);
1470 }
1471 return datasize; 1475 return datasize;
1472 } 1476 }
1477
1473 void CFX_FolderFontInfo::DeleteFont(void* hFont) {} 1478 void CFX_FolderFontInfo::DeleteFont(void* hFont) {}
1474 FX_BOOL CFX_FolderFontInfo::GetFaceName(void* hFont, CFX_ByteString& name) { 1479 FX_BOOL CFX_FolderFontInfo::GetFaceName(void* hFont, CFX_ByteString& name) {
1475 if (hFont == NULL) { 1480 if (hFont == NULL) {
1476 return FALSE; 1481 return FALSE;
1477 } 1482 }
1478 CFX_FontFaceInfo* pFont = (CFX_FontFaceInfo*)hFont; 1483 CFX_FontFaceInfo* pFont = (CFX_FontFaceInfo*)hFont;
1479 name = pFont->m_FaceName; 1484 name = pFont->m_FaceName;
1480 return TRUE; 1485 return TRUE;
1481 } 1486 }
1482 FX_BOOL CFX_FolderFontInfo::GetFontCharset(void* hFont, int& charset) { 1487 FX_BOOL CFX_FolderFontInfo::GetFontCharset(void* hFont, int& charset) {
1483 return FALSE; 1488 return FALSE;
1484 } 1489 }
1485 1490
1486 int PDF_GetStandardFontName(CFX_ByteString* name) { 1491 int PDF_GetStandardFontName(CFX_ByteString* name) {
1487 AltFontName* found = static_cast<AltFontName*>( 1492 AltFontName* found = static_cast<AltFontName*>(
1488 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames), 1493 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames),
1489 sizeof(AltFontName), CompareString)); 1494 sizeof(AltFontName), CompareString));
1490 if (!found) 1495 if (!found)
1491 return -1; 1496 return -1;
1492 1497
1493 *name = g_Base14FontNames[found->m_Index]; 1498 *name = g_Base14FontNames[found->m_Index];
1494 return found->m_Index; 1499 return found->m_Index;
1495 } 1500 }
OLDNEW
« no previous file with comments | « core/include/fxcrt/fx_system.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698