Home | History | Annotate | Download | only in fxge
      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/cfx_folderfontinfo.h"
     12 #include "core/fxge/cfx_fontmgr.h"
     13 #include "core/fxge/cfx_gemodule.h"
     14 #include "core/fxge/ifx_systemfontinfo.h"
     15 #include "third_party/base/ptr_util.h"
     16 
     17 #if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
     18 namespace {
     19 
     20 const size_t kLinuxGpNameSize = 6;
     21 
     22 const char* const g_LinuxGpFontList[][kLinuxGpNameSize] = {
     23     {"TakaoPGothic", "VL PGothic", "IPAPGothic", "VL Gothic", "Kochi Gothic",
     24      "VL Gothic regular"},
     25     {"TakaoGothic", "VL Gothic", "IPAGothic", "Kochi Gothic", nullptr,
     26      "VL Gothic regular"},
     27     {"TakaoPMincho", "IPAPMincho", "VL Gothic", "Kochi Mincho", nullptr,
     28      "VL Gothic regular"},
     29     {"TakaoMincho", "IPAMincho", "VL Gothic", "Kochi Mincho", nullptr,
     30      "VL Gothic regular"},
     31 };
     32 
     33 const char* const g_LinuxGbFontList[] = {
     34     "AR PL UMing CN Light", "WenQuanYi Micro Hei", "AR PL UKai CN",
     35 };
     36 
     37 const char* const g_LinuxB5FontList[] = {
     38     "AR PL UMing TW Light", "WenQuanYi Micro Hei", "AR PL UKai TW",
     39 };
     40 
     41 const char* const g_LinuxHGFontList[] = {
     42     "UnDotum",
     43 };
     44 
     45 size_t GetJapanesePreference(const char* facearr,
     46                              int weight,
     47                              int pitch_family) {
     48   ByteString face = facearr;
     49   if (face.Contains("Gothic") ||
     50       face.Contains("\x83\x53\x83\x56\x83\x62\x83\x4e")) {
     51     if (face.Contains("PGothic") ||
     52         face.Contains("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e")) {
     53       return 0;
     54     }
     55     return 1;
     56   }
     57   if (face.Contains("Mincho") || face.Contains("\x96\xbe\x92\xa9")) {
     58     if (face.Contains("PMincho") || face.Contains("\x82\x6f\x96\xbe\x92\xa9")) {
     59       return 2;
     60     }
     61     return 3;
     62   }
     63   if (!FontFamilyIsRoman(pitch_family) && weight > 400)
     64     return 0;
     65 
     66   return 2;
     67 }
     68 
     69 class CFX_LinuxFontInfo : public CFX_FolderFontInfo {
     70  public:
     71   CFX_LinuxFontInfo() {}
     72   ~CFX_LinuxFontInfo() override {}
     73 
     74   void* MapFont(int weight,
     75                 bool bItalic,
     76                 int charset,
     77                 int pitch_family,
     78                 const char* family) override;
     79   bool ParseFontCfg(const char** pUserPaths);
     80 };
     81 
     82 void* CFX_LinuxFontInfo::MapFont(int weight,
     83                                  bool bItalic,
     84                                  int charset,
     85                                  int pitch_family,
     86                                  const char* cstr_face) {
     87   void* font = GetSubstFont(cstr_face);
     88   if (font)
     89     return font;
     90 
     91   bool bCJK = true;
     92   switch (charset) {
     93     case FX_CHARSET_ShiftJIS: {
     94       size_t index = GetJapanesePreference(cstr_face, weight, pitch_family);
     95       ASSERT(index < FX_ArraySize(g_LinuxGpFontList));
     96       for (size_t i = 0; i < kLinuxGpNameSize; i++) {
     97         auto it = m_FontList.find(g_LinuxGpFontList[index][i]);
     98         if (it != m_FontList.end())
     99           return it->second.get();
    100       }
    101       break;
    102     }
    103     case FX_CHARSET_ChineseSimplified: {
    104       for (const char* name : g_LinuxGbFontList) {
    105         auto it = m_FontList.find(name);
    106         if (it != m_FontList.end())
    107           return it->second.get();
    108       }
    109       break;
    110     }
    111     case FX_CHARSET_ChineseTraditional: {
    112       for (const char* name : g_LinuxB5FontList) {
    113         auto it = m_FontList.find(name);
    114         if (it != m_FontList.end())
    115           return it->second.get();
    116       }
    117       break;
    118     }
    119     case FX_CHARSET_Hangul: {
    120       for (const char* name : g_LinuxHGFontList) {
    121         auto it = m_FontList.find(name);
    122         if (it != m_FontList.end())
    123           return it->second.get();
    124       }
    125       break;
    126     }
    127     default:
    128       bCJK = false;
    129       break;
    130   }
    131   return FindFont(weight, bItalic, charset, pitch_family, cstr_face, !bCJK);
    132 }
    133 
    134 bool CFX_LinuxFontInfo::ParseFontCfg(const char** pUserPaths) {
    135   if (!pUserPaths)
    136     return false;
    137 
    138   for (const char** pPath = pUserPaths; *pPath; ++pPath)
    139     AddPath(*pPath);
    140   return true;
    141 }
    142 
    143 }  // namespace
    144 
    145 std::unique_ptr<IFX_SystemFontInfo> IFX_SystemFontInfo::CreateDefault(
    146     const char** pUserPaths) {
    147   auto pInfo = pdfium::MakeUnique<CFX_LinuxFontInfo>();
    148   if (!pInfo->ParseFontCfg(pUserPaths)) {
    149     pInfo->AddPath("/usr/share/fonts");
    150     pInfo->AddPath("/usr/share/X11/fonts/Type1");
    151     pInfo->AddPath("/usr/share/X11/fonts/TTF");
    152     pInfo->AddPath("/usr/local/share/fonts");
    153   }
    154   return std::move(pInfo);
    155 }
    156 
    157 void CFX_GEModule::InitPlatform() {
    158   m_pFontMgr->SetSystemFontInfo(
    159       IFX_SystemFontInfo::CreateDefault(m_pUserFontPaths));
    160 }
    161 
    162 void CFX_GEModule::DestroyPlatform() {}
    163 #endif  // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
    164