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

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: cleanup 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 | « no previous file | 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 "../../../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"
11 #include "../fontdata/chromefontdata/chromefontdata.h" 11 #include "../fontdata/chromefontdata/chromefontdata.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 namespace { 19 namespace {
19 20
20 struct BuiltinFont { 21 struct BuiltinFont {
21 const uint8_t* m_pFontData; 22 const uint8_t* m_pFontData;
22 FX_DWORD m_dwSize; 23 FX_DWORD m_dwSize;
(...skipping 14 matching lines...) Expand all
37 {g_FoxitSerifItalicFontData, 21227}, 38 {g_FoxitSerifItalicFontData, 21227},
38 {g_FoxitSymbolFontData, 16729}, 39 {g_FoxitSymbolFontData, 16729},
39 {g_FoxitDingbatsFontData, 29513}, 40 {g_FoxitDingbatsFontData, 29513},
40 }; 41 };
41 42
42 const BuiltinFont g_MMFonts[2] = { 43 const BuiltinFont g_MMFonts[2] = {
43 {g_FoxitSerifMMFontData, 113417}, 44 {g_FoxitSerifMMFontData, 113417},
44 {g_FoxitSansMMFontData, 66919}, 45 {g_FoxitSansMMFontData, 66919},
45 }; 46 };
46 47
48 const FX_DWORD kTableNAME = 0x6e616d65;
Tom Sepez 2015/11/09 18:27:51 nit: again, want to use a macro to make a big-endi
Lei Zhang 2015/11/09 19:20:00 Done. I locally added a static_assert(kFoo, kFooM
49 const FX_DWORD kTableTTCF = 0x74746366;
Tom Sepez 2015/11/09 18:27:51 nit: and FXDWORD_GET_MSBFIRST("ttcf") but doublech
Lei Zhang 2015/11/09 19:20:00 Done.
50
47 CFX_ByteString KeyNameFromFace(const CFX_ByteString& face_name, 51 CFX_ByteString KeyNameFromFace(const CFX_ByteString& face_name,
48 int weight, 52 int weight,
49 FX_BOOL bItalic) { 53 FX_BOOL bItalic) {
50 CFX_ByteString key(face_name); 54 CFX_ByteString key(face_name);
51 key += ','; 55 key += ',';
52 key += CFX_ByteString::FormatInteger(weight); 56 key += CFX_ByteString::FormatInteger(weight);
53 key += bItalic ? 'I' : 'N'; 57 key += bItalic ? 'I' : 'N';
54 return key; 58 return key;
55 } 59 }
56 60
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 CFX_ByteString buffer; 511 CFX_ByteString buffer;
508 if (!pFile->ReadBlock(buffer.GetBuffer(size), offset, size)) { 512 if (!pFile->ReadBlock(buffer.GetBuffer(size), offset, size)) {
509 return CFX_ByteString(); 513 return CFX_ByteString();
510 } 514 }
511 buffer.ReleaseBuffer(size); 515 buffer.ReleaseBuffer(size);
512 return buffer; 516 return buffer;
513 } 517 }
514 } 518 }
515 return CFX_ByteString(); 519 return CFX_ByteString();
516 } 520 }
521
517 CFX_ByteString CFX_FontMapper::GetPSNameFromTT(void* hFont) { 522 CFX_ByteString CFX_FontMapper::GetPSNameFromTT(void* hFont) {
518 if (m_pFontInfo == NULL) {
519 return CFX_ByteString();
520 }
521 CFX_ByteString result; 523 CFX_ByteString result;
Tom Sepez 2015/11/09 18:27:51 nit: no need for "result" local, just return CFX_B
Lei Zhang 2015/11/09 19:20:00 Done. Some say, it makes it easier for compilers t
522 FX_DWORD size = m_pFontInfo->GetFontData(hFont, 0x6e616d65, NULL, 0); 524 if (!m_pFontInfo)
523 if (size) { 525 return result;
524 uint8_t* buffer = FX_Alloc(uint8_t, size); 526
525 m_pFontInfo->GetFontData(hFont, 0x6e616d65, buffer, size); 527 FX_DWORD size = m_pFontInfo->GetFontData(hFont, kTableNAME, nullptr, 0);
526 result = GetNameFromTT(buffer, 6); 528 if (!size)
527 FX_Free(buffer); 529 return result;
528 } 530
531 std::vector<uint8_t> buffer(size);
532 uint8_t* buffer_ptr = pdfium::vector_as_array(&buffer);
533 FX_DWORD bytes_read =
534 m_pFontInfo->GetFontData(hFont, kTableNAME, buffer_ptr, size);
535 if (bytes_read == size)
536 result = GetNameFromTT(buffer_ptr, 6);
Tom Sepez 2015/11/09 18:27:51 nit: prefer early return on error.
Lei Zhang 2015/11/09 19:20:00 Acknowledged.
529 return result; 537 return result;
530 } 538 }
539
531 void CFX_FontMapper::AddInstalledFont(const CFX_ByteString& name, int charset) { 540 void CFX_FontMapper::AddInstalledFont(const CFX_ByteString& name, int charset) {
532 if (m_pFontInfo == NULL) { 541 if (m_pFontInfo == NULL) {
533 return; 542 return;
534 } 543 }
535 if (m_CharsetArray.Find((FX_DWORD)charset) == -1) { 544 if (m_CharsetArray.Find((FX_DWORD)charset) == -1) {
536 m_CharsetArray.Add((FX_DWORD)charset); 545 m_CharsetArray.Add((FX_DWORD)charset);
537 m_FaceArray.Add(name); 546 m_FaceArray.Add(name);
538 } 547 }
539 if (name == m_LastFamily) { 548 if (name == m_LastFamily) {
540 return; 549 return;
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 } 1114 }
1106 int index = m_CharsetArray.Find(Charset); 1115 int index = m_CharsetArray.Find(Charset);
1107 if (index < 0) { 1116 if (index < 0) {
1108 return UseInternalSubst(pSubstFont, iBaseFont, italic_angle, old_weight, 1117 return UseInternalSubst(pSubstFont, iBaseFont, italic_angle, old_weight,
1109 PitchFamily); 1118 PitchFamily);
1110 } 1119 }
1111 hFont = m_pFontInfo->GetFont(m_FaceArray[index]); 1120 hFont = m_pFontInfo->GetFont(m_FaceArray[index]);
1112 } 1121 }
1113 } 1122 }
1114 pSubstFont->m_ExtHandle = m_pFontInfo->RetainFont(hFont); 1123 pSubstFont->m_ExtHandle = m_pFontInfo->RetainFont(hFont);
1115 if (hFont == NULL) { 1124 if (!hFont)
1116 return NULL; 1125 return nullptr;
1117 } 1126
1118 m_pFontInfo->GetFaceName(hFont, SubstName); 1127 m_pFontInfo->GetFaceName(hFont, SubstName);
1119 if (Charset == FXFONT_DEFAULT_CHARSET) { 1128 if (Charset == FXFONT_DEFAULT_CHARSET) {
1120 m_pFontInfo->GetFontCharset(hFont, Charset); 1129 m_pFontInfo->GetFontCharset(hFont, Charset);
1121 } 1130 }
1122 FX_DWORD ttc_size = m_pFontInfo->GetFontData(hFont, 0x74746366, NULL, 0); 1131 FX_DWORD ttc_size = m_pFontInfo->GetFontData(hFont, kTableTTCF, nullptr, 0);
1123 FX_DWORD font_size = m_pFontInfo->GetFontData(hFont, 0, NULL, 0); 1132 FX_DWORD font_size = m_pFontInfo->GetFontData(hFont, 0, nullptr, 0);
1124 if (font_size == 0 && ttc_size == 0) { 1133 if (font_size == 0 && ttc_size == 0) {
1125 m_pFontInfo->DeleteFont(hFont); 1134 m_pFontInfo->DeleteFont(hFont);
1126 return NULL; 1135 return nullptr;
1127 } 1136 }
1128 FXFT_Face face = NULL; 1137 FXFT_Face face = nullptr;
1129 if (ttc_size) { 1138 if (ttc_size) {
1130 uint8_t temp[1024]; 1139 uint8_t temp[1024];
1131 m_pFontInfo->GetFontData(hFont, 0x74746366, temp, 1024); 1140 m_pFontInfo->GetFontData(hFont, kTableTTCF, temp, 1024);
1132 FX_DWORD checksum = 0; 1141 FX_DWORD checksum = 0;
1133 for (int i = 0; i < 256; i++) { 1142 for (int i = 0; i < 256; i++) {
1134 checksum += ((FX_DWORD*)temp)[i]; 1143 checksum += ((FX_DWORD*)temp)[i];
1135 } 1144 }
1136 uint8_t* pFontData; 1145 uint8_t* pFontData;
1137 face = m_pFontMgr->GetCachedTTCFace(ttc_size, checksum, 1146 face = m_pFontMgr->GetCachedTTCFace(ttc_size, checksum,
1138 ttc_size - font_size, pFontData); 1147 ttc_size - font_size, pFontData);
1139 if (face == NULL) { 1148 if (face == NULL) {
1140 pFontData = FX_Alloc(uint8_t, ttc_size); 1149 pFontData = FX_Alloc(uint8_t, ttc_size);
1141 m_pFontInfo->GetFontData(hFont, 0x74746366, pFontData, ttc_size); 1150 m_pFontInfo->GetFontData(hFont, kTableTTCF, pFontData, ttc_size);
1142 face = m_pFontMgr->AddCachedTTCFace(ttc_size, checksum, pFontData, 1151 face = m_pFontMgr->AddCachedTTCFace(ttc_size, checksum, pFontData,
1143 ttc_size, ttc_size - font_size); 1152 ttc_size, ttc_size - font_size);
1144 } 1153 }
1145 } else { 1154 } else {
1146 uint8_t* pFontData; 1155 uint8_t* pFontData;
1147 face = m_pFontMgr->GetCachedFace(SubstName, weight, bItalic, pFontData); 1156 face = m_pFontMgr->GetCachedFace(SubstName, weight, bItalic, pFontData);
1148 if (face == NULL) { 1157 if (face == NULL) {
1149 pFontData = FX_Alloc(uint8_t, font_size); 1158 pFontData = FX_Alloc(uint8_t, font_size);
1150 m_pFontInfo->GetFontData(hFont, 0, pFontData, font_size); 1159 m_pFontInfo->GetFontData(hFont, 0, pFontData, font_size);
1151 face = m_pFontMgr->AddCachedFace(SubstName, weight, bItalic, pFontData, 1160 face = m_pFontMgr->AddCachedFace(SubstName, weight, bItalic, pFontData,
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1260 FXSYS_fseek(pFile, 0, FXSYS_SEEK_END); 1269 FXSYS_fseek(pFile, 0, FXSYS_SEEK_END);
1261 FX_DWORD filesize = FXSYS_ftell(pFile); 1270 FX_DWORD filesize = FXSYS_ftell(pFile);
1262 uint8_t buffer[16]; 1271 uint8_t buffer[16];
1263 FXSYS_fseek(pFile, 0, FXSYS_SEEK_SET); 1272 FXSYS_fseek(pFile, 0, FXSYS_SEEK_SET);
1264 size_t readCnt = FXSYS_fread(buffer, 12, 1, pFile); 1273 size_t readCnt = FXSYS_fread(buffer, 12, 1, pFile);
1265 if (readCnt != 1) { 1274 if (readCnt != 1) {
1266 FXSYS_fclose(pFile); 1275 FXSYS_fclose(pFile);
1267 return; 1276 return;
1268 } 1277 }
1269 1278
1270 if (GET_TT_LONG(buffer) == 0x74746366) { 1279 if (GET_TT_LONG(buffer) == kTableTTCF) {
1271 FX_DWORD nFaces = GET_TT_LONG(buffer + 8); 1280 FX_DWORD nFaces = GET_TT_LONG(buffer + 8);
1272 if (nFaces > std::numeric_limits<FX_DWORD>::max() / 4) { 1281 if (nFaces > std::numeric_limits<FX_DWORD>::max() / 4) {
1273 FXSYS_fclose(pFile); 1282 FXSYS_fclose(pFile);
1274 return; 1283 return;
1275 } 1284 }
1276 FX_DWORD face_bytes = nFaces * 4; 1285 FX_DWORD face_bytes = nFaces * 4;
1277 uint8_t* offsets = FX_Alloc(uint8_t, face_bytes); 1286 uint8_t* offsets = FX_Alloc(uint8_t, face_bytes);
1278 readCnt = FXSYS_fread(offsets, 1, face_bytes, pFile); 1287 readCnt = FXSYS_fread(offsets, 1, face_bytes, pFile);
1279 if (readCnt != face_bytes) { 1288 if (readCnt != face_bytes) {
1280 FX_Free(offsets); 1289 FX_Free(offsets);
(...skipping 18 matching lines...) Expand all
1299 char buffer[16]; 1308 char buffer[16];
1300 if (!FXSYS_fread(buffer, 12, 1, pFile)) { 1309 if (!FXSYS_fread(buffer, 12, 1, pFile)) {
1301 return; 1310 return;
1302 } 1311 }
1303 FX_DWORD nTables = GET_TT_SHORT(buffer + 4); 1312 FX_DWORD nTables = GET_TT_SHORT(buffer + 4);
1304 CFX_ByteString tables = _FPDF_ReadStringFromFile(pFile, nTables * 16); 1313 CFX_ByteString tables = _FPDF_ReadStringFromFile(pFile, nTables * 16);
1305 if (tables.IsEmpty()) { 1314 if (tables.IsEmpty()) {
1306 return; 1315 return;
1307 } 1316 }
1308 CFX_ByteString names = 1317 CFX_ByteString names =
1309 _FPDF_LoadTableFromTT(pFile, tables, nTables, 0x6e616d65); 1318 _FPDF_LoadTableFromTT(pFile, tables, nTables, kTableNAME);
1310 CFX_ByteString facename = GetNameFromTT(names, 1); 1319 CFX_ByteString facename = GetNameFromTT(names, 1);
1311 CFX_ByteString style = GetNameFromTT(names, 2); 1320 CFX_ByteString style = GetNameFromTT(names, 2);
1312 if (style != "Regular") { 1321 if (style != "Regular") {
1313 facename += " " + style; 1322 facename += " " + style;
1314 } 1323 }
1315 if (m_FontList.find(facename) != m_FontList.end()) { 1324 if (m_FontList.find(facename) != m_FontList.end()) {
1316 return; 1325 return;
1317 } 1326 }
1318 CFX_FontFaceInfo* pInfo = 1327 CFX_FontFaceInfo* pInfo =
1319 new CFX_FontFaceInfo(path, facename, tables, offset, filesize); 1328 new CFX_FontFaceInfo(path, facename, tables, offset, filesize);
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
1462 int charset, 1471 int charset,
1463 int pitch_family, 1472 int pitch_family,
1464 const FX_CHAR* family, 1473 const FX_CHAR* family,
1465 int& iExact) { 1474 int& iExact) {
1466 return NULL; 1475 return NULL;
1467 } 1476 }
1468 void* CFX_FolderFontInfo::GetFont(const FX_CHAR* face) { 1477 void* CFX_FolderFontInfo::GetFont(const FX_CHAR* face) {
1469 auto it = m_FontList.find(face); 1478 auto it = m_FontList.find(face);
1470 return it != m_FontList.end() ? it->second : nullptr; 1479 return it != m_FontList.end() ? it->second : nullptr;
1471 } 1480 }
1481
1472 FX_DWORD CFX_FolderFontInfo::GetFontData(void* hFont, 1482 FX_DWORD CFX_FolderFontInfo::GetFontData(void* hFont,
1473 FX_DWORD table, 1483 FX_DWORD table,
1474 uint8_t* buffer, 1484 uint8_t* buffer,
1475 FX_DWORD size) { 1485 FX_DWORD size) {
1476 if (hFont == NULL) { 1486 if (!hFont)
1477 return 0; 1487 return 0;
1478 } 1488
1479 CFX_FontFaceInfo* pFont = (CFX_FontFaceInfo*)hFont; 1489 const CFX_FontFaceInfo* pFont = static_cast<CFX_FontFaceInfo*>(hFont);
1480 FXSYS_FILE* pFile = NULL;
1481 if (size > 0) {
1482 pFile = FXSYS_fopen(pFont->m_FilePath, "rb");
1483 if (pFile == NULL) {
1484 return 0;
1485 }
1486 }
1487 FX_DWORD datasize = 0; 1490 FX_DWORD datasize = 0;
1488 FX_DWORD offset = 0; 1491 FX_DWORD offset = 0;
1489 if (table == 0) { 1492 if (table == 0) {
1490 datasize = pFont->m_FontOffset ? 0 : pFont->m_FileSize; 1493 datasize = pFont->m_FontOffset ? 0 : pFont->m_FileSize;
1491 } else if (table == 0x74746366) { 1494 } else if (table == kTableTTCF) {
1492 datasize = pFont->m_FontOffset ? pFont->m_FileSize : 0; 1495 datasize = pFont->m_FontOffset ? pFont->m_FileSize : 0;
1493 } else { 1496 } else {
1494 FX_DWORD nTables = pFont->m_FontTables.GetLength() / 16; 1497 FX_DWORD nTables = pFont->m_FontTables.GetLength() / 16;
1495 for (FX_DWORD i = 0; i < nTables; i++) { 1498 for (FX_DWORD i = 0; i < nTables; i++) {
1496 const uint8_t* p = (const uint8_t*)pFont->m_FontTables + i * 16; 1499 const uint8_t* p =
1500 static_cast<const uint8_t*>(pFont->m_FontTables) + i * 16;
1497 if (GET_TT_LONG(p) == table) { 1501 if (GET_TT_LONG(p) == table) {
1498 offset = GET_TT_LONG(p + 8); 1502 offset = GET_TT_LONG(p + 8);
1499 datasize = GET_TT_LONG(p + 12); 1503 datasize = GET_TT_LONG(p + 12);
1500 } 1504 }
1501 } 1505 }
1502 } 1506 }
1503 if (datasize && size >= datasize && pFile) { 1507
1504 if (FXSYS_fseek(pFile, offset, FXSYS_SEEK_SET) < 0 || 1508 if (!datasize || size < datasize)
1505 FXSYS_fread(buffer, datasize, 1, pFile) != 1) { 1509 return datasize;
1506 datasize = 0; 1510
1507 } 1511 FXSYS_FILE* pFile = FXSYS_fopen(pFont->m_FilePath, "rb");
1512 if (!pFile)
1513 return 0;
1514
1515 if (FXSYS_fseek(pFile, offset, FXSYS_SEEK_SET) < 0 ||
1516 FXSYS_fread(buffer, datasize, 1, pFile) != 1) {
1517 datasize = 0;
1508 } 1518 }
1509 if (pFile) { 1519 FXSYS_fclose(pFile);
1510 FXSYS_fclose(pFile);
1511 }
1512 return datasize; 1520 return datasize;
1513 } 1521 }
1522
1514 void CFX_FolderFontInfo::DeleteFont(void* hFont) {} 1523 void CFX_FolderFontInfo::DeleteFont(void* hFont) {}
1515 FX_BOOL CFX_FolderFontInfo::GetFaceName(void* hFont, CFX_ByteString& name) { 1524 FX_BOOL CFX_FolderFontInfo::GetFaceName(void* hFont, CFX_ByteString& name) {
1516 if (hFont == NULL) { 1525 if (hFont == NULL) {
1517 return FALSE; 1526 return FALSE;
1518 } 1527 }
1519 CFX_FontFaceInfo* pFont = (CFX_FontFaceInfo*)hFont; 1528 CFX_FontFaceInfo* pFont = (CFX_FontFaceInfo*)hFont;
1520 name = pFont->m_FaceName; 1529 name = pFont->m_FaceName;
1521 return TRUE; 1530 return TRUE;
1522 } 1531 }
1523 FX_BOOL CFX_FolderFontInfo::GetFontCharset(void* hFont, int& charset) { 1532 FX_BOOL CFX_FolderFontInfo::GetFontCharset(void* hFont, int& charset) {
1524 return FALSE; 1533 return FALSE;
1525 } 1534 }
1526 1535
1527 int PDF_GetStandardFontName(CFX_ByteString* name) { 1536 int PDF_GetStandardFontName(CFX_ByteString* name) {
1528 AltFontName* found = static_cast<AltFontName*>( 1537 AltFontName* found = static_cast<AltFontName*>(
1529 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames), 1538 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames),
1530 sizeof(AltFontName), compareString)); 1539 sizeof(AltFontName), compareString));
1531 if (!found) 1540 if (!found)
1532 return -1; 1541 return -1;
1533 1542
1534 *name = g_Base14FontNames[found->m_Index]; 1543 *name = g_Base14FontNames[found->m_Index];
1535 return found->m_Index; 1544 return found->m_Index;
1536 } 1545 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698