From 3c6abae75c6f258736872164c4d1c70fb5d7d66d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans-Ulrich=20J=C3=BCttner?= Date: Thu, 17 Aug 2017 10:00:52 +0200 Subject: [PATCH] Added methods to get and set the font size of text fields Fixes bug #101692 --- poppler/Annot.cc | 23 +++---------- poppler/Form.cc | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ poppler/Form.h | 16 +++++++++ qt5/src/poppler-form.cc | 12 ++++++- qt5/src/poppler-form.h | 10 ++++++ 5 files changed, 129 insertions(+), 20 deletions(-) diff --git a/poppler/Annot.cc b/poppler/Annot.cc index bda5a90..4db0e8e 100644 --- a/poppler/Annot.cc +++ b/poppler/Annot.cc @@ -2839,29 +2839,14 @@ void AnnotFreeText::parseAppearanceString(GooString *da, double &fontsize, Annot fontcolor = NULL; if (da) { GooList * daToks = new GooList(); - int j, i = 0; + int i = FormFieldText::tokenizeDA(da, daToks); - // Tokenize - while (i < da->getLength()) { - while (i < da->getLength() && Lexer::isSpace(da->getChar(i))) { - ++i; - } - if (i < da->getLength()) { - for (j = i + 1; j < da->getLength() && !Lexer::isSpace(da->getChar(j)); ++j) { - } - daToks->append(new GooString(da, i, j - i)); - i = j; - } + if (i >= 1) { + fontsize = gatof(( (GooString *)daToks->get(i) )->getCString()); + // TODO: Font name } - // Scan backwards: we are looking for the last set value for (i = daToks->getLength()-1; i >= 0; --i) { - if (fontsize == -1) { - if (!((GooString *)daToks->get(i))->cmp("Tf") && i >= 2) { - // TODO: Font name - fontsize = gatof(( (GooString *)daToks->get(i-1) )->getCString()); - } - } if (fontcolor == NULL) { if (!((GooString *)daToks->get(i))->cmp("g") && i >= 1) { fontcolor = new AnnotColor(gatof(( (GooString *)daToks->get(i-1) )->getCString())); diff --git a/poppler/Form.cc b/poppler/Form.cc index 0f4718e..5fa8849 100644 --- a/poppler/Form.cc +++ b/poppler/Form.cc @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include #include "goo/gmem.h" #include "goo/GooString.h" @@ -312,6 +314,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); @@ -1185,6 +1197,82 @@ 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::tokenizeDA(GooString* da, GooList* daToks) +{ + int idx = -1; + if(da && daToks) { + 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; +} + +int FormFieldText::parseDA(GooList* daToks) +{ + int idx = -1; + if (obj.isDict()) { + Object objDA(obj.dictLookup("DA")); + if (objDA.isString()) { + GooString* da = objDA.getString(); + tokenizeDA(da, daToks); + } + } + return idx; +} + //------------------------------------------------------------------------ // FormFieldChoice diff --git a/poppler/Form.h b/poppler/Form.h index 8498752..e783e13 100644 --- a/poppler/Form.h +++ b/poppler/Form.h @@ -25,6 +25,7 @@ #pragma interface #endif +#include "goo/GooList.h" #include "Object.h" #include "Annot.h" @@ -206,6 +207,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; }; @@ -421,10 +427,20 @@ 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 + + static int tokenizeDA(GooString* daString, GooList* daToks); + 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 02c3a5d..f6607b2 100644 --- a/qt5/src/poppler-form.cc +++ b/qt5/src/poppler-form.cc @@ -186,7 +186,6 @@ Link *FormField::additionalAction(AdditionalActionType type) const return action; } - FormFieldButton::FormFieldButton(DocumentData *doc, ::Page *p, ::FormWidgetButton *w) : FormField(*new FormFieldData(doc, p, w)) { @@ -348,6 +347,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 e39d6a2..140cc6c 100644 --- a/qt5/src/poppler-form.h +++ b/qt5/src/poppler-form.h @@ -275,6 +275,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