blob: 0552f1c58c2de31c7fb0fc1e43047ea4b5798e99 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhang95e854f2015-06-13 00:58:06 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
thestig61159722016-08-04 10:59:55 -07007#include <memory>
Tom Sepeza0a69232017-05-01 11:39:33 -07008#include <utility>
thestig61159722016-08-04 10:59:55 -07009
Dan Sinclairf51a02a2017-04-19 12:46:53 -040010#include "core/fxcrt/fx_codepage.h"
Dan Sinclair92e22762017-07-10 15:14:26 -040011#include "core/fxge/cfx_folderfontinfo.h"
dsinclair74a34fc2016-09-29 16:41:42 -070012#include "core/fxge/cfx_gemodule.h"
dsinclair74a34fc2016-09-29 16:41:42 -070013#include "core/fxge/ifx_systemfontinfo.h"
Tom Sepeza0a69232017-05-01 11:39:33 -070014#include "third_party/base/ptr_util.h"
Tom Sepezb7d358b2015-06-17 10:01:00 -070015
John Abd-El-Malek207299b2014-12-15 12:13:45 -080016#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_
thestig61159722016-08-04 10:59:55 -070017namespace {
18
thestig61159722016-08-04 10:59:55 -070019const size_t kLinuxGpNameSize = 6;
20
Dan Sinclair812e96c2017-03-13 16:43:37 -040021const char* const g_LinuxGpFontList[][kLinuxGpNameSize] = {
npmc6833c22016-08-12 10:29:31 -070022 {"TakaoPGothic", "VL PGothic", "IPAPGothic", "VL Gothic", "Kochi Gothic",
23 "VL Gothic regular"},
24 {"TakaoGothic", "VL Gothic", "IPAGothic", "Kochi Gothic", nullptr,
25 "VL Gothic regular"},
26 {"TakaoPMincho", "IPAPMincho", "VL Gothic", "Kochi Mincho", nullptr,
27 "VL Gothic regular"},
28 {"TakaoMincho", "IPAMincho", "VL Gothic", "Kochi Mincho", nullptr,
29 "VL Gothic regular"},
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030};
thestig61159722016-08-04 10:59:55 -070031
Dan Sinclair812e96c2017-03-13 16:43:37 -040032const char* const g_LinuxGbFontList[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 "AR PL UMing CN Light", "WenQuanYi Micro Hei", "AR PL UKai CN",
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034};
thestig61159722016-08-04 10:59:55 -070035
Dan Sinclair812e96c2017-03-13 16:43:37 -040036const char* const g_LinuxB5FontList[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 "AR PL UMing TW Light", "WenQuanYi Micro Hei", "AR PL UKai TW",
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038};
thestig61159722016-08-04 10:59:55 -070039
Dan Sinclair812e96c2017-03-13 16:43:37 -040040const char* const g_LinuxHGFontList[] = {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041 "UnDotum",
42};
thestig61159722016-08-04 10:59:55 -070043
Dan Sinclair812e96c2017-03-13 16:43:37 -040044size_t GetJapanesePreference(const char* facearr,
thestig61159722016-08-04 10:59:55 -070045 int weight,
46 int pitch_family) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 CFX_ByteString face = facearr;
Ryan Harrisonda129ab2017-08-01 15:16:59 -040048 if (face.Find("Gothic") != FX_STRNPOS ||
49 face.Find("\x83\x53\x83\x56\x83\x62\x83\x4e") != FX_STRNPOS) {
50 if (face.Find("PGothic") != FX_STRNPOS ||
51 face.Find("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e") != FX_STRNPOS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 return 0;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070053 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 return 1;
55 }
Ryan Harrisonda129ab2017-08-01 15:16:59 -040056 if (face.Find("Mincho") != FX_STRNPOS ||
57 face.Find("\x96\xbe\x92\xa9") != FX_STRNPOS) {
58 if (face.Find("PMincho") != FX_STRNPOS ||
59 face.Find("\x82\x6f\x96\xbe\x92\xa9") != FX_STRNPOS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 return 2;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 return 3;
63 }
npm80ddd612016-11-28 11:27:14 -080064 if (!(pitch_family & FXFONT_FF_ROMAN) && weight > 400)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065 return 0;
npm80ddd612016-11-28 11:27:14 -080066
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 return 2;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068}
thestig61159722016-08-04 10:59:55 -070069
npm80ddd612016-11-28 11:27:14 -080070class CFX_LinuxFontInfo : public CFX_FolderFontInfo {
71 public:
72 CFX_LinuxFontInfo() {}
73 ~CFX_LinuxFontInfo() override {}
74
75 void* MapFont(int weight,
76 bool bItalic,
77 int charset,
78 int pitch_family,
Dan Sinclair812e96c2017-03-13 16:43:37 -040079 const char* family,
npm80ddd612016-11-28 11:27:14 -080080 int& iExact) override;
81 bool ParseFontCfg(const char** pUserPaths);
82};
thestig61159722016-08-04 10:59:55 -070083
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084void* CFX_LinuxFontInfo::MapFont(int weight,
tsepez12f3e4a2016-11-02 15:17:29 -070085 bool bItalic,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 int charset,
87 int pitch_family,
Dan Sinclair812e96c2017-03-13 16:43:37 -040088 const char* cstr_face,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 int& iExact) {
Tom Sepez563f7f82015-10-05 13:11:43 -070090 void* font = GetSubstFont(cstr_face);
91 if (font) {
92 iExact = 1;
93 return font;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 }
tsepez12f3e4a2016-11-02 15:17:29 -070095 bool bCJK = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 switch (charset) {
Dan Sinclairf51a02a2017-04-19 12:46:53 -040097 case FX_CHARSET_ShiftJIS: {
thestig61159722016-08-04 10:59:55 -070098 size_t index = GetJapanesePreference(cstr_face, weight, pitch_family);
99 ASSERT(index < FX_ArraySize(g_LinuxGpFontList));
100 for (size_t i = 0; i < kLinuxGpNameSize; i++) {
npmc6833c22016-08-12 10:29:31 -0700101 auto it = m_FontList.find(g_LinuxGpFontList[index][i]);
npm80ddd612016-11-28 11:27:14 -0800102 if (it != m_FontList.end())
Tom Sepez25694832017-04-21 15:42:49 -0700103 return it->second.get();
Tom Sepez09d33bc2015-08-19 09:49:24 -0700104 }
thestig61159722016-08-04 10:59:55 -0700105 break;
106 }
Dan Sinclairf51a02a2017-04-19 12:46:53 -0400107 case FX_CHARSET_ChineseSimplified: {
Tom Sepez24e733e2015-10-16 14:18:50 -0700108 for (size_t i = 0; i < FX_ArraySize(g_LinuxGbFontList); ++i) {
Tom Sepez09d33bc2015-08-19 09:49:24 -0700109 auto it = m_FontList.find(g_LinuxGbFontList[i]);
npm80ddd612016-11-28 11:27:14 -0800110 if (it != m_FontList.end())
Tom Sepez25694832017-04-21 15:42:49 -0700111 return it->second.get();
Tom Sepez09d33bc2015-08-19 09:49:24 -0700112 }
thestig61159722016-08-04 10:59:55 -0700113 break;
114 }
Dan Sinclairf51a02a2017-04-19 12:46:53 -0400115 case FX_CHARSET_ChineseTraditional: {
Tom Sepez24e733e2015-10-16 14:18:50 -0700116 for (size_t i = 0; i < FX_ArraySize(g_LinuxB5FontList); ++i) {
Tom Sepez09d33bc2015-08-19 09:49:24 -0700117 auto it = m_FontList.find(g_LinuxB5FontList[i]);
npm80ddd612016-11-28 11:27:14 -0800118 if (it != m_FontList.end())
Tom Sepez25694832017-04-21 15:42:49 -0700119 return it->second.get();
Tom Sepez09d33bc2015-08-19 09:49:24 -0700120 }
thestig61159722016-08-04 10:59:55 -0700121 break;
122 }
Dan Sinclairf51a02a2017-04-19 12:46:53 -0400123 case FX_CHARSET_Hangul: {
Tom Sepez24e733e2015-10-16 14:18:50 -0700124 for (size_t i = 0; i < FX_ArraySize(g_LinuxHGFontList); ++i) {
Tom Sepez09d33bc2015-08-19 09:49:24 -0700125 auto it = m_FontList.find(g_LinuxHGFontList[i]);
npm80ddd612016-11-28 11:27:14 -0800126 if (it != m_FontList.end())
Tom Sepez25694832017-04-21 15:42:49 -0700127 return it->second.get();
Tom Sepez09d33bc2015-08-19 09:49:24 -0700128 }
thestig61159722016-08-04 10:59:55 -0700129 break;
130 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 default:
tsepez12f3e4a2016-11-02 15:17:29 -0700132 bCJK = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 break;
134 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 return FindFont(weight, bItalic, charset, pitch_family, cstr_face, !bCJK);
136}
thestig24508df2016-05-27 15:14:20 -0700137
npm80ddd612016-11-28 11:27:14 -0800138bool CFX_LinuxFontInfo::ParseFontCfg(const char** pUserPaths) {
139 if (!pUserPaths)
140 return false;
141
142 for (const char** pPath = pUserPaths; *pPath; ++pPath)
143 AddPath(*pPath);
144 return true;
145}
146
147} // namespace
148
thestig24508df2016-05-27 15:14:20 -0700149std::unique_ptr<IFX_SystemFontInfo> IFX_SystemFontInfo::CreateDefault(
150 const char** pUserPaths) {
Tom Sepeza0a69232017-05-01 11:39:33 -0700151 auto pInfo = pdfium::MakeUnique<CFX_LinuxFontInfo>();
Lei Zhang6f62d532015-09-23 15:31:44 -0700152 if (!pInfo->ParseFontCfg(pUserPaths)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 pInfo->AddPath("/usr/share/fonts");
154 pInfo->AddPath("/usr/share/X11/fonts/Type1");
155 pInfo->AddPath("/usr/share/X11/fonts/TTF");
156 pInfo->AddPath("/usr/local/share/fonts");
157 }
Tom Sepeza0a69232017-05-01 11:39:33 -0700158 return std::move(pInfo);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159}
thestig24508df2016-05-27 15:14:20 -0700160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161void CFX_GEModule::InitPlatform() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700162 m_pFontMgr->SetSystemFontInfo(
163 IFX_SystemFontInfo::CreateDefault(m_pUserFontPaths));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164}
thestig61159722016-08-04 10:59:55 -0700165
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166void CFX_GEModule::DestroyPlatform() {}
Tom Sepezb7d358b2015-06-17 10:01:00 -0700167#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_