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/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
18namespace {
19
20struct Substs {
21 const char* m_pName;
22 const char* m_pSubstName;
23};
24
25constexpr 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
40class 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
55constexpr char kJapanGothic[] = "Hiragino Kaku Gothic Pro W6";
56constexpr char kJapanMincho[] = "Hiragino Mincho Pro W6";
57
58ByteString 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
68void* 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) {
109 other_face = GetJapanesePreference(face, weight, pitch_family);
110 break;
112 other_face = "STSong";
113 break;
115 other_face = "AppleMyungjo";
116 break;
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
128bool 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
138CApplePlatform::CApplePlatform() = default;
139
140CApplePlatform::~CApplePlatform() = default;
141
142void CApplePlatform::Init() {}
143
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
155void* CApplePlatform::CreatePlatformFont(
156 pdfium::span<const uint8_t> font_span) {
157 return m_quartz2d.CreateFont(font_span);
158}
159
160// static
161std::unique_ptr<CFX_GEModule::PlatformIface>
162CFX_GEModule::PlatformIface::Create() {
163 return std::make_unique<CApplePlatform>();
164}
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
void * CreateFont(pdfium::span< const uint8_t > pFontData)
ByteString & operator+=(const char *str)
ByteString & operator=(const char *str)
ByteString & operator=(const ByteString &that)
ByteString & operator=(ByteString &&that) noexcept
FX_Charset
Definition fx_codepage.h:70
@ kChineseTraditional
@ kChineseSimplified
bool FontFamilyIsFixedPitch(uint32_t family)
Definition fx_font.h:84
bool FontFamilyIsRoman(uint32_t family)
Definition fx_font.h:87