summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOliver Sander <oliver.sander@tu-dresden.de>2021-12-29 13:25:13 +0100
committerAlbert Astals Cid <tsdgeos@yahoo.es>2022-01-05 13:45:07 +0000
commit9389df54ca8f9c182153aa13eb1109ea98fb307c (patch)
tree38dc1364f0ceaa9b72bec4e184847ebe68ce8a3c
parent9451ca15dc63b908786e93f86f84862b11fc45dd (diff)
Allocate GfxFontLoc object on the stack
Compared to the heap storage used previously, this saves one layer of indirection and the heap memory management. The new code uses C++17's std::optional class to model the 'no fontLoc' value previously encoded by 'nullptr'.
-rw-r--r--[-rwxr-xr-x]poppler/CairoFontEngine.cc6
-rw-r--r--poppler/GfxFont.cc114
-rw-r--r--poppler/GfxFont.h12
-rw-r--r--poppler/PSOutputDev.cc10
-rw-r--r--poppler/SplashOutputDev.cc11
-rw-r--r--qt5/src/QPainterOutputDev.cc6
-rw-r--r--qt6/src/QPainterOutputDev.cc6
7 files changed, 80 insertions, 85 deletions
diff --git a/poppler/CairoFontEngine.cc b/poppler/CairoFontEngine.cc
index b9a1ad60..c39a3a55 100755..100644
--- a/poppler/CairoFontEngine.cc
+++ b/poppler/CairoFontEngine.cc
@@ -31,6 +31,7 @@
// Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
// Copyright (C) 2019 Christian Persch <chpe@src.gnome.org>
// Copyright (C) 2020 Michal <sudolskym@gmail.com>
+// Copyright (C) 2021 Oliver Sander <oliver.sander@tu-dresden.de>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
@@ -347,7 +348,7 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref, FT_Li
int font_data_len;
int i, n;
GfxFontType fontType;
- GfxFontLoc *fontLoc;
+ std::optional<GfxFontLoc> fontLoc;
char **enc;
const char *name;
FoFiTrueType *ff;
@@ -533,12 +534,9 @@ CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref, FT_Li
break;
}
- delete fontLoc;
return new CairoFreeTypeFont(ref, font_face, codeToGID, codeToGIDLen, substitute);
err2:
- /* hmm? */
- delete fontLoc;
gfree(codeToGID);
gfree(font_data);
fprintf(stderr, "some font thing failed\n");
diff --git a/poppler/GfxFont.cc b/poppler/GfxFont.cc
index 3abde7a4..e99985d4 100644
--- a/poppler/GfxFont.cc
+++ b/poppler/GfxFont.cc
@@ -178,6 +178,10 @@ GfxFontLoc::GfxFontLoc()
GfxFontLoc::~GfxFontLoc() = default;
+GfxFontLoc::GfxFontLoc(GfxFontLoc &&other) noexcept = default;
+
+GfxFontLoc &GfxFontLoc::operator=(GfxFontLoc &&other) noexcept = default;
+
void GfxFontLoc::setPath(GooString *pathA)
{
path = pathA->toStr();
@@ -619,16 +623,15 @@ CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits, CharCod
return ctu;
}
-GfxFontLoc *GfxFont::locateFont(XRef *xref, PSOutputDev *ps)
+std::optional<GfxFontLoc> GfxFont::locateFont(XRef *xref, PSOutputDev *ps)
{
- GfxFontLoc *fontLoc;
SysFontType sysFontType;
GooString *path, *base14Name;
int substIdx, fontNum;
bool embed;
if (type == fontType3) {
- return nullptr;
+ return std::nullopt;
}
//----- embedded font
@@ -665,36 +668,36 @@ GfxFontLoc *GfxFont::locateFont(XRef *xref, PSOutputDev *ps)
}
}
if (embed) {
- fontLoc = new GfxFontLoc();
- fontLoc->locType = gfxFontLocEmbedded;
- fontLoc->fontType = type;
- fontLoc->embFontID = embFontID;
- return fontLoc;
+ GfxFontLoc fontLoc;
+ fontLoc.locType = gfxFontLocEmbedded;
+ fontLoc.fontType = type;
+ fontLoc.embFontID = embFontID;
+ return std::move(fontLoc); // std::move only required to please g++-7
}
}
}
//----- PS passthrough
if (ps && !isCIDFont() && ps->getFontPassthrough()) {
- fontLoc = new GfxFontLoc();
- fontLoc->locType = gfxFontLocResident;
- fontLoc->fontType = fontType1;
- fontLoc->setPath(name->copy());
- return fontLoc;
+ GfxFontLoc fontLoc;
+ fontLoc.locType = gfxFontLocResident;
+ fontLoc.fontType = fontType1;
+ fontLoc.setPath(name->copy());
+ return std::move(fontLoc); // std::move only required to please g++-7
}
//----- PS resident Base-14 font
if (ps && !isCIDFont() && ((Gfx8BitFont *)this)->base14) {
- fontLoc = new GfxFontLoc();
- fontLoc->locType = gfxFontLocResident;
- fontLoc->fontType = fontType1;
- fontLoc->path = ((Gfx8BitFont *)this)->base14->base14Name;
- return fontLoc;
+ GfxFontLoc fontLoc;
+ fontLoc.locType = gfxFontLocResident;
+ fontLoc.fontType = fontType1;
+ fontLoc.path = ((Gfx8BitFont *)this)->base14->base14Name;
+ return std::move(fontLoc); // std::move only required to please g++-7
}
//----- external font file (fontFile, fontDir)
if (name && (path = globalParams->findFontFile(name))) {
- if ((fontLoc = getExternalFont(path, isCIDFont()))) {
+ if (std::optional<GfxFontLoc> fontLoc = getExternalFont(path, isCIDFont())) {
return fontLoc;
}
}
@@ -703,7 +706,7 @@ GfxFontLoc *GfxFont::locateFont(XRef *xref, PSOutputDev *ps)
if (!ps && !isCIDFont() && ((Gfx8BitFont *)this)->base14) {
base14Name = new GooString(((Gfx8BitFont *)this)->base14->base14Name);
if ((path = globalParams->findBase14FontFile(base14Name, this))) {
- if ((fontLoc = getExternalFont(path, false))) {
+ if (std::optional<GfxFontLoc> fontLoc = getExternalFont(path, false)) {
delete base14Name;
return fontLoc;
}
@@ -715,27 +718,27 @@ GfxFontLoc *GfxFont::locateFont(XRef *xref, PSOutputDev *ps)
if ((path = globalParams->findSystemFontFile(this, &sysFontType, &fontNum))) {
if (isCIDFont()) {
if (sysFontType == sysFontTTF || sysFontType == sysFontTTC) {
- fontLoc = new GfxFontLoc();
- fontLoc->locType = gfxFontLocExternal;
- fontLoc->fontType = fontCIDType2;
- fontLoc->setPath(path);
- fontLoc->fontNum = fontNum;
- return fontLoc;
+ GfxFontLoc fontLoc;
+ fontLoc.locType = gfxFontLocExternal;
+ fontLoc.fontType = fontCIDType2;
+ fontLoc.setPath(path);
+ fontLoc.fontNum = fontNum;
+ return std::move(fontLoc); // std::move only required to please g++-7
}
} else {
if (sysFontType == sysFontTTF || sysFontType == sysFontTTC) {
- fontLoc = new GfxFontLoc();
- fontLoc->locType = gfxFontLocExternal;
- fontLoc->fontType = fontTrueType;
- fontLoc->setPath(path);
- return fontLoc;
+ GfxFontLoc fontLoc;
+ fontLoc.locType = gfxFontLocExternal;
+ fontLoc.fontType = fontTrueType;
+ fontLoc.setPath(path);
+ return std::move(fontLoc); // std::move only required to please g++-7
} else if (sysFontType == sysFontPFA || sysFontType == sysFontPFB) {
- fontLoc = new GfxFontLoc();
- fontLoc->locType = gfxFontLocExternal;
- fontLoc->fontType = fontType1;
- fontLoc->setPath(path);
- fontLoc->fontNum = fontNum;
- return fontLoc;
+ GfxFontLoc fontLoc;
+ fontLoc.locType = gfxFontLocExternal;
+ fontLoc.fontType = fontType1;
+ fontLoc.setPath(path);
+ fontLoc.fontNum = fontNum;
+ return std::move(fontLoc); // std::move only required to please g++-7
}
}
delete path;
@@ -760,16 +763,16 @@ GfxFontLoc *GfxFont::locateFont(XRef *xref, PSOutputDev *ps)
GooString substName(base14SubstFonts[substIdx]);
if (ps) {
error(errSyntaxWarning, -1, "Substituting font '{0:s}' for '{1:s}'", base14SubstFonts[substIdx], name ? name->c_str() : "null");
- fontLoc = new GfxFontLoc();
- fontLoc->locType = gfxFontLocResident;
- fontLoc->fontType = fontType1;
- fontLoc->path = substName.toStr();
- fontLoc->substIdx = substIdx;
- return fontLoc;
+ GfxFontLoc fontLoc;
+ fontLoc.locType = gfxFontLocResident;
+ fontLoc.fontType = fontType1;
+ fontLoc.path = substName.toStr();
+ fontLoc.substIdx = substIdx;
+ return std::move(fontLoc); // std::move only required to please g++-7
} else {
path = globalParams->findFontFile(&substName);
if (path) {
- if ((fontLoc = getExternalFont(path, false))) {
+ if (std::optional<GfxFontLoc> fontLoc = getExternalFont(path, false)) {
error(errSyntaxWarning, -1, "Substituting font '{0:s}' for '{1:s}'", base14SubstFonts[substIdx], name ? name->c_str() : "");
name = new GooString(base14SubstFonts[substIdx]);
fontLoc->substIdx = substIdx;
@@ -779,29 +782,28 @@ GfxFontLoc *GfxFont::locateFont(XRef *xref, PSOutputDev *ps)
}
// failed to find a substitute font
- return nullptr;
+ return std::nullopt;
}
// failed to find a substitute font
- return nullptr;
+ return std::nullopt;
}
-GfxFontLoc *GfxFont::locateBase14Font(const GooString *base14Name)
+std::optional<GfxFontLoc> GfxFont::locateBase14Font(const GooString *base14Name)
{
GooString *path;
path = globalParams->findFontFile(base14Name);
if (!path) {
- return nullptr;
+ return std::nullopt;
}
return getExternalFont(path, false);
}
-GfxFontLoc *GfxFont::getExternalFont(GooString *path, bool cid)
+std::optional<GfxFontLoc> GfxFont::getExternalFont(GooString *path, bool cid)
{
FoFiIdentifierType fft;
GfxFontType fontType;
- GfxFontLoc *fontLoc;
fft = FoFiIdentifier::identifyFile(path->c_str());
switch (fft) {
@@ -833,13 +835,13 @@ GfxFontLoc *GfxFont::getExternalFont(GooString *path, bool cid)
}
if (fontType == fontUnknownType || (cid ? (fontType < fontCIDType0) : (fontType >= fontCIDType0))) {
delete path;
- return nullptr;
+ return std::nullopt;
}
- fontLoc = new GfxFontLoc();
- fontLoc->locType = gfxFontLocExternal;
- fontLoc->fontType = fontType;
- fontLoc->setPath(path);
- return fontLoc;
+ GfxFontLoc fontLoc;
+ fontLoc.locType = gfxFontLocExternal;
+ fontLoc.fontType = fontType;
+ fontLoc.setPath(path);
+ return std::move(fontLoc); // std::move only required to please g++-7
}
char *GfxFont::readEmbFontFile(XRef *xref, int *len)
diff --git a/poppler/GfxFont.h b/poppler/GfxFont.h
index 0510acb1..e8cb4e3d 100644
--- a/poppler/GfxFont.h
+++ b/poppler/GfxFont.h
@@ -34,6 +34,8 @@
#ifndef GFXFONT_H
#define GFXFONT_H
+#include <optional>
+
#include "goo/GooString.h"
#include "Object.h"
#include "CharTypes.h"
@@ -119,7 +121,9 @@ public:
~GfxFontLoc();
GfxFontLoc(const GfxFontLoc &) = delete;
+ GfxFontLoc(GfxFontLoc &&) noexcept;
GfxFontLoc &operator=(const GfxFontLoc &) = delete;
+ GfxFontLoc &operator=(GfxFontLoc &&other) noexcept;
// Set the 'path' string from a GooString on the heap.
// Ownership of the object is taken.
@@ -272,11 +276,11 @@ public:
virtual int getWMode() { return 0; }
// Locate the font file for this font. If <ps> is not null, includes PS
- // printer-resident fonts. Returns NULL on failure.
- GfxFontLoc *locateFont(XRef *xref, PSOutputDev *ps);
+ // printer-resident fonts. Returns std::optional without a value on failure.
+ std::optional<GfxFontLoc> locateFont(XRef *xref, PSOutputDev *ps);
// Locate a Base-14 font file for a specified font name.
- static GfxFontLoc *locateBase14Font(const GooString *base14Name);
+ static std::optional<GfxFontLoc> locateBase14Font(const GooString *base14Name);
// Read an external or embedded font file into a buffer.
char *readEmbFontFile(XRef *xref, int *len);
@@ -308,7 +312,7 @@ protected:
static GfxFontType getFontType(XRef *xref, Dict *fontDict, Ref *embID);
void readFontDescriptor(XRef *xref, Dict *fontDict);
CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits, CharCodeToUnicode *ctu);
- static GfxFontLoc *getExternalFont(GooString *path, bool cid);
+ static std::optional<GfxFontLoc> getExternalFont(GooString *path, bool cid);
const std::string tag; // PDF font tag
const Ref id; // reference (used as unique ID)
diff --git a/poppler/PSOutputDev.cc b/poppler/PSOutputDev.cc
index 743a223e..d014f471 100644
--- a/poppler/PSOutputDev.cc
+++ b/poppler/PSOutputDev.cc
@@ -35,7 +35,7 @@
// Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
// Copyright (C) 2018 Philipp Knechtges <philipp-dev@knechtges.com>
// Copyright (C) 2019, 2021 Christian Persch <chpe@src.gnome.org>
-// Copyright (C) 2019 Oliver Sander <oliver.sander@tu-dresden.de>
+// Copyright (C) 2019, 2021 Oliver Sander <oliver.sander@tu-dresden.de>
// Copyright (C) 2020, 2021 Philipp Knechtges <philipp-dev@knechtges.com>
// Copyright (C) 2021 Hubert Figuiere <hub@figuiere.net>
//
@@ -1947,7 +1947,6 @@ void PSOutputDev::setupFonts(Dict *resDict)
void PSOutputDev::setupFont(GfxFont *font, Dict *parentResDict)
{
- GfxFontLoc *fontLoc;
GooString *psName;
char buf[16];
bool subst;
@@ -1979,8 +1978,8 @@ void PSOutputDev::setupFont(GfxFont *font, Dict *parentResDict)
psName = GooString::format("T3_{0:d}_{1:d}", font->getID()->num, font->getID()->gen);
setupType3Font(font, psName, parentResDict);
} else {
- fontLoc = font->locateFont(xref, this);
- if (fontLoc != nullptr) {
+ std::optional<GfxFontLoc> fontLoc = font->locateFont(xref, this);
+ if (fontLoc) {
switch (fontLoc->locType) {
case gfxFontLocEmbedded:
switch (fontLoc->fontType) {
@@ -2068,7 +2067,6 @@ void PSOutputDev::setupFont(GfxFont *font, Dict *parentResDict)
} else {
error(errSyntaxError, -1, "Couldn't find a font to substitute for '{0:s}'", font->getName() ? font->getName()->c_str() : "(unnamed)");
}
- delete fontLoc;
return;
}
@@ -2091,8 +2089,6 @@ void PSOutputDev::setupFont(GfxFont *font, Dict *parentResDict)
xs = 1;
}
}
-
- delete fontLoc;
}
// generate PostScript code to set up the font
diff --git a/poppler/SplashOutputDev.cc b/poppler/SplashOutputDev.cc
index cbfb209d..4be799ce 100644
--- a/poppler/SplashOutputDev.cc
+++ b/poppler/SplashOutputDev.cc
@@ -42,7 +42,7 @@
// Copyright (C) 2018, 2019 Stefan Brüns <stefan.bruens@rwth-aachen.de>
// Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
// Copyright (C) 2019 Christian Persch <chpe@src.gnome.org>
-// Copyright (C) 2020 Oliver Sander <oliver.sander@tu-dresden.de>
+// Copyright (C) 2020, 2021 Oliver Sander <oliver.sander@tu-dresden.de>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
@@ -1824,7 +1824,6 @@ void SplashOutputDev::updateFont(GfxState * /*state*/)
void SplashOutputDev::doUpdateFont(GfxState *state)
{
GfxFont *gfxFont;
- GfxFontLoc *fontLoc;
GfxFontType fontType;
SplashOutFontFileID *id = nullptr;
SplashFontFile *fontFile;
@@ -1842,7 +1841,6 @@ void SplashOutputDev::doUpdateFont(GfxState *state)
needFontUpdate = false;
font = nullptr;
tmpBuf = nullptr;
- fontLoc = nullptr;
if (!(gfxFont = state->getFont())) {
goto err1;
@@ -1861,8 +1859,6 @@ void SplashOutputDev::doUpdateFont(GfxState *state)
// check the font file cache
reload:
delete id;
- delete fontLoc;
- fontLoc = nullptr;
if (fontsrc && !fontsrc->isFile) {
fontsrc->unref();
fontsrc = nullptr;
@@ -1874,7 +1870,8 @@ reload:
} else {
- if (!(fontLoc = gfxFont->locateFont((xref) ? xref : doc->getXRef(), nullptr))) {
+ std::optional<GfxFontLoc> fontLoc = gfxFont->locateFont((xref) ? xref : doc->getXRef(), nullptr);
+ if (!fontLoc) {
error(errSyntaxError, -1, "Couldn't find a font for '{0:s}'", gfxFont->getName() ? gfxFont->getName()->c_str() : "(unnamed)");
goto err2;
}
@@ -2076,14 +2073,12 @@ reload:
font = fontEngine->getFont(fontFile, mat, splash->getMatrix());
}
- delete fontLoc;
if (fontsrc && !fontsrc->isFile)
fontsrc->unref();
return;
err2:
delete id;
- delete fontLoc;
err1:
if (fontsrc && !fontsrc->isFile)
fontsrc->unref();
diff --git a/qt5/src/QPainterOutputDev.cc b/qt5/src/QPainterOutputDev.cc
index ab7ac045..1b3101dc 100644
--- a/qt5/src/QPainterOutputDev.cc
+++ b/qt5/src/QPainterOutputDev.cc
@@ -23,7 +23,7 @@
// Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
// Copyright (C) 2013 Dominik Haumann <dhaumann@kde.org>
// Copyright (C) 2013 Mihai Niculescu <q.quark@gmail.com>
-// Copyright (C) 2017, 2018, 2020 Oliver Sander <oliver.sander@tu-dresden.de>
+// Copyright (C) 2017, 2018, 2020, 2021 Oliver Sander <oliver.sander@tu-dresden.de>
// Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
// Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
@@ -456,7 +456,7 @@ void QPainterOutputDev::updateFont(GfxState *state)
// New font: load it into the cache
float fontSize = state->getFontSize();
- std::unique_ptr<GfxFontLoc> fontLoc(gfxFont->locateFont(xref, nullptr));
+ std::optional<GfxFontLoc> fontLoc = gfxFont->locateFont(xref, nullptr);
if (fontLoc) {
// load the font from respective location
@@ -524,7 +524,7 @@ void QPainterOutputDev::updateFont(GfxState *state)
std::unique_ptr<char[], void (*)(char *)> tmpBuf(nullptr, [](char *b) { free(b); });
int tmpBufLen = 0;
- std::unique_ptr<GfxFontLoc> fontLoc(gfxFont->locateFont(xref, nullptr));
+ std::optional<GfxFontLoc> fontLoc = gfxFont->locateFont(xref, nullptr);
if (!fontLoc) {
error(errSyntaxError, -1, "Couldn't find a font for '{0:s}'", gfxFont->getName() ? gfxFont->getName()->c_str() : "(unnamed)");
return;
diff --git a/qt6/src/QPainterOutputDev.cc b/qt6/src/QPainterOutputDev.cc
index ab7ac045..1b3101dc 100644
--- a/qt6/src/QPainterOutputDev.cc
+++ b/qt6/src/QPainterOutputDev.cc
@@ -23,7 +23,7 @@
// Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
// Copyright (C) 2013 Dominik Haumann <dhaumann@kde.org>
// Copyright (C) 2013 Mihai Niculescu <q.quark@gmail.com>
-// Copyright (C) 2017, 2018, 2020 Oliver Sander <oliver.sander@tu-dresden.de>
+// Copyright (C) 2017, 2018, 2020, 2021 Oliver Sander <oliver.sander@tu-dresden.de>
// Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
// Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
@@ -456,7 +456,7 @@ void QPainterOutputDev::updateFont(GfxState *state)
// New font: load it into the cache
float fontSize = state->getFontSize();
- std::unique_ptr<GfxFontLoc> fontLoc(gfxFont->locateFont(xref, nullptr));
+ std::optional<GfxFontLoc> fontLoc = gfxFont->locateFont(xref, nullptr);
if (fontLoc) {
// load the font from respective location
@@ -524,7 +524,7 @@ void QPainterOutputDev::updateFont(GfxState *state)
std::unique_ptr<char[], void (*)(char *)> tmpBuf(nullptr, [](char *b) { free(b); });
int tmpBufLen = 0;
- std::unique_ptr<GfxFontLoc> fontLoc(gfxFont->locateFont(xref, nullptr));
+ std::optional<GfxFontLoc> fontLoc = gfxFont->locateFont(xref, nullptr);
if (!fontLoc) {
error(errSyntaxError, -1, "Couldn't find a font for '{0:s}'", gfxFont->getName() ? gfxFont->getName()->c_str() : "(unnamed)");
return;