mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-03-12 17:30:17 +01:00
Editor plugin: Edit text
This commit is contained in:
parent
d83689cddb
commit
9222a60039
@ -151,7 +151,7 @@ void EditorPlugin::setWidget(pdf::PDFWidget* widget)
|
|||||||
connect(&m_scene, &pdf::PDFPageContentScene::selectionChanged, this, &EditorPlugin::onSceneSelectionChanged);
|
connect(&m_scene, &pdf::PDFPageContentScene::selectionChanged, this, &EditorPlugin::onSceneSelectionChanged);
|
||||||
connect(&m_scene, &pdf::PDFPageContentScene::editElementRequest, this, &EditorPlugin::onSceneEditElement);
|
connect(&m_scene, &pdf::PDFPageContentScene::editElementRequest, this, &EditorPlugin::onSceneEditElement);
|
||||||
connect(clearAction, &QAction::triggered, &m_scene, &pdf::PDFPageContentScene::clear);
|
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);
|
connect(m_widget->getDrawWidgetProxy(), &pdf::PDFDrawWidgetProxy::drawSpaceChanged, this, &EditorPlugin::onDrawSpaceChanged);
|
||||||
|
|
||||||
updateActions();
|
updateActions();
|
||||||
@ -200,7 +200,7 @@ bool EditorPlugin::save()
|
|||||||
const pdf::PDFPage* page = m_document->getCatalog()->getPage(pageIndex);
|
const pdf::PDFPage* page = m_document->getCatalog()->getPage(pageIndex);
|
||||||
const pdf::PDFEditedPageContent& editedPageContent = m_editedPageContent.at(pageIndex);
|
const pdf::PDFEditedPageContent& editedPageContent = m_editedPageContent.at(pageIndex);
|
||||||
|
|
||||||
pdf::PDFPageContentEditorContentStreamBuilder contentStreamBuilder;
|
pdf::PDFPageContentEditorContentStreamBuilder contentStreamBuilder(m_document);
|
||||||
contentStreamBuilder.setFontDictionary(editedPageContent.getFontDictionary());
|
contentStreamBuilder.setFontDictionary(editedPageContent.getFontDictionary());
|
||||||
|
|
||||||
auto it = elementsByPage.find(pageIndex);
|
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()
|
void EditorPlugin::updateActions()
|
||||||
{
|
{
|
||||||
m_actions[Activate]->setEnabled(m_document);
|
m_actions[Activate]->setEnabled(m_document);
|
||||||
|
@ -103,6 +103,7 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
void setActive(bool active);
|
void setActive(bool active);
|
||||||
|
void onSetActive(bool active);
|
||||||
|
|
||||||
void updateActions();
|
void updateActions();
|
||||||
void updateGraphics();
|
void updateGraphics();
|
||||||
|
@ -778,7 +778,8 @@ void PDFEditedPageContentElementText::setItemsAsText(const QString& newItemsAsTe
|
|||||||
m_itemsAsText = newItemsAsText;
|
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;
|
stream << "q BT" << Qt::endl;
|
||||||
|
|
||||||
QXmlStreamReader reader(text);
|
QString xml = QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><doc>%1</doc>").arg(text);
|
||||||
m_textFont = m_currentState.getTextFont();
|
|
||||||
|
|
||||||
auto isCommand = [&reader](const char* tag) -> bool
|
QXmlStreamReader reader(xml);
|
||||||
{
|
m_textFont = m_currentState.getTextFont();
|
||||||
QString tagString = reader.name().toString();
|
|
||||||
QXmlStreamAttributes attributes = reader.attributes();
|
|
||||||
return tagString == "tr" && attributes.size() == 1 && attributes.hasAttribute("v");
|
|
||||||
};
|
|
||||||
|
|
||||||
while (!reader.atEnd() && !reader.hasError())
|
while (!reader.atEnd() && !reader.hasError())
|
||||||
{
|
{
|
||||||
reader.readNext();
|
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();
|
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"))
|
if (isCommand("tr"))
|
||||||
{
|
{
|
||||||
const QXmlStreamAttribute& attribute = attributes.front();
|
const QXmlStreamAttribute& attribute = attributes.front();
|
||||||
@ -1344,34 +1408,6 @@ void PDFPageContentEditorContentStreamBuilder::writeText(QTextStream& stream, co
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
void PDFPageContentEditorContentStreamBuilder::writeImage(QTextStream& stream, const QImage& image)
|
||||||
{
|
{
|
||||||
QByteArray key;
|
QByteArray key;
|
||||||
|
@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
class QXmlStreamReader;
|
||||||
|
|
||||||
namespace pdf
|
namespace pdf
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -211,7 +213,7 @@ private:
|
|||||||
class PDF4QTLIBCORESHARED_EXPORT PDFPageContentEditorContentStreamBuilder
|
class PDF4QTLIBCORESHARED_EXPORT PDFPageContentEditorContentStreamBuilder
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PDFPageContentEditorContentStreamBuilder();
|
PDFPageContentEditorContentStreamBuilder(PDFDocument* document);
|
||||||
|
|
||||||
void writeStateDifference(QTextStream& stream, const PDFPageContentProcessorState& state);
|
void writeStateDifference(QTextStream& stream, const PDFPageContentProcessorState& state);
|
||||||
void writeElement(const PDFEditedPageContentElement* element);
|
void writeElement(const PDFEditedPageContentElement* element);
|
||||||
@ -232,7 +234,9 @@ private:
|
|||||||
const QPainterPath& path,
|
const QPainterPath& path,
|
||||||
bool isStroking,
|
bool isStroking,
|
||||||
bool isFilling);
|
bool isFilling);
|
||||||
|
|
||||||
void writeText(QTextStream& stream, const QString& text);
|
void writeText(QTextStream& stream, const QString& text);
|
||||||
|
void writeTextCommand(QTextStream& stream, const QXmlStreamReader& reader);
|
||||||
|
|
||||||
void writeImage(QTextStream& stream, const QImage& image);
|
void writeImage(QTextStream& stream, const QImage& image);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user