[poppler] 3 commits - poppler/PSOutputDev.cc poppler/PSOutputDev.h

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Jun 16 09:16:22 UTC 2023


 poppler/PSOutputDev.cc |   57 +++++++++++++------------------------------------
 poppler/PSOutputDev.h  |   14 ++++++++++--
 2 files changed, 28 insertions(+), 43 deletions(-)

New commits:
commit b8c55037fcfc0d3910cad5a4e65b3161bd5ea585
Author: Oliver Sander <oliver.sander at tu-dresden.de>
Date:   Wed Jun 14 14:11:52 2023 +0200

    Store PSOutPaperSize objects by value
    
    This simplifies the code, and saves a few heap allocations.

diff --git a/poppler/PSOutputDev.cc b/poppler/PSOutputDev.cc
index 79b54844..7b264f57 100644
--- a/poppler/PSOutputDev.cc
+++ b/poppler/PSOutputDev.cc
@@ -972,18 +972,6 @@ struct PSOutImgClipRect
     int x0, x1, y0, y1;
 };
 
-//------------------------------------------------------------------------
-
-struct PSOutPaperSize
-{
-    PSOutPaperSize(std::string &&nameA, int wA, int hA) : name(nameA), w(wA), h(hA) { }
-    ~PSOutPaperSize() = default;
-    PSOutPaperSize(const PSOutPaperSize &) = delete;
-    PSOutPaperSize &operator=(const PSOutPaperSize &) = delete;
-    std::string name;
-    int w, h;
-};
-
 //------------------------------------------------------------------------
 // DeviceNRecoder
 //------------------------------------------------------------------------
@@ -1307,7 +1295,6 @@ void PSOutputDev::postInit()
 {
     Catalog *catalog;
     PDFRectangle *box;
-    PSOutPaperSize *size;
     int w, h, i;
 
     if (postInitDone || !ok) {
@@ -1361,8 +1348,8 @@ void PSOutputDev::postInit()
             paperHeight = h;
         }
         for (i = 0; i < (int)paperSizes.size(); ++i) {
-            size = paperSizes[i];
-            if (pageDimensionEqual(w, size->w) && pageDimensionEqual(h, size->h)) {
+            const PSOutPaperSize &size = paperSizes[i];
+            if (pageDimensionEqual(w, size.w) && pageDimensionEqual(h, size.h)) {
                 break;
             }
         }
@@ -1381,7 +1368,7 @@ void PSOutputDev::postInit()
             if (name.empty()) {
                 name = GooString::format("{0:d}x{1:d}mm", int(w * 25.4 / 72), int(h * 25.4 / 72))->toStr();
             }
-            paperSizes.push_back(new PSOutPaperSize(std::move(name), w, h));
+            paperSizes.emplace_back(std::move(name), w, h);
         }
         pagePaperSize.insert(std::pair<int, int>(pg, i));
         if (!paperMatch) {
@@ -1534,9 +1521,6 @@ PSOutputDev::~PSOutputDev()
         }
 #endif
     }
-    for (auto entry : paperSizes) {
-        delete entry;
-    }
     if (embFontList) {
         delete embFontList;
     }
@@ -1573,7 +1557,6 @@ PSOutputDev::~PSOutputDev()
 
 void PSOutputDev::writeHeader(int nPages, const PDFRectangle *mediaBox, const PDFRectangle *cropBox, int pageRotate, const char *title)
 {
-    PSOutPaperSize *size;
     double x1, y1, x2, y2;
 
     switch (mode) {
@@ -1619,16 +1602,15 @@ void PSOutputDev::writeHeader(int nPages, const PDFRectangle *mediaBox, const PD
     switch (mode) {
     case psModePS:
         for (std::size_t i = 0; i < paperSizes.size(); ++i) {
-            size = paperSizes[i];
-            writePSFmt("%%{0:s} {1:s} {2:d} {3:d} 0 () ()\n", i == 0 ? "DocumentMedia:" : "+", size->name.c_str(), size->w, size->h);
+            const PSOutPaperSize &size = paperSizes[i];
+            writePSFmt("%%{0:s} {1:s} {2:d} {3:d} 0 () ()\n", i == 0 ? "DocumentMedia:" : "+", size.name.c_str(), size.w, size.h);
         }
         writePSFmt("%%BoundingBox: 0 0 {0:d} {1:d}\n", paperWidth, paperHeight);
         writePSFmt("%%Pages: {0:d}\n", nPages);
         writePS("%%EndComments\n");
         if (!paperMatch) {
-            size = paperSizes[0];
             writePS("%%BeginDefaults\n");
-            writePSFmt("%%PageMedia: {0:s}\n", size->name.c_str());
+            writePSFmt("%%PageMedia: {0:s}\n", paperSizes[0].name.c_str());
             writePS("%%EndDefaults\n");
         }
         break;
@@ -3608,7 +3590,6 @@ void PSOutputDev::startPage(int pageNum, GfxState *state, XRef *xrefA)
     int imgWidth, imgHeight, imgWidth2, imgHeight2;
     bool landscape;
     GooString *s;
-    PSOutPaperSize *paperSize;
 
     if (!postInitDone) {
         postInit();
@@ -3760,8 +3741,8 @@ void PSOutputDev::startPage(int pageNum, GfxState *state, XRef *xrefA)
         ty += (rotate == 0 || rotate == 180) ? imgLLY : -imgLLX;
 
         if (paperMatch) {
-            paperSize = paperSizes[pagePaperSize[pageNum]];
-            writePSFmt("%%PageMedia: {0:s}\n", paperSize->name.c_str());
+            const PSOutPaperSize &paperSize = paperSizes[pagePaperSize[pageNum]];
+            writePSFmt("%%PageMedia: {0:s}\n", paperSize.name.c_str());
         }
 
         // Create a matrix with the same transform that will be output to PS
diff --git a/poppler/PSOutputDev.h b/poppler/PSOutputDev.h
index d1cd7922..cf6f1755 100644
--- a/poppler/PSOutputDev.h
+++ b/poppler/PSOutputDev.h
@@ -360,6 +360,16 @@ public:
     }
 
 private:
+    struct PSOutPaperSize
+    {
+        PSOutPaperSize() = default;
+        PSOutPaperSize(std::string &&nameA, int wA, int hA) : name(nameA), w(wA), h(hA) { }
+        ~PSOutPaperSize() = default;
+        PSOutPaperSize &operator=(const PSOutPaperSize &) = delete;
+        std::string name;
+        int w, h;
+    };
+
     void init(FoFiOutputFunc outputFuncA, void *outputStreamA, PSFileType fileTypeA, char *psTitleA, PDFDoc *doc, const std::vector<int> &pages, PSOutMode modeA, int imgLLXA, int imgLLYA, int imgURXA, int imgURYA, bool manualCtrlA,
               int paperWidthA, int paperHeightA, bool noCropA, bool duplexA, PSLevel levelA);
     void postInit();
@@ -470,8 +480,8 @@ private:
     int numTilingPatterns; // current number of nested tiling patterns
     int nextFunc; // next unique number to use for a function
 
-    std::vector<PSOutPaperSize *> paperSizes; // list of used paper sizes, if paperMatch
-                                              //   is true
+    std::vector<PSOutPaperSize> paperSizes; // list of used paper sizes, if paperMatch
+                                            //   is true
     std::map<int, int> pagePaperSize; // page num to paperSize entry mapping
     double tx0, ty0; // global translation
     double xScale0, yScale0; // global scaling
commit b2ae26814909047b27f30cffaed225b5bb9f96e9
Author: Oliver Sander <oliver.sander at tu-dresden.de>
Date:   Wed Jun 14 13:23:54 2023 +0200

    Store PSOutPaperSize::name as std::string
    
    Rather than as a pointer to a GooString
    
    Safer and simpler code, and less memory allocations.

diff --git a/poppler/PSOutputDev.cc b/poppler/PSOutputDev.cc
index 0cc7d859..79b54844 100644
--- a/poppler/PSOutputDev.cc
+++ b/poppler/PSOutputDev.cc
@@ -976,11 +976,11 @@ struct PSOutImgClipRect
 
 struct PSOutPaperSize
 {
-    PSOutPaperSize(std::unique_ptr<GooString> &&nameA, int wA, int hA) : name(std::move(nameA)), w(wA), h(hA) { }
+    PSOutPaperSize(std::string &&nameA, int wA, int hA) : name(nameA), w(wA), h(hA) { }
     ~PSOutPaperSize() = default;
     PSOutPaperSize(const PSOutPaperSize &) = delete;
     PSOutPaperSize &operator=(const PSOutPaperSize &) = delete;
-    std::unique_ptr<GooString> name;
+    std::string name;
     int w, h;
 };
 
@@ -1368,18 +1368,18 @@ void PSOutputDev::postInit()
         }
         if (i == (int)paperSizes.size()) {
             const StandardMedia *media = standardMedia;
-            std::unique_ptr<GooString> name;
+            std::string name;
             while (media->name) {
                 if (pageDimensionEqual(w, media->width) && pageDimensionEqual(h, media->height)) {
-                    name = std::make_unique<GooString>(media->name);
+                    name = std::string(media->name);
                     w = media->width;
                     h = media->height;
                     break;
                 }
                 media++;
             }
-            if (!name) {
-                name = GooString::format("{0:d}x{1:d}mm", int(w * 25.4 / 72), int(h * 25.4 / 72));
+            if (name.empty()) {
+                name = GooString::format("{0:d}x{1:d}mm", int(w * 25.4 / 72), int(h * 25.4 / 72))->toStr();
             }
             paperSizes.push_back(new PSOutPaperSize(std::move(name), w, h));
         }
@@ -1620,7 +1620,7 @@ void PSOutputDev::writeHeader(int nPages, const PDFRectangle *mediaBox, const PD
     case psModePS:
         for (std::size_t i = 0; i < paperSizes.size(); ++i) {
             size = paperSizes[i];
-            writePSFmt("%%{0:s} {1:t} {2:d} {3:d} 0 () ()\n", i == 0 ? "DocumentMedia:" : "+", size->name.get(), size->w, size->h);
+            writePSFmt("%%{0:s} {1:s} {2:d} {3:d} 0 () ()\n", i == 0 ? "DocumentMedia:" : "+", size->name.c_str(), size->w, size->h);
         }
         writePSFmt("%%BoundingBox: 0 0 {0:d} {1:d}\n", paperWidth, paperHeight);
         writePSFmt("%%Pages: {0:d}\n", nPages);
@@ -1628,7 +1628,7 @@ void PSOutputDev::writeHeader(int nPages, const PDFRectangle *mediaBox, const PD
         if (!paperMatch) {
             size = paperSizes[0];
             writePS("%%BeginDefaults\n");
-            writePSFmt("%%PageMedia: {0:t}\n", size->name.get());
+            writePSFmt("%%PageMedia: {0:s}\n", size->name.c_str());
             writePS("%%EndDefaults\n");
         }
         break;
@@ -3761,7 +3761,7 @@ void PSOutputDev::startPage(int pageNum, GfxState *state, XRef *xrefA)
 
         if (paperMatch) {
             paperSize = paperSizes[pagePaperSize[pageNum]];
-            writePSFmt("%%PageMedia: {0:t}\n", paperSize->name.get());
+            writePSFmt("%%PageMedia: {0:s}\n", paperSize->name.c_str());
         }
 
         // Create a matrix with the same transform that will be output to PS
commit 9306058027b24b926e98da9651df9d56e5f744b0
Author: Oliver Sander <oliver.sander at tu-dresden.de>
Date:   Wed Jun 14 11:16:13 2023 +0200

    Store the paperSizes vector of PSOutputDev as object
    
    Rather than storing a pointer and allocating the std::vector
    on the heap.
    
    This simplifies the code and saves a few heap allocations.

diff --git a/poppler/PSOutputDev.cc b/poppler/PSOutputDev.cc
index 80212e37..0cc7d859 100644
--- a/poppler/PSOutputDev.cc
+++ b/poppler/PSOutputDev.cc
@@ -1097,7 +1097,6 @@ PSOutputDev::PSOutputDev(const char *fileName, PDFDoc *docA, char *psTitleA, con
     font16Enc = nullptr;
     imgIDs = nullptr;
     formIDs = nullptr;
-    paperSizes = nullptr;
     embFontList = nullptr;
     customColors = nullptr;
     haveTextClip = false;
@@ -1155,7 +1154,6 @@ PSOutputDev::PSOutputDev(int fdA, PDFDoc *docA, char *psTitleA, const std::vecto
     font16Enc = nullptr;
     imgIDs = nullptr;
     formIDs = nullptr;
-    paperSizes = nullptr;
     embFontList = nullptr;
     customColors = nullptr;
     haveTextClip = false;
@@ -1194,7 +1192,6 @@ PSOutputDev::PSOutputDev(FoFiOutputFunc outputFuncA, void *outputStreamA, char *
     font16Enc = nullptr;
     imgIDs = nullptr;
     formIDs = nullptr;
-    paperSizes = nullptr;
     embFontList = nullptr;
     customColors = nullptr;
     haveTextClip = false;
@@ -1328,7 +1325,7 @@ void PSOutputDev::postInit()
         paperMatch = false;
     }
 
-    paperSizes = new std::vector<PSOutPaperSize *>();
+    paperSizes.clear();
     for (const int pg : pages) {
         Page *page = catalog->getPage(pg);
         if (page == nullptr) {
@@ -1363,13 +1360,13 @@ void PSOutputDev::postInit()
         if (h > paperHeight) {
             paperHeight = h;
         }
-        for (i = 0; i < (int)paperSizes->size(); ++i) {
-            size = (*paperSizes)[i];
+        for (i = 0; i < (int)paperSizes.size(); ++i) {
+            size = paperSizes[i];
             if (pageDimensionEqual(w, size->w) && pageDimensionEqual(h, size->h)) {
                 break;
             }
         }
-        if (i == (int)paperSizes->size()) {
+        if (i == (int)paperSizes.size()) {
             const StandardMedia *media = standardMedia;
             std::unique_ptr<GooString> name;
             while (media->name) {
@@ -1384,7 +1381,7 @@ void PSOutputDev::postInit()
             if (!name) {
                 name = GooString::format("{0:d}x{1:d}mm", int(w * 25.4 / 72), int(h * 25.4 / 72));
             }
-            paperSizes->push_back(new PSOutPaperSize(std::move(name), w, h));
+            paperSizes.push_back(new PSOutPaperSize(std::move(name), w, h));
         }
         pagePaperSize.insert(std::pair<int, int>(pg, i));
         if (!paperMatch) {
@@ -1537,11 +1534,8 @@ PSOutputDev::~PSOutputDev()
         }
 #endif
     }
-    if (paperSizes) {
-        for (auto entry : *paperSizes) {
-            delete entry;
-        }
-        delete paperSizes;
+    for (auto entry : paperSizes) {
+        delete entry;
     }
     if (embFontList) {
         delete embFontList;
@@ -1624,15 +1618,15 @@ void PSOutputDev::writeHeader(int nPages, const PDFRectangle *mediaBox, const PD
 
     switch (mode) {
     case psModePS:
-        for (std::size_t i = 0; i < paperSizes->size(); ++i) {
-            size = (*paperSizes)[i];
+        for (std::size_t i = 0; i < paperSizes.size(); ++i) {
+            size = paperSizes[i];
             writePSFmt("%%{0:s} {1:t} {2:d} {3:d} 0 () ()\n", i == 0 ? "DocumentMedia:" : "+", size->name.get(), size->w, size->h);
         }
         writePSFmt("%%BoundingBox: 0 0 {0:d} {1:d}\n", paperWidth, paperHeight);
         writePSFmt("%%Pages: {0:d}\n", nPages);
         writePS("%%EndComments\n");
         if (!paperMatch) {
-            size = (*paperSizes)[0];
+            size = paperSizes[0];
             writePS("%%BeginDefaults\n");
             writePSFmt("%%PageMedia: {0:t}\n", size->name.get());
             writePS("%%EndDefaults\n");
@@ -3766,7 +3760,7 @@ void PSOutputDev::startPage(int pageNum, GfxState *state, XRef *xrefA)
         ty += (rotate == 0 || rotate == 180) ? imgLLY : -imgLLX;
 
         if (paperMatch) {
-            paperSize = (*paperSizes)[pagePaperSize[pageNum]];
+            paperSize = paperSizes[pagePaperSize[pageNum]];
             writePSFmt("%%PageMedia: {0:t}\n", paperSize->name.get());
         }
 
diff --git a/poppler/PSOutputDev.h b/poppler/PSOutputDev.h
index 1d3ac143..d1cd7922 100644
--- a/poppler/PSOutputDev.h
+++ b/poppler/PSOutputDev.h
@@ -470,8 +470,8 @@ private:
     int numTilingPatterns; // current number of nested tiling patterns
     int nextFunc; // next unique number to use for a function
 
-    std::vector<PSOutPaperSize *> *paperSizes; // list of used paper sizes, if paperMatch
-                                               //   is true
+    std::vector<PSOutPaperSize *> paperSizes; // list of used paper sizes, if paperMatch
+                                              //   is true
     std::map<int, int> pagePaperSize; // page num to paperSize entry mapping
     double tx0, ty0; // global translation
     double xScale0, yScale0; // global scaling


More information about the poppler mailing list