Commit f3502635 authored by João Netto's avatar João Netto Committed by Albert Astals Cid

Implemented support for modifying the text appearance stream text

parent 355fd8d5
......@@ -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();
......
......@@ -26,6 +26,7 @@
// Copyright 2018, 2019 Nelson Benítez León <nbenitezl@gmail.com>
// Copyright 2019 Oliver Sander <oliver.sander@tu-dresden.de>
// Copyright 2019 Tomoyuki Kubota <himajin100000@gmail.com>
// Copyright 2019 João Netto <joaonetto901@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()
......
......@@ -19,6 +19,7 @@
// Copyright 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
// Copyright 2018 Chinmoy Ranjan Pradhan <chinmoyrp65@protonmail.com>
// Copyright 2019 Oliver Sander <oliver.sander@tu-dresden.de>
// Copyright 2019 João Netto <joaonetto901@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;
......
......@@ -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);
......
......@@ -80,6 +80,9 @@ namespace Poppler {
\since 0.6
*/
class POPPLER_QT5_EXPORT FormField {
friend class FormFieldData;
public:
/**
......@@ -323,6 +326,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.
......
......@@ -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;
......
......@@ -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;
......
......@@ -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"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment