From 1a84a22a401988dc5ed2cdbeffe5603e491dd9f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans-Ulrich=20J=C3=BCttner?= Date: Mon, 17 Jul 2017 12:18:43 +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 | 12 ++++++++ qt5/src/poppler-form.cc | 12 +++++++- qt5/src/poppler-form.h | 10 ++++++ 4 files changed, 114 insertions(+), 1 deletion(-) diff --git a/poppler/Form.cc b/poppler/Form.cc index 4627a43..21e5c89 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" @@ -316,6 +318,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); @@ -1078,6 +1090,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); + GooString da; + for (int i = 0; i < daToks->getLength(); ++i) { + if (i > 0) + da.append(' '); + if (i == idx) { + da.appendf("{0:d}", fontSize); + } else { + da.append(static_cast(daToks->get(i))); + } + } + deleteGooList(daToks, GooString); + Object objDA; + obj.dictSet("DA", objDA.initString(da.copy())); + xref->setModifiedObject(&obj, ref); + } +} + +int FormFieldText::parseDA(GooList* daToks) +{ + int idx = -1; + if (obj.isDict()) { + Object objDA; + obj.dictLookup("DA", &objDA); + 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; + } + } + } + } + objDA.free(); + } + return idx; +} + //------------------------------------------------------------------------ // FormFieldChoice diff --git a/poppler/Form.h b/poppler/Form.h index 8ddb6fe..c9f711b 100644 --- a/poppler/Form.h +++ b/poppler/Form.h @@ -197,6 +197,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 +406,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 1ccb26c..2ac8258 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)) { @@ -343,6 +342,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