[poppler] poppler/Annot.cc poppler/Form.cc poppler/Form.h qt5/src qt5/tests

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Jul 31 22:36:21 UTC 2019


 poppler/Annot.cc           |    2 +-
 poppler/Form.cc            |   19 +++++++++++++++++++
 poppler/Form.h             |    6 ++++++
 qt5/src/poppler-form.cc    |    8 ++++++++
 qt5/src/poppler-form.h     |   10 ++++++++++
 qt5/src/poppler-private.cc |    5 +++++
 qt5/src/poppler-private.h  |    2 ++
 qt5/tests/check_forms.cpp  |   40 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 91 insertions(+), 1 deletion(-)

New commits:
commit f3502635eed45d8783c44fdc90487786c8fc4f23
Author: João Netto <joaonetto901 at gmail.com>
Date:   Sat Jun 29 10:44:33 2019 -0300

    Implemented support for modifying the text appearance stream text

diff --git a/poppler/Annot.cc b/poppler/Annot.cc
index bd3aaa5f..e896468b 100644
--- a/poppler/Annot.cc
+++ b/poppler/Annot.cc
@@ -4856,7 +4856,7 @@ bool AnnotAppearanceBuilder::drawFormFieldText(const FormFieldText *fieldText, c
   VariableTextQuadding quadding;
   const GooString *contents;
 
-  contents = fieldText->getContent();
+  contents = fieldText->getAppearanceContent();
   if (contents) {
     quadding = fieldText->hasTextQuadding() ? fieldText->getTextQuadding() : form->getTextQuadding();
 
diff --git a/poppler/Form.cc b/poppler/Form.cc
index 258d235e..b1231e19 100644
--- a/poppler/Form.cc
+++ b/poppler/Form.cc
@@ -26,6 +26,7 @@
 // Copyright 2018, 2019 Nelson Benítez León <nbenitezl at gmail.com>
 // Copyright 2019 Oliver Sander <oliver.sander at tu-dresden.de>
 // Copyright 2019 Tomoyuki Kubota <himajin100000 at gmail.com>
+// Copyright 2019 João Netto <joaonetto901 at gmail.com>
 //
 //========================================================================
 
@@ -335,6 +336,11 @@ void FormWidgetText::setContent(const GooString* new_content)
   parent()->setContentCopy(new_content);
 }
 
+void FormWidgetText::setAppearanceContent(const GooString* new_content)
+{
+  parent()->setAppearanceContentCopy(new_content);
+}
+
 FormFieldText *FormWidgetText::parent() const
 {
   return static_cast<FormFieldText*>(field);
@@ -1147,6 +1153,7 @@ FormFieldText::FormFieldText(PDFDoc *docA, Object &&aobj, const Ref refA, FormFi
   Dict* dict = obj.getDict();
   Object obj1;
   content = nullptr;
+  internalContent = nullptr;
   multiline = password = fileSelect = doNotSpellCheck = doNotScroll = comb = richText = false;
   maxLen = 0;
 
@@ -1214,9 +1221,21 @@ void FormFieldText::setContentCopy (const GooString* new_content)
   updateChildrenAppearance();
 }
 
+void FormFieldText::setAppearanceContentCopy (const GooString* new_content)
+{
+  delete internalContent;
+  internalContent = nullptr;
+
+  if (new_content) {
+    internalContent = new_content->copy();
+  }
+  updateChildrenAppearance();
+}
+
 FormFieldText::~FormFieldText()
 {
   delete content;
+  delete internalContent;
 }
 
 double FormFieldText::getTextFontSize()
diff --git a/poppler/Form.h b/poppler/Form.h
index 923b6c8f..b6ec9140 100644
--- a/poppler/Form.h
+++ b/poppler/Form.h
@@ -19,6 +19,7 @@
 // Copyright 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info at kdab.com>. Work sponsored by the LiMux project of the city of Munich
 // Copyright 2018 Chinmoy Ranjan Pradhan <chinmoyrp65 at protonmail.com>
 // Copyright 2019 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright 2019 João Netto <joaonetto901 at gmail.com>
 //
 //========================================================================
 
@@ -193,6 +194,8 @@ public:
 
   //expects a UTF16BE string
   void setContent(const GooString* new_content);
+  //sets the text inside the field appearance stream
+  void setAppearanceContent(const GooString* new_content);
 
   void updateWidgetAppearance() override;
 
@@ -407,7 +410,9 @@ public:
   FormFieldText(PDFDoc *docA, Object &&dict, const Ref ref, FormField *parent, std::set<int> *usedParents);
   
   const GooString* getContent () const { return content; }
+  const GooString* getAppearanceContent () const { return internalContent ? internalContent : content; }
   void setContentCopy (const GooString* new_content);
+  void setAppearanceContentCopy (const GooString* new_content);
   ~FormFieldText();
 
   bool isMultiline () const { return multiline; }
@@ -433,6 +438,7 @@ protected:
   int parseDA(std::vector<GooString*>* daToks);
 
   GooString* content;
+  GooString* internalContent;
   bool multiline;
   bool password;
   bool fileSelect;
diff --git a/qt5/src/poppler-form.cc b/qt5/src/poppler-form.cc
index c95a9623..5e377242 100644
--- a/qt5/src/poppler-form.cc
+++ b/qt5/src/poppler-form.cc
@@ -431,6 +431,14 @@ void FormFieldText::setText( const QString& text )
   delete goo;
 }
 
+void FormFieldText::setAppearanceText( const QString& text )
+{
+  FormWidgetText* fwt = static_cast<FormWidgetText*>(m_formData->fm);
+  GooString * goo = QStringToUnicodeGooString( text );
+  fwt->setAppearanceContent( goo );
+  delete goo;
+}
+
 bool FormFieldText::isPassword() const
 {
   FormWidgetText* fwt = static_cast<FormWidgetText*>(m_formData->fm);
diff --git a/qt5/src/poppler-form.h b/qt5/src/poppler-form.h
index d252ff8a..6b60a26c 100644
--- a/qt5/src/poppler-form.h
+++ b/qt5/src/poppler-form.h
@@ -80,6 +80,9 @@ namespace Poppler {
       \since 0.6
      */
     class POPPLER_QT5_EXPORT FormField {
+
+    friend class FormFieldData;
+
     public:
 
 	/**
@@ -324,6 +327,13 @@ namespace Poppler {
 	void setText( const QString& text );
 
 	/**
+	  Sets the text inside the Appearance Stream to the specified
+	  \p text
+	  \since 0.80
+	 */
+	void setAppearanceText( const QString& text );
+
+	/**
 	  Whether this text field is a password input, eg its text \b must be
 	  replaced with asterisks.
 
diff --git a/qt5/src/poppler-private.cc b/qt5/src/poppler-private.cc
index 8735b699..6553eb08 100644
--- a/qt5/src/poppler-private.cc
+++ b/qt5/src/poppler-private.cc
@@ -304,6 +304,11 @@ namespace Debug {
         }
     }
 
+    FormWidget *FormFieldData::getFormWidget( const FormField *f )
+    {
+        return f->m_formData->fm;
+    }
+
     FormFieldIconData *FormFieldIconData::getData( const FormFieldIcon &f )
     {
         return f.d_ptr;
diff --git a/qt5/src/poppler-private.h b/qt5/src/poppler-private.h
index 227972dc..80f6581e 100644
--- a/qt5/src/poppler-private.h
+++ b/qt5/src/poppler-private.h
@@ -45,6 +45,7 @@
 #include <config.h>
 #include <GfxState.h>
 #include <GlobalParams.h>
+#include <Form.h>
 #include <PDFDoc.h>
 #include <FontInfo.h>
 #include <OutputDev.h>
@@ -237,6 +238,7 @@ namespace Poppler {
 		::Page *page;
 		::FormWidget *fm;
 		QRectF box;
+		static POPPLER_QT5_EXPORT ::FormWidget *getFormWidget( const FormField *f );
     };
     
     class FormFieldIcon;
diff --git a/qt5/tests/check_forms.cpp b/qt5/tests/check_forms.cpp
index f139ed1f..ac60fdc4 100644
--- a/qt5/tests/check_forms.cpp
+++ b/qt5/tests/check_forms.cpp
@@ -15,6 +15,7 @@ private slots:
     void testCheckboxIssue159();// Test for issue #159
     void testSetIcon();// Test that setIcon will always be valid.
     void testSetPrintable();
+    void testSetAppearanceText();
 };
 
 void TestForms::testCheckbox()
@@ -168,5 +169,44 @@ void TestForms::testSetPrintable()
     }
 }
 
+void TestForms::testSetAppearanceText()
+{
+    QScopedPointer< Poppler::Document > document(Poppler::Document::load(TESTDATADIR "/unittestcases/checkbox_issue_159.pdf"));
+    QVERIFY( document );
+
+    QScopedPointer< Poppler::Page > page(document->page(0));
+    QVERIFY( page );
+
+    QList<Poppler::FormField*> forms = page->formFields();
+
+    int nTextForms = 0;
+
+    Q_FOREACH (Poppler::FormField *field, forms) {
+
+        if (field->type() != Poppler::FormField::FormText)
+            continue;
+
+        nTextForms++;
+
+        Poppler::FormFieldText *fft = static_cast< Poppler::FormFieldText * >( field );
+
+        const QString textToSet = "HOLA" + fft->name();
+        fft->setAppearanceText( textToSet );
+
+        Dict *dict = Poppler::FormFieldData::getFormWidget( fft )->getObj()->getDict();
+        Object strObject = dict->lookup( "AP" ).dictLookup( "N" );
+
+        QVERIFY( strObject.isStream() );
+
+        GooString s;
+        strObject.getStream()->fillGooString(&s);
+
+        const QString textToFind = QStringLiteral("\n(%1) Tj\n").arg(textToSet);
+        QVERIFY( s.toStr().find( textToFind.toStdString() ) != std::string::npos );
+    }
+
+    QCOMPARE( nTextForms, 5 );
+}
+
 QTEST_GUILESS_MAIN(TestForms)
 #include "check_forms.moc"


More information about the poppler mailing list