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

Side by Side Diff: core/fxge/ge/cfx_fontmapper.cpp

Issue 2185533006: Splitting fx_ge_fontmap.cpp (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Add missing includes Created 4 years, 4 months 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/fxge/ge/cfx_folderfontinfo.cpp ('k') | core/fxge/ge/cfx_fontmgr.cpp » ('j') | 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 2016 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 <algorithm> 7 #include "core/fxge/include/cfx_fontmapper.h"
8 #include <limits>
9 #include <utility>
10 #include <vector>
11 8
9 #include "core/fxge/include/ifx_systemfontinfo.h"
12 #include "core/fxge/include/fx_font.h" 10 #include "core/fxge/include/fx_font.h"
13 11
14 #include "core/fxge/fontdata/chromefontdata/chromefontdata.h"
15 #include "core/fxge/include/fx_freetype.h"
16 #include "core/fxge/include/fx_ge.h"
17 #include "third_party/base/stl_util.h" 12 #include "third_party/base/stl_util.h"
18 13
19 #define GET_TT_SHORT(w) (uint16_t)(((w)[0] << 8) | (w)[1])
20 #define GET_TT_LONG(w) \
21 (uint32_t)(((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
22
23 #define FX_FONT_STYLE_None 0x00 14 #define FX_FONT_STYLE_None 0x00
24 #define FX_FONT_STYLE_Bold 0x01 15 #define FX_FONT_STYLE_Bold 0x01
25 #define FX_FONT_STYLE_Italic 0x02 16 #define FX_FONT_STYLE_Italic 0x02
26 #define FX_FONT_STYLE_BoldBold 0x04 17 #define FX_FONT_STYLE_BoldBold 0x04
27 18
28 namespace { 19 namespace {
29 20
30 struct BuiltinFont {
31 const uint8_t* m_pFontData;
32 uint32_t m_dwSize;
33 };
34
35 const BuiltinFont g_FoxitFonts[14] = {
36 {g_FoxitFixedFontData, 17597},
37 {g_FoxitFixedBoldFontData, 18055},
38 {g_FoxitFixedBoldItalicFontData, 19151},
39 {g_FoxitFixedItalicFontData, 18746},
40 {g_FoxitSansFontData, 15025},
41 {g_FoxitSansBoldFontData, 16344},
42 {g_FoxitSansBoldItalicFontData, 16418},
43 {g_FoxitSansItalicFontData, 16339},
44 {g_FoxitSerifFontData, 19469},
45 {g_FoxitSerifBoldFontData, 19395},
46 {g_FoxitSerifBoldItalicFontData, 20733},
47 {g_FoxitSerifItalicFontData, 21227},
48 {g_FoxitSymbolFontData, 16729},
49 {g_FoxitDingbatsFontData, 29513},
50 };
51
52 const BuiltinFont g_MMFonts[2] = {
53 {g_FoxitSerifMMFontData, 113417},
54 {g_FoxitSansMMFontData, 66919},
55 };
56
57 const FX_CHAR* const g_Base14FontNames[14] = { 21 const FX_CHAR* const g_Base14FontNames[14] = {
58 "Courier", 22 "Courier",
59 "Courier-Bold", 23 "Courier-Bold",
60 "Courier-BoldOblique", 24 "Courier-BoldOblique",
61 "Courier-Oblique", 25 "Courier-Oblique",
62 "Helvetica", 26 "Helvetica",
63 "Helvetica-Bold", 27 "Helvetica-Bold",
64 "Helvetica-BoldOblique", 28 "Helvetica-BoldOblique",
65 "Helvetica-Oblique", 29 "Helvetica-Oblique",
66 "Times-Roman", 30 "Times-Roman",
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 {"TimesNewRomanPS-BoldMT", 9}, 123 {"TimesNewRomanPS-BoldMT", 9},
160 {"TimesNewRomanPS-Italic", 11}, 124 {"TimesNewRomanPS-Italic", 11},
161 {"TimesNewRomanPS-ItalicMT", 11}, 125 {"TimesNewRomanPS-ItalicMT", 11},
162 {"TimesNewRomanPSMT", 8}, 126 {"TimesNewRomanPSMT", 8},
163 {"TimesNewRomanPSMT,Bold", 9}, 127 {"TimesNewRomanPSMT,Bold", 9},
164 {"TimesNewRomanPSMT,BoldItalic", 10}, 128 {"TimesNewRomanPSMT,BoldItalic", 10},
165 {"TimesNewRomanPSMT,Italic", 11}, 129 {"TimesNewRomanPSMT,Italic", 11},
166 {"ZapfDingbats", 13}, 130 {"ZapfDingbats", 13},
167 }; 131 };
168 132
169 const struct {
170 const FX_CHAR* m_pName;
171 const FX_CHAR* m_pSubstName;
172 } Base14Substs[] = {
173 {"Courier", "Courier New"},
174 {"Courier-Bold", "Courier New Bold"},
175 {"Courier-BoldOblique", "Courier New Bold Italic"},
176 {"Courier-Oblique", "Courier New Italic"},
177 {"Helvetica", "Arial"},
178 {"Helvetica-Bold", "Arial Bold"},
179 {"Helvetica-BoldOblique", "Arial Bold Italic"},
180 {"Helvetica-Oblique", "Arial Italic"},
181 {"Times-Roman", "Times New Roman"},
182 {"Times-Bold", "Times New Roman Bold"},
183 {"Times-BoldItalic", "Times New Roman Bold Italic"},
184 {"Times-Italic", "Times New Roman Italic"},
185 };
186
187 const struct AltFontFamily { 133 const struct AltFontFamily {
188 const FX_CHAR* m_pFontName; 134 const FX_CHAR* m_pFontName;
189 const FX_CHAR* m_pFontFamily; 135 const FX_CHAR* m_pFontFamily;
190 } g_AltFontFamilies[] = { 136 } g_AltFontFamilies[] = {
191 {"AGaramondPro", "Adobe Garamond Pro"}, 137 {"AGaramondPro", "Adobe Garamond Pro"},
192 {"BankGothicBT-Medium", "BankGothic Md BT"}, 138 {"BankGothicBT-Medium", "BankGothic Md BT"},
193 {"ForteMT", "Forte"}, 139 {"ForteMT", "Forte"},
194 }; 140 };
195 141
196 const struct FX_FontStyle { 142 const struct FX_FontStyle {
197 const FX_CHAR* style; 143 const FX_CHAR* style;
198 int32_t len; 144 int32_t len;
199 } g_FontStyles[] = { 145 } g_FontStyles[] = {
200 {"Bold", 4}, {"Italic", 6}, {"BoldItalic", 10}, {"Reg", 3}, {"Regular", 7}, 146 {"Bold", 4}, {"Italic", 6}, {"BoldItalic", 10}, {"Reg", 3}, {"Regular", 7},
201 }; 147 };
202 148
203 const struct CODEPAGE_MAP { 149 const struct CODEPAGE_MAP {
204 uint16_t codepage; 150 uint16_t codepage;
205 uint8_t charset; 151 uint8_t charset;
206 } g_Codepage2CharsetTable[] = { 152 } g_Codepage2CharsetTable[] = {
207 {0, 1}, {42, 2}, {437, 254}, {850, 255}, {874, 222}, 153 {0, 1}, {42, 2}, {437, 254}, {850, 255}, {874, 222},
208 {932, 128}, {936, 134}, {949, 129}, {950, 136}, {1250, 238}, 154 {932, 128}, {936, 134}, {949, 129}, {950, 136}, {1250, 238},
209 {1251, 204}, {1252, 0}, {1253, 161}, {1254, 162}, {1255, 177}, 155 {1251, 204}, {1252, 0}, {1253, 161}, {1254, 162}, {1255, 177},
210 {1256, 178}, {1257, 186}, {1258, 163}, {1361, 130}, {10000, 77}, 156 {1256, 178}, {1257, 186}, {1258, 163}, {1361, 130}, {10000, 77},
211 {10001, 78}, {10002, 81}, {10003, 79}, {10004, 84}, {10005, 83}, 157 {10001, 78}, {10002, 81}, {10003, 79}, {10004, 84}, {10005, 83},
212 {10006, 85}, {10007, 89}, {10008, 80}, {10021, 87}, {10029, 88}, 158 {10006, 85}, {10007, 89}, {10008, 80}, {10021, 87}, {10029, 88},
213 {10081, 86}, 159 {10081, 86},
214 }; 160 };
215 161
216 const uint32_t kTableNAME = FXDWORD_GET_MSBFIRST("name");
217 const uint32_t kTableTTCF = FXDWORD_GET_MSBFIRST("ttcf");
218
219 int CompareFontFamilyString(const void* key, const void* element) { 162 int CompareFontFamilyString(const void* key, const void* element) {
220 CFX_ByteString str_key((const FX_CHAR*)key); 163 CFX_ByteString str_key((const FX_CHAR*)key);
221 if (str_key.Find(((AltFontFamily*)element)->m_pFontName) != -1) { 164 if (str_key.Find(((AltFontFamily*)element)->m_pFontName) != -1) {
222 return 0; 165 return 0;
223 } 166 }
224 return FXSYS_stricmp((const FX_CHAR*)key, 167 return FXSYS_stricmp((const FX_CHAR*)key,
225 ((AltFontFamily*)element)->m_pFontName); 168 ((AltFontFamily*)element)->m_pFontName);
226 } 169 }
227 170
228 int CompareString(const void* key, const void* element) { 171 int CompareString(const void* key, const void* element) {
229 return FXSYS_stricmp((const FX_CHAR*)key, ((AltFontName*)element)->m_pName); 172 return FXSYS_stricmp((const FX_CHAR*)key, ((AltFontName*)element)->m_pName);
230 } 173 }
231 174
232 CFX_ByteString KeyNameFromFace(const CFX_ByteString& face_name,
233 int weight,
234 FX_BOOL bItalic) {
235 CFX_ByteString key(face_name);
236 key += ',';
237 key += CFX_ByteString::FormatInteger(weight);
238 key += bItalic ? 'I' : 'N';
239 return key;
240 }
241
242 CFX_ByteString KeyNameFromSize(int ttc_size, uint32_t checksum) {
243 CFX_ByteString key;
244 key.Format("%d:%d", ttc_size, checksum);
245 return key;
246 }
247
248 CFX_ByteString TT_NormalizeName(const FX_CHAR* family) { 175 CFX_ByteString TT_NormalizeName(const FX_CHAR* family) {
249 CFX_ByteString norm(family); 176 CFX_ByteString norm(family);
250 norm.Remove(' '); 177 norm.Remove(' ');
251 norm.Remove('-'); 178 norm.Remove('-');
252 norm.Remove(','); 179 norm.Remove(',');
253 int pos = norm.Find('+'); 180 int pos = norm.Find('+');
254 if (pos > 0) { 181 if (pos > 0) {
255 norm = norm.Left(pos); 182 norm = norm.Left(pos);
256 } 183 }
257 norm.MakeLower(); 184 norm.MakeLower();
258 return norm; 185 return norm;
259 } 186 }
260 187
261 CFX_ByteString FPDF_ReadStringFromFile(FXSYS_FILE* pFile, uint32_t size) {
262 CFX_ByteString buffer;
263 if (!FXSYS_fread(buffer.GetBuffer(size), size, 1, pFile)) {
264 return CFX_ByteString();
265 }
266 buffer.ReleaseBuffer(size);
267 return buffer;
268 }
269
270 CFX_ByteString FPDF_LoadTableFromTT(FXSYS_FILE* pFile,
271 const uint8_t* pTables,
272 uint32_t nTables,
273 uint32_t tag) {
274 for (uint32_t i = 0; i < nTables; i++) {
275 const uint8_t* p = pTables + i * 16;
276 if (GET_TT_LONG(p) == tag) {
277 uint32_t offset = GET_TT_LONG(p + 8);
278 uint32_t size = GET_TT_LONG(p + 12);
279 FXSYS_fseek(pFile, offset, FXSYS_SEEK_SET);
280 return FPDF_ReadStringFromFile(pFile, size);
281 }
282 }
283 return CFX_ByteString();
284 }
285
286 uint8_t GetCharsetFromCodePage(uint16_t codepage) { 188 uint8_t GetCharsetFromCodePage(uint16_t codepage) {
287 const CODEPAGE_MAP* pEnd = 189 const CODEPAGE_MAP* pEnd =
288 g_Codepage2CharsetTable + FX_ArraySize(g_Codepage2CharsetTable); 190 g_Codepage2CharsetTable + FX_ArraySize(g_Codepage2CharsetTable);
289 const CODEPAGE_MAP* pCharmap = 191 const CODEPAGE_MAP* pCharmap =
290 std::lower_bound(g_Codepage2CharsetTable, pEnd, codepage, 192 std::lower_bound(g_Codepage2CharsetTable, pEnd, codepage,
291 [](const CODEPAGE_MAP& charset, uint16_t page) { 193 [](const CODEPAGE_MAP& charset, uint16_t page) {
292 return charset.codepage < page; 194 return charset.codepage < page;
293 }); 195 });
294 if (pCharmap < pEnd && codepage == pCharmap->codepage) 196 if (pCharmap < pEnd && codepage == pCharmap->codepage)
295 return pCharmap->charset; 197 return pCharmap->charset;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 } 259 }
358 260
359 FX_BOOL CheckSupportThirdPartFont(CFX_ByteString name, int& PitchFamily) { 261 FX_BOOL CheckSupportThirdPartFont(CFX_ByteString name, int& PitchFamily) {
360 if (name == "MyriadPro") { 262 if (name == "MyriadPro") {
361 PitchFamily &= ~FXFONT_FF_ROMAN; 263 PitchFamily &= ~FXFONT_FF_ROMAN;
362 return TRUE; 264 return TRUE;
363 } 265 }
364 return FALSE; 266 return FALSE;
365 } 267 }
366 268
367 uint32_t GetCharset(int charset) {
368 switch (charset) {
369 case FXFONT_SHIFTJIS_CHARSET:
370 return CHARSET_FLAG_SHIFTJIS;
371 case FXFONT_GB2312_CHARSET:
372 return CHARSET_FLAG_GB;
373 case FXFONT_CHINESEBIG5_CHARSET:
374 return CHARSET_FLAG_BIG5;
375 case FXFONT_HANGEUL_CHARSET:
376 return CHARSET_FLAG_KOREAN;
377 case FXFONT_SYMBOL_CHARSET:
378 return CHARSET_FLAG_SYMBOL;
379 case FXFONT_ANSI_CHARSET:
380 return CHARSET_FLAG_ANSI;
381 default:
382 break;
383 }
384 return 0;
385 }
386
387 int32_t GetSimilarValue(int weight,
388 FX_BOOL bItalic,
389 int pitch_family,
390 uint32_t style) {
391 int32_t iSimilarValue = 0;
392 if (!!(style & FXFONT_BOLD) == (weight > 400)) {
393 iSimilarValue += 16;
394 }
395 if (!!(style & FXFONT_ITALIC) == bItalic) {
396 iSimilarValue += 16;
397 }
398 if (!!(style & FXFONT_SERIF) == !!(pitch_family & FXFONT_FF_ROMAN)) {
399 iSimilarValue += 16;
400 }
401 if (!!(style & FXFONT_SCRIPT) == !!(pitch_family & FXFONT_FF_SCRIPT)) {
402 iSimilarValue += 8;
403 }
404 if (!!(style & FXFONT_FIXED_PITCH) ==
405 !!(pitch_family & FXFONT_FF_FIXEDPITCH)) {
406 iSimilarValue += 8;
407 }
408 return iSimilarValue;
409 }
410
411 } // namespace 269 } // namespace
412 270
413 CFX_SubstFont::CFX_SubstFont() {
414 m_ExtHandle = nullptr;
415 m_Charset = FXFONT_ANSI_CHARSET;
416 m_SubstFlags = 0;
417 m_Weight = 0;
418 m_ItalicAngle = 0;
419 m_bSubstCJK = false;
420 m_WeightCJK = 0;
421 m_bItalicCJK = false;
422 }
423
424 CTTFontDesc::~CTTFontDesc() {
425 if (m_Type == 1) {
426 if (m_SingleFace.m_pFace) {
427 FXFT_Done_Face(m_SingleFace.m_pFace);
428 }
429 } else if (m_Type == 2) {
430 for (int i = 0; i < 16; i++)
431 if (m_TTCFace.m_pFaces[i]) {
432 FXFT_Done_Face(m_TTCFace.m_pFaces[i]);
433 }
434 }
435 FX_Free(m_pFontData);
436 }
437 int CTTFontDesc::ReleaseFace(FXFT_Face face) {
438 if (m_Type == 1) {
439 if (m_SingleFace.m_pFace != face) {
440 return -1;
441 }
442 } else if (m_Type == 2) {
443 int i;
444 for (i = 0; i < 16; i++)
445 if (m_TTCFace.m_pFaces[i] == face) {
446 break;
447 }
448 if (i == 16) {
449 return -1;
450 }
451 }
452 m_RefCount--;
453 if (m_RefCount) {
454 return m_RefCount;
455 }
456 delete this;
457 return 0;
458 }
459
460 CFX_FontMgr::CFX_FontMgr()
461 : m_FTLibrary(nullptr), m_FTLibrarySupportsHinting(false) {
462 m_pBuiltinMapper.reset(new CFX_FontMapper(this));
463 }
464
465 CFX_FontMgr::~CFX_FontMgr() {
466 for (const auto& pair : m_FaceMap)
467 delete pair.second;
468
469 // |m_pBuiltinMapper| references |m_FTLibrary|, so it has to be destroyed
470 // first.
471 m_pBuiltinMapper.reset();
472 FXFT_Done_FreeType(m_FTLibrary);
473 }
474
475 void CFX_FontMgr::InitFTLibrary() {
476 if (m_FTLibrary)
477 return;
478 FXFT_Init_FreeType(&m_FTLibrary);
479 m_FTLibrarySupportsHinting =
480 FXFT_Library_SetLcdFilter(m_FTLibrary, FT_LCD_FILTER_DEFAULT) !=
481 FT_Err_Unimplemented_Feature;
482 }
483
484 void CFX_FontMgr::SetSystemFontInfo(
485 std::unique_ptr<IFX_SystemFontInfo> pFontInfo) {
486 m_pBuiltinMapper->SetSystemFontInfo(std::move(pFontInfo));
487 }
488
489 FXFT_Face CFX_FontMgr::FindSubstFont(const CFX_ByteString& face_name,
490 FX_BOOL bTrueType,
491 uint32_t flags,
492 int weight,
493 int italic_angle,
494 int CharsetCP,
495 CFX_SubstFont* pSubstFont) {
496 InitFTLibrary();
497 return m_pBuiltinMapper->FindSubstFont(face_name, bTrueType, flags, weight,
498 italic_angle, CharsetCP, pSubstFont);
499 }
500
501 FXFT_Face CFX_FontMgr::GetCachedFace(const CFX_ByteString& face_name,
502 int weight,
503 FX_BOOL bItalic,
504 uint8_t*& pFontData) {
505 auto it = m_FaceMap.find(KeyNameFromFace(face_name, weight, bItalic));
506 if (it == m_FaceMap.end())
507 return nullptr;
508
509 CTTFontDesc* pFontDesc = it->second;
510 pFontData = pFontDesc->m_pFontData;
511 pFontDesc->m_RefCount++;
512 return pFontDesc->m_SingleFace.m_pFace;
513 }
514 FXFT_Face CFX_FontMgr::AddCachedFace(const CFX_ByteString& face_name,
515 int weight,
516 FX_BOOL bItalic,
517 uint8_t* pData,
518 uint32_t size,
519 int face_index) {
520 CTTFontDesc* pFontDesc = new CTTFontDesc;
521 pFontDesc->m_Type = 1;
522 pFontDesc->m_SingleFace.m_pFace = nullptr;
523 pFontDesc->m_SingleFace.m_bBold = weight;
524 pFontDesc->m_SingleFace.m_bItalic = bItalic;
525 pFontDesc->m_pFontData = pData;
526 pFontDesc->m_RefCount = 1;
527
528 InitFTLibrary();
529 FXFT_Library library = m_FTLibrary;
530 int ret = FXFT_New_Memory_Face(library, pData, size, face_index,
531 &pFontDesc->m_SingleFace.m_pFace);
532 if (ret) {
533 delete pFontDesc;
534 return nullptr;
535 }
536 ret = FXFT_Set_Pixel_Sizes(pFontDesc->m_SingleFace.m_pFace, 64, 64);
537 if (ret) {
538 delete pFontDesc;
539 return nullptr;
540 }
541 m_FaceMap[KeyNameFromFace(face_name, weight, bItalic)] = pFontDesc;
542 return pFontDesc->m_SingleFace.m_pFace;
543 }
544
545 int GetTTCIndex(const uint8_t* pFontData,
546 uint32_t ttc_size,
547 uint32_t font_offset) {
548 int face_index = 0;
549 const uint8_t* p = pFontData + 8;
550 uint32_t nfont = GET_TT_LONG(p);
551 uint32_t index;
552 for (index = 0; index < nfont; index++) {
553 p = pFontData + 12 + index * 4;
554 if (GET_TT_LONG(p) == font_offset) {
555 break;
556 }
557 }
558 if (index >= nfont) {
559 face_index = 0;
560 } else {
561 face_index = index;
562 }
563 return face_index;
564 }
565 FXFT_Face CFX_FontMgr::GetCachedTTCFace(int ttc_size,
566 uint32_t checksum,
567 int font_offset,
568 uint8_t*& pFontData) {
569 auto it = m_FaceMap.find(KeyNameFromSize(ttc_size, checksum));
570 if (it == m_FaceMap.end())
571 return nullptr;
572
573 CTTFontDesc* pFontDesc = it->second;
574 pFontData = pFontDesc->m_pFontData;
575 pFontDesc->m_RefCount++;
576 int face_index = GetTTCIndex(pFontDesc->m_pFontData, ttc_size, font_offset);
577 if (!pFontDesc->m_TTCFace.m_pFaces[face_index]) {
578 pFontDesc->m_TTCFace.m_pFaces[face_index] =
579 GetFixedFace(pFontDesc->m_pFontData, ttc_size, face_index);
580 }
581 return pFontDesc->m_TTCFace.m_pFaces[face_index];
582 }
583 FXFT_Face CFX_FontMgr::AddCachedTTCFace(int ttc_size,
584 uint32_t checksum,
585 uint8_t* pData,
586 uint32_t size,
587 int font_offset) {
588 CTTFontDesc* pFontDesc = new CTTFontDesc;
589 pFontDesc->m_Type = 2;
590 pFontDesc->m_pFontData = pData;
591 for (int i = 0; i < 16; i++) {
592 pFontDesc->m_TTCFace.m_pFaces[i] = nullptr;
593 }
594 pFontDesc->m_RefCount++;
595 m_FaceMap[KeyNameFromSize(ttc_size, checksum)] = pFontDesc;
596 int face_index = GetTTCIndex(pFontDesc->m_pFontData, ttc_size, font_offset);
597 pFontDesc->m_TTCFace.m_pFaces[face_index] =
598 GetFixedFace(pFontDesc->m_pFontData, ttc_size, face_index);
599 return pFontDesc->m_TTCFace.m_pFaces[face_index];
600 }
601
602 FXFT_Face CFX_FontMgr::GetFixedFace(const uint8_t* pData,
603 uint32_t size,
604 int face_index) {
605 InitFTLibrary();
606 FXFT_Library library = m_FTLibrary;
607 FXFT_Face face = nullptr;
608 if (FXFT_New_Memory_Face(library, pData, size, face_index, &face))
609 return nullptr;
610 return FXFT_Set_Pixel_Sizes(face, 64, 64) ? nullptr : face;
611 }
612
613 FXFT_Face CFX_FontMgr::GetFileFace(const FX_CHAR* filename, int face_index) {
614 InitFTLibrary();
615 FXFT_Library library = m_FTLibrary;
616 FXFT_Face face = nullptr;
617 if (FXFT_New_Face(library, filename, face_index, &face))
618 return nullptr;
619 return FXFT_Set_Pixel_Sizes(face, 64, 64) ? nullptr : face;
620 }
621
622 void CFX_FontMgr::ReleaseFace(FXFT_Face face) {
623 if (!face) {
624 return;
625 }
626 FX_BOOL bNeedFaceDone = TRUE;
627 auto it = m_FaceMap.begin();
628 while (it != m_FaceMap.end()) {
629 auto temp = it++;
630 int nRet = temp->second->ReleaseFace(face);
631 if (nRet == -1)
632 continue;
633 bNeedFaceDone = FALSE;
634 if (nRet == 0)
635 m_FaceMap.erase(temp);
636 break;
637 }
638 if (bNeedFaceDone && !m_pBuiltinMapper->IsBuiltinFace(face))
639 FXFT_Done_Face(face);
640 }
641
642 bool CFX_FontMgr::GetBuiltinFont(size_t index,
643 const uint8_t** pFontData,
644 uint32_t* size) {
645 if (index < FX_ArraySize(g_FoxitFonts)) {
646 *pFontData = g_FoxitFonts[index].m_pFontData;
647 *size = g_FoxitFonts[index].m_dwSize;
648 return true;
649 }
650 index -= FX_ArraySize(g_FoxitFonts);
651 if (index < FX_ArraySize(g_MMFonts)) {
652 *pFontData = g_MMFonts[index].m_pFontData;
653 *size = g_MMFonts[index].m_dwSize;
654 return true;
655 }
656 return false;
657 }
658
659 CFX_FontMapper::CFX_FontMapper(CFX_FontMgr* mgr) 271 CFX_FontMapper::CFX_FontMapper(CFX_FontMgr* mgr)
660 : m_bListLoaded(FALSE), 272 : m_bListLoaded(FALSE), m_pFontMgr(mgr) {
661 m_pFontMgr(mgr) {
662 m_MMFaces[0] = nullptr; 273 m_MMFaces[0] = nullptr;
663 m_MMFaces[1] = nullptr; 274 m_MMFaces[1] = nullptr;
664 FXSYS_memset(m_FoxitFaces, 0, sizeof(m_FoxitFaces)); 275 FXSYS_memset(m_FoxitFaces, 0, sizeof(m_FoxitFaces));
665 } 276 }
666 277
667 CFX_FontMapper::~CFX_FontMapper() { 278 CFX_FontMapper::~CFX_FontMapper() {
668 for (size_t i = 0; i < FX_ArraySize(m_FoxitFaces); ++i) { 279 for (size_t i = 0; i < FX_ArraySize(m_FoxitFaces); ++i) {
669 if (m_FoxitFaces[i]) 280 if (m_FoxitFaces[i])
670 FXFT_Done_Face(m_FoxitFaces[i]); 281 FXFT_Done_Face(m_FoxitFaces[i]);
671 } 282 }
672 if (m_MMFaces[0]) 283 if (m_MMFaces[0])
673 FXFT_Done_Face(m_MMFaces[0]); 284 FXFT_Done_Face(m_MMFaces[0]);
674 if (m_MMFaces[1]) 285 if (m_MMFaces[1])
675 FXFT_Done_Face(m_MMFaces[1]); 286 FXFT_Done_Face(m_MMFaces[1]);
676 } 287 }
677 288
678 void CFX_FontMapper::SetSystemFontInfo( 289 void CFX_FontMapper::SetSystemFontInfo(
679 std::unique_ptr<IFX_SystemFontInfo> pFontInfo) { 290 std::unique_ptr<IFX_SystemFontInfo> pFontInfo) {
680 if (!pFontInfo) 291 if (!pFontInfo)
681 return; 292 return;
682 293
683 m_pFontInfo = std::move(pFontInfo); 294 m_pFontInfo = std::move(pFontInfo);
684 } 295 }
685 296
686 static CFX_ByteString GetStringFromTable(const uint8_t* string_ptr,
687 uint32_t string_ptr_length,
688 uint16_t offset,
689 uint16_t length) {
690 if (string_ptr_length < static_cast<uint32_t>(offset + length)) {
691 return CFX_ByteString();
692 }
693 return CFX_ByteString(string_ptr + offset, length);
694 }
695
696 CFX_ByteString GetNameFromTT(const uint8_t* name_table,
697 uint32_t name_table_size,
698 uint32_t name_id) {
699 if (!name_table || name_table_size < 6) {
700 return CFX_ByteString();
701 }
702 uint32_t name_count = GET_TT_SHORT(name_table + 2);
703 uint32_t string_offset = GET_TT_SHORT(name_table + 4);
704 // We will ignore the possibility of overlap of structures and
705 // string table as if it's all corrupt there's not a lot we can do.
706 if (name_table_size < string_offset) {
707 return CFX_ByteString();
708 }
709
710 const uint8_t* string_ptr = name_table + string_offset;
711 uint32_t string_ptr_size = name_table_size - string_offset;
712 name_table += 6;
713 name_table_size -= 6;
714 if (name_table_size < name_count * 12) {
715 return CFX_ByteString();
716 }
717
718 for (uint32_t i = 0; i < name_count; i++, name_table += 12) {
719 if (GET_TT_SHORT(name_table + 6) == name_id &&
720 GET_TT_SHORT(name_table) == 1 && GET_TT_SHORT(name_table + 2) == 0) {
721 return GetStringFromTable(string_ptr, string_ptr_size,
722 GET_TT_SHORT(name_table + 10),
723 GET_TT_SHORT(name_table + 8));
724 }
725 }
726 return CFX_ByteString();
727 }
728
729 CFX_ByteString CFX_FontMapper::GetPSNameFromTT(void* hFont) { 297 CFX_ByteString CFX_FontMapper::GetPSNameFromTT(void* hFont) {
730 if (!m_pFontInfo) 298 if (!m_pFontInfo)
731 return CFX_ByteString(); 299 return CFX_ByteString();
732 300
733 uint32_t size = m_pFontInfo->GetFontData(hFont, kTableNAME, nullptr, 0); 301 uint32_t size = m_pFontInfo->GetFontData(hFont, kTableNAME, nullptr, 0);
734 if (!size) 302 if (!size)
735 return CFX_ByteString(); 303 return CFX_ByteString();
736 304
737 std::vector<uint8_t> buffer(size); 305 std::vector<uint8_t> buffer(size);
738 uint8_t* buffer_ptr = buffer.data(); 306 uint8_t* buffer_ptr = buffer.data();
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
1303 } 871 }
1304 m_pFontInfo->GetFontData(hFont, 0, pFontData, font_size); 872 m_pFontInfo->GetFontData(hFont, 0, pFontData, font_size);
1305 face = m_pFontMgr->AddCachedFace(SubstName, weight, bItalic, pFontData, 873 face = m_pFontMgr->AddCachedFace(SubstName, weight, bItalic, pFontData,
1306 font_size, 874 font_size,
1307 m_pFontInfo->GetFaceIndex(hFont)); 875 m_pFontInfo->GetFaceIndex(hFont));
1308 } 876 }
1309 } 877 }
1310 m_pFontInfo->DeleteFont(hFont); 878 m_pFontInfo->DeleteFont(hFont);
1311 return face; 879 return face;
1312 } 880 }
1313
1314 void* IFX_SystemFontInfo::MapFontByUnicode(uint32_t dwUnicode,
1315 int weight,
1316 FX_BOOL bItalic,
1317 int pitch_family) {
1318 return nullptr;
1319 }
1320 #endif // PDF_ENABLE_XFA 881 #endif // PDF_ENABLE_XFA
1321 882
1322 int IFX_SystemFontInfo::GetFaceIndex(void* hFont) {
1323 return 0;
1324 }
1325
1326 void* IFX_SystemFontInfo::RetainFont(void* hFont) {
1327 return nullptr;
1328 }
1329
1330 int CFX_FontMapper::GetFaceSize() const { 883 int CFX_FontMapper::GetFaceSize() const {
1331 return pdfium::CollectionSize<int>(m_FaceArray); 884 return pdfium::CollectionSize<int>(m_FaceArray);
1332 } 885 }
1333 886
1334 FX_BOOL CFX_FontMapper::IsBuiltinFace(const FXFT_Face face) const { 887 FX_BOOL CFX_FontMapper::IsBuiltinFace(const FXFT_Face face) const {
1335 for (size_t i = 0; i < MM_FACE_COUNT; ++i) { 888 for (size_t i = 0; i < MM_FACE_COUNT; ++i) {
1336 if (m_MMFaces[i] == face) { 889 if (m_MMFaces[i] == face) {
1337 return TRUE; 890 return TRUE;
1338 } 891 }
1339 } 892 }
1340 for (size_t i = 0; i < FOXIT_FACE_COUNT; ++i) { 893 for (size_t i = 0; i < FOXIT_FACE_COUNT; ++i) {
1341 if (m_FoxitFaces[i] == face) { 894 if (m_FoxitFaces[i] == face) {
1342 return TRUE; 895 return TRUE;
1343 } 896 }
1344 } 897 }
1345 return FALSE; 898 return FALSE;
1346 } 899 }
1347 900
1348 extern "C" {
1349 unsigned long _FTStreamRead(FXFT_Stream stream,
1350 unsigned long offset,
1351 unsigned char* buffer,
1352 unsigned long count);
1353 void _FTStreamClose(FXFT_Stream stream);
1354 };
1355
1356 #if _FX_OS_ == _FX_ANDROID_
1357 std::unique_ptr<IFX_SystemFontInfo> IFX_SystemFontInfo::CreateDefault(
1358 const char** pUnused) {
1359 return nullptr;
1360 }
1361 #endif
1362
1363 CFX_FontFaceInfo::CFX_FontFaceInfo(CFX_ByteString filePath,
1364 CFX_ByteString faceName,
1365 CFX_ByteString fontTables,
1366 uint32_t fontOffset,
1367 uint32_t fileSize)
1368 : m_FilePath(filePath),
1369 m_FaceName(faceName),
1370 m_FontTables(fontTables),
1371 m_FontOffset(fontOffset),
1372 m_FileSize(fileSize),
1373 m_Styles(0),
1374 m_Charsets(0) {}
1375
1376 CFX_FolderFontInfo::CFX_FolderFontInfo() {}
1377
1378 CFX_FolderFontInfo::~CFX_FolderFontInfo() {
1379 for (const auto& pair : m_FontList) {
1380 delete pair.second;
1381 }
1382 }
1383
1384 void CFX_FolderFontInfo::AddPath(const CFX_ByteStringC& path) {
1385 m_PathList.push_back(CFX_ByteString(path));
1386 }
1387
1388 FX_BOOL CFX_FolderFontInfo::EnumFontList(CFX_FontMapper* pMapper) {
1389 m_pMapper = pMapper;
1390 for (const auto& path : m_PathList)
1391 ScanPath(path);
1392 return TRUE;
1393 }
1394 void CFX_FolderFontInfo::ScanPath(const CFX_ByteString& path) {
1395 void* handle = FX_OpenFolder(path.c_str());
1396 if (!handle)
1397 return;
1398
1399 CFX_ByteString filename;
1400 FX_BOOL bFolder;
1401 while (FX_GetNextFile(handle, filename, bFolder)) {
1402 if (bFolder) {
1403 if (filename == "." || filename == "..")
1404 continue;
1405 } else {
1406 CFX_ByteString ext = filename.Right(4);
1407 ext.MakeUpper();
1408 if (ext != ".TTF" && ext != ".OTF" && ext != ".TTC")
1409 continue;
1410 }
1411
1412 CFX_ByteString fullpath = path;
1413 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
1414 fullpath += "\\";
1415 #else
1416 fullpath += "/";
1417 #endif
1418
1419 fullpath += filename;
1420 bFolder ? ScanPath(fullpath) : ScanFile(fullpath);
1421 }
1422 FX_CloseFolder(handle);
1423 }
1424
1425 void CFX_FolderFontInfo::ScanFile(const CFX_ByteString& path) {
1426 FXSYS_FILE* pFile = FXSYS_fopen(path.c_str(), "rb");
1427 if (!pFile)
1428 return;
1429
1430 FXSYS_fseek(pFile, 0, FXSYS_SEEK_END);
1431
1432 uint32_t filesize = FXSYS_ftell(pFile);
1433 uint8_t buffer[16];
1434 FXSYS_fseek(pFile, 0, FXSYS_SEEK_SET);
1435
1436 size_t readCnt = FXSYS_fread(buffer, 12, 1, pFile);
1437 if (readCnt != 1) {
1438 FXSYS_fclose(pFile);
1439 return;
1440 }
1441
1442 if (GET_TT_LONG(buffer) == kTableTTCF) {
1443 uint32_t nFaces = GET_TT_LONG(buffer + 8);
1444 if (nFaces > std::numeric_limits<uint32_t>::max() / 4) {
1445 FXSYS_fclose(pFile);
1446 return;
1447 }
1448 uint32_t face_bytes = nFaces * 4;
1449 uint8_t* offsets = FX_Alloc(uint8_t, face_bytes);
1450 readCnt = FXSYS_fread(offsets, 1, face_bytes, pFile);
1451 if (readCnt != face_bytes) {
1452 FX_Free(offsets);
1453 FXSYS_fclose(pFile);
1454 return;
1455 }
1456 for (uint32_t i = 0; i < nFaces; i++) {
1457 uint8_t* p = offsets + i * 4;
1458 ReportFace(path, pFile, filesize, GET_TT_LONG(p));
1459 }
1460 FX_Free(offsets);
1461 } else {
1462 ReportFace(path, pFile, filesize, 0);
1463 }
1464 FXSYS_fclose(pFile);
1465 }
1466 void CFX_FolderFontInfo::ReportFace(const CFX_ByteString& path,
1467 FXSYS_FILE* pFile,
1468 uint32_t filesize,
1469 uint32_t offset) {
1470 FXSYS_fseek(pFile, offset, FXSYS_SEEK_SET);
1471 char buffer[16];
1472 if (!FXSYS_fread(buffer, 12, 1, pFile))
1473 return;
1474
1475 uint32_t nTables = GET_TT_SHORT(buffer + 4);
1476 CFX_ByteString tables = FPDF_ReadStringFromFile(pFile, nTables * 16);
1477 if (tables.IsEmpty())
1478 return;
1479
1480 CFX_ByteString names =
1481 FPDF_LoadTableFromTT(pFile, tables.raw_str(), nTables, 0x6e616d65);
1482 if (names.IsEmpty())
1483 return;
1484
1485 CFX_ByteString facename =
1486 GetNameFromTT(names.raw_str(), names.GetLength(), 1);
1487 if (facename.IsEmpty())
1488 return;
1489
1490 CFX_ByteString style = GetNameFromTT(names.raw_str(), names.GetLength(), 2);
1491 if (style != "Regular")
1492 facename += " " + style;
1493
1494 if (pdfium::ContainsKey(m_FontList, facename))
1495 return;
1496
1497 CFX_FontFaceInfo* pInfo =
1498 new CFX_FontFaceInfo(path, facename, tables, offset, filesize);
1499 CFX_ByteString os2 =
1500 FPDF_LoadTableFromTT(pFile, tables.raw_str(), nTables, 0x4f532f32);
1501 if (os2.GetLength() >= 86) {
1502 const uint8_t* p = os2.raw_str() + 78;
1503 uint32_t codepages = GET_TT_LONG(p);
1504 if (codepages & (1 << 17)) {
1505 m_pMapper->AddInstalledFont(facename, FXFONT_SHIFTJIS_CHARSET);
1506 pInfo->m_Charsets |= CHARSET_FLAG_SHIFTJIS;
1507 }
1508 if (codepages & (1 << 18)) {
1509 m_pMapper->AddInstalledFont(facename, FXFONT_GB2312_CHARSET);
1510 pInfo->m_Charsets |= CHARSET_FLAG_GB;
1511 }
1512 if (codepages & (1 << 20)) {
1513 m_pMapper->AddInstalledFont(facename, FXFONT_CHINESEBIG5_CHARSET);
1514 pInfo->m_Charsets |= CHARSET_FLAG_BIG5;
1515 }
1516 if ((codepages & (1 << 19)) || (codepages & (1 << 21))) {
1517 m_pMapper->AddInstalledFont(facename, FXFONT_HANGEUL_CHARSET);
1518 pInfo->m_Charsets |= CHARSET_FLAG_KOREAN;
1519 }
1520 if (codepages & (1 << 31)) {
1521 m_pMapper->AddInstalledFont(facename, FXFONT_SYMBOL_CHARSET);
1522 pInfo->m_Charsets |= CHARSET_FLAG_SYMBOL;
1523 }
1524 }
1525 m_pMapper->AddInstalledFont(facename, FXFONT_ANSI_CHARSET);
1526 pInfo->m_Charsets |= CHARSET_FLAG_ANSI;
1527 pInfo->m_Styles = 0;
1528 if (style.Find("Bold") > -1)
1529 pInfo->m_Styles |= FXFONT_BOLD;
1530 if (style.Find("Italic") > -1 || style.Find("Oblique") > -1)
1531 pInfo->m_Styles |= FXFONT_ITALIC;
1532 if (facename.Find("Serif") > -1)
1533 pInfo->m_Styles |= FXFONT_SERIF;
1534
1535 m_FontList[facename] = pInfo;
1536 }
1537
1538 void* CFX_FolderFontInfo::GetSubstFont(const CFX_ByteString& face) {
1539 for (size_t iBaseFont = 0; iBaseFont < FX_ArraySize(Base14Substs);
1540 iBaseFont++) {
1541 if (face == Base14Substs[iBaseFont].m_pName)
1542 return GetFont(Base14Substs[iBaseFont].m_pSubstName);
1543 }
1544 return nullptr;
1545 }
1546
1547 void* CFX_FolderFontInfo::FindFont(int weight,
1548 FX_BOOL bItalic,
1549 int charset,
1550 int pitch_family,
1551 const FX_CHAR* family,
1552 FX_BOOL bMatchName) {
1553 CFX_FontFaceInfo* pFind = nullptr;
1554 if (charset == FXFONT_ANSI_CHARSET && (pitch_family & FXFONT_FF_FIXEDPITCH)) {
1555 return GetFont("Courier New");
1556 }
1557 uint32_t charset_flag = GetCharset(charset);
1558 int32_t iBestSimilar = 0;
1559 for (const auto& it : m_FontList) {
1560 const CFX_ByteString& bsName = it.first;
1561 CFX_FontFaceInfo* pFont = it.second;
1562 if (!(pFont->m_Charsets & charset_flag) &&
1563 charset != FXFONT_DEFAULT_CHARSET) {
1564 continue;
1565 }
1566 int32_t index = bsName.Find(family);
1567 if (bMatchName && index < 0) {
1568 continue;
1569 }
1570 int32_t iSimilarValue =
1571 GetSimilarValue(weight, bItalic, pitch_family, pFont->m_Styles);
1572 if (iSimilarValue > iBestSimilar) {
1573 iBestSimilar = iSimilarValue;
1574 pFind = pFont;
1575 }
1576 }
1577 return pFind;
1578 }
1579 void* CFX_FolderFontInfo::MapFont(int weight,
1580 FX_BOOL bItalic,
1581 int charset,
1582 int pitch_family,
1583 const FX_CHAR* family,
1584 int& iExact) {
1585 return nullptr;
1586 }
1587
1588 #ifdef PDF_ENABLE_XFA
1589 void* CFX_FolderFontInfo::MapFontByUnicode(uint32_t dwUnicode,
1590 int weight,
1591 FX_BOOL bItalic,
1592 int pitch_family) {
1593 return nullptr;
1594 }
1595 #endif // PDF_ENABLE_XFA
1596
1597 void* CFX_FolderFontInfo::GetFont(const FX_CHAR* face) {
1598 auto it = m_FontList.find(face);
1599 return it != m_FontList.end() ? it->second : nullptr;
1600 }
1601
1602 uint32_t CFX_FolderFontInfo::GetFontData(void* hFont,
1603 uint32_t table,
1604 uint8_t* buffer,
1605 uint32_t size) {
1606 if (!hFont)
1607 return 0;
1608
1609 const CFX_FontFaceInfo* pFont = static_cast<CFX_FontFaceInfo*>(hFont);
1610 uint32_t datasize = 0;
1611 uint32_t offset = 0;
1612 if (table == 0) {
1613 datasize = pFont->m_FontOffset ? 0 : pFont->m_FileSize;
1614 } else if (table == kTableTTCF) {
1615 datasize = pFont->m_FontOffset ? pFont->m_FileSize : 0;
1616 } else {
1617 uint32_t nTables = pFont->m_FontTables.GetLength() / 16;
1618 for (uint32_t i = 0; i < nTables; i++) {
1619 const uint8_t* p = pFont->m_FontTables.raw_str() + i * 16;
1620 if (GET_TT_LONG(p) == table) {
1621 offset = GET_TT_LONG(p + 8);
1622 datasize = GET_TT_LONG(p + 12);
1623 }
1624 }
1625 }
1626
1627 if (!datasize || size < datasize)
1628 return datasize;
1629
1630 FXSYS_FILE* pFile = FXSYS_fopen(pFont->m_FilePath.c_str(), "rb");
1631 if (!pFile)
1632 return 0;
1633
1634 if (FXSYS_fseek(pFile, offset, FXSYS_SEEK_SET) < 0 ||
1635 FXSYS_fread(buffer, datasize, 1, pFile) != 1) {
1636 datasize = 0;
1637 }
1638 FXSYS_fclose(pFile);
1639 return datasize;
1640 }
1641
1642 void CFX_FolderFontInfo::DeleteFont(void* hFont) {}
1643 FX_BOOL CFX_FolderFontInfo::GetFaceName(void* hFont, CFX_ByteString& name) {
1644 if (!hFont) {
1645 return FALSE;
1646 }
1647 CFX_FontFaceInfo* pFont = (CFX_FontFaceInfo*)hFont;
1648 name = pFont->m_FaceName;
1649 return TRUE;
1650 }
1651 FX_BOOL CFX_FolderFontInfo::GetFontCharset(void* hFont, int& charset) {
1652 return FALSE;
1653 }
1654
1655 int PDF_GetStandardFontName(CFX_ByteString* name) { 901 int PDF_GetStandardFontName(CFX_ByteString* name) {
1656 AltFontName* found = static_cast<AltFontName*>( 902 AltFontName* found = static_cast<AltFontName*>(
1657 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames), 903 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames),
1658 sizeof(AltFontName), CompareString)); 904 sizeof(AltFontName), CompareString));
1659 if (!found) 905 if (!found)
1660 return -1; 906 return -1;
1661 907
1662 *name = g_Base14FontNames[found->m_Index]; 908 *name = g_Base14FontNames[found->m_Index];
1663 return found->m_Index; 909 return found->m_Index;
1664 } 910 }
OLDNEW
« no previous file with comments | « core/fxge/ge/cfx_folderfontinfo.cpp ('k') | core/fxge/ge/cfx_fontmgr.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698