freetype-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[freetype2-demos] gsoc-2022-chariri-3 a19c547 16/36: [ftinspect] Move `C


From: Werner Lemberg
Subject: [freetype2-demos] gsoc-2022-chariri-3 a19c547 16/36: [ftinspect] Move `CharMapInfo` struct out from `engine.[ch]pp`
Date: Wed, 27 Jul 2022 06:32:45 -0400 (EDT)

branch: gsoc-2022-chariri-3
commit a19c5474cc6fb5928cad50b2643457d064f3b7bd
Author: Charlie Jiang <w@chariri.moe>
Commit: Charlie Jiang <w@chariri.moe>

    [ftinspect] Move `CharMapInfo` struct out from `engine.[ch]pp`
    
    (Pure mechnical commit)
    
    * src/ftinspect/engine/engine.hpp, src/ftinspect/engine/engine.cpp:
      Remove related code but add `include` directive.
    
    * src/ftinspect/engine/charmap.hpp, src/ftinspect/engine/charmap.cpp:
      New file.
    
    * src/ftinspect/CMakeLists.txt, src/ftinspect/meson.build: Updated.
---
 src/ftinspect/CMakeLists.txt     |   1 +
 src/ftinspect/engine/charmap.cpp | 140 +++++++++++++++++++++++++++++++++++++++
 src/ftinspect/engine/charmap.hpp |  36 ++++++++++
 src/ftinspect/engine/engine.cpp  | 131 ------------------------------------
 src/ftinspect/engine/engine.hpp  |  24 +------
 src/ftinspect/meson.build        |   1 +
 6 files changed, 179 insertions(+), 154 deletions(-)

diff --git a/src/ftinspect/CMakeLists.txt b/src/ftinspect/CMakeLists.txt
index b07d9c6..f8f7dea 100644
--- a/src/ftinspect/CMakeLists.txt
+++ b/src/ftinspect/CMakeLists.txt
@@ -22,6 +22,7 @@ add_executable(ftinspect
   
   "engine/engine.cpp"
   "engine/fontfilemanager.cpp"
+  "engine/charmap.cpp"
 
   "rendering/glyphbitmap.cpp"
   "rendering/glyphoutline.cpp"
diff --git a/src/ftinspect/engine/charmap.cpp b/src/ftinspect/engine/charmap.cpp
new file mode 100644
index 0000000..284bd0c
--- /dev/null
+++ b/src/ftinspect/engine/charmap.cpp
@@ -0,0 +1,140 @@
+// charmap.cpp
+
+// Copyright (C) 2022 by Charlie Jiang.
+
+#include "charmap.hpp"
+
+#include <QHash>
+
+QHash<FT_Encoding, QString> encodingNamesCache;
+QHash<FT_Encoding, QString>&
+encodingNames()
+{
+  if (encodingNamesCache.empty())
+  {
+    encodingNamesCache[static_cast<FT_Encoding>(FT_ENCODING_OTHER)]
+     = "Unknown Encoding";
+    encodingNamesCache[FT_ENCODING_NONE] = "No Encoding";
+    encodingNamesCache[FT_ENCODING_MS_SYMBOL] = "MS Symbol (symb)";
+    encodingNamesCache[FT_ENCODING_UNICODE] = "Unicode (unic)";
+    encodingNamesCache[FT_ENCODING_SJIS] = "Shift JIS (sjis)";
+    encodingNamesCache[FT_ENCODING_PRC] = "PRC/GB 18030 (gb)";
+    encodingNamesCache[FT_ENCODING_BIG5] = "Big5 (big5)";
+    encodingNamesCache[FT_ENCODING_WANSUNG] = "Wansung (wans)";
+    encodingNamesCache[FT_ENCODING_JOHAB] = "Johab (joha)";
+    encodingNamesCache[FT_ENCODING_ADOBE_STANDARD] = "Adobe Standard (ADOB)";
+    encodingNamesCache[FT_ENCODING_ADOBE_EXPERT] = "Adobe Expert (ADBE)";
+    encodingNamesCache[FT_ENCODING_ADOBE_CUSTOM] = "Adobe Custom (ADBC)";
+    encodingNamesCache[FT_ENCODING_ADOBE_LATIN_1] = "Latin 1 (lat1)";
+    encodingNamesCache[FT_ENCODING_OLD_LATIN_2] = "Latin 2 (lat2)";
+    encodingNamesCache[FT_ENCODING_APPLE_ROMAN] = "Apple Roman (armn)";
+  }
+
+  return encodingNamesCache;
+}
+
+
+CharMapInfo::CharMapInfo(int index, FT_CharMap cmap)
+: index(index), ptr(cmap), encoding(cmap->encoding), maxIndex(-1)
+{
+  auto& names = encodingNames();
+  auto it = names.find(encoding);
+  if (it == names.end())
+    encodingName = &names[static_cast<FT_Encoding>(FT_ENCODING_OTHER)];
+  else
+    encodingName = &it.value();
+
+  if (encoding != FT_ENCODING_OTHER)
+    maxIndex = computeMaxIndex();
+}
+
+
+QString
+CharMapInfo::stringifyIndex(int code, int index)
+{
+  return QString("CharCode: %1 (glyph idx %2)")
+           .arg(stringifyIndexShort(code))
+           .arg(index);
+}
+
+
+QString
+CharMapInfo::stringifyIndexShort(int code)
+{
+  return (encoding == FT_ENCODING_UNICODE ? "U+" : "0x")
+         + QString::number(code, 16).rightJustified(4, '0').toUpper();
+}
+
+
+int
+CharMapInfo::computeMaxIndex()
+{
+  int maxIndex;
+  switch (encoding)
+  {
+  case FT_ENCODING_UNICODE:
+    maxIndex = maxIndexForFaceAndCharMap(ptr, 0x110000) + 1;
+    break;
+
+  case FT_ENCODING_ADOBE_LATIN_1:
+  case FT_ENCODING_ADOBE_STANDARD:
+  case FT_ENCODING_ADOBE_EXPERT:
+  case FT_ENCODING_ADOBE_CUSTOM:
+  case FT_ENCODING_APPLE_ROMAN:
+    maxIndex = 0x100;
+    break;
+
+  /* some fonts use range 0x00-0x100, others have 0xF000-0xF0FF */
+  case FT_ENCODING_MS_SYMBOL:
+    maxIndex = maxIndexForFaceAndCharMap(ptr, 0x10000) + 1;
+    break;
+
+  default:
+    // Some encodings can reach > 0x10000, e.g. GB 18030.
+    maxIndex = maxIndexForFaceAndCharMap(ptr, 0x110000) + 1;
+  }
+  return maxIndex;
+}
+
+
+int
+CharMapInfo::maxIndexForFaceAndCharMap(FT_CharMap charMap,
+                                       unsigned max)
+{
+  // code adopted from `ftcommon.c`
+  FT_ULong min = 0;
+  FT_UInt glyphIndex;
+  FT_Face face = charMap->face;
+
+  if (FT_Set_Charmap(face, charMap))
+    return -1;
+
+  do
+  {
+    FT_ULong mid = (min + max) >> 1;
+    FT_ULong res = FT_Get_Next_Char(face, mid, &glyphIndex);
+
+    if (glyphIndex)
+      min = res;
+    else
+    {
+      max = mid;
+
+      // once moved, it helps to advance min through sparse regions
+      if (min)
+      {
+        res = FT_Get_Next_Char(face, min, &glyphIndex);
+
+        if (glyphIndex)
+          min = res;
+        else
+          max = min; // found it
+      }
+    }
+  } while (max > min);
+
+  return static_cast<int>(max);
+}
+
+
+// end of charmap.cpp
diff --git a/src/ftinspect/engine/charmap.hpp b/src/ftinspect/engine/charmap.hpp
new file mode 100644
index 0000000..f64d08b
--- /dev/null
+++ b/src/ftinspect/engine/charmap.hpp
@@ -0,0 +1,36 @@
+// charmap.hpp
+
+// Copyright (C) 2022 by Charlie Jiang.
+
+#pragma once
+
+#include <QString>
+
+#include <ft2build.h>
+#include <freetype/freetype.h>
+
+class Engine;
+
+#define FT_ENCODING_OTHER 0xFFFE
+struct CharMapInfo
+{
+  int index;
+  FT_CharMap ptr;
+  FT_Encoding encoding;
+  QString* encodingName;
+
+  // Actually this shouldn't go here, but for convenience...
+  int maxIndex;
+
+  CharMapInfo(int index, FT_CharMap cmap);
+
+  QString stringifyIndex(int code, int index);
+  QString stringifyIndexShort(int code);
+
+private:
+  int computeMaxIndex();
+  static int maxIndexForFaceAndCharMap(FT_CharMap charMap, unsigned max);
+};
+
+
+// end of charmap.hpp
diff --git a/src/ftinspect/engine/engine.cpp b/src/ftinspect/engine/engine.cpp
index 9dc33fd..6e73ec3 100644
--- a/src/ftinspect/engine/engine.cpp
+++ b/src/ftinspect/engine/engine.cpp
@@ -901,135 +901,4 @@ glyphFormatToName(FT_Glyph_Format format)
 }
 
 
-QHash<FT_Encoding, QString> encodingNamesCache;
-QHash<FT_Encoding, QString>&
-encodingNames()
-{
-  if (encodingNamesCache.empty())
-  {
-    encodingNamesCache[static_cast<FT_Encoding>(FT_ENCODING_OTHER)]
-     = "Unknown Encoding";
-    encodingNamesCache[FT_ENCODING_NONE] = "No Encoding";
-    encodingNamesCache[FT_ENCODING_MS_SYMBOL] = "MS Symbol (symb)";
-    encodingNamesCache[FT_ENCODING_UNICODE] = "Unicode (unic)";
-    encodingNamesCache[FT_ENCODING_SJIS] = "Shift JIS (sjis)";
-    encodingNamesCache[FT_ENCODING_PRC] = "PRC/GB 18030 (gb)";
-    encodingNamesCache[FT_ENCODING_BIG5] = "Big5 (big5)";
-    encodingNamesCache[FT_ENCODING_WANSUNG] = "Wansung (wans)";
-    encodingNamesCache[FT_ENCODING_JOHAB] = "Johab (joha)";
-    encodingNamesCache[FT_ENCODING_ADOBE_STANDARD] = "Adobe Standard (ADOB)";
-    encodingNamesCache[FT_ENCODING_ADOBE_EXPERT] = "Adobe Expert (ADBE)";
-    encodingNamesCache[FT_ENCODING_ADOBE_CUSTOM] = "Adobe Custom (ADBC)";
-    encodingNamesCache[FT_ENCODING_ADOBE_LATIN_1] = "Latin 1 (lat1)";
-    encodingNamesCache[FT_ENCODING_OLD_LATIN_2] = "Latin 2 (lat2)";
-    encodingNamesCache[FT_ENCODING_APPLE_ROMAN] = "Apple Roman (armn)";
-  }
-
-  return encodingNamesCache;
-}
-
-
-CharMapInfo::CharMapInfo(int index, FT_CharMap cmap)
-: index(index), ptr(cmap), encoding(cmap->encoding), maxIndex(-1)
-{
-  auto& names = encodingNames();
-  auto it = names.find(encoding);
-  if (it == names.end())
-    encodingName = &names[static_cast<FT_Encoding>(FT_ENCODING_OTHER)];
-  else
-    encodingName = &it.value();
-
-  if (encoding != FT_ENCODING_OTHER)
-    maxIndex = computeMaxIndex();
-}
-
-
-QString
-CharMapInfo::stringifyIndex(int code, int index)
-{
-  return QString("CharCode: %1 (glyph idx %2)")
-           .arg(stringifyIndexShort(code))
-           .arg(index);
-}
-
-
-QString
-CharMapInfo::stringifyIndexShort(int code)
-{
-  return (encoding == FT_ENCODING_UNICODE ? "U+" : "0x")
-         + QString::number(code, 16).rightJustified(4, '0').toUpper();
-}
-
-
-int
-CharMapInfo::computeMaxIndex()
-{
-  int maxIndex = 0;
-  switch (encoding)
-  {
-  case FT_ENCODING_UNICODE:
-    maxIndex = maxIndexForFaceAndCharMap(ptr, 0x110000) + 1;
-    break;
-
-  case FT_ENCODING_ADOBE_LATIN_1:
-  case FT_ENCODING_ADOBE_STANDARD:
-  case FT_ENCODING_ADOBE_EXPERT:
-  case FT_ENCODING_ADOBE_CUSTOM:
-  case FT_ENCODING_APPLE_ROMAN:
-    maxIndex = 0x100;
-    break;
-
-  /* some fonts use range 0x00-0x100, others have 0xF000-0xF0FF */
-  case FT_ENCODING_MS_SYMBOL:
-    maxIndex = maxIndexForFaceAndCharMap(ptr, 0x10000) + 1;
-    break;
-
-  default:
-    // Some encodings can reach > 0x10000, e.g. GB 18030.
-    maxIndex = maxIndexForFaceAndCharMap(ptr, 0x110000) + 1;
-  }
-  return maxIndex;
-}
-
-
-int
-CharMapInfo::maxIndexForFaceAndCharMap(FT_CharMap charMap,
-                                       unsigned max)
-{
-  // code adopted from `ftcommon.c`
-  FT_ULong min = 0;
-  FT_UInt glyphIndex;
-  FT_Face face = charMap->face;
-
-  if (FT_Set_Charmap(face, charMap))
-    return -1;
-
-  do
-  {
-    FT_ULong mid = (min + max) >> 1;
-    FT_ULong res = FT_Get_Next_Char(face, mid, &glyphIndex);
-
-    if (glyphIndex)
-      min = res;
-    else
-    {
-      max = mid;
-
-      // once moved, it helps to advance min through sparse regions
-      if (min)
-      {
-        res = FT_Get_Next_Char(face, min, &glyphIndex);
-
-        if (glyphIndex)
-          min = res;
-        else
-          max = min; // found it
-      }
-    }
-  } while (max > min);
-
-  return static_cast<int>(max);
-}
-
-
 // end of engine.cpp
diff --git a/src/ftinspect/engine/engine.hpp b/src/ftinspect/engine/engine.hpp
index 9c2d6e5..247e310 100644
--- a/src/ftinspect/engine/engine.hpp
+++ b/src/ftinspect/engine/engine.hpp
@@ -6,6 +6,7 @@
 #pragma once
 
 #include "fontfilemanager.hpp"
+#include "charmap.hpp"
 
 #include <vector>
 #include <QString>
@@ -39,29 +40,6 @@ struct FaceID
   bool operator<(const FaceID& other) const;
 };
 
-class Engine;
-
-#define FT_ENCODING_OTHER 0xFFFE
-struct CharMapInfo
-{
-  int index;
-  FT_CharMap ptr;
-  FT_Encoding encoding;
-  QString* encodingName;
-
-  // Actually this shouldn't go here, but for convenience...
-  int maxIndex;
-
-  CharMapInfo(int index, FT_CharMap cmap);
-
-  QString stringifyIndex(int code, int index);
-  QString stringifyIndexShort(int code);
-
-private:
-  int computeMaxIndex();
-  static int maxIndexForFaceAndCharMap(FT_CharMap charMap, unsigned max);
-};
-
 // Some helper functions.
 
 QString* glyphFormatToName(FT_Glyph_Format format);
diff --git a/src/ftinspect/meson.build b/src/ftinspect/meson.build
index 77f116f..dc3a02f 100644
--- a/src/ftinspect/meson.build
+++ b/src/ftinspect/meson.build
@@ -22,6 +22,7 @@ if qt5_dep.found()
   sources = files([
     'engine/engine.cpp',
     'engine/fontfilemanager.cpp',
+    'engine/charmap.cpp',
 
     'rendering/glyphbitmap.cpp',
     'rendering/glyphoutline.cpp',



reply via email to

[Prev in Thread] Current Thread [Next in Thread]