Revert "Make CFX_FontMgr member variables private."
[pdfium.git] / core / src / fxge / ge / fx_ge_fontmap.cpp
index 90b3c52..e88791a 100644 (file)
@@ -179,7 +179,7 @@ const FX_CHAR* const g_Base14FontNames[14] = {
     "Symbol",
     "ZapfDingbats",
 };
-const struct _AltFontName {
+const struct AltFontName {
   const FX_CHAR* m_pName;
   int m_Index;
 } g_AltFontNames[] = {
@@ -275,20 +275,10 @@ const struct _AltFontName {
 };
 extern "C" {
 static int compareString(const void* key, const void* element) {
-  return FXSYS_stricmp((const FX_CHAR*)key, ((_AltFontName*)element)->m_pName);
+  return FXSYS_stricmp((const FX_CHAR*)key, ((AltFontName*)element)->m_pName);
 }
 }
-int _PDF_GetStandardFontName(CFX_ByteString& name) {
-  _AltFontName* found =
-      (_AltFontName*)FXSYS_bsearch(name.c_str(), g_AltFontNames,
-                                   sizeof g_AltFontNames / sizeof(_AltFontName),
-                                   sizeof(_AltFontName), compareString);
-  if (found == NULL) {
-    return -1;
-  }
-  name = g_Base14FontNames[found->m_Index];
-  return found->m_Index;
-}
+
 int GetTTCIndex(const uint8_t* pFontData,
                 FX_DWORD ttc_size,
                 FX_DWORD font_offset) {
@@ -465,7 +455,7 @@ void CFX_FontMapper::SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo) {
   m_pFontInfo = pFontInfo;
 }
 static CFX_ByteString _TT_NormalizeName(const FX_CHAR* family) {
-  CFX_ByteString norm(family, -1);
+  CFX_ByteString norm(family);
   norm.Remove(' ');
   norm.Remove('-');
   norm.Remove(',');
@@ -846,7 +836,7 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name,
       SubstName = name.Mid(1);
     }
   }
-  _PDF_GetStandardFontName(SubstName);
+  PDF_GetStandardFontName(&SubstName);
   if (SubstName == FX_BSTRC("Symbol") && !bTrueType) {
     pSubstFont->m_Family = "Chrome Symbol";
     pSubstFont->m_Charset = FXFONT_SYMBOL_CHARSET;
@@ -880,7 +870,7 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name,
   int find = SubstName.Find(FX_BSTRC(","), 0);
   if (find >= 0) {
     family = SubstName.Left(find);
-    _PDF_GetStandardFontName(family);
+    PDF_GetStandardFontName(&family);
     style = SubstName.Mid(find + 1);
     bHasComma = TRUE;
   } else {
@@ -1407,6 +1397,105 @@ void CFX_FolderFontInfo::ReportFace(CFX_ByteString& path,
   }
   m_FontList[facename] = pInfo;
 }
+static const struct {
+  const FX_CHAR* m_pName;
+  const FX_CHAR* m_pSubstName;
+} Base14Substs[] = {
+    {"Courier", "Courier New"},
+    {"Courier-Bold", "Courier New Bold"},
+    {"Courier-BoldOblique", "Courier New Bold Italic"},
+    {"Courier-Oblique", "Courier New Italic"},
+    {"Helvetica", "Arial"},
+    {"Helvetica-Bold", "Arial Bold"},
+    {"Helvetica-BoldOblique", "Arial Bold Italic"},
+    {"Helvetica-Oblique", "Arial Italic"},
+    {"Times-Roman", "Times New Roman"},
+    {"Times-Bold", "Times New Roman Bold"},
+    {"Times-BoldItalic", "Times New Roman Bold Italic"},
+    {"Times-Italic", "Times New Roman Italic"},
+};
+void* CFX_FolderFontInfo::GetSubstFont(const CFX_ByteString& face) {
+  for (size_t iBaseFont = 0; iBaseFont < FX_ArraySize(Base14Substs);
+       iBaseFont++) {
+    if (face == Base14Substs[iBaseFont].m_pName) {
+      return GetFont(Base14Substs[iBaseFont].m_pSubstName);
+    }
+  }
+  return nullptr;
+}
+static FX_DWORD _GetCharset(int charset) {
+  switch (charset) {
+    case FXFONT_SHIFTJIS_CHARSET:
+      return CHARSET_FLAG_SHIFTJIS;
+    case FXFONT_GB2312_CHARSET:
+      return CHARSET_FLAG_GB;
+    case FXFONT_CHINESEBIG5_CHARSET:
+      return CHARSET_FLAG_BIG5;
+    case FXFONT_HANGEUL_CHARSET:
+      return CHARSET_FLAG_KOREAN;
+    case FXFONT_SYMBOL_CHARSET:
+      return CHARSET_FLAG_SYMBOL;
+    case FXFONT_ANSI_CHARSET:
+      return CHARSET_FLAG_ANSI;
+    default:
+      break;
+  }
+  return 0;
+}
+static int32_t _GetSimilarValue(int weight,
+                                FX_BOOL bItalic,
+                                int pitch_family,
+                                FX_DWORD style) {
+  int32_t iSimilarValue = 0;
+  if ((style & FXFONT_BOLD) == (weight > 400)) {
+    iSimilarValue += 16;
+  }
+  if ((style & FXFONT_ITALIC) == bItalic) {
+    iSimilarValue += 16;
+  }
+  if ((style & FXFONT_SERIF) == (pitch_family & FXFONT_FF_ROMAN)) {
+    iSimilarValue += 16;
+  }
+  if ((style & FXFONT_SCRIPT) == (pitch_family & FXFONT_FF_SCRIPT)) {
+    iSimilarValue += 8;
+  }
+  if ((style & FXFONT_FIXED_PITCH) == (pitch_family & FXFONT_FF_FIXEDPITCH)) {
+    iSimilarValue += 8;
+  }
+  return iSimilarValue;
+}
+void* CFX_FolderFontInfo::FindFont(int weight,
+                                   FX_BOOL bItalic,
+                                   int charset,
+                                   int pitch_family,
+                                   const FX_CHAR* family,
+                                   FX_BOOL bMatchName) {
+  CFX_FontFaceInfo* pFind = nullptr;
+  if (charset == FXFONT_ANSI_CHARSET && (pitch_family & FXFONT_FF_FIXEDPITCH)) {
+    return GetFont("Courier New");
+  }
+  FX_DWORD charset_flag = _GetCharset(charset);
+  int32_t iBestSimilar = 0;
+  for (const auto& it : m_FontList) {
+    const CFX_ByteString& bsName = it.first;
+    CFX_FontFaceInfo* pFont = it.second;
+    if (!(pFont->m_Charsets & charset_flag) &&
+        charset != FXFONT_DEFAULT_CHARSET) {
+      continue;
+    }
+    int32_t index = bsName.Find(family);
+    if (bMatchName && index < 0) {
+      continue;
+    }
+    int32_t iSimilarValue =
+        _GetSimilarValue(weight, bItalic, pitch_family, pFont->m_Styles);
+    if (iSimilarValue > iBestSimilar) {
+      iBestSimilar = iSimilarValue;
+      pFind = pFont;
+    }
+  }
+  return pFind;
+}
 void* CFX_FolderFontInfo::MapFont(int weight,
                                   FX_BOOL bItalic,
                                   int charset,
@@ -1471,3 +1560,14 @@ FX_BOOL CFX_FolderFontInfo::GetFaceName(void* hFont, CFX_ByteString& name) {
 FX_BOOL CFX_FolderFontInfo::GetFontCharset(void* hFont, int& charset) {
   return FALSE;
 }
+
+int PDF_GetStandardFontName(CFX_ByteString* name) {
+  AltFontName* found = static_cast<AltFontName*>(
+      FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames),
+                    sizeof(AltFontName), compareString));
+  if (!found)
+    return -1;
+
+  *name = g_Base14FontNames[found->m_Index];
+  return found->m_Index;
+}