From a22f68069b40ab711cfab4acf3aae109e402f2c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans-Ulrich=20J=C3=BCttner?= Date: Wed, 2 Aug 2017 13:48:21 +0200 Subject: [PATCH] Added methods to get and set the font size of text fields Fixes bug #101692 --- poppler/Form.cc | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ poppler/Form.h | 13 ++++++++ qt5/src/poppler-form.cc | 12 +++++++- qt5/src/poppler-form.h | 10 ++++++ 4 files changed, 115 insertions(+), 1 deletion(-) diff --git a/poppler/Form.cc b/poppler/Form.cc index aaf9684..6a6f26c 100644 --- a/poppler/Form.cc +++ b/poppler/Form.cc @@ -29,6 +29,8 @@ #include #include +#include +#include #include #include "goo/gmem.h" #include "goo/GooString.h" @@ -311,6 +313,16 @@ int FormWidgetText::getMaxLen () const return parent()->getMaxLen (); } +double FormWidgetText::getTextFontSize() +{ + return parent()->getTextFontSize(); +} + +void FormWidgetText::setTextFontSize(int fontSize) +{ + parent()->setTextFontSize(fontSize); +} + void FormWidgetText::setContent(GooString* new_content) { parent()->setContentCopy(new_content); @@ -1045,6 +1057,75 @@ FormFieldText::~FormFieldText() delete content; } +double FormFieldText::getTextFontSize() +{ + GooList* daToks = new GooList(); + int idx = parseDA(daToks); + double fontSize = 0; + if (idx >= 0) { + char* p = nullptr; + fontSize = strtod(static_cast(daToks->get(idx))->getCString(), &p); + if (!p || *p) + fontSize = 0; + } + deleteGooList(daToks, GooString); + return fontSize; +} + +void FormFieldText::setTextFontSize(int fontSize) +{ + if (fontSize > 0 && obj.isDict()) { + GooList* daToks = new GooList(); + int idx = parseDA(daToks); + if (defaultAppearance) + delete defaultAppearance; + defaultAppearance = new GooString; + for (int i = 0; i < daToks->getLength(); ++i) { + if (i > 0) + defaultAppearance->append(' '); + if (i == idx) { + defaultAppearance->appendf("{0:d}", fontSize); + } else { + defaultAppearance->append(static_cast(daToks->get(i))); + } + } + deleteGooList(daToks, GooString); + obj.dictSet("DA", Object(defaultAppearance->copy())); + xref->setModifiedObject(&obj, ref); + updateChildrenAppearance(); + } +} + +int FormFieldText::parseDA(GooList* daToks) +{ + int idx = -1; + if (obj.isDict()) { + Object objDA(obj.dictLookup("DA")); + if (objDA.isString()) { + GooString* da = objDA.getString(); + if (da) { + int i = 0; + int j = 0; + while (i < da->getLength()) { + while (i < da->getLength() && isspace(da->getChar(i))) { + ++i; + } + if (i < da->getLength()) { + for (j = i + 1; j < da->getLength() && !isspace(da->getChar(j)); ++j) { + } + GooString* tok = new GooString(da, i, j - i); + if (!tok->cmp("Tf")) + idx = daToks->getLength() - 1; + daToks->append(tok); + i = j; + } + } + } + } + } + return idx; +} + //------------------------------------------------------------------------ // FormFieldChoice diff --git a/poppler/Form.h b/poppler/Form.h index d73ab89..32b35cf 100644 --- a/poppler/Form.h +++ b/poppler/Form.h @@ -23,6 +23,7 @@ #pragma interface #endif +#include "goo/GooList.h" #include "Object.h" #include "Annot.h" @@ -197,6 +198,11 @@ public: bool isComb () const; bool isRichText () const; int getMaxLen () const; + //return the font size of the field's text + double getTextFontSize(); + //set the font size of the field's text (currently only integer values) + void setTextFontSize(int fontSize); + protected: FormFieldText *parent() const; }; @@ -401,10 +407,17 @@ public: int getMaxLen () const { return maxLen; } + //return the font size of the field's text + double getTextFontSize(); + //set the font size of the field's text (currently only integer values) + void setTextFontSize(int fontSize); + #ifdef DEBUG_FORMS void print(int indent = 0); #endif protected: + int parseDA(GooList* daToks); + GooString* content; bool multiline; bool password; diff --git a/qt5/src/poppler-form.cc b/qt5/src/poppler-form.cc index 6cbceb0..dbec85e 100644 --- a/qt5/src/poppler-form.cc +++ b/qt5/src/poppler-form.cc @@ -180,7 +180,6 @@ Link *FormField::additionalAction(AdditionalActionType type) const return action; } - FormFieldButton::FormFieldButton(DocumentData *doc, ::Page *p, ::FormWidgetButton *w) : FormField(*new FormFieldData(doc, p, w)) { @@ -342,6 +341,17 @@ bool FormFieldText::canBeSpellChecked() const return !fwt->noSpellCheck(); } +double FormFieldText::getFontSize() const +{ + FormWidgetText* fwt = static_cast(m_formData->fm); + return fwt->getTextFontSize(); +} + +void FormFieldText::setFontSize(int fontSize) +{ + FormWidgetText* fwt = static_cast(m_formData->fm); + fwt->setTextFontSize(fontSize); +} FormFieldChoice::FormFieldChoice(DocumentData *doc, ::Page *p, ::FormWidgetChoice *w) : FormField(*new FormFieldData(doc, p, w)) diff --git a/qt5/src/poppler-form.h b/qt5/src/poppler-form.h index 44928b3..cd4692b 100644 --- a/qt5/src/poppler-form.h +++ b/qt5/src/poppler-form.h @@ -272,6 +272,16 @@ namespace Poppler { */ bool canBeSpellChecked() const; + /** + The font size of the text in the form field + */ + double getFontSize() const; + + /** + Set the font size of the text in the form field (currently only as integer) + */ + void setFontSize(int fontSize); + private: Q_DISABLE_COPY(FormFieldText) }; -- 1.9.1