[poppler] glib/poppler-document.cc poppler/FontInfo.cc poppler/FontInfo.h

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Feb 21 16:04:18 UTC 2022


 glib/poppler-document.cc |   17 +++++++----------
 poppler/FontInfo.cc      |   34 +++++-----------------------------
 poppler/FontInfo.h       |   18 +++++++++---------
 3 files changed, 21 insertions(+), 48 deletions(-)

New commits:
commit 996dfb015f5567cdaf191c127c2cf804f852d80b
Author: Oliver Sander <oliver.sander at tu-dresden.de>
Date:   Fri Feb 18 10:46:57 2022 +0100

    Store the strings in FontInfo in std::optional<std::string>
    
    This saves some memory allocations, because the strings are now
    stored by value rather than by a pointer pointing to the heap.
    
    Also, the costum copy constructor can be replaced by the default
    one with this change.

diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index 7da76987..9089023c 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -2781,14 +2781,13 @@ G_DEFINE_BOXED_TYPE(PopplerFontsIter, poppler_fonts_iter, poppler_fonts_iter_cop
  */
 const char *poppler_fonts_iter_get_full_name(PopplerFontsIter *iter)
 {
-    const GooString *name;
     FontInfo *info;
 
     info = iter->items[iter->index];
 
-    name = info->getName();
-    if (name != nullptr) {
-        return info->getName()->c_str();
+    std::optional<std::string> name = info->getName();
+    if (name) {
+        return name->c_str();
     } else {
         return nullptr;
     }
@@ -2834,13 +2833,12 @@ const char *poppler_fonts_iter_get_name(PopplerFontsIter *iter)
  */
 const char *poppler_fonts_iter_get_substitute_name(PopplerFontsIter *iter)
 {
-    const GooString *name;
     FontInfo *info;
 
     info = iter->items[iter->index];
 
-    name = info->getSubstituteName();
-    if (name != nullptr) {
+    std::optional<std::string> name = info->getSubstituteName();
+    if (name) {
         return name->c_str();
     } else {
         return nullptr;
@@ -2858,13 +2856,12 @@ const char *poppler_fonts_iter_get_substitute_name(PopplerFontsIter *iter)
  */
 const char *poppler_fonts_iter_get_file_name(PopplerFontsIter *iter)
 {
-    const GooString *file;
     FontInfo *info;
 
     info = iter->items[iter->index];
 
-    file = info->getFile();
-    if (file != nullptr) {
+    std::optional<std::string> file = info->getFile();
+    if (file) {
         return file->c_str();
     } else {
         return nullptr;
diff --git a/poppler/FontInfo.cc b/poppler/FontInfo.cc
index 53c9bf2d..038255c0 100644
--- a/poppler/FontInfo.cc
+++ b/poppler/FontInfo.cc
@@ -169,9 +169,7 @@ FontInfo::FontInfo(GfxFont *font, XRef *xref)
     // font name
     origName = font->getName();
     if (origName != nullptr) {
-        name = font->getName()->copy();
-    } else {
-        name = nullptr;
+        name = font->getName()->toStr();
     }
 
     // font type
@@ -184,15 +182,15 @@ FontInfo::FontInfo(GfxFont *font, XRef *xref)
         emb = font->getEmbeddedFontID(&embRef);
     }
 
-    file = nullptr;
-    substituteName = nullptr;
     if (!emb) {
         SysFontType dummy;
         int dummy2;
         GooString substituteNameAux;
-        file = globalParams->findSystemFontFile(font, &dummy, &dummy2, &substituteNameAux);
+        std::unique_ptr<GooString> tmpFile(globalParams->findSystemFontFile(font, &dummy, &dummy2, &substituteNameAux));
+        if (tmpFile)
+            file = tmpFile->toStr();
         if (substituteNameAux.getLength() > 0)
-            substituteName = substituteNameAux.copy();
+            substituteName = substituteNameAux.toStr();
     }
     encoding = font->getEncodingName();
 
@@ -207,25 +205,3 @@ FontInfo::FontInfo(GfxFont *font, XRef *xref)
     // sign
     subset = font->isSubset();
 }
-
-FontInfo::FontInfo(const FontInfo &f)
-{
-    name = f.name ? f.name->copy() : nullptr;
-    file = f.file ? f.file->copy() : nullptr;
-    encoding = f.encoding;
-    substituteName = f.substituteName ? f.substituteName->copy() : nullptr;
-    type = f.type;
-    emb = f.emb;
-    subset = f.subset;
-    hasToUnicode = f.hasToUnicode;
-    fontRef = f.fontRef;
-    embRef = f.embRef;
-}
-
-FontInfo::~FontInfo()
-{
-    delete name;
-    delete file;
-    if (substituteName)
-        delete substituteName;
-}
diff --git a/poppler/FontInfo.h b/poppler/FontInfo.h
index a798c5d2..0c920769 100644
--- a/poppler/FontInfo.h
+++ b/poppler/FontInfo.h
@@ -30,6 +30,8 @@
 #include "Object.h"
 #include "poppler_private_export.h"
 
+#include <optional>
+#include <string>
 #include <unordered_set>
 
 class GfxFont;
@@ -57,15 +59,13 @@ public:
     // Constructor.
     FontInfo(GfxFont *fontA, XRef *xrefA);
     // Copy constructor
-    FontInfo(const FontInfo &f);
-    // Destructor.
-    ~FontInfo();
+    FontInfo(const FontInfo &f) = default;
 
     FontInfo &operator=(const FontInfo &) = delete;
 
-    const GooString *getName() const { return name; };
-    const GooString *getSubstituteName() const { return substituteName; };
-    const GooString *getFile() const { return file; };
+    const std::optional<std::string> &getName() const { return name; };
+    const std::optional<std::string> &getSubstituteName() const { return substituteName; };
+    const std::optional<std::string> &getFile() const { return file; };
     const std::string &getEncoding() const { return encoding; };
     Type getType() const { return type; };
     bool getEmbedded() const { return emb; };
@@ -75,9 +75,9 @@ public:
     Ref getEmbRef() const { return embRef; };
 
 private:
-    GooString *name;
-    GooString *substituteName;
-    GooString *file;
+    std::optional<std::string> name;
+    std::optional<std::string> substituteName;
+    std::optional<std::string> file;
     std::string encoding;
     Type type;
     bool emb;


More information about the poppler mailing list