1 // Copyright 2017 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 "xfa/fgas/font/cfgas_defaultfontmanager.h"
8
9 #include "core/fxcrt/compiler_specific.h"
10 #include "core/fxcrt/fx_codepage.h"
11 #include "core/fxcrt/numerics/safe_conversions.h"
12 #include "core/fxge/fx_font.h"
13 #include "xfa/fgas/font/cfgas_fontmgr.h"
14 #include "xfa/fgas/font/cfgas_gefont.h"
15 #include "xfa/fgas/font/cfgas_gemodule.h"
16 #include "xfa/fgas/font/fgas_fontutils.h"
17
18 // static
GetFont(WideString wsFontName,uint32_t dwFontStyles)19 RetainPtr<CFGAS_GEFont> CFGAS_DefaultFontManager::GetFont(
20 WideString wsFontName,
21 uint32_t dwFontStyles) {
22 CFGAS_FontMgr* pFontMgr = CFGAS_GEModule::Get()->GetFontMgr();
23 RetainPtr<CFGAS_GEFont> pFont = pFontMgr->LoadFont(
24 wsFontName.c_str(), dwFontStyles, FX_CodePage::kFailure);
25 if (pFont) {
26 return pFont;
27 }
28 const FGAS_FontInfo* pCurFont =
29 FGAS_FontInfoByFontName(wsFontName.AsStringView());
30 if (!pCurFont || !pCurFont->pReplaceFont) {
31 return nullptr;
32 }
33 uint32_t dwStyle = 0;
34 // TODO(dsinclair): Why doesn't this check the other flags?
35 if (FontStyleIsForceBold(dwFontStyles)) {
36 dwStyle |= FXFONT_FORCE_BOLD;
37 }
38 if (FontStyleIsItalic(dwFontStyles)) {
39 dwStyle |= FXFONT_ITALIC;
40 }
41 ByteStringView replace_view(pCurFont->pReplaceFont);
42 while (!replace_view.IsEmpty()) {
43 ByteStringView segment;
44 auto found = replace_view.Find(',');
45 if (found.has_value()) {
46 segment = replace_view.First(found.value());
47 replace_view = replace_view.Substr(found.value() + 1);
48 } else {
49 segment = replace_view;
50 replace_view = ByteStringView();
51 }
52 pFont = pFontMgr->LoadFont(WideString::FromASCII(segment).c_str(), dwStyle,
53 FX_CodePage::kFailure);
54 if (pFont) {
55 return pFont;
56 }
57 }
58 return nullptr;
59 }
60
61 // static
GetDefaultFont(uint32_t dwFontStyles)62 RetainPtr<CFGAS_GEFont> CFGAS_DefaultFontManager::GetDefaultFont(
63 uint32_t dwFontStyles) {
64 CFGAS_FontMgr* pFontMgr = CFGAS_GEModule::Get()->GetFontMgr();
65 RetainPtr<CFGAS_GEFont> pFont =
66 pFontMgr->LoadFont(L"Arial Narrow", dwFontStyles, FX_CodePage::kFailure);
67 if (pFont)
68 return pFont;
69
70 return pFontMgr->LoadFont(nullptr, dwFontStyles, FX_CodePage::kFailure);
71 }
72