diff options
author | Erich E. Hoover <erich.e.hoover@gmail.com> | 2022-10-26 11:33:48 -0600 |
---|---|---|
committer | Albert Astals Cid <aacid@kde.org> | 2024-02-18 17:45:13 +0000 |
commit | 7b189dc13c1e8de7df23326ce5b2ff93b76dc712 (patch) | |
tree | 1fe17bcf1dd46ef92ed6dcdfa825de4223c7861d | |
parent | 4a278246628850d5b7dc0ee3f6cbc9d38999e85a (diff) |
Form: Allow signDocumentWithAppearance to autocalculate the font size
-rw-r--r-- | poppler/Form.cc | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/poppler/Form.cc b/poppler/Form.cc index acb381ff..f950026f 100644 --- a/poppler/Form.cc +++ b/poppler/Form.cc @@ -701,6 +701,23 @@ bool FormWidgetSignature::signDocument(const std::string &saveFilename, const st return true; } +static std::tuple<double, double> calculateDxDy(int rot, const PDFRectangle *rect) +{ + switch (rot) { + case 90: + return { rect->y2 - rect->y1, rect->x2 - rect->x1 }; + + case 180: + return { rect->x2 - rect->y2, rect->y2 - rect->y1 }; + + case 270: + return { rect->y2 - rect->y1, rect->x2 - rect->x1 }; + + default: // assume rot == 0 + return { rect->x2 - rect->x1, rect->y2 - rect->y1 }; + } +} + bool FormWidgetSignature::signDocumentWithAppearance(const std::string &saveFilename, const std::string &certNickname, const std::string &password, const GooString *reason, const GooString *location, const std::optional<GooString> &ownerPassword, const std::optional<GooString> &userPassword, 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) @@ -714,11 +731,26 @@ bool FormWidgetSignature::signDocumentWithAppearance(const std::string &saveFile if (pdfFontName.empty()) { return false; } + std::shared_ptr<GfxFont> font = form->getDefaultResources()->lookupFont(pdfFontName.c_str()); + double x1, y1, x2, y2; + getRect(&x1, &y1, &x2, &y2); + const PDFRectangle rect(x1, y1, x2, y2); + std::unique_ptr<AnnotAppearanceCharacs> origAppearCharacs = getWidgetAnnotation()->getAppearCharacs() ? getWidgetAnnotation()->getAppearCharacs()->copy() : nullptr; + const int rot = origAppearCharacs ? origAppearCharacs->getRotation() : 0; + const auto dxdy = calculateDxDy(rot, &rect); + const double dx = std::get<0>(dxdy); + const double dy = std::get<1>(dxdy); + const double wMax = dx - 2 * borderWidth - 4; + if (fontSize == 0) { + fontSize = Annot::calculateFontSize(form, font.get(), &signatureText, wMax / 2.0, dy); + } + if (leftFontSize == 0) { + leftFontSize = Annot::calculateFontSize(form, font.get(), &signatureTextLeft, wMax / 2.0, dy); + } const DefaultAppearance da { { objName, pdfFontName.c_str() }, fontSize, std::move(fontColor) }; getField()->setDefaultAppearance(da.toAppearanceString()); - std::unique_ptr<AnnotAppearanceCharacs> origAppearCharacs = getWidgetAnnotation()->getAppearCharacs() ? getWidgetAnnotation()->getAppearCharacs()->copy() : nullptr; auto appearCharacs = std::make_unique<AnnotAppearanceCharacs>(nullptr); appearCharacs->setBorderColor(std::move(borderColor)); appearCharacs->setBackColor(std::move(backgroundColor)); |