1 // Copyright 2014 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 <memory>
8 #include <utility>
9
10 #include "core/fxcrt/fx_codepage.h"
11 #include "core/fxge/apple/apple_int.h"
12 #include "core/fxge/cfx_folderfontinfo.h"
13 #include "core/fxge/cfx_fontmgr.h"
14 #include "core/fxge/cfx_gemodule.h"
15 #include "core/fxge/ifx_systemfontinfo.h"
16 #include "third_party/base/ptr_util.h"
17
18 namespace {
19
20 const struct {
21 const char* m_pName;
22 const char* m_pSubstName;
23 } g_Base14Substs[] = {
24 {"Courier", "Courier New"},
25 {"Courier-Bold", "Courier New Bold"},
26 {"Courier-BoldOblique", "Courier New Bold Italic"},
27 {"Courier-Oblique", "Courier New Italic"},
28 {"Helvetica", "Arial"},
29 {"Helvetica-Bold", "Arial Bold"},
30 {"Helvetica-BoldOblique", "Arial Bold Italic"},
31 {"Helvetica-Oblique", "Arial Italic"},
32 {"Times-Roman", "Times New Roman"},
33 {"Times-Bold", "Times New Roman Bold"},
34 {"Times-BoldItalic", "Times New Roman Bold Italic"},
35 {"Times-Italic", "Times New Roman Italic"},
36 };
37
38 class CFX_MacFontInfo : public CFX_FolderFontInfo {
39 public:
CFX_MacFontInfo()40 CFX_MacFontInfo() {}
~CFX_MacFontInfo()41 ~CFX_MacFontInfo() override {}
42
43 // CFX_FolderFontInfo
44 void* MapFont(int weight,
45 bool bItalic,
46 int charset,
47 int pitch_family,
48 const char* family) override;
49 };
50
51 const char JAPAN_GOTHIC[] = "Hiragino Kaku Gothic Pro W6";
52 const char JAPAN_MINCHO[] = "Hiragino Mincho Pro W6";
53
GetJapanesePreference(ByteString * face,int weight,int pitch_family)54 void GetJapanesePreference(ByteString* face, int weight, int pitch_family) {
55 if (face->Contains("Gothic")) {
56 *face = JAPAN_GOTHIC;
57 return;
58 }
59 *face = (FontFamilyIsRoman(pitch_family) || weight <= 400) ? JAPAN_MINCHO
60 : JAPAN_GOTHIC;
61 }
62
MapFont(int weight,bool bItalic,int charset,int pitch_family,const char * cstr_face)63 void* CFX_MacFontInfo::MapFont(int weight,
64 bool bItalic,
65 int charset,
66 int pitch_family,
67 const char* cstr_face) {
68 ByteString face = cstr_face;
69 for (const auto& sub : g_Base14Substs) {
70 if (face == ByteStringView(sub.m_pName)) {
71 face = sub.m_pSubstName;
72 return GetFont(face.c_str());
73 }
74 }
75
76 // The request may not ask for the bold and/or italic version of a font by
77 // name. So try to construct the appropriate name. This is not 100% foolproof
78 // as there are fonts that have "Oblique" or "BoldOblique" or "Heavy" in their
79 // names instead. But this at least works for common fonts like Arial and
80 // Times New Roman. A more sophisticated approach would be to find all the
81 // fonts in |m_FontList| with |face| in the name, and examine the fonts to
82 // see which best matches the requested characteristics.
83 if (!face.Contains("Bold") && !face.Contains("Italic")) {
84 ByteString new_face = face;
85 if (weight > 400)
86 new_face += " Bold";
87 if (bItalic)
88 new_face += " Italic";
89 auto it = m_FontList.find(new_face);
90 if (it != m_FontList.end())
91 return it->second.get();
92 }
93
94 auto it = m_FontList.find(face);
95 if (it != m_FontList.end())
96 return it->second.get();
97
98 if (charset == FX_CHARSET_ANSI && FontFamilyIsFixedPitch(pitch_family))
99 return GetFont("Courier New");
100
101 if (charset == FX_CHARSET_ANSI || charset == FX_CHARSET_Symbol)
102 return nullptr;
103
104 switch (charset) {
105 case FX_CHARSET_ShiftJIS:
106 GetJapanesePreference(&face, weight, pitch_family);
107 break;
108 case FX_CHARSET_ChineseSimplified:
109 face = "STSong";
110 break;
111 case FX_CHARSET_Hangul:
112 face = "AppleMyungjo";
113 break;
114 case FX_CHARSET_ChineseTraditional:
115 face = "LiSong Pro Light";
116 }
117 it = m_FontList.find(face);
118 return it != m_FontList.end() ? it->second.get() : nullptr;
119 }
120
121 } // namespace
122
CreateDefault(const char ** pUnused)123 std::unique_ptr<IFX_SystemFontInfo> IFX_SystemFontInfo::CreateDefault(
124 const char** pUnused) {
125 auto pInfo = pdfium::MakeUnique<CFX_MacFontInfo>();
126 pInfo->AddPath("~/Library/Fonts");
127 pInfo->AddPath("/Library/Fonts");
128 pInfo->AddPath("/System/Library/Fonts");
129 return std::move(pInfo);
130 }
131
InitPlatform()132 void CFX_GEModule::InitPlatform() {
133 m_pPlatformData = new CApplePlatform;
134 m_pFontMgr->SetSystemFontInfo(IFX_SystemFontInfo::CreateDefault(nullptr));
135 }
136
DestroyPlatform()137 void CFX_GEModule::DestroyPlatform() {
138 delete reinterpret_cast<CApplePlatform*>(m_pPlatformData);
139 m_pPlatformData = nullptr;
140 }
141