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