xref: /aosp_15_r20/external/pdfium/xfa/fgas/font/cfgas_defaultfontmanager.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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/fx_codepage.h"
10 #include "core/fxge/fx_font.h"
11 #include "third_party/base/numerics/safe_conversions.h"
12 #include "xfa/fgas/font/cfgas_fontmgr.h"
13 #include "xfa/fgas/font/cfgas_gefont.h"
14 #include "xfa/fgas/font/cfgas_gemodule.h"
15 #include "xfa/fgas/font/fgas_fontutils.h"
16 
17 // static
GetFont(WideString wsFontName,uint32_t dwFontStyles)18 RetainPtr<CFGAS_GEFont> CFGAS_DefaultFontManager::GetFont(
19     WideString wsFontName,
20     uint32_t dwFontStyles) {
21   CFGAS_FontMgr* pFontMgr = CFGAS_GEModule::Get()->GetFontMgr();
22   RetainPtr<CFGAS_GEFont> pFont = pFontMgr->LoadFont(
23       wsFontName.c_str(), dwFontStyles, FX_CodePage::kFailure);
24   if (pFont)
25     return pFont;
26 
27   const FGAS_FontInfo* pCurFont =
28       FGAS_FontInfoByFontName(wsFontName.AsStringView());
29   if (!pCurFont || !pCurFont->pReplaceFont)
30     return pFont;
31 
32   uint32_t dwStyle = 0;
33   // TODO(dsinclair): Why doesn't this check the other flags?
34   if (FontStyleIsForceBold(dwFontStyles))
35     dwStyle |= FXFONT_FORCE_BOLD;
36   if (FontStyleIsItalic(dwFontStyles))
37     dwStyle |= FXFONT_ITALIC;
38 
39   const char* pReplace = pCurFont->pReplaceFont;
40   int32_t iLength = pdfium::base::checked_cast<int32_t>(strlen(pReplace));
41   while (iLength > 0) {
42     const char* pNameText = pReplace;
43     while (*pNameText != ',' && iLength > 0) {
44       pNameText++;
45       iLength--;
46     }
47     WideString wsReplace =
48         WideString::FromASCII(ByteStringView(pReplace, pNameText - pReplace));
49     pFont =
50         pFontMgr->LoadFont(wsReplace.c_str(), dwStyle, FX_CodePage::kFailure);
51     if (pFont)
52       break;
53 
54     iLength--;
55     pNameText++;
56     pReplace = pNameText;
57   }
58   return pFont;
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