[poppler] 6 commits - cpp/poppler-document.cpp cpp/poppler-document-private.h cpp/poppler-global.cpp cpp/poppler-private.cpp cpp/poppler-private.h glib/poppler.cc glib/poppler-document.cc glib/poppler-private.h poppler/CairoOutputDev.cc poppler/Error.cc poppler/Error.h poppler/GlobalParams.cc poppler/GlobalParams.h qt5/src test/perf-test.cc

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Sat Jan 4 22:18:20 UTC 2020


 cpp/poppler-document-private.h |   21 +-----------
 cpp/poppler-document.cpp       |   69 ++++++++---------------------------------
 cpp/poppler-global.cpp         |    2 -
 cpp/poppler-private.cpp        |    2 -
 cpp/poppler-private.h          |    2 -
 glib/poppler-document.cc       |   31 +++++++-----------
 glib/poppler-private.h         |    3 +
 glib/poppler.cc                |   21 +++---------
 poppler/CairoOutputDev.cc      |    3 -
 poppler/Error.cc               |   11 +-----
 poppler/Error.h                |    6 +--
 poppler/GlobalParams.cc        |   52 ++++++++++++++++++++++++++++++
 poppler/GlobalParams.h         |   23 +++++++++++++
 qt5/src/poppler-document.cc    |    3 -
 qt5/src/poppler-private.cc     |   30 +----------------
 qt5/src/poppler-private.h      |   12 +++----
 test/perf-test.cc              |    4 +-
 17 files changed, 134 insertions(+), 161 deletions(-)

New commits:
commit 9349ed71f9fabc8961cc1dd9ca57aca8be7939db
Author: Adam Reichold <adam.reichold at t-online.de>
Date:   Sat Jan 4 09:40:03 2020 +0100

    Make use of the memoizing UTF-8 UnicodemMap getter in CairoOutputDev::drawChar.

diff --git a/poppler/CairoOutputDev.cc b/poppler/CairoOutputDev.cc
index eac8ed09..52b05cd4 100644
--- a/poppler/CairoOutputDev.cc
+++ b/poppler/CairoOutputDev.cc
@@ -1402,8 +1402,7 @@ void CairoOutputDev::drawChar(GfxState *state, double x, double y,
     glyphs[glyphCount].y = y - originY;
     glyphCount++;
     if (use_show_text_glyphs) {
-      GooString enc("UTF-8");
-      UnicodeMap *utf8Map = globalParams->getUnicodeMap(&enc);
+      const UnicodeMap *utf8Map = globalParams->getUtf8Map();
       if (utf8Max - utf8Count < uLen*6) {
         // utf8 encoded characters can be up to 6 bytes
 	if (utf8Max > uLen*6)
commit 5c7ec727e059738828ae61bafb5fe5f8b482b267
Author: Adam Reichold <adam.reichold at t-online.de>
Date:   Fri Jan 3 22:12:20 2020 +0100

    Remove the error callback data pointer as none of the frontends make use of it.

diff --git a/cpp/poppler-document.cpp b/cpp/poppler-document.cpp
index 15b83454..c62fe80b 100644
--- a/cpp/poppler-document.cpp
+++ b/cpp/poppler-document.cpp
@@ -85,7 +85,7 @@ document_private::document_private(const char *file_data, int file_data_length,
 }
 
 document_private::document_private()
-    : GlobalParamsIniter(detail::error_function, nullptr)
+    : GlobalParamsIniter(detail::error_function)
     , doc(nullptr)
     , raw_doc_data(nullptr)
     , raw_doc_data_length(0)
diff --git a/cpp/poppler-private.cpp b/cpp/poppler-private.cpp
index a532b350..4eb360fd 100644
--- a/cpp/poppler-private.cpp
+++ b/cpp/poppler-private.cpp
@@ -40,7 +40,7 @@ static void stderr_debug_function(const std::string &msg, void * /*data*/)
 debug_func detail::user_debug_function = stderr_debug_function;
 void *detail::debug_closure = nullptr;
 
-void detail::error_function(void * /*data*/, ErrorCategory /*category*/, Goffset pos, const char *msg)
+void detail::error_function(ErrorCategory /*category*/, Goffset pos, const char *msg)
 {
     std::ostringstream oss;
     if (pos >= 0) {
diff --git a/cpp/poppler-private.h b/cpp/poppler-private.h
index 7f2ca383..b5f6599e 100644
--- a/cpp/poppler-private.h
+++ b/cpp/poppler-private.h
@@ -47,7 +47,7 @@ namespace detail
 
 extern debug_func user_debug_function;
 extern void *debug_closure;
-void error_function(void *data, ErrorCategory category, Goffset pos, const char *msg);
+void error_function(ErrorCategory category, Goffset pos, const char *msg);
 
 rectf pdfrectangle_to_rectf(const PDFRectangle &pdfrect);
 
diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index 26621365..6615334c 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -193,7 +193,7 @@ poppler_document_new_from_file (const char  *uri,
   GooString *password_g;
   char *filename;
 
-  auto initer = new GlobalParamsIniter(_poppler_error_cb, nullptr);
+  auto initer = new GlobalParamsIniter(_poppler_error_cb);
 
   filename = g_filename_from_uri (uri, nullptr, error);
   if (!filename)
@@ -253,7 +253,7 @@ poppler_document_new_from_data (char        *data,
   MemStream *str;
   GooString *password_g;
 
-  auto initer = new GlobalParamsIniter(_poppler_error_cb, nullptr);
+  auto initer = new GlobalParamsIniter(_poppler_error_cb);
   
   // create stream
   str = new MemStream(data, 0, length, Object(objNull));
@@ -305,7 +305,7 @@ poppler_document_new_from_bytes (GBytes      *bytes,
   g_return_val_if_fail(bytes != nullptr, nullptr);
   g_return_val_if_fail(error == nullptr || *error == nullptr, nullptr);
 
-  auto initer = new GlobalParamsIniter(_poppler_error_cb, nullptr);
+  auto initer = new GlobalParamsIniter(_poppler_error_cb);
 
   // create stream
   str = new BytesStream(bytes, Object(objNull));
@@ -356,7 +356,7 @@ poppler_document_new_from_stream (GInputStream *stream,
   g_return_val_if_fail(G_IS_INPUT_STREAM(stream), NULL);
   g_return_val_if_fail(length == (goffset)-1 || length > 0, NULL);
 
-  auto initer = new GlobalParamsIniter(_poppler_error_cb, nullptr);
+  auto initer = new GlobalParamsIniter(_poppler_error_cb);
 
   if (!G_IS_SEEKABLE(stream) || !g_seekable_can_seek(G_SEEKABLE(stream))) {
     g_set_error_literal(error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
diff --git a/glib/poppler-private.h b/glib/poppler-private.h
index cba1de76..86b587fd 100644
--- a/glib/poppler-private.h
+++ b/glib/poppler-private.h
@@ -163,6 +163,6 @@ type_name##_get_type (void)
 	return g_define_type_id__volatile;                                            \
 }
 
-void _poppler_error_cb (void *data, ErrorCategory category, Goffset pos, const char *message);
+void _poppler_error_cb (ErrorCategory category, Goffset pos, const char *message);
 
 #endif
diff --git a/glib/poppler.cc b/glib/poppler.cc
index 31183372..5fffcbbb 100644
--- a/glib/poppler.cc
+++ b/glib/poppler.cc
@@ -84,8 +84,7 @@ poppler_get_version (void)
  */
 
 void
-_poppler_error_cb (void *data G_GNUC_UNUSED,
-                   ErrorCategory category,
+_poppler_error_cb (ErrorCategory category,
                    Goffset pos,
                    const char *message)
 {
diff --git a/poppler/Error.cc b/poppler/Error.cc
index f38666da..f161c4d7 100644
--- a/poppler/Error.cc
+++ b/poppler/Error.cc
@@ -46,11 +46,9 @@ static const char *errorCategoryNames[] = {
 };
 
 static ErrorCallback errorCbk = nullptr;
-static void *errorCbkData = nullptr;
 
-void setErrorCallback(ErrorCallback cbk, void *data) {
+void setErrorCallback(ErrorCallback cbk) {
   errorCbk = cbk;
-  errorCbkData = data;
 }
 
 void CDECL error(ErrorCategory category, Goffset pos, const char *msg, ...) {
@@ -76,7 +74,7 @@ void CDECL error(ErrorCategory category, Goffset pos, const char *msg, ...) {
   }
 
   if (errorCbk) {
-    (*errorCbk)(errorCbkData, category, pos, sanitized->c_str());
+    (*errorCbk)(category, pos, sanitized->c_str());
   } else {
     if (pos >= 0) {
       fprintf(stderr, "%s (%lld): %s\n",
diff --git a/poppler/Error.h b/poppler/Error.h
index 52ac80f0..22329c7e 100644
--- a/poppler/Error.h
+++ b/poppler/Error.h
@@ -47,9 +47,9 @@ enum ErrorCategory {
   errInternal          // internal error - malfunction within the Xpdf code
 };
 
-using ErrorCallback = void (*)(void *data, ErrorCategory category, Goffset pos, const char *msg);
+using ErrorCallback = void (*)(ErrorCategory category, Goffset pos, const char *msg);
 
-extern void setErrorCallback(ErrorCallback cbk, void *data);
+extern void setErrorCallback(ErrorCallback cbk);
 
 extern void CDECL error(ErrorCategory category, Goffset pos, const char *msg, ...) GOOSTRING_FORMAT;
 
diff --git a/poppler/GlobalParams.cc b/poppler/GlobalParams.cc
index 3c82bd48..ae0a1b6f 100644
--- a/poppler/GlobalParams.cc
+++ b/poppler/GlobalParams.cc
@@ -1258,14 +1258,14 @@ void GlobalParams::setErrQuiet(bool errQuietA) {
   errQuiet = errQuietA;
 }
 
-GlobalParamsIniter::GlobalParamsIniter(ErrorCallback errorCallback, void *errorCallbackData)
+GlobalParamsIniter::GlobalParamsIniter(ErrorCallback errorCallback)
 {
   std::lock_guard<std::mutex> lock{mutex};
 
   if (count == 0) {
     globalParams = std::make_unique<GlobalParams>(!customDataDir.empty() ? customDataDir.c_str() : nullptr);
 
-    setErrorCallback(errorCallback, errorCallbackData);
+    setErrorCallback(errorCallback);
   }
 
   count++;
diff --git a/poppler/GlobalParams.h b/poppler/GlobalParams.h
index 5ac536e2..eac31ade 100644
--- a/poppler/GlobalParams.h
+++ b/poppler/GlobalParams.h
@@ -223,7 +223,7 @@ private:
 class GlobalParamsIniter
 {
 public:
-  GlobalParamsIniter(ErrorCallback errorCallback, void *errorCallbackData);
+  GlobalParamsIniter(ErrorCallback errorCallback);
   ~GlobalParamsIniter();
 
   GlobalParamsIniter(const GlobalParamsIniter &) = delete;
diff --git a/qt5/src/poppler-private.cc b/qt5/src/poppler-private.cc
index b1346ad1..35ebbda6 100644
--- a/qt5/src/poppler-private.cc
+++ b/qt5/src/poppler-private.cc
@@ -60,7 +60,7 @@ namespace Debug {
         Debug::debugClosure = closure;
     }
 
-    void qt5ErrorFunction(void * /*data*/, ErrorCategory /*category*/, Goffset pos, const char *msg)
+    void qt5ErrorFunction(ErrorCategory /*category*/, Goffset pos, const char *msg)
     {
         QString emsg;
 
diff --git a/qt5/src/poppler-private.h b/qt5/src/poppler-private.h
index 3fc7f7ec..63158461 100644
--- a/qt5/src/poppler-private.h
+++ b/qt5/src/poppler-private.h
@@ -75,7 +75,7 @@ namespace Poppler {
 
     GooString *QDateTimeToUnicodeGooString(const QDateTime &dt);
 
-    void qt5ErrorFunction(void * /*data*/, ErrorCategory /*category*/, Goffset pos, const char *msg);
+    void qt5ErrorFunction(ErrorCategory /*category*/, Goffset pos, const char *msg);
 
     Annot::AdditionalActionsType toPopplerAdditionalActionType(Annotation::AdditionalActionType type);
 
@@ -96,7 +96,7 @@ namespace Poppler {
     class DocumentData : private GlobalParamsIniter {
     public:
 	DocumentData(const QString &filePath, GooString *ownerPassword, GooString *userPassword) :
-	GlobalParamsIniter(qt5ErrorFunction, nullptr)
+	GlobalParamsIniter(qt5ErrorFunction)
 	    {
 		init();
 		m_filePath = filePath;	
@@ -113,7 +113,7 @@ namespace Poppler {
 	    }
 	
 	DocumentData(const QByteArray &data, GooString *ownerPassword, GooString *userPassword) :
-	GlobalParamsIniter(qt5ErrorFunction, nullptr)
+	GlobalParamsIniter(qt5ErrorFunction)
 	    {
 		fileContents = data;
 		MemStream *str = new MemStream((char*)fileContents.data(), 0, fileContents.length(), Object(objNull));
diff --git a/test/perf-test.cc b/test/perf-test.cc
index 70220cd3..03f78788 100644
--- a/test/perf-test.cc
+++ b/test/perf-test.cc
@@ -739,7 +739,7 @@ static void StrList_Destroy(StrList **root)
     *root = nullptr;
 }
 
-static void my_error(void *, ErrorCategory, Goffset pos, const char *msg) {
+static void my_error(ErrorCategory, Goffset pos, const char *msg) {
 #if 0
     char        buf[4096], *p = buf;
 
@@ -1230,7 +1230,7 @@ static void RenderCmdLineArg(char *cmdLineArg)
 
 int main(int argc, char **argv)
 {
-    setErrorCallback(my_error, nullptr);
+    setErrorCallback(my_error);
     ParseCommandLine(argc, argv);
     if (0 == StrList_Len(&gArgsListRoot))
         PrintUsageAndExit(argc, argv);
commit 6de0bba0b7d064195b27619667dbbcb7cd810647
Author: Adam Reichold <adam.reichold at t-online.de>
Date:   Fri Jan 3 22:05:53 2020 +0100

    Make use of the GlobalParamsIniter to make the glib frontend initialization thread safe.

diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index e1b422a7..26621365 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -100,8 +100,9 @@ struct _PopplerDocumentClass
 G_DEFINE_TYPE (PopplerDocument, poppler_document, G_TYPE_OBJECT)
 
 static PopplerDocument *
-_poppler_document_new_from_pdfdoc (PDFDoc  *newDoc,
-                                   GError **error)
+_poppler_document_new_from_pdfdoc (GlobalParamsIniter *initer,
+                                   PDFDoc             *newDoc,
+                                   GError             **error)
 {
   PopplerDocument *document;
 
@@ -145,6 +146,7 @@ _poppler_document_new_from_pdfdoc (PDFDoc  *newDoc,
   }
 
   document = (PopplerDocument *) g_object_new (POPPLER_TYPE_DOCUMENT, nullptr);
+  document->initer = initer;
   document->doc = newDoc;
 
   document->output_dev = new CairoOutputDev ();
@@ -191,9 +193,7 @@ poppler_document_new_from_file (const char  *uri,
   GooString *password_g;
   char *filename;
 
-  if (!globalParams) {
-    globalParams = std::make_unique<GlobalParams>();
-  }
+  auto initer = new GlobalParamsIniter(_poppler_error_cb, nullptr);
 
   filename = g_filename_from_uri (uri, nullptr, error);
   if (!filename)
@@ -224,7 +224,7 @@ poppler_document_new_from_file (const char  *uri,
 
   delete password_g;
 
-  return _poppler_document_new_from_pdfdoc (newDoc, error);
+  return _poppler_document_new_from_pdfdoc (initer, newDoc, error);
 }
 
 /**
@@ -253,9 +253,7 @@ poppler_document_new_from_data (char        *data,
   MemStream *str;
   GooString *password_g;
 
-  if (!globalParams) {
-    globalParams = std::make_unique<GlobalParams>();
-  }
+  auto initer = new GlobalParamsIniter(_poppler_error_cb, nullptr);
   
   // create stream
   str = new MemStream(data, 0, length, Object(objNull));
@@ -264,7 +262,7 @@ poppler_document_new_from_data (char        *data,
   newDoc = new PDFDoc(str, password_g, password_g);
   delete password_g;
 
-  return _poppler_document_new_from_pdfdoc (newDoc, error);
+  return _poppler_document_new_from_pdfdoc (initer, newDoc, error);
 }
 
 class BytesStream : public MemStream
@@ -307,9 +305,7 @@ poppler_document_new_from_bytes (GBytes      *bytes,
   g_return_val_if_fail(bytes != nullptr, nullptr);
   g_return_val_if_fail(error == nullptr || *error == nullptr, nullptr);
 
-  if (!globalParams) {
-    globalParams = std::make_unique<GlobalParams>();
-  }
+  auto initer = new GlobalParamsIniter(_poppler_error_cb, nullptr);
 
   // create stream
   str = new BytesStream(bytes, Object(objNull));
@@ -318,7 +314,7 @@ poppler_document_new_from_bytes (GBytes      *bytes,
   newDoc = new PDFDoc(str, password_g, password_g);
   delete password_g;
 
-  return _poppler_document_new_from_pdfdoc (newDoc, error);
+  return _poppler_document_new_from_pdfdoc (initer, newDoc, error);
 }
 
 static inline gboolean
@@ -360,9 +356,7 @@ poppler_document_new_from_stream (GInputStream *stream,
   g_return_val_if_fail(G_IS_INPUT_STREAM(stream), NULL);
   g_return_val_if_fail(length == (goffset)-1 || length > 0, NULL);
 
-  if (!globalParams) {
-    globalParams = std::make_unique<GlobalParams>();
-  }
+  auto initer = new GlobalParamsIniter(_poppler_error_cb, nullptr);
 
   if (!G_IS_SEEKABLE(stream) || !g_seekable_can_seek(G_SEEKABLE(stream))) {
     g_set_error_literal(error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
@@ -388,7 +382,7 @@ poppler_document_new_from_stream (GInputStream *stream,
   newDoc = new PDFDoc(str, password_g, password_g);
   delete password_g;
 
-  return _poppler_document_new_from_pdfdoc (newDoc, error);
+  return _poppler_document_new_from_pdfdoc (initer, newDoc, error);
 }
 
 /**
@@ -548,6 +542,7 @@ poppler_document_finalize (GObject *object)
   poppler_document_layers_free (document);
   delete document->output_dev;
   delete document->doc;
+  delete document->initer;
 
   G_OBJECT_CLASS (poppler_document_parent_class)->finalize (object);
 }
diff --git a/glib/poppler-private.h b/glib/poppler-private.h
index 3e6b4b17..cba1de76 100644
--- a/glib/poppler-private.h
+++ b/glib/poppler-private.h
@@ -24,6 +24,7 @@ struct _PopplerDocument
 {
   /*< private >*/
   GObject parent_instance;
+  GlobalParamsIniter *initer;
   PDFDoc *doc;
 
   GList *layers;
@@ -162,4 +163,6 @@ type_name##_get_type (void)
 	return g_define_type_id__volatile;                                            \
 }
 
+void _poppler_error_cb (void *data, ErrorCategory category, Goffset pos, const char *message);
+
 #endif
diff --git a/glib/poppler.cc b/glib/poppler.cc
index 55347c2d..31183372 100644
--- a/glib/poppler.cc
+++ b/glib/poppler.cc
@@ -23,6 +23,8 @@
 #include <Error.h>
 #endif
 
+#include "poppler-private.h"
+
 /**
  * SECTION: poppler-errors
  * @title: Error handling
@@ -76,18 +78,16 @@ poppler_get_version (void)
   return poppler_version;
 }
 
-#if  __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
-
 /* We want to install an error callback so that PDF syntax warnings etc
  * can be redirected through the GLib logging API instead of always just
  * going to stderr.
  */
 
-static void
-error_cb (void *data G_GNUC_UNUSED,
-          ErrorCategory category,
-          Goffset pos,
-          const char *message)
+void
+_poppler_error_cb (void *data G_GNUC_UNUSED,
+                   ErrorCategory category,
+                   Goffset pos,
+                   const char *message)
 {
   static const char * const cat_str[] = {
     "Syntax warning",
@@ -110,11 +110,3 @@ error_cb (void *data G_GNUC_UNUSED,
          "%s at position %" G_GOFFSET_FORMAT ": %s",
          cat_str[category], (goffset) pos, message);
 }
-
-static void __attribute__((__constructor__))
-poppler_constructor ()
-{
-  setErrorCallback (error_cb, nullptr);
-}
-
-#endif /* GNUC */
commit a8220105f964ee4102e4ee886df2d0b162b42ae6
Author: Adam Reichold <adam.reichold at t-online.de>
Date:   Fri Jan 3 21:51:25 2020 +0100

    Reuse the GlobalParamsIniter in the qt5 frontend.

diff --git a/poppler/GlobalParams.cc b/poppler/GlobalParams.cc
index d091ce4e..3c82bd48 100644
--- a/poppler/GlobalParams.cc
+++ b/poppler/GlobalParams.cc
@@ -414,6 +414,8 @@ GlobalParams::GlobalParams(const char *customPopplerDataDir)
   unicodeMapCache = new UnicodeMapCache();
   cMapCache = new CMapCache();
 
+  utf8Map = nullptr;
+
   baseFontsInitialized = false;
 
   // set up the initial nameToUnicode tables
@@ -1132,6 +1134,16 @@ std::string GlobalParams::getTextEncodingName() const {
   return textEncoding->toStr();
 }
 
+const UnicodeMap *GlobalParams::getUtf8Map() {
+  if (!utf8Map) {
+    GooString enc("UTF-8");
+    utf8Map = globalParams->getUnicodeMap(&enc);
+    utf8Map->incRefCnt();
+  }
+
+  return utf8Map;
+}
+
 bool GlobalParams::getPrintCommands() {
   globalParamsLocker();
   return printCommands;
diff --git a/poppler/GlobalParams.h b/poppler/GlobalParams.h
index 415cae4e..5ac536e2 100644
--- a/poppler/GlobalParams.h
+++ b/poppler/GlobalParams.h
@@ -139,6 +139,8 @@ public:
   CMap *getCMap(const GooString *collection, const GooString *cMapName, Stream *stream = nullptr);
   UnicodeMap *getTextEncoding();
 
+  const UnicodeMap *getUtf8Map();
+
   std::vector<GooString*> *getEncodingNames();
 
   //----- functions to set parameters
@@ -208,6 +210,8 @@ private:
   CharCodeToUnicodeCache *unicodeToUnicodeCache;
   UnicodeMapCache *unicodeMapCache;
   CMapCache *cMapCache;
+
+  UnicodeMap *utf8Map;
   
   mutable std::recursive_mutex mutex;
   mutable std::recursive_mutex unicodeMapCacheMutex;
diff --git a/qt5/src/poppler-document.cc b/qt5/src/poppler-document.cc
index ba9b86cd..658cba0f 100644
--- a/qt5/src/poppler-document.cc
+++ b/qt5/src/poppler-document.cc
@@ -57,9 +57,6 @@
 
 namespace Poppler {
 
-  int DocumentData::count = 0;
-  QMutex DocumentData::mutex;
-
   Document *Document::load(const QString &filePath, const QByteArray &ownerPassword,
 			   const QByteArray &userPassword)
     {
diff --git a/qt5/src/poppler-private.cc b/qt5/src/poppler-private.cc
index 3c9bf512..b1346ad1 100644
--- a/qt5/src/poppler-private.cc
+++ b/qt5/src/poppler-private.cc
@@ -54,15 +54,13 @@ namespace Debug {
 
 }
 
-    static UnicodeMap *utf8Map = nullptr;
-
     void setDebugErrorFunction(PopplerDebugFunc function, const QVariant &closure)
     {
         Debug::debugFunction = function ? function : Debug::qDebugDebugFunction;
         Debug::debugClosure = closure;
     }
 
-    static void qt5ErrorFunction(void * /*data*/, ErrorCategory /*category*/, Goffset pos, const char *msg)
+    void qt5ErrorFunction(void * /*data*/, ErrorCategory /*category*/, Goffset pos, const char *msg)
     {
         QString emsg;
 
@@ -79,12 +77,7 @@ namespace Debug {
     }
 
     QString unicodeToQString(const Unicode* u, int len) {
-        if (!utf8Map)
-        {
-                GooString enc("UTF-8");
-                utf8Map = globalParams->getUnicodeMap(&enc);
-                utf8Map->incRefCnt();
-        }
+        const UnicodeMap *utf8Map = globalParams->getUtf8Map();
 
         // ignore the last character if it is 0x0
         if ((len > 0) && (u[len - 1] == 0))
@@ -242,15 +235,6 @@ namespace Debug {
         qDeleteAll(m_embeddedFiles);
         delete (OptContentModel *)m_optContentModel;
         delete doc;
-    
-        QMutexLocker locker{&mutex};
-
-        count --;
-        if ( count == 0 )
-        {
-            utf8Map = nullptr;
-            globalParams.reset();
-        }
       }
     
     void DocumentData::init()
@@ -259,16 +243,6 @@ namespace Debug {
         paperColor = Qt::white;
         m_hints = 0;
         m_optContentModel = nullptr;
-      
-        QMutexLocker locker{&mutex};
-
-        if ( count == 0 )
-        {
-            utf8Map = nullptr;
-            globalParams = std::make_unique<GlobalParams>();
-            setErrorCallback(qt5ErrorFunction, nullptr);
-        }
-        count ++;
     }
 
 
diff --git a/qt5/src/poppler-private.h b/qt5/src/poppler-private.h
index 28b5ca30..3fc7f7ec 100644
--- a/qt5/src/poppler-private.h
+++ b/qt5/src/poppler-private.h
@@ -75,7 +75,7 @@ namespace Poppler {
 
     GooString *QDateTimeToUnicodeGooString(const QDateTime &dt);
 
-    void qt5ErrorFunction(int pos, char *msg, va_list args);
+    void qt5ErrorFunction(void * /*data*/, ErrorCategory /*category*/, Goffset pos, const char *msg);
 
     Annot::AdditionalActionsType toPopplerAdditionalActionType(Annotation::AdditionalActionType type);
 
@@ -93,9 +93,10 @@ namespace Poppler {
             bool externalDest;
     };
 
-    class DocumentData {
+    class DocumentData : private GlobalParamsIniter {
     public:
-	DocumentData(const QString &filePath, GooString *ownerPassword, GooString *userPassword)
+	DocumentData(const QString &filePath, GooString *ownerPassword, GooString *userPassword) :
+	GlobalParamsIniter(qt5ErrorFunction, nullptr)
 	    {
 		init();
 		m_filePath = filePath;	
@@ -111,7 +112,8 @@ namespace Poppler {
 		delete userPassword;
 	    }
 	
-	DocumentData(const QByteArray &data, GooString *ownerPassword, GooString *userPassword)
+	DocumentData(const QByteArray &data, GooString *ownerPassword, GooString *userPassword) :
+	GlobalParamsIniter(qt5ErrorFunction, nullptr)
 	    {
 		fileContents = data;
 		MemStream *str = new MemStream((char*)fileContents.data(), 0, fileContents.length(), Object(objNull));
@@ -158,8 +160,6 @@ namespace Poppler {
 	QPointer<OptContentModel> m_optContentModel;
 	QColor paperColor;
 	int m_hints;
-	static int count;
-	static QMutex mutex;
     };
 
     class FontInfoData
commit 7daaa2925858ef1740ec38b25be7bf74c0aefa33
Author: Adam Reichold <adam.reichold at t-online.de>
Date:   Fri Jan 3 21:29:23 2020 +0100

    Move the initer of the cpp frontend into the core to able to reuse it for the qt5 and glib frontends.

diff --git a/cpp/poppler-document-private.h b/cpp/poppler-document-private.h
index fbf330e8..7af13658 100644
--- a/cpp/poppler-document-private.h
+++ b/cpp/poppler-document-private.h
@@ -26,6 +26,7 @@
 #include "poppler-config.h"
 #include "GooString.h"
 #include "PDFDoc.h"
+#include "GlobalParams.h"
 
 #include <vector>
 
@@ -35,24 +36,7 @@ namespace poppler
 class document;
 class embedded_file;
 
-class initer
-{
-public:
-    initer();
-    ~initer();
-
-    initer(const initer &) = delete;
-    initer& operator=(const initer &) = delete;
-
-    static bool set_data_dir(const std::string &new_data_dir);
-
-private:
-    static std::mutex mutex;
-    static unsigned int count;
-    static std::string data_dir;
-};
-
-class document_private : private initer
+class document_private : private GlobalParamsIniter
 {
 public:
     document_private(GooString *file_path, const std::string &owner_password,
@@ -62,6 +46,7 @@ public:
     document_private(const char *file_data, int file_data_length,
                      const std::string &owner_password,
                      const std::string &user_password);
+    document_private();
     ~document_private();
 
     static document* check_document(document_private *doc, byte_array *file_data);
diff --git a/cpp/poppler-document.cpp b/cpp/poppler-document.cpp
index 7023c0f4..15b83454 100644
--- a/cpp/poppler-document.cpp
+++ b/cpp/poppler-document.cpp
@@ -50,53 +50,9 @@
 
 using namespace poppler;
 
-std::mutex poppler::initer::mutex;
-unsigned int poppler::initer::count = 0U;
-std::string poppler::initer::data_dir;
-
-initer::initer()
-{
-    std::lock_guard<std::mutex> lock{mutex};
-
-    if (!count) {
-        globalParams = std::make_unique<GlobalParams>(!data_dir.empty() ? data_dir.c_str() : nullptr);
-        setErrorCallback(detail::error_function, nullptr);
-    }
-    count++;
-}
-
-initer::~initer()
-{
-    std::lock_guard<std::mutex> lock{mutex};
-
-    if (count > 0) {
-        --count;
-        if (!count) {
-            globalParams.reset();
-        }
-    }
-}
-
-bool initer::set_data_dir(const std::string &new_data_dir)
-{
-    std::lock_guard<std::mutex> lock{mutex};
-
-    if (count == 0) {
-        data_dir = new_data_dir;
-        return true;
-    }
-
-    return false;
-}
-
-
 document_private::document_private(GooString *file_path, const std::string &owner_password,
                                    const std::string &user_password)
-    : initer()
-    , doc(nullptr)
-    , raw_doc_data(nullptr)
-    , raw_doc_data_length(0)
-    , is_locked(false)
+    : document_private()
 {
     GooString goo_owner_password(owner_password.c_str());
     GooString goo_user_password(user_password.c_str());
@@ -106,11 +62,7 @@ document_private::document_private(GooString *file_path, const std::string &owne
 document_private::document_private(byte_array *file_data,
                                    const std::string &owner_password,
                                    const std::string &user_password)
-    : initer()
-    , doc(nullptr)
-    , raw_doc_data(nullptr)
-    , raw_doc_data_length(0)
-    , is_locked(false)
+    : document_private()
 {
     file_data->swap(doc_data);
     MemStream *memstr = new MemStream(&doc_data[0], 0, doc_data.size(), Object(objNull));
@@ -122,18 +74,25 @@ document_private::document_private(byte_array *file_data,
 document_private::document_private(const char *file_data, int file_data_length,
                                    const std::string &owner_password,
                                    const std::string &user_password)
-    : initer()
-    , doc(nullptr)
-    , raw_doc_data(file_data)
-    , raw_doc_data_length(file_data_length)
-    , is_locked(false)
+    : document_private()
 {
+    raw_doc_data = file_data;
+    raw_doc_data_length = file_data_length;
     MemStream *memstr = new MemStream(const_cast<char *>(raw_doc_data), 0, raw_doc_data_length, Object(objNull));
     GooString goo_owner_password(owner_password.c_str());
     GooString goo_user_password(user_password.c_str());
     doc = new PDFDoc(memstr, &goo_owner_password, &goo_user_password);
 }
 
+document_private::document_private()
+    : GlobalParamsIniter(detail::error_function, nullptr)
+    , doc(nullptr)
+    , raw_doc_data(nullptr)
+    , raw_doc_data_length(0)
+    , is_locked(false)
+{
+}
+
 document_private::~document_private()
 {
     delete_all(embedded_files);
diff --git a/cpp/poppler-global.cpp b/cpp/poppler-global.cpp
index 41ce08f7..adca42ba 100644
--- a/cpp/poppler-global.cpp
+++ b/cpp/poppler-global.cpp
@@ -374,7 +374,7 @@ std::ostream& poppler::operator<<(std::ostream& stream, const byte_array &array)
  */
 bool poppler::set_data_dir(const std::string &new_data_dir)
 {
-    return initer::set_data_dir(new_data_dir);
+    return GlobalParamsIniter::setCustomDataDir(new_data_dir);
 }
 
 /**
diff --git a/poppler/GlobalParams.cc b/poppler/GlobalParams.cc
index 71824bc2..d091ce4e 100644
--- a/poppler/GlobalParams.cc
+++ b/poppler/GlobalParams.cc
@@ -1245,3 +1245,43 @@ void GlobalParams::setErrQuiet(bool errQuietA) {
   globalParamsLocker();
   errQuiet = errQuietA;
 }
+
+GlobalParamsIniter::GlobalParamsIniter(ErrorCallback errorCallback, void *errorCallbackData)
+{
+  std::lock_guard<std::mutex> lock{mutex};
+
+  if (count == 0) {
+    globalParams = std::make_unique<GlobalParams>(!customDataDir.empty() ? customDataDir.c_str() : nullptr);
+
+    setErrorCallback(errorCallback, errorCallbackData);
+  }
+
+  count++;
+}
+
+GlobalParamsIniter::~GlobalParamsIniter()
+{
+  std::lock_guard<std::mutex> lock{mutex};
+
+  --count;
+
+  if (count == 0) {
+    globalParams.reset();
+  }
+}
+
+bool GlobalParamsIniter::setCustomDataDir(const std::string &dir)
+{
+  std::lock_guard<std::mutex> lock{mutex};
+
+  if (count == 0) {
+    customDataDir = dir;
+    return true;
+  }
+
+  return false;
+}
+
+std::mutex GlobalParamsIniter::mutex;
+int GlobalParamsIniter::count = 0;
+std::string GlobalParamsIniter::customDataDir;
diff --git a/poppler/GlobalParams.h b/poppler/GlobalParams.h
index 2e72982a..415cae4e 100644
--- a/poppler/GlobalParams.h
+++ b/poppler/GlobalParams.h
@@ -42,6 +42,7 @@
 #include <cstdio>
 #include "CharTypes.h"
 #include "UnicodeMap.h"
+#include "Error.h"
 #include <unordered_map>
 #include <string>
 #include <memory>
@@ -215,4 +216,22 @@ private:
   const char *popplerDataDir;
 };
 
+class GlobalParamsIniter
+{
+public:
+  GlobalParamsIniter(ErrorCallback errorCallback, void *errorCallbackData);
+  ~GlobalParamsIniter();
+
+  GlobalParamsIniter(const GlobalParamsIniter &) = delete;
+  GlobalParamsIniter &operator=(const GlobalParamsIniter &) = delete;
+
+  static bool setCustomDataDir(const std::string &dir);
+
+private:
+  static std::mutex mutex;
+  static int count;
+
+  static std::string customDataDir;
+};
+
 #endif
commit f2197842c8e7a8c4f7ee15eba1bd42379803e229
Author: Adam Reichold <adam.reichold at t-online.de>
Date:   Fri Jan 3 21:22:44 2020 +0100

    Improve readability of Error module by using a type alias for the error callback function pointer.

diff --git a/poppler/Error.cc b/poppler/Error.cc
index d8817b95..f38666da 100644
--- a/poppler/Error.cc
+++ b/poppler/Error.cc
@@ -45,13 +45,10 @@ static const char *errorCategoryNames[] = {
   "Internal Error"
 };
 
-static void (*errorCbk)(void *data, ErrorCategory category,
-			Goffset pos, const char *msg) = nullptr;
+static ErrorCallback errorCbk = nullptr;
 static void *errorCbkData = nullptr;
 
-void setErrorCallback(void (*cbk)(void *data, ErrorCategory category,
-				  Goffset pos, const char *msg),
-		      void *data) {
+void setErrorCallback(ErrorCallback cbk, void *data) {
   errorCbk = cbk;
   errorCbkData = data;
 }
diff --git a/poppler/Error.h b/poppler/Error.h
index e6df4461..52ac80f0 100644
--- a/poppler/Error.h
+++ b/poppler/Error.h
@@ -47,9 +47,9 @@ enum ErrorCategory {
   errInternal          // internal error - malfunction within the Xpdf code
 };
 
-extern void setErrorCallback(void (*cbk)(void *data, ErrorCategory category,
-					 Goffset pos, const char *msg),
-			     void *data);
+using ErrorCallback = void (*)(void *data, ErrorCategory category, Goffset pos, const char *msg);
+
+extern void setErrorCallback(ErrorCallback cbk, void *data);
 
 extern void CDECL error(ErrorCategory category, Goffset pos, const char *msg, ...) GOOSTRING_FORMAT;
 


More information about the poppler mailing list