1 // Copyright 2017 PDFium Authors. All rights reserved.
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/fxge/fx_font.h"
10 #include "xfa/fgas/font/cfgas_gefont.h"
11 #include "xfa/fgas/font/fgas_fontutils.h"
12
13 // static
GetFont(CFGAS_FontMgr * pFontMgr,WideStringView wsFontFamily,uint32_t dwFontStyles)14 RetainPtr<CFGAS_GEFont> CFGAS_DefaultFontManager::GetFont(
15 CFGAS_FontMgr* pFontMgr,
16 WideStringView wsFontFamily,
17 uint32_t dwFontStyles) {
18 WideString wsFontName(wsFontFamily);
19 RetainPtr<CFGAS_GEFont> pFont =
20 pFontMgr->LoadFont(wsFontName.c_str(), dwFontStyles, 0xFFFF);
21 if (pFont)
22 return pFont;
23
24 const FGAS_FontInfo* pCurFont =
25 FGAS_FontInfoByFontName(wsFontName.AsStringView());
26 if (!pCurFont || !pCurFont->pReplaceFont)
27 return pFont;
28
29 uint32_t dwStyle = 0;
30 // TODO(dsinclair): Why doesn't this check the other flags?
31 if (FontStyleIsForceBold(dwFontStyles))
32 dwStyle |= FXFONT_FORCE_BOLD;
33 if (FontStyleIsItalic(dwFontStyles))
34 dwStyle |= FXFONT_ITALIC;
35
36 const char* pReplace = pCurFont->pReplaceFont;
37 int32_t iLength = strlen(pReplace);
38 while (iLength > 0) {
39 const char* pNameText = pReplace;
40 while (*pNameText != ',' && iLength > 0) {
41 pNameText++;
42 iLength--;
43 }
44 WideString wsReplace =
45 WideString::FromASCII(ByteStringView(pReplace, pNameText - pReplace));
46 pFont = pFontMgr->LoadFont(wsReplace.c_str(), dwStyle, 0xFFFF);
47 if (pFont)
48 break;
49
50 iLength--;
51 pNameText++;
52 pReplace = pNameText;
53 }
54 return pFont;
55 }
56
57 // static
GetDefaultFont(CFGAS_FontMgr * pFontMgr,WideStringView wsFontFamily,uint32_t dwFontStyles)58 RetainPtr<CFGAS_GEFont> CFGAS_DefaultFontManager::GetDefaultFont(
59 CFGAS_FontMgr* pFontMgr,
60 WideStringView wsFontFamily,
61 uint32_t dwFontStyles) {
62 RetainPtr<CFGAS_GEFont> pFont =
63 pFontMgr->LoadFont(L"Arial Narrow", dwFontStyles, 0xFFFF);
64 if (!pFont) {
65 pFont = pFontMgr->LoadFont(static_cast<const wchar_t*>(nullptr),
66 dwFontStyles, 0xFFFF);
67 }
68 return pFont;
69 }
70