Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fx_apple_platform.cpp
Go to the documentation of this file.
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
19namespace {
20
21struct Substs {
22 const char* m_pName;
23 const char* m_pSubstName;
24};
25
26constexpr 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
41class 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
56constexpr char kJapanGothic[] = "Hiragino Kaku Gothic Pro W6";
57constexpr char kJapanMincho[] = "Hiragino Mincho Pro W6";
58
59ByteString 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
69void* 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) {
110 other_face = GetJapanesePreference(face, weight, pitch_family);
111 break;
113 other_face = "STSong";
114 break;
116 other_face = "AppleMyungjo";
117 break;
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
129bool CFX_MacFontInfo::ParseFontCfg(const char** pUserPaths) {
130 if (!pUserPaths) {
131 return false;
132 }
134 for (const char** pPath = pUserPaths; *pPath; ++pPath) {
135 AddPath(*pPath);
136 }
137 });
138 return true;
139}
140
141} // namespace
142
143CApplePlatform::CApplePlatform() = default;
144
145CApplePlatform::~CApplePlatform() = default;
146
147void CApplePlatform::Init() {}
148
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
160void* CApplePlatform::CreatePlatformFont(
161 pdfium::span<const uint8_t> font_span) {
162 return m_quartz2d.CreateFont(font_span);
163}
164
165// static
166std::unique_ptr<CFX_GEModule::PlatformIface>
167CFX_GEModule::PlatformIface::Create() {
168 return std::make_unique<CApplePlatform>();
169}
fxcrt::ByteString ByteString
Definition bytestring.h:180
void Init() override
std::unique_ptr< SystemFontInfoIface > CreateDefaultSystemFontInfo() override
void * CreatePlatformFont(pdfium::span< const uint8_t > font_span) override
~CApplePlatform() override
void AddPath(const ByteString &path)
void * GetFont(const ByteString &face) override
static CFX_GEModule * Get()
const char ** GetUserFontPaths() const
ByteString & operator+=(const char *str)
ByteString & operator=(const char *str)
ByteString & operator=(const ByteString &that)
ByteString & operator=(ByteString &&that) noexcept
#define UNSAFE_TODO(...)
FX_Charset
Definition fx_codepage.h:71
@ kChineseTraditional
Definition fx_codepage.h:91
@ kChineseSimplified
Definition fx_codepage.h:90
bool FontFamilyIsFixedPitch(uint32_t family)
Definition fx_font.h:86
bool FontFamilyIsRoman(uint32_t family)
Definition fx_font.h:89