Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfx_fontmapper.cpp
Go to the documentation of this file.
1// Copyright 2016 The PDFium Authors
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/cfx_fontmapper.h"
8
9#include <stdint.h>
10
11#include <algorithm>
12#include <memory>
13#include <utility>
14
15#include "build/build_config.h"
16#include "core/fxcrt/data_vector.h"
17#include "core/fxcrt/fx_codepage.h"
18#include "core/fxcrt/fx_extension.h"
19#include "core/fxcrt/fx_memory.h"
20#include "core/fxcrt/fx_memory_wrappers.h"
21#include "core/fxcrt/unowned_ptr_exclusion.h"
22#include "core/fxge/cfx_fontmgr.h"
23#include "core/fxge/cfx_substfont.h"
24#include "core/fxge/fx_font.h"
25#include "core/fxge/systemfontinfo_iface.h"
26#include "third_party/base/check_op.h"
27#include "third_party/base/containers/adapters.h"
28#include "third_party/base/containers/contains.h"
29
30namespace {
31
33 "StandardFont enum count mismatch");
34
35const char* const kBase14FontNames[CFX_FontMapper::kNumStandardFonts] = {
36 "Courier",
37 "Courier-Bold",
38 "Courier-BoldOblique",
39 "Courier-Oblique",
40 "Helvetica",
41 "Helvetica-Bold",
42 "Helvetica-BoldOblique",
43 "Helvetica-Oblique",
44 "Times-Roman",
45 "Times-Bold",
46 "Times-BoldItalic",
47 "Times-Italic",
48 "Symbol",
49 "ZapfDingbats",
50};
51
52struct AltFontName {
53 const char* m_pName; // Raw, POD struct.
55};
56
57constexpr AltFontName kAltFontNames[] = {
59 {"Arial,Bold", CFX_FontMapper::kHelveticaBold},
60 {"Arial,BoldItalic", CFX_FontMapper::kHelveticaBoldOblique},
61 {"Arial,Italic", CFX_FontMapper::kHelveticaOblique},
62 {"Arial-Bold", CFX_FontMapper::kHelveticaBold},
63 {"Arial-BoldItalic", CFX_FontMapper::kHelveticaBoldOblique},
64 {"Arial-BoldItalicMT", CFX_FontMapper::kHelveticaBoldOblique},
65 {"Arial-BoldMT", CFX_FontMapper::kHelveticaBold},
66 {"Arial-Italic", CFX_FontMapper::kHelveticaOblique},
67 {"Arial-ItalicMT", CFX_FontMapper::kHelveticaOblique},
68 {"ArialBold", CFX_FontMapper::kHelveticaBold},
69 {"ArialBoldItalic", CFX_FontMapper::kHelveticaBoldOblique},
70 {"ArialItalic", CFX_FontMapper::kHelveticaOblique},
71 {"ArialMT", CFX_FontMapper::kHelvetica},
72 {"ArialMT,Bold", CFX_FontMapper::kHelveticaBold},
73 {"ArialMT,BoldItalic", CFX_FontMapper::kHelveticaBoldOblique},
74 {"ArialMT,Italic", CFX_FontMapper::kHelveticaOblique},
75 {"ArialRoundedMTBold", CFX_FontMapper::kHelveticaBold},
76 {"Courier", CFX_FontMapper::kCourier},
77 {"Courier,Bold", CFX_FontMapper::kCourierBold},
78 {"Courier,BoldItalic", CFX_FontMapper::kCourierBoldOblique},
79 {"Courier,Italic", CFX_FontMapper::kCourierOblique},
80 {"Courier-Bold", CFX_FontMapper::kCourierBold},
81 {"Courier-BoldOblique", CFX_FontMapper::kCourierBoldOblique},
82 {"Courier-Oblique", CFX_FontMapper::kCourierOblique},
83 {"CourierBold", CFX_FontMapper::kCourierBold},
84 {"CourierBoldItalic", CFX_FontMapper::kCourierBoldOblique},
85 {"CourierItalic", CFX_FontMapper::kCourierOblique},
86 {"CourierNew", CFX_FontMapper::kCourier},
87 {"CourierNew,Bold", CFX_FontMapper::kCourierBold},
88 {"CourierNew,BoldItalic", CFX_FontMapper::kCourierBoldOblique},
89 {"CourierNew,Italic", CFX_FontMapper::kCourierOblique},
90 {"CourierNew-Bold", CFX_FontMapper::kCourierBold},
91 {"CourierNew-BoldItalic", CFX_FontMapper::kCourierBoldOblique},
92 {"CourierNew-Italic", CFX_FontMapper::kCourierOblique},
93 {"CourierNewBold", CFX_FontMapper::kCourierBold},
94 {"CourierNewBoldItalic", CFX_FontMapper::kCourierBoldOblique},
95 {"CourierNewItalic", CFX_FontMapper::kCourierOblique},
96 {"CourierNewPS-BoldItalicMT", CFX_FontMapper::kCourierBoldOblique},
97 {"CourierNewPS-BoldMT", CFX_FontMapper::kCourierBold},
98 {"CourierNewPS-ItalicMT", CFX_FontMapper::kCourierOblique},
99 {"CourierNewPSMT", CFX_FontMapper::kCourier},
100 {"CourierStd", CFX_FontMapper::kCourier},
101 {"CourierStd-Bold", CFX_FontMapper::kCourierBold},
102 {"CourierStd-BoldOblique", CFX_FontMapper::kCourierBoldOblique},
103 {"CourierStd-Oblique", CFX_FontMapper::kCourierOblique},
104 {"Helvetica", CFX_FontMapper::kHelvetica},
105 {"Helvetica,Bold", CFX_FontMapper::kHelveticaBold},
106 {"Helvetica,BoldItalic", CFX_FontMapper::kHelveticaBoldOblique},
107 {"Helvetica,Italic", CFX_FontMapper::kHelveticaOblique},
108 {"Helvetica-Bold", CFX_FontMapper::kHelveticaBold},
109 {"Helvetica-BoldItalic", CFX_FontMapper::kHelveticaBoldOblique},
110 {"Helvetica-BoldOblique", CFX_FontMapper::kHelveticaBoldOblique},
111 {"Helvetica-Italic", CFX_FontMapper::kHelveticaOblique},
112 {"Helvetica-Oblique", CFX_FontMapper::kHelveticaOblique},
113 {"HelveticaBold", CFX_FontMapper::kHelveticaBold},
114 {"HelveticaBoldItalic", CFX_FontMapper::kHelveticaBoldOblique},
115 {"HelveticaItalic", CFX_FontMapper::kHelveticaOblique},
116 {"Symbol", CFX_FontMapper::kSymbol},
117 {"SymbolMT", CFX_FontMapper::kSymbol},
118 {"Times-Bold", CFX_FontMapper::kTimesBold},
119 {"Times-BoldItalic", CFX_FontMapper::kTimesBoldOblique},
120 {"Times-Italic", CFX_FontMapper::kTimesOblique},
121 {"Times-Roman", CFX_FontMapper::kTimes},
122 {"TimesBold", CFX_FontMapper::kTimesBold},
123 {"TimesBoldItalic", CFX_FontMapper::kTimesBoldOblique},
124 {"TimesItalic", CFX_FontMapper::kTimesOblique},
125 {"TimesNewRoman", CFX_FontMapper::kTimes},
126 {"TimesNewRoman,Bold", CFX_FontMapper::kTimesBold},
127 {"TimesNewRoman,BoldItalic", CFX_FontMapper::kTimesBoldOblique},
128 {"TimesNewRoman,Italic", CFX_FontMapper::kTimesOblique},
129 {"TimesNewRoman-Bold", CFX_FontMapper::kTimesBold},
130 {"TimesNewRoman-BoldItalic", CFX_FontMapper::kTimesBoldOblique},
131 {"TimesNewRoman-Italic", CFX_FontMapper::kTimesOblique},
132 {"TimesNewRomanBold", CFX_FontMapper::kTimesBold},
133 {"TimesNewRomanBoldItalic", CFX_FontMapper::kTimesBoldOblique},
134 {"TimesNewRomanItalic", CFX_FontMapper::kTimesOblique},
135 {"TimesNewRomanPS", CFX_FontMapper::kTimes},
136 {"TimesNewRomanPS-Bold", CFX_FontMapper::kTimesBold},
137 {"TimesNewRomanPS-BoldItalic", CFX_FontMapper::kTimesBoldOblique},
138 {"TimesNewRomanPS-BoldItalicMT", CFX_FontMapper::kTimesBoldOblique},
139 {"TimesNewRomanPS-BoldMT", CFX_FontMapper::kTimesBold},
140 {"TimesNewRomanPS-Italic", CFX_FontMapper::kTimesOblique},
141 {"TimesNewRomanPS-ItalicMT", CFX_FontMapper::kTimesOblique},
142 {"TimesNewRomanPSMT", CFX_FontMapper::kTimes},
143 {"TimesNewRomanPSMT,Bold", CFX_FontMapper::kTimesBold},
144 {"TimesNewRomanPSMT,BoldItalic", CFX_FontMapper::kTimesBoldOblique},
145 {"TimesNewRomanPSMT,Italic", CFX_FontMapper::kTimesOblique},
146 {"ZapfDingbats", CFX_FontMapper::kDingbats},
147};
148
149struct AltFontFamily {
150 const char* m_pFontName; // Raw, POD struct.
151 const char* m_pFontFamily; // Raw, POD struct.
152};
153
154constexpr AltFontFamily kAltFontFamilies[] = {
155 {"AGaramondPro", "Adobe Garamond Pro"},
156 {"BankGothicBT-Medium", "BankGothic Md BT"},
157 {"ForteMT", "Forte"},
158};
159
160#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || defined(OS_ASMJS)
161const char kNarrowFamily[] = "LiberationSansNarrow";
162#elif BUILDFLAG(IS_ANDROID)
163const char kNarrowFamily[] = "RobotoCondensed";
164#else
165const char kNarrowFamily[] = "ArialNarrow";
166#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || defined(OS_ASMJS)
167
168ByteString TT_NormalizeName(ByteString norm) {
169 norm.Remove(' ');
170 norm.Remove('-');
171 norm.Remove(',');
172 auto pos = norm.Find('+');
173 if (pos.has_value() && pos.value() != 0)
174 norm = norm.First(pos.value());
175 norm.MakeLower();
176 return norm;
177}
178
179void GetFontFamily(uint32_t nStyle, ByteString* fontName) {
180 if (fontName->Contains("Script")) {
181 if (FontStyleIsForceBold(nStyle))
182 *fontName = "ScriptMTBold";
183 else if (fontName->Contains("Palace"))
184 *fontName = "PalaceScriptMT";
185 else if (fontName->Contains("French"))
186 *fontName = "FrenchScriptMT";
187 else if (fontName->Contains("FreeStyle"))
188 *fontName = "FreeStyleScript";
189 return;
190 }
191 for (const auto& alternate : kAltFontFamilies) {
192 if (fontName->Contains(alternate.m_pFontName)) {
193 *fontName = alternate.m_pFontFamily;
194 return;
195 }
196 }
197}
198
199ByteString ParseStyle(const ByteString& bsStyle, size_t iStart) {
200 ByteStringView bsRegion = bsStyle.AsStringView().Substr(iStart);
201 size_t iIndex = bsRegion.Find(',').value_or(bsRegion.GetLength());
202 return ByteString(bsRegion.First(iIndex));
203}
204
205struct FX_FontStyle {
206 const char* name;
207 size_t len;
208 uint32_t style;
209};
210
211constexpr FX_FontStyle kFontStyles[] = {
212 {"Regular", 7, FXFONT_NORMAL},
213 {"Reg", 3, FXFONT_NORMAL},
214 {"BoldItalic", 10, FXFONT_FORCE_BOLD | FXFONT_ITALIC},
215 {"Italic", 6, FXFONT_ITALIC},
216 {"Bold", 4, FXFONT_FORCE_BOLD},
217};
218
219const FX_FontStyle* GetStyleType(ByteStringView font_name,
220 bool reverse_search) {
221 if (font_name.IsEmpty())
222 return nullptr;
223
224 for (const FX_FontStyle& style : kFontStyles) {
225 if (style.len > font_name.GetLength())
226 continue;
227
228 ByteStringView style_view =
229 reverse_search ? font_name.Last(style.len) : font_name.First(style.len);
230 if (style_view == style.name)
231 return &style;
232 }
233 return nullptr;
234}
235
236bool ParseStyles(const ByteString& style_str,
237 bool* is_style_available,
238 int* weight,
239 uint32_t* style) {
240 if (style_str.IsEmpty())
241 return false;
242
243 size_t i = 0;
244 bool is_first_item = true;
245 while (i < style_str.GetLength()) {
246 ByteString buf = ParseStyle(style_str, i);
247 const FX_FontStyle* style_result =
248 GetStyleType(buf.AsStringView(), /*reverse_search=*/false);
249 if ((i && !*is_style_available) || (!i && !style_result))
250 return true;
251
252 uint32_t parsed_style;
253 if (style_result) {
254 *is_style_available = true;
255 parsed_style = style_result->style;
256 } else {
257 parsed_style = FXFONT_NORMAL;
258 }
259
260 if (FontStyleIsForceBold(parsed_style)) {
261 // If we're already bold, then we're double bold, use special weight.
262 if (FontStyleIsForceBold(*style)) {
263 *weight = FXFONT_FW_BOLD_BOLD;
264 } else {
265 *weight = FXFONT_FW_BOLD;
266 *style |= FXFONT_FORCE_BOLD;
267 }
268
269 is_first_item = false;
270 }
271 if (FontStyleIsItalic(parsed_style) && FontStyleIsForceBold(parsed_style)) {
272 *style |= FXFONT_ITALIC;
273 } else if (FontStyleIsItalic(parsed_style)) {
274 if (!is_first_item)
275 return true;
276
277 *style |= FXFONT_ITALIC;
278 break;
279 }
280 i += buf.GetLength() + 1;
281 }
282 return false;
283}
284
285bool CheckSupportThirdPartFont(const ByteString& name, int* pitch_family) {
286 if (name != "MyriadPro")
287 return false;
288 *pitch_family &= ~FXFONT_FF_ROMAN;
289 return true;
290}
291
292uint32_t GetStyleFromBaseFont(int base_font) {
293 int pos = base_font % 4;
294 uint32_t style = FXFONT_NORMAL;
295 if (pos == 1 || pos == 2)
296 style |= FXFONT_FORCE_BOLD;
297 if (pos / 2)
298 style |= FXFONT_ITALIC;
299 return style;
300}
301
302int GetPitchFamilyFromBaseFont(int base_font) {
303 if (base_font < 4)
305 if (base_font >= 8)
306 return FXFONT_FF_ROMAN;
307 return 0;
308}
309
310int GetPitchFamilyFromFlags(uint32_t flags) {
311 int pitch_family = 0;
312 if (FontStyleIsSerif(flags))
313 pitch_family |= FXFONT_FF_ROMAN;
314 if (FontStyleIsScript(flags))
315 pitch_family |= FXFONT_FF_SCRIPT;
317 pitch_family |= FXFONT_FF_FIXEDPITCH;
318 return pitch_family;
319}
320
321int AdjustBaseFontForStyle(int base_font, uint32_t style) {
322 if (!style || (base_font % 4))
323 return base_font;
324
326 base_font += 2;
327 else if (FontStyleIsForceBold(style))
328 base_font += 1;
329 else if (FontStyleIsItalic(style))
330 base_font += 3;
331 return base_font;
332}
333
334FX_Charset GetCharset(FX_CodePage code_page, int base_font, uint32_t flags) {
335 if (code_page != FX_CodePage::kDefANSI)
336 return FX_GetCharsetFromCodePage(code_page);
337 if (FontStyleIsSymbolic(flags) &&
339 return FX_Charset::kSymbol;
340 }
341 return FX_Charset::kANSI;
342}
343
344bool IsStrUpper(const ByteString& str) {
345 for (size_t i = 0; i < str.GetLength(); ++i) {
346 if (!FXSYS_IsUpperASCII(str[i]))
347 return false;
348 }
349 return true;
350}
351
352void RemoveSubsettedFontPrefix(ByteString* subst_name) {
353 constexpr size_t kPrefixLength = 6;
354 if (subst_name->GetLength() > kPrefixLength &&
355 (*subst_name)[kPrefixLength] == '+' &&
356 IsStrUpper(subst_name->First(kPrefixLength))) {
357 *subst_name =
358 subst_name->Last(subst_name->GetLength() - (kPrefixLength + 1));
359 }
360}
361
362ByteString GetSubstName(const ByteString& name, bool is_truetype) {
363 ByteString subst_name = name;
364 if (is_truetype && name.Front() == '@')
365 subst_name.Delete(0);
366 else
367 subst_name.Remove(' ');
368 RemoveSubsettedFontPrefix(&subst_name);
369 CFX_FontMapper::GetStandardFontName(&subst_name);
370 return subst_name;
371}
372
373bool IsNarrowFontName(const ByteString& name) {
374 static const char kNarrowFonts[][10] = {"Narrow", "Condensed"};
375 for (const char* font : kNarrowFonts) {
376 absl::optional<size_t> pos = name.Find(font);
377 if (pos.has_value() && pos.value() != 0)
378 return true;
379 }
380 return false;
381}
382
383class ScopedFontDeleter {
384 public:
385 FX_STACK_ALLOCATED();
386
387 ScopedFontDeleter(SystemFontInfoIface* font_info, void* font)
388 : font_info_(font_info), font_(font) {}
389 ~ScopedFontDeleter() { font_info_->DeleteFont(font_); }
390
391 private:
392 UnownedPtr<SystemFontInfoIface> const font_info_;
393 UNOWNED_PTR_EXCLUSION void* const font_; // void type incompatible.
394};
395
396} // namespace
397
399
400CFX_FontMapper::~CFX_FontMapper() = default;
401
403 std::unique_ptr<SystemFontInfoIface> pFontInfo) {
404 if (!pFontInfo)
405 return;
406
407 m_bListLoaded = false;
408 m_pFontInfo = std::move(pFontInfo);
409}
410
412 return std::move(m_pFontInfo);
413}
414
415uint32_t CFX_FontMapper::GetChecksumFromTT(void* font_handle) {
416 uint32_t buffer[256];
417 m_pFontInfo->GetFontData(
418 font_handle, kTableTTCF,
419 pdfium::as_writable_bytes(pdfium::make_span(buffer)));
420
421 uint32_t checksum = 0;
422 for (auto x : buffer)
423 checksum += x;
424
425 return checksum;
426}
427
428ByteString CFX_FontMapper::GetPSNameFromTT(void* font_handle) {
429 size_t size = m_pFontInfo->GetFontData(font_handle, kTableNAME, {});
430 if (!size)
431 return ByteString();
432
433 DataVector<uint8_t> buffer(size);
434 size_t bytes_read = m_pFontInfo->GetFontData(font_handle, kTableNAME, buffer);
435 return bytes_read == size ? GetNameFromTT(buffer, 6) : ByteString();
436}
437
438void CFX_FontMapper::AddInstalledFont(const ByteString& name,
439 FX_Charset charset) {
440 if (!m_pFontInfo)
441 return;
442
443 m_FaceArray.push_back({name, static_cast<uint32_t>(charset)});
444 if (name == m_LastFamily)
445 return;
446
447 bool is_localized = std::any_of(name.begin(), name.end(), [](const char& c) {
448 return static_cast<uint8_t>(c) > 0x80;
449 });
450
451 if (is_localized) {
452 void* font_handle = m_pFontInfo->GetFont(name);
453 if (!font_handle) {
454 font_handle =
455 m_pFontInfo->MapFont(0, false, FX_Charset::kDefault, 0, name);
456 if (!font_handle)
457 return;
458 }
459
460 ScopedFontDeleter scoped_font(m_pFontInfo.get(), font_handle);
461 ByteString new_name = GetPSNameFromTT(font_handle);
462 if (!new_name.IsEmpty())
463 m_LocalizedTTFonts.emplace_back(new_name, name);
464 }
465 m_InstalledTTFonts.push_back(name);
466 m_LastFamily = name;
467}
468
470 if (!m_pFontInfo || m_bListLoaded)
471 return;
472
473 m_pFontInfo->EnumFontList(this);
474 m_bListLoaded = true;
475}
476
477ByteString CFX_FontMapper::MatchInstalledFonts(const ByteString& norm_name) {
479 for (const ByteString& font : pdfium::base::Reversed(m_InstalledTTFonts)) {
480 if (TT_NormalizeName(font) == norm_name) {
481 return font;
482 }
483 }
484 for (const auto& font_data : pdfium::base::Reversed(m_LocalizedTTFonts)) {
485 if (TT_NormalizeName(font_data.first) == norm_name) {
486 return font_data.second;
487 }
488 }
489 return ByteString();
490}
491
492RetainPtr<CFX_Face> CFX_FontMapper::UseInternalSubst(
493 int base_font,
494 int weight,
495 int italic_angle,
496 int pitch_family,
497 CFX_SubstFont* subst_font) {
498 if (base_font < kNumStandardFonts) {
499 if (!m_StandardFaces[base_font]) {
500 m_StandardFaces[base_font] = m_pFontMgr->NewFixedFace(
501 nullptr, m_pFontMgr->GetStandardFont(base_font), 0);
502 }
503 return m_StandardFaces[base_font];
504 }
505
507 subst_font->m_ItalicAngle = italic_angle;
508 if (weight)
509 subst_font->m_Weight = weight;
510 if (FontFamilyIsRoman(pitch_family)) {
511 subst_font->UseChromeSerif();
512 if (!m_GenericSerifFace) {
513 m_GenericSerifFace = m_pFontMgr->NewFixedFace(
514 nullptr, m_pFontMgr->GetGenericSerifFont(), 0);
515 }
516 return m_GenericSerifFace;
517 }
518 subst_font->m_Family = "Chrome Sans";
519 if (!m_GenericSansFace) {
520 m_GenericSansFace =
521 m_pFontMgr->NewFixedFace(nullptr, m_pFontMgr->GetGenericSansFont(), 0);
522 }
523 return m_GenericSansFace;
524}
525
526RetainPtr<CFX_Face> CFX_FontMapper::UseExternalSubst(
527 void* font_handle,
528 ByteString face_name,
529 int weight,
530 bool is_italic,
531 int italic_angle,
532 FX_Charset charset,
533 CFX_SubstFont* subst_font) {
534 DCHECK(font_handle);
535
536 ScopedFontDeleter scoped_font(m_pFontInfo.get(), font_handle);
537 m_pFontInfo->GetFaceName(font_handle, &face_name);
538 if (charset == FX_Charset::kDefault)
539 m_pFontInfo->GetFontCharset(font_handle, &charset);
540 size_t ttc_size = m_pFontInfo->GetFontData(font_handle, kTableTTCF, {});
541 size_t font_size = m_pFontInfo->GetFontData(font_handle, 0, {});
542 if (font_size == 0 && ttc_size == 0)
543 return nullptr;
544
545 RetainPtr<CFX_Face> face =
546 ttc_size
547 ? GetCachedTTCFace(font_handle, ttc_size, font_size)
548 : GetCachedFace(font_handle, face_name, weight, is_italic, font_size);
549 if (!face)
550 return nullptr;
551
552 subst_font->m_Family = face_name;
553 subst_font->m_Charset = charset;
554 int face_weight = face->IsBold() ? FXFONT_FW_BOLD : FXFONT_FW_NORMAL;
555 if (weight != face_weight)
556 subst_font->m_Weight = weight;
557 if (is_italic && !face->IsItalic()) {
558 if (italic_angle == 0)
559 italic_angle = -12;
560 else if (abs(italic_angle) < 5)
561 italic_angle = 0;
562 subst_font->m_ItalicAngle = italic_angle;
563 }
564 return face;
565}
566
567RetainPtr<CFX_Face> CFX_FontMapper::FindSubstFont(const ByteString& name,
568 bool is_truetype,
569 uint32_t flags,
570 int weight,
571 int italic_angle,
572 FX_CodePage code_page,
573 CFX_SubstFont* subst_font) {
574 if (weight == 0)
575 weight = FXFONT_FW_NORMAL;
576
577 if (!(flags & FXFONT_USEEXTERNATTR)) {
578 weight = FXFONT_FW_NORMAL;
579 italic_angle = 0;
580 }
581 const ByteString subst_name = GetSubstName(name, is_truetype);
582 if (subst_name == "Symbol" && !is_truetype) {
583 subst_font->m_Family = "Chrome Symbol";
585 return UseInternalSubst(kSymbol, weight, italic_angle, 0, subst_font);
586 }
587 if (subst_name == "ZapfDingbats") {
588 subst_font->m_Family = "Chrome Dingbats";
590 return UseInternalSubst(kDingbats, weight, italic_angle, 0, subst_font);
591 }
592 int base_font = 0;
593 ByteString family;
594 ByteString style;
595 bool has_comma = false;
596 bool has_hyphen = false;
597 {
598 absl::optional<size_t> pos = subst_name.Find(",");
599 if (pos.has_value()) {
600 family = subst_name.First(pos.value());
601 GetStandardFontName(&family);
602 style = subst_name.Last(subst_name.GetLength() - (pos.value() + 1));
603 has_comma = true;
604 } else {
605 family = subst_name;
606 }
607 }
608 for (; base_font < kSymbol; base_font++) {
609 if (family == kBase14FontNames[base_font])
610 break;
611 }
612 int pitch_family;
613 uint32_t nStyle;
614 bool is_style_available = false;
615 if (base_font < kSymbol) {
616 nStyle = GetStyleFromBaseFont(base_font);
617 pitch_family = GetPitchFamilyFromBaseFont(base_font);
618 } else {
619 base_font = kNumStandardFonts;
620 nStyle = FXFONT_NORMAL;
621 if (!has_comma) {
622 absl::optional<size_t> pos = family.ReverseFind('-');
623 if (pos.has_value()) {
624 style = family.Last(family.GetLength() - (pos.value() + 1));
625 family = family.First(pos.value());
626 has_hyphen = true;
627 }
628 }
629 if (!has_hyphen) {
630 size_t nLen = family.GetLength();
631 const FX_FontStyle* style_result =
632 GetStyleType(family.AsStringView(), /*reverse_search=*/true);
633 if (style_result) {
634 family = family.First(nLen - style_result->len);
635 nStyle |= style_result->style;
636 }
637 }
638 pitch_family = GetPitchFamilyFromFlags(flags);
639 }
640
641 const int old_weight = weight;
642 if (FontStyleIsForceBold(nStyle))
643 weight = FXFONT_FW_BOLD;
644
645 if (ParseStyles(style, &is_style_available, &weight, &nStyle)) {
646 family = subst_name;
647 base_font = kNumStandardFonts;
648 }
649
650 if (!m_pFontInfo) {
651 return UseInternalSubst(base_font, old_weight, italic_angle, pitch_family,
652 subst_font);
653 }
654
655 const FX_Charset Charset = GetCharset(code_page, base_font, flags);
656 const bool is_cjk = FX_CharSetIsCJK(Charset);
657 bool is_italic = FontStyleIsItalic(nStyle);
658
659 GetFontFamily(nStyle, &family);
660 ByteString match = MatchInstalledFonts(TT_NormalizeName(family));
661 if (match.IsEmpty() && family != subst_name &&
662 (!has_comma && (!has_hyphen || (has_hyphen && !is_style_available)))) {
663 match = MatchInstalledFonts(TT_NormalizeName(subst_name));
664 }
665 if (match.IsEmpty() && base_font >= kNumStandardFonts) {
666 if (!is_cjk) {
667 if (!CheckSupportThirdPartFont(family, &pitch_family)) {
668 is_italic = italic_angle != 0;
669 weight = old_weight;
670 }
671 if (IsNarrowFontName(subst_name))
672 family = kNarrowFamily;
673 } else {
674 subst_font->m_bSubstCJK = true;
675 if (nStyle)
676 subst_font->m_WeightCJK = nStyle ? weight : FXFONT_FW_NORMAL;
677 if (FontStyleIsItalic(nStyle))
678 subst_font->m_bItalicCJK = true;
679 }
680 } else {
681 italic_angle = 0;
682 if (nStyle == FXFONT_NORMAL)
683 weight = FXFONT_FW_NORMAL;
684 }
685
686 if (!match.IsEmpty() || base_font < kNumStandardFonts) {
687 if (!match.IsEmpty())
688 family = match;
689 if (base_font < kNumStandardFonts) {
690 base_font = AdjustBaseFontForStyle(base_font, nStyle);
691 family = kBase14FontNames[base_font];
692 }
693 } else if (FontStyleIsItalic(flags)) {
694 is_italic = true;
695 }
696 void* font_handle =
697 m_pFontInfo->MapFont(weight, is_italic, Charset, pitch_family, family);
698 if (font_handle) {
699 return UseExternalSubst(font_handle, subst_name, weight, is_italic,
700 italic_angle, Charset, subst_font);
701 }
702
703 if (is_cjk) {
704 is_italic = italic_angle != 0;
705 weight = old_weight;
706 }
707 if (!match.IsEmpty()) {
708 font_handle = m_pFontInfo->GetFont(match);
709 if (!font_handle) {
710 return UseInternalSubst(base_font, old_weight, italic_angle, pitch_family,
711 subst_font);
712 }
713 return UseExternalSubst(font_handle, subst_name, weight, is_italic,
714 italic_angle, Charset, subst_font);
715 }
716
717 if (Charset == FX_Charset::kSymbol) {
718#if !BUILDFLAG(IS_WIN)
719 if (subst_name == "Symbol") {
720 subst_font->m_Family = "Chrome Symbol";
721 subst_font->m_Charset = FX_Charset::kSymbol;
722 return UseInternalSubst(kSymbol, old_weight, italic_angle, pitch_family,
723 subst_font);
724 }
725#endif
726 return FindSubstFont(family, is_truetype, flags & ~FXFONT_SYMBOLIC, weight,
727 italic_angle, FX_CodePage::kDefANSI, subst_font);
728 }
729
730 if (Charset == FX_Charset::kANSI) {
731 return UseInternalSubst(base_font, old_weight, italic_angle, pitch_family,
732 subst_font);
733 }
734
735 auto it = std::find_if(
736 m_FaceArray.begin(), m_FaceArray.end(), [Charset](const FaceData& face) {
737 return face.charset == static_cast<uint32_t>(Charset);
738 });
739 if (it == m_FaceArray.end()) {
740 return UseInternalSubst(base_font, old_weight, italic_angle, pitch_family,
741 subst_font);
742 }
743 font_handle = m_pFontInfo->GetFont(it->name);
744 if (!font_handle)
745 return nullptr;
746 return UseExternalSubst(font_handle, subst_name, weight, is_italic,
747 italic_angle, Charset, subst_font);
748}
749
751 return m_FaceArray.size();
752}
753
754ByteString CFX_FontMapper::GetFaceName(size_t index) const {
755 CHECK_LT(index, m_FaceArray.size());
756 return m_FaceArray[index].name;
757}
758
759bool CFX_FontMapper::HasInstalledFont(ByteStringView name) const {
760 for (const auto& font : m_InstalledTTFonts) {
761 if (font == name)
762 return true;
763 }
764 return false;
765}
766
767bool CFX_FontMapper::HasLocalizedFont(ByteStringView name) const {
768 for (const auto& fontPair : m_LocalizedTTFonts) {
769 if (fontPair.first == name)
770 return true;
771 }
772 return false;
773}
774
775#if BUILDFLAG(IS_WIN)
776absl::optional<ByteString> CFX_FontMapper::InstalledFontNameStartingWith(
777 const ByteString& name) const {
778 for (const auto& thisname : m_InstalledTTFonts) {
779 if (thisname.First(name.GetLength()) == name)
780 return thisname;
781 }
782 return absl::nullopt;
783}
784
785absl::optional<ByteString> CFX_FontMapper::LocalizedFontNameStartingWith(
786 const ByteString& name) const {
787 for (const auto& thispair : m_LocalizedTTFonts) {
788 if (thispair.first.First(name.GetLength()) == name)
789 return thispair.second;
790 }
791 return absl::nullopt;
792}
793#endif // BUILDFLAG(IS_WIN)
794
795#ifdef PDF_ENABLE_XFA
796FixedSizeDataVector<uint8_t> CFX_FontMapper::RawBytesForIndex(size_t index) {
797 CHECK_LT(index, m_FaceArray.size());
798
799 void* font_handle = m_pFontInfo->MapFont(0, false, FX_Charset::kDefault, 0,
800 GetFaceName(index));
801 if (!font_handle) {
802 return FixedSizeDataVector<uint8_t>();
803 }
804 ScopedFontDeleter scoped_font(m_pFontInfo.get(), font_handle);
805 size_t required_size = m_pFontInfo->GetFontData(font_handle, 0, {});
806 if (required_size == 0) {
807 return FixedSizeDataVector<uint8_t>();
808 }
809 auto result = FixedSizeDataVector<uint8_t>::Uninit(required_size);
810 size_t actual_size = m_pFontInfo->GetFontData(font_handle, 0, result.span());
811 if (actual_size != required_size) {
812 return FixedSizeDataVector<uint8_t>();
813 }
814 return result;
815}
816#endif // PDF_ENABLE_XFA
817
818RetainPtr<CFX_Face> CFX_FontMapper::GetCachedTTCFace(void* font_handle,
819 size_t ttc_size,
820 size_t data_size) {
821 CHECK_GE(ttc_size, data_size);
822 uint32_t checksum = GetChecksumFromTT(font_handle);
823 RetainPtr<CFX_FontMgr::FontDesc> pFontDesc =
824 m_pFontMgr->GetCachedTTCFontDesc(ttc_size, checksum);
825 if (!pFontDesc) {
826 auto font_data = FixedSizeDataVector<uint8_t>::Uninit(ttc_size);
827 size_t size =
828 m_pFontInfo->GetFontData(font_handle, kTableTTCF, font_data.span());
829 if (size != ttc_size)
830 return nullptr;
831
832 pFontDesc = m_pFontMgr->AddCachedTTCFontDesc(ttc_size, checksum,
833 std::move(font_data));
834 }
835 size_t font_offset = ttc_size - data_size;
836 size_t face_index =
837 GetTTCIndex(pFontDesc->FontData().first(ttc_size), font_offset);
838 RetainPtr<CFX_Face> pFace(pFontDesc->GetFace(face_index));
839 if (pFace)
840 return pFace;
841
842 pFace = m_pFontMgr->NewFixedFace(
843 pFontDesc, pFontDesc->FontData().first(ttc_size), face_index);
844 if (!pFace)
845 return nullptr;
846
847 pFontDesc->SetFace(face_index, pFace.Get());
848 return pFace;
849}
850
851RetainPtr<CFX_Face> CFX_FontMapper::GetCachedFace(void* font_handle,
852 ByteString subst_name,
853 int weight,
854 bool is_italic,
855 size_t data_size) {
856 RetainPtr<CFX_FontMgr::FontDesc> pFontDesc =
857 m_pFontMgr->GetCachedFontDesc(subst_name, weight, is_italic);
858 if (!pFontDesc) {
859 auto font_data = FixedSizeDataVector<uint8_t>::Uninit(data_size);
860 size_t size = m_pFontInfo->GetFontData(font_handle, 0, font_data.span());
861 if (size != data_size)
862 return nullptr;
863
864 pFontDesc = m_pFontMgr->AddCachedFontDesc(subst_name, weight, is_italic,
865 std::move(font_data));
866 }
867 RetainPtr<CFX_Face> pFace(pFontDesc->GetFace(0));
868 if (pFace)
869 return pFace;
870
871 pFace = m_pFontMgr->NewFixedFace(pFontDesc,
872 pFontDesc->FontData().first(data_size), 0);
873 if (!pFace)
874 return nullptr;
875
876 pFontDesc->SetFace(0, pFace.Get());
877 return pFace;
878}
879
880// static
881absl::optional<CFX_FontMapper::StandardFont>
882CFX_FontMapper::GetStandardFontName(ByteString* name) {
883 const auto* end = std::end(kAltFontNames);
884 const auto* found =
885 std::lower_bound(std::begin(kAltFontNames), end, name->c_str(),
886 [](const AltFontName& element, const char* name) {
887 return FXSYS_stricmp(element.m_pName, name) < 0;
888 });
889 if (found == end || FXSYS_stricmp(found->m_pName, name->c_str()))
890 return absl::nullopt;
891
892 *name = kBase14FontNames[static_cast<size_t>(found->m_Index)];
893 return found->m_Index;
894}
895
896// static
897bool CFX_FontMapper::IsStandardFontName(const ByteString& name) {
898 return pdfium::Contains(kBase14FontNames, name);
899}
900
901// static
905
906// static
void AddInstalledFont(const ByteString &name, FX_Charset charset)
void SetSystemFontInfo(std::unique_ptr< SystemFontInfoIface > pFontInfo)
bool HasLocalizedFont(ByteStringView name) const
static bool IsFixedFont(StandardFont font)
static constexpr int kNumStandardFonts
bool HasInstalledFont(ByteStringView name) const
CFX_FontMapper(CFX_FontMgr *mgr)
size_t GetFaceSize() const
static bool IsStandardFontName(const ByteString &name)
ByteString GetFaceName(size_t index) const
static bool IsSymbolicFont(StandardFont font)
RetainPtr< CFX_Face > FindSubstFont(const ByteString &face_name, bool is_truetype, uint32_t flags, int weight, int italic_angle, FX_CodePage code_page, CFX_SubstFont *subst_font)
std::unique_ptr< SystemFontInfoIface > TakeSystemFontInfo()
void SetIsBuiltInGenericFont()
FX_Charset m_Charset
const_iterator begin() const
Definition bytestring.h:102
bool operator==(const char *ptr) const
const_iterator end() const
Definition bytestring.h:103
bool operator!=(const ByteString &other) const
Definition bytestring.h:132
ByteString & operator=(const char *str)
const char * c_str() const
Definition bytestring.h:76
ByteString & operator=(const ByteString &that)
ByteString & operator=(ByteString &&that) noexcept
bool IsEmpty() const
Definition bytestring.h:119
CharType Front() const
Definition bytestring.h:155
bool operator!=(const char *ptr) const
Definition bytestring.h:130
FX_Charset
Definition fx_codepage.h:70
FX_Charset FX_GetCharsetFromCodePage(FX_CodePage codepage)
FX_CodePage
Definition fx_codepage.h:18
bool FX_CharSetIsCJK(FX_Charset uCharset)
#define FXFONT_FW_BOLD
Definition fx_font.h:23
#define FXFONT_USEEXTERNATTR
Definition fx_font.h:39
#define FXFONT_FF_SCRIPT
Definition fx_font.h:19
#define FXFONT_FF_ROMAN
Definition fx_font.h:18
#define FXFONT_NORMAL
Definition fx_font.h:27
bool FontStyleIsSerif(uint32_t style)
Definition fx_font.h:77
bool FontStyleIsFixedPitch(uint32_t style)
Definition fx_font.h:65
#define FXFONT_FW_NORMAL
Definition fx_font.h:22
#define FXFONT_ITALIC
Definition fx_font.h:33
bool FontStyleIsSymbolic(uint32_t style)
Definition fx_font.h:68
bool FontFamilyIsRoman(uint32_t family)
Definition fx_font.h:87
#define FXFONT_SYMBOLIC
Definition fx_font.h:30
bool FontStyleIsItalic(uint32_t style)
Definition fx_font.h:62
#define FXFONT_FORCE_BOLD
Definition fx_font.h:36
#define FXFONT_FW_BOLD_BOLD
Definition fx_font.h:24
#define FXFONT_FF_FIXEDPITCH
Definition fx_font.h:17
bool FontStyleIsScript(uint32_t style)
Definition fx_font.h:80
bool FontStyleIsForceBold(uint32_t style)
Definition fx_font.h:59
int FXSYS_stricmp(const char *str1, const char *str2)
#define UNOWNED_PTR_EXCLUSION