1 // Copyright 2014 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/apple/fx_apple_platform.h"
8
9 #include <memory>
10 #include <utility>
11
12 #include "core/fxcrt/compiler_specific.h"
13 #include "core/fxcrt/fx_codepage.h"
14 #include "core/fxge/cfx_folderfontinfo.h"
15 #include "core/fxge/cfx_fontmgr.h"
16 #include "core/fxge/fx_font.h"
17 #include "core/fxge/systemfontinfo_iface.h"
18
19 namespace {
20
21 struct Substs {
22 const char* m_pName;
23 const char* m_pSubstName;
24 };
25
26 constexpr Substs kBase14Substs[] = {
27 {"Courier", "Courier New"},
28 {"Courier-Bold", "Courier New Bold"},
29 {"Courier-BoldOblique", "Courier New Bold Italic"},
30 {"Courier-Oblique", "Courier New Italic"},
31 {"Helvetica", "Arial"},
32 {"Helvetica-Bold", "Arial Bold"},
33 {"Helvetica-BoldOblique", "Arial Bold Italic"},
34 {"Helvetica-Oblique", "Arial Italic"},
35 {"Times-Roman", "Times New Roman"},
36 {"Times-Bold", "Times New Roman Bold"},
37 {"Times-BoldItalic", "Times New Roman Bold Italic"},
38 {"Times-Italic", "Times New Roman Italic"},
39 };
40
41 class CFX_MacFontInfo final : public CFX_FolderFontInfo {
42 public:
43 CFX_MacFontInfo() = default;
44 ~CFX_MacFontInfo() override = default;
45
46 // CFX_FolderFontInfo
47 void* MapFont(int weight,
48 bool bItalic,
49 FX_Charset charset,
50 int pitch_family,
51 const ByteString& face) override;
52
53 bool ParseFontCfg(const char** pUserPaths);
54 };
55
56 constexpr char kJapanGothic[] = "Hiragino Kaku Gothic Pro W6";
57 constexpr char kJapanMincho[] = "Hiragino Mincho Pro W6";
58
GetJapanesePreference(const ByteString & face,int weight,int pitch_family)59 ByteString GetJapanesePreference(const ByteString& face,
60 int weight,
61 int pitch_family) {
62 if (face.Contains("Gothic"))
63 return kJapanGothic;
64 if (FontFamilyIsRoman(pitch_family) || weight <= 400)
65 return kJapanMincho;
66 return kJapanGothic;
67 }
68
MapFont(int weight,bool bItalic,FX_Charset charset,int pitch_family,const ByteString & face)69 void* CFX_MacFontInfo::MapFont(int weight,
70 bool bItalic,
71 FX_Charset charset,
72 int pitch_family,
73 const ByteString& face) {
74 for (const auto& sub : kBase14Substs) {
75 if (face == ByteStringView(sub.m_pName))
76 return GetFont(sub.m_pSubstName);
77 }
78
79 // The request may not ask for the bold and/or italic version of a font by
80 // name. So try to construct the appropriate name. This is not 100% foolproof
81 // as there are fonts that have "Oblique" or "BoldOblique" or "Heavy" in their
82 // names instead. But this at least works for common fonts like Arial and
83 // Times New Roman. A more sophisticated approach would be to find all the
84 // fonts in |m_FontList| with |face| in the name, and examine the fonts to
85 // see which best matches the requested characteristics.
86 if (!face.Contains("Bold") && !face.Contains("Italic")) {
87 ByteString new_face = face;
88 if (weight > 400)
89 new_face += " Bold";
90 if (bItalic)
91 new_face += " Italic";
92 auto it = m_FontList.find(new_face);
93 if (it != m_FontList.end())
94 return it->second.get();
95 }
96
97 auto it = m_FontList.find(face);
98 if (it != m_FontList.end())
99 return it->second.get();
100
101 if (charset == FX_Charset::kANSI && FontFamilyIsFixedPitch(pitch_family))
102 return GetFont("Courier New");
103
104 if (charset == FX_Charset::kANSI || charset == FX_Charset::kSymbol)
105 return nullptr;
106
107 ByteString other_face;
108 switch (charset) {
109 case FX_Charset::kShiftJIS:
110 other_face = GetJapanesePreference(face, weight, pitch_family);
111 break;
112 case FX_Charset::kChineseSimplified:
113 other_face = "STSong";
114 break;
115 case FX_Charset::kHangul:
116 other_face = "AppleMyungjo";
117 break;
118 case FX_Charset::kChineseTraditional:
119 other_face = "LiSong Pro Light";
120 break;
121 default:
122 other_face = face;
123 break;
124 }
125 it = m_FontList.find(other_face);
126 return it != m_FontList.end() ? it->second.get() : nullptr;
127 }
128
ParseFontCfg(const char ** pUserPaths)129 bool CFX_MacFontInfo::ParseFontCfg(const char** pUserPaths) {
130 if (!pUserPaths) {
131 return false;
132 }
133 UNSAFE_TODO({
134 for (const char** pPath = pUserPaths; *pPath; ++pPath) {
135 AddPath(*pPath);
136 }
137 });
138 return true;
139 }
140
141 } // namespace
142
143 CApplePlatform::CApplePlatform() = default;
144
145 CApplePlatform::~CApplePlatform() = default;
146
Init()147 void CApplePlatform::Init() {}
148
149 std::unique_ptr<SystemFontInfoIface>
CreateDefaultSystemFontInfo()150 CApplePlatform::CreateDefaultSystemFontInfo() {
151 auto pInfo = std::make_unique<CFX_MacFontInfo>();
152 if (!pInfo->ParseFontCfg(CFX_GEModule::Get()->GetUserFontPaths())) {
153 pInfo->AddPath("~/Library/Fonts");
154 pInfo->AddPath("/Library/Fonts");
155 pInfo->AddPath("/System/Library/Fonts");
156 }
157 return pInfo;
158 }
159
CreatePlatformFont(pdfium::span<const uint8_t> font_span)160 void* CApplePlatform::CreatePlatformFont(
161 pdfium::span<const uint8_t> font_span) {
162 return m_quartz2d.CreateFont(font_span);
163 }
164
165 // static
166 std::unique_ptr<CFX_GEModule::PlatformIface>
Create()167 CFX_GEModule::PlatformIface::Create() {
168 return std::make_unique<CApplePlatform>();
169 }
170