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