summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Heinecke <aheinecke@gnupg.org>2023-05-05 15:09:00 +0200
committerAlbert Astals Cid <tsdgeos@yahoo.es>2023-05-12 11:59:07 +0000
commitbeb63c33457ed41033fb071eb25bb61e393280f6 (patch)
treeee2d7633a167c18ef5cbc4fe8b68de6daeb2e9a3
parentaefd09f9eb69b91ae2cae0351280aaa66742dd84 (diff)
Fix crash when signing existing signature
On windows, helvetica is not available and that makes poppler crash, so fix crash and add fallback to arial. This has already been implemented for "new" signatures, both the fallback and the crash in 09fdfecea3a13d30b5c52e1258d17549739d9ea8 and a563801e1a6be5e70645472d82f4ba8534b454d1 Refactor the fallback code to be reusable and use the same crash guard.
-rw-r--r--poppler/Form.cc25
-rw-r--r--poppler/Form.h4
-rw-r--r--poppler/PDFDoc.cc21
3 files changed, 28 insertions, 22 deletions
diff --git a/poppler/Form.cc b/poppler/Form.cc
index 9e86abc2..958ff3e1 100644
--- a/poppler/Form.cc
+++ b/poppler/Form.cc
@@ -41,6 +41,7 @@
#include <config.h>
+#include <array>
#include <set>
#include <limits>
#include <cstddef>
@@ -702,9 +703,9 @@ bool FormWidgetSignature::signDocumentWithAppearance(const std::string &saveFile
std::string originalDefaultAppearance = aux ? aux->toStr() : std::string();
Form *form = doc->getCatalog()->getCreateForm();
- std::string pdfFontName = form->findFontInDefaultResources("Helvetica", "");
+ const std::string pdfFontName = form->findPdfFontNameToUseForSigning();
if (pdfFontName.empty()) {
- pdfFontName = form->addFontToDefaultResources("Helvetica", "").fontName;
+ return false;
}
const DefaultAppearance da { { objName, pdfFontName.c_str() }, fontSize, std::move(fontColor) };
@@ -3113,6 +3114,26 @@ void Form::reset(const std::vector<std::string> &fields, bool excludeFields)
}
}
+std::string Form::findPdfFontNameToUseForSigning()
+{
+ static constexpr std::array<const char *, 2> fontsToUseToSign = { "Helvetica", "Arial" };
+ for (const char *fontToUseToSign : fontsToUseToSign) {
+ std::string pdfFontName = findFontInDefaultResources(fontToUseToSign, "");
+ if (!pdfFontName.empty()) {
+ return pdfFontName;
+ }
+
+ pdfFontName = addFontToDefaultResources(fontToUseToSign, "").fontName;
+ if (!pdfFontName.empty()) {
+ return pdfFontName;
+ }
+ }
+
+ error(errInternal, -1, "Form::findPdfFontNameToUseForSigning: No suitable font found'\n");
+
+ return {};
+}
+
//------------------------------------------------------------------------
// FormPageWidgets
//------------------------------------------------------------------------
diff --git a/poppler/Form.h b/poppler/Form.h
index 23c40701..0ead4ea6 100644
--- a/poppler/Form.h
+++ b/poppler/Form.h
@@ -685,6 +685,10 @@ public:
// has the given fontFamily and fontStyle. This makes us relatively sure that we added that font ourselves
std::string findFontInDefaultResources(const std::string &fontFamily, const std::string &fontStyle) const;
+ // Finds in the default resources a font that is suitable to create a signature annotation.
+ // If none is found then it is added to the default resources.
+ std::string findPdfFontNameToUseForSigning();
+
struct AddFontResult
{
std::string fontName;
diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc
index f1b9bfcb..fc2f1c37 100644
--- a/poppler/PDFDoc.cc
+++ b/poppler/PDFDoc.cc
@@ -64,7 +64,6 @@
#include <config.h>
#include <poppler-config.h>
-#include <array>
#include <cctype>
#include <clocale>
#include <cstdio>
@@ -2154,24 +2153,6 @@ bool PDFDoc::hasJavascript()
return jsInfo.containsJS();
}
-static std::string findPdfFontNameToUseForSigning(Form *form)
-{
- static constexpr std::array<const char *, 2> fontsToUseToSign = { "Helvetica", "Arial" };
- for (const char *fontToUseToSign : fontsToUseToSign) {
- std::string pdfFontName = form->findFontInDefaultResources(fontToUseToSign, "");
- if (!pdfFontName.empty()) {
- return pdfFontName;
- }
-
- pdfFontName = form->addFontToDefaultResources(fontToUseToSign, "").fontName;
- if (!pdfFontName.empty()) {
- return pdfFontName;
- }
- }
-
- return {};
-}
-
bool PDFDoc::sign(const std::string &saveFilename, const std::string &certNickname, const std::string &password, GooString *partialFieldName, int page, const PDFRectangle &rect, const GooString &signatureText,
const GooString &signatureTextLeft, double fontSize, double leftFontSize, std::unique_ptr<AnnotColor> &&fontColor, double borderWidth, std::unique_ptr<AnnotColor> &&borderColor,
std::unique_ptr<AnnotColor> &&backgroundColor, const GooString *reason, const GooString *location, const std::string &imagePath, const std::optional<GooString> &ownerPassword, const std::optional<GooString> &userPassword)
@@ -2189,7 +2170,7 @@ bool PDFDoc::sign(const std::string &saveFilename, const std::string &certNickna
}
Form *form = catalog->getCreateForm();
- const std::string pdfFontName = findPdfFontNameToUseForSigning(form);
+ const std::string pdfFontName = form->findPdfFontNameToUseForSigning();
if (pdfFontName.empty()) {
return false;
}