Editor plugin: Edit text

This commit is contained in:
Jakub Melka 2024-06-02 16:57:47 +02:00
parent d83689cddb
commit 9222a60039
4 changed files with 250 additions and 198 deletions

View File

@ -151,7 +151,7 @@ void EditorPlugin::setWidget(pdf::PDFWidget* widget)
connect(&m_scene, &pdf::PDFPageContentScene::selectionChanged, this, &EditorPlugin::onSceneSelectionChanged);
connect(&m_scene, &pdf::PDFPageContentScene::editElementRequest, this, &EditorPlugin::onSceneEditElement);
connect(clearAction, &QAction::triggered, &m_scene, &pdf::PDFPageContentScene::clear);
connect(activateAction, &QAction::triggered, this, &EditorPlugin::setActive);
connect(activateAction, &QAction::triggered, this, &EditorPlugin::onSetActive);
connect(m_widget->getDrawWidgetProxy(), &pdf::PDFDrawWidgetProxy::drawSpaceChanged, this, &EditorPlugin::onDrawSpaceChanged);
updateActions();
@ -200,7 +200,7 @@ bool EditorPlugin::save()
const pdf::PDFPage* page = m_document->getCatalog()->getPage(pageIndex);
const pdf::PDFEditedPageContent& editedPageContent = m_editedPageContent.at(pageIndex);
pdf::PDFPageContentEditorContentStreamBuilder contentStreamBuilder;
pdf::PDFPageContentEditorContentStreamBuilder contentStreamBuilder(m_document);
contentStreamBuilder.setFontDictionary(editedPageContent.getFontDictionary());
auto it = elementsByPage.find(pageIndex);
@ -477,6 +477,17 @@ void EditorPlugin::setActive(bool active)
}
}
void EditorPlugin::onSetActive(bool active)
{
if (!active && !save())
{
updateActions();
return;
}
setActive(active);
}
void EditorPlugin::updateActions()
{
m_actions[Activate]->setEnabled(m_document);

View File

@ -103,6 +103,7 @@ private:
};
void setActive(bool active);
void onSetActive(bool active);
void updateActions();
void updateGraphics();

View File

@ -778,7 +778,8 @@ void PDFEditedPageContentElementText::setItemsAsText(const QString& newItemsAsTe
m_itemsAsText = newItemsAsText;
}
PDFPageContentEditorContentStreamBuilder::PDFPageContentEditorContentStreamBuilder()
PDFPageContentEditorContentStreamBuilder::PDFPageContentEditorContentStreamBuilder(PDFDocument* document) :
m_document(document)
{
}
@ -1147,24 +1148,87 @@ void PDFPageContentEditorContentStreamBuilder::writeText(QTextStream& stream, co
{
stream << "q BT" << Qt::endl;
QXmlStreamReader reader(text);
m_textFont = m_currentState.getTextFont();
QString xml = QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><doc>%1</doc>").arg(text);
auto isCommand = [&reader](const char* tag) -> bool
{
QString tagString = reader.name().toString();
QXmlStreamAttributes attributes = reader.attributes();
return tagString == "tr" && attributes.size() == 1 && attributes.hasAttribute("v");
};
QXmlStreamReader reader(xml);
m_textFont = m_currentState.getTextFont();
while (!reader.atEnd() && !reader.hasError())
{
reader.readNext();
if (reader.isStartElement())
switch (reader.tokenType())
{
case QXmlStreamReader::NoToken:
break;
case QXmlStreamReader::Invalid:
addError(PDFTranslationContext::tr("Invalid XML text."));
break;
case QXmlStreamReader::StartDocument:
case QXmlStreamReader::EndDocument:
case QXmlStreamReader::EndElement:
case QXmlStreamReader::Comment:
case QXmlStreamReader::DTD:
case QXmlStreamReader::ProcessingInstruction:
case QXmlStreamReader::EntityReference:
break;
case QXmlStreamReader::StartElement:
writeTextCommand(stream, reader);
break;
case QXmlStreamReader::Characters:
{
QString characters = reader.text().toString();
if (m_textFont)
{
PDFEncodedText encodedText = m_textFont->encodeText(characters);
if (!encodedText.encodedText.isEmpty())
{
stream << "<" << encodedText.encodedText.toHex() << "> Tj" << Qt::endl;
}
if (!encodedText.isValid)
{
addError(PDFTranslationContext::tr("Error during converting text to font encoding. Some characters were not converted: '%1'.").arg(encodedText.errorString));
}
}
else
{
addError(PDFTranslationContext::tr("Text font not defined!"));
}
break;
}
default:
Q_ASSERT(false);
break;
}
}
stream << "ET Q" << Qt::endl;
}
void PDFPageContentEditorContentStreamBuilder::writeTextCommand(QTextStream& stream, const QXmlStreamReader& reader)
{
QXmlStreamAttributes attributes = reader.attributes();
auto isCommand = [&reader](const char* tag) -> bool
{
QString tagString = reader.name().toString();
QXmlStreamAttributes attributes = reader.attributes();
return tagString == QLatin1String(tag) && attributes.size() == 1 && attributes.hasAttribute("v");
};
if (reader.name().toString() == "doc")
{
return;
}
if (isCommand("tr"))
{
const QXmlStreamAttribute& attribute = attributes.front();
@ -1342,34 +1406,6 @@ void PDFPageContentEditorContentStreamBuilder::writeText(QTextStream& stream, co
{
addError(PDFTranslationContext::tr("Invalid command '%1'.").arg(reader.name().toString()));
}
}
if (reader.isCharacters())
{
QString characters = reader.text().toString();
if (m_textFont)
{
PDFEncodedText encodedText = m_textFont->encodeText(characters);
if (!encodedText.encodedText.isEmpty())
{
stream << "<" << encodedText.encodedText.toHex() << "> Tj" << Qt::endl;
}
if (!encodedText.isValid)
{
addError(PDFTranslationContext::tr("Error during converting text to font encoding. Some characters were not converted: '%1'.").arg(encodedText.errorString));
}
}
else
{
addError(PDFTranslationContext::tr("Text font not defined!"));
}
}
}
stream << "ET Q" << Qt::endl;
}
void PDFPageContentEditorContentStreamBuilder::writeImage(QTextStream& stream, const QImage& image)

View File

@ -22,6 +22,8 @@
#include <memory>
class QXmlStreamReader;
namespace pdf
{
@ -211,7 +213,7 @@ private:
class PDF4QTLIBCORESHARED_EXPORT PDFPageContentEditorContentStreamBuilder
{
public:
PDFPageContentEditorContentStreamBuilder();
PDFPageContentEditorContentStreamBuilder(PDFDocument* document);
void writeStateDifference(QTextStream& stream, const PDFPageContentProcessorState& state);
void writeElement(const PDFEditedPageContentElement* element);
@ -232,7 +234,9 @@ private:
const QPainterPath& path,
bool isStroking,
bool isFilling);
void writeText(QTextStream& stream, const QString& text);
void writeTextCommand(QTextStream& stream, const QXmlStreamReader& reader);
void writeImage(QTextStream& stream, const QImage& image);