• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/fx_font.h"
16 #include "core/fxge/systemfontinfo_iface.h"
17 #include "third_party/base/ptr_util.h"
18 
19 namespace {
20 
21 const struct {
22   const char* m_pName;
23   const char* m_pSubstName;
24 } g_Base14Substs[] = {
25     {"Courier", "Courier New"},
26     {"Courier-Bold", "Courier New Bold"},
27     {"Courier-BoldOblique", "Courier New Bold Italic"},
28     {"Courier-Oblique", "Courier New Italic"},
29     {"Helvetica", "Arial"},
30     {"Helvetica-Bold", "Arial Bold"},
31     {"Helvetica-BoldOblique", "Arial Bold Italic"},
32     {"Helvetica-Oblique", "Arial Italic"},
33     {"Times-Roman", "Times New Roman"},
34     {"Times-Bold", "Times New Roman Bold"},
35     {"Times-BoldItalic", "Times New Roman Bold Italic"},
36     {"Times-Italic", "Times New Roman Italic"},
37 };
38 
39 class CFX_MacFontInfo final : public CFX_FolderFontInfo {
40  public:
CFX_MacFontInfo()41   CFX_MacFontInfo() {}
~CFX_MacFontInfo()42   ~CFX_MacFontInfo() override {}
43 
44   // CFX_FolderFontInfo
45   void* MapFont(int weight,
46                 bool bItalic,
47                 int charset,
48                 int pitch_family,
49                 const char* family) override;
50 
51   bool ParseFontCfg(const char** pUserPaths);
52 };
53 
54 const char JAPAN_GOTHIC[] = "Hiragino Kaku Gothic Pro W6";
55 const char JAPAN_MINCHO[] = "Hiragino Mincho Pro W6";
56 
GetJapanesePreference(ByteString * face,int weight,int pitch_family)57 void GetJapanesePreference(ByteString* face, int weight, int pitch_family) {
58   if (face->Contains("Gothic")) {
59     *face = JAPAN_GOTHIC;
60     return;
61   }
62   *face = (FontFamilyIsRoman(pitch_family) || weight <= 400) ? JAPAN_MINCHO
63                                                              : JAPAN_GOTHIC;
64 }
65 
MapFont(int weight,bool bItalic,int charset,int pitch_family,const char * cstr_face)66 void* CFX_MacFontInfo::MapFont(int weight,
67                                bool bItalic,
68                                int charset,
69                                int pitch_family,
70                                const char* cstr_face) {
71   ByteString face = cstr_face;
72   for (const auto& sub : g_Base14Substs) {
73     if (face == ByteStringView(sub.m_pName)) {
74       face = sub.m_pSubstName;
75       return GetFont(face.c_str());
76     }
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_ANSI && FontFamilyIsFixedPitch(pitch_family))
102     return GetFont("Courier New");
103 
104   if (charset == FX_CHARSET_ANSI || charset == FX_CHARSET_Symbol)
105     return nullptr;
106 
107   switch (charset) {
108     case FX_CHARSET_ShiftJIS:
109       GetJapanesePreference(&face, weight, pitch_family);
110       break;
111     case FX_CHARSET_ChineseSimplified:
112       face = "STSong";
113       break;
114     case FX_CHARSET_Hangul:
115       face = "AppleMyungjo";
116       break;
117     case FX_CHARSET_ChineseTraditional:
118       face = "LiSong Pro Light";
119   }
120   it = m_FontList.find(face);
121   return it != m_FontList.end() ? it->second.get() : nullptr;
122 }
123 
ParseFontCfg(const char ** pUserPaths)124 bool CFX_MacFontInfo::ParseFontCfg(const char** pUserPaths) {
125   if (!pUserPaths)
126     return false;
127 
128   for (const char** pPath = pUserPaths; *pPath; ++pPath)
129     AddPath(*pPath);
130   return true;
131 }
132 }  // namespace
133 
CreateDefault(const char ** pUserPaths)134 std::unique_ptr<SystemFontInfoIface> SystemFontInfoIface::CreateDefault(
135     const char** pUserPaths) {
136   auto pInfo = pdfium::MakeUnique<CFX_MacFontInfo>();
137   if (!pInfo->ParseFontCfg(pUserPaths)) {
138     pInfo->AddPath("~/Library/Fonts");
139     pInfo->AddPath("/Library/Fonts");
140     pInfo->AddPath("/System/Library/Fonts");
141   }
142   return std::move(pInfo);
143 }
144 
145 CApplePlatform::CApplePlatform() = default;
146 
147 CApplePlatform::~CApplePlatform() = default;
148 
Init()149 void CApplePlatform::Init() {
150   CFX_GEModule* pModule = CFX_GEModule::Get();
151   pModule->GetFontMgr()->SetSystemFontInfo(
152       SystemFontInfoIface::CreateDefault(pModule->GetUserFontPaths()));
153 }
154 
155 // static
156 std::unique_ptr<CFX_GEModule::PlatformIface>
Create()157 CFX_GEModule::PlatformIface::Create() {
158   return pdfium::MakeUnique<CApplePlatform>();
159 }
160