1*3ac0a46fSAndroid Build Coastguard Worker // Copyright 2017 The PDFium Authors
2*3ac0a46fSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*3ac0a46fSAndroid Build Coastguard Worker // found in the LICENSE file.
4*3ac0a46fSAndroid Build Coastguard Worker
5*3ac0a46fSAndroid Build Coastguard Worker // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6*3ac0a46fSAndroid Build Coastguard Worker
7*3ac0a46fSAndroid Build Coastguard Worker #include "xfa/fgas/font/cfgas_defaultfontmanager.h"
8*3ac0a46fSAndroid Build Coastguard Worker
9*3ac0a46fSAndroid Build Coastguard Worker #include "core/fxcrt/fx_codepage.h"
10*3ac0a46fSAndroid Build Coastguard Worker #include "core/fxge/fx_font.h"
11*3ac0a46fSAndroid Build Coastguard Worker #include "third_party/base/numerics/safe_conversions.h"
12*3ac0a46fSAndroid Build Coastguard Worker #include "xfa/fgas/font/cfgas_fontmgr.h"
13*3ac0a46fSAndroid Build Coastguard Worker #include "xfa/fgas/font/cfgas_gefont.h"
14*3ac0a46fSAndroid Build Coastguard Worker #include "xfa/fgas/font/cfgas_gemodule.h"
15*3ac0a46fSAndroid Build Coastguard Worker #include "xfa/fgas/font/fgas_fontutils.h"
16*3ac0a46fSAndroid Build Coastguard Worker
17*3ac0a46fSAndroid Build Coastguard Worker // static
GetFont(WideString wsFontName,uint32_t dwFontStyles)18*3ac0a46fSAndroid Build Coastguard Worker RetainPtr<CFGAS_GEFont> CFGAS_DefaultFontManager::GetFont(
19*3ac0a46fSAndroid Build Coastguard Worker WideString wsFontName,
20*3ac0a46fSAndroid Build Coastguard Worker uint32_t dwFontStyles) {
21*3ac0a46fSAndroid Build Coastguard Worker CFGAS_FontMgr* pFontMgr = CFGAS_GEModule::Get()->GetFontMgr();
22*3ac0a46fSAndroid Build Coastguard Worker RetainPtr<CFGAS_GEFont> pFont = pFontMgr->LoadFont(
23*3ac0a46fSAndroid Build Coastguard Worker wsFontName.c_str(), dwFontStyles, FX_CodePage::kFailure);
24*3ac0a46fSAndroid Build Coastguard Worker if (pFont)
25*3ac0a46fSAndroid Build Coastguard Worker return pFont;
26*3ac0a46fSAndroid Build Coastguard Worker
27*3ac0a46fSAndroid Build Coastguard Worker const FGAS_FontInfo* pCurFont =
28*3ac0a46fSAndroid Build Coastguard Worker FGAS_FontInfoByFontName(wsFontName.AsStringView());
29*3ac0a46fSAndroid Build Coastguard Worker if (!pCurFont || !pCurFont->pReplaceFont)
30*3ac0a46fSAndroid Build Coastguard Worker return pFont;
31*3ac0a46fSAndroid Build Coastguard Worker
32*3ac0a46fSAndroid Build Coastguard Worker uint32_t dwStyle = 0;
33*3ac0a46fSAndroid Build Coastguard Worker // TODO(dsinclair): Why doesn't this check the other flags?
34*3ac0a46fSAndroid Build Coastguard Worker if (FontStyleIsForceBold(dwFontStyles))
35*3ac0a46fSAndroid Build Coastguard Worker dwStyle |= FXFONT_FORCE_BOLD;
36*3ac0a46fSAndroid Build Coastguard Worker if (FontStyleIsItalic(dwFontStyles))
37*3ac0a46fSAndroid Build Coastguard Worker dwStyle |= FXFONT_ITALIC;
38*3ac0a46fSAndroid Build Coastguard Worker
39*3ac0a46fSAndroid Build Coastguard Worker const char* pReplace = pCurFont->pReplaceFont;
40*3ac0a46fSAndroid Build Coastguard Worker int32_t iLength = pdfium::base::checked_cast<int32_t>(strlen(pReplace));
41*3ac0a46fSAndroid Build Coastguard Worker while (iLength > 0) {
42*3ac0a46fSAndroid Build Coastguard Worker const char* pNameText = pReplace;
43*3ac0a46fSAndroid Build Coastguard Worker while (*pNameText != ',' && iLength > 0) {
44*3ac0a46fSAndroid Build Coastguard Worker pNameText++;
45*3ac0a46fSAndroid Build Coastguard Worker iLength--;
46*3ac0a46fSAndroid Build Coastguard Worker }
47*3ac0a46fSAndroid Build Coastguard Worker WideString wsReplace =
48*3ac0a46fSAndroid Build Coastguard Worker WideString::FromASCII(ByteStringView(pReplace, pNameText - pReplace));
49*3ac0a46fSAndroid Build Coastguard Worker pFont =
50*3ac0a46fSAndroid Build Coastguard Worker pFontMgr->LoadFont(wsReplace.c_str(), dwStyle, FX_CodePage::kFailure);
51*3ac0a46fSAndroid Build Coastguard Worker if (pFont)
52*3ac0a46fSAndroid Build Coastguard Worker break;
53*3ac0a46fSAndroid Build Coastguard Worker
54*3ac0a46fSAndroid Build Coastguard Worker iLength--;
55*3ac0a46fSAndroid Build Coastguard Worker pNameText++;
56*3ac0a46fSAndroid Build Coastguard Worker pReplace = pNameText;
57*3ac0a46fSAndroid Build Coastguard Worker }
58*3ac0a46fSAndroid Build Coastguard Worker return pFont;
59*3ac0a46fSAndroid Build Coastguard Worker }
60*3ac0a46fSAndroid Build Coastguard Worker
61*3ac0a46fSAndroid Build Coastguard Worker // static
GetDefaultFont(uint32_t dwFontStyles)62*3ac0a46fSAndroid Build Coastguard Worker RetainPtr<CFGAS_GEFont> CFGAS_DefaultFontManager::GetDefaultFont(
63*3ac0a46fSAndroid Build Coastguard Worker uint32_t dwFontStyles) {
64*3ac0a46fSAndroid Build Coastguard Worker CFGAS_FontMgr* pFontMgr = CFGAS_GEModule::Get()->GetFontMgr();
65*3ac0a46fSAndroid Build Coastguard Worker RetainPtr<CFGAS_GEFont> pFont =
66*3ac0a46fSAndroid Build Coastguard Worker pFontMgr->LoadFont(L"Arial Narrow", dwFontStyles, FX_CodePage::kFailure);
67*3ac0a46fSAndroid Build Coastguard Worker if (pFont)
68*3ac0a46fSAndroid Build Coastguard Worker return pFont;
69*3ac0a46fSAndroid Build Coastguard Worker
70*3ac0a46fSAndroid Build Coastguard Worker return pFontMgr->LoadFont(nullptr, dwFontStyles, FX_CodePage::kFailure);
71*3ac0a46fSAndroid Build Coastguard Worker }
72