Rendering options

This commit is contained in:
Jakub Melka 2019-07-06 15:55:37 +02:00
parent 584211bf36
commit 9ed17fc8ca
11 changed files with 241 additions and 24 deletions

View File

@ -347,7 +347,8 @@ PDFDrawWidgetProxy::PDFDrawWidgetProxy(QObject* parent) :
m_controller(nullptr),
m_widget(nullptr),
m_horizontalScrollbar(nullptr),
m_verticalScrollbar(nullptr)
m_verticalScrollbar(nullptr),
m_features(PDFRenderer::Antialiasing | PDFRenderer::TextAntialiasing)
{
m_controller = new PDFDrawSpaceController(this);
connect(m_controller, &PDFDrawSpaceController::drawSpaceChanged, this, &PDFDrawWidgetProxy::update);
@ -549,7 +550,7 @@ void PDFDrawWidgetProxy::draw(QPainter* painter, QRect rect)
// Clear the page space by white color
painter->fillRect(placedRect, Qt::white);
PDFRenderer renderer(m_controller->getDocument(), m_controller->getFontCache(), m_controller->getOptionalContentActivity());
PDFRenderer renderer(m_controller->getDocument(), m_controller->getFontCache(), m_controller->getOptionalContentActivity(), m_features);
QList<PDFRenderError> errors = renderer.render(painter, placedRect, item.pageIndex);
if (!errors.empty())
@ -810,4 +811,18 @@ void PDFDrawWidgetProxy::updateVerticalScrollbarFromOffset()
}
}
PDFRenderer::Features PDFDrawWidgetProxy::getFeatures() const
{
return m_features;
}
void PDFDrawWidgetProxy::setFeatures(PDFRenderer::Features features)
{
if (m_features != features)
{
m_features = features;
emit repaintNeeded();
}
}
} // namespace pdf

View File

@ -213,6 +213,9 @@ public:
static constexpr PDFReal ZOOM_STEP = 1.2;
PDFRenderer::Features getFeatures() const;
void setFeatures(PDFRenderer::Features features);
signals:
void drawSpaceChanged();
void pageLayoutChanged();
@ -326,6 +329,9 @@ private:
/// Current page layout
Layout m_layout;
/// Renderer features
PDFRenderer::Features m_features;
};
} // namespace pdf

View File

@ -1866,6 +1866,23 @@ void PDFPageContentProcessor::operatorPaintXObject(PDFPageContentProcessor::PDFO
{
if (m_xobjectDictionary)
{
// According to the specification, XObjects are skipped entirely, as no operator was invoked.
if (m_xobjectDictionary->hasKey("OC"))
{
const PDFObject& object = m_xobjectDictionary->get("OC");
if (object.isReference())
{
if (isContentSuppressedByOC(object.getReference()))
{
return;
}
}
else
{
throw PDFRendererException(RenderErrorType::Error, PDFTranslationContext::tr("Reference to optional content expected."));
}
}
const PDFObject& object = m_document->getObject(m_xobjectDictionary->get(name.name));
if (object.isStream())
{
@ -1969,8 +1986,6 @@ void PDFPageContentProcessor::operatorMarkedContentBegin(PDFOperandName name)
void PDFPageContentProcessor::operatorMarkedContentBeginWithProperties(PDFOperandName name, PDFObject properties)
{
// TODO: Handle optional content of images/forms
// Handle the optional content
if (name.name == "OC")
{

View File

@ -406,6 +406,10 @@ protected:
/// Returns true, if graphic content is suppressed
bool isContentSuppressed() const;
/// Computes visibility of OCG/OCMD - returns false, if it is not suppressed,
/// or true, if it is suppressed.
virtual bool isContentSuppressedByOC(PDFObjectReference ocgOrOcmd);
private:
/// Initializes the resources dictionaries
void initDictionaries(const PDFObject& resourcesObject);
@ -665,10 +669,6 @@ private:
/// Read object from operand stack
PDFObject readObjectFromOperandStack(size_t startPosition) const;
/// Computes visibility of OCG/OCMD - returns false, if it is not suppressed,
/// or true, if it is suppressed.
bool isContentSuppressedByOC(PDFObjectReference ocgOrOcmd);
const PDFPage* m_page;
const PDFDocument* m_document;
const PDFFontCache* m_fontCache;

View File

@ -81,8 +81,6 @@ void PDFPainter::performPathPainting(const QPainterPath& path, bool stroke, bool
m_painter->setBrush(Qt::NoBrush);
}
m_painter->setRenderHint(QPainter::Antialiasing, m_features.testFlag(PDFRenderer::Antialiasing));
Q_ASSERT(path.fillRule() == fillRule);
m_painter->drawPath(path);
}
@ -162,6 +160,16 @@ void PDFPainter::performRestoreGraphicState(ProcessOrder order)
}
}
bool PDFPainter::isContentSuppressedByOC(PDFObjectReference ocgOrOcmd)
{
if (m_features.testFlag(PDFRenderer::IgnoreOptionalContent))
{
return false;
}
return PDFPageContentProcessor::isContentSuppressedByOC(ocgOrOcmd);
}
QPen PDFPainter::getCurrentPenImpl() const
{
const PDFPageContentProcessorState* graphicState = getGraphicState();

View File

@ -57,6 +57,7 @@ protected:
virtual void performUpdateGraphicsState(const PDFPageContentProcessorState& state) override;
virtual void performSaveGraphicState(ProcessOrder order) override;
virtual void performRestoreGraphicState(ProcessOrder order) override;
virtual bool isContentSuppressedByOC(PDFObjectReference ocgOrOcmd) override;
private:
/// Returns current pen

View File

@ -22,11 +22,11 @@
namespace pdf
{
PDFRenderer::PDFRenderer(const PDFDocument* document, const PDFFontCache* fontCache, const PDFOptionalContentActivity* optionalContentActivity) :
PDFRenderer::PDFRenderer(const PDFDocument* document, const PDFFontCache* fontCache, const PDFOptionalContentActivity* optionalContentActivity, Features features) :
m_document(document),
m_fontCache(fontCache),
m_optionalContentActivity(optionalContentActivity),
m_features(Antialiasing | TextAntialiasing)
m_features(features)
{
Q_ASSERT(document);
}

View File

@ -32,17 +32,19 @@ class PDFOptionalContentActivity;
class PDFRenderer
{
public:
explicit PDFRenderer(const PDFDocument* document, const PDFFontCache* fontCache, const PDFOptionalContentActivity* optionalContentActivity);
enum Feature
{
Antialiasing = 0x0001, ///< Antialiasing for lines, shapes, etc.
TextAntialiasing = 0x0002, ///< Antialiasing for drawing text
SmoothImages = 0x0004 ///< Adjust images to the device space using smooth transformation (slower, but better performance quality)
Antialiasing = 0x0001, ///< Antialiasing for lines, shapes, etc.
TextAntialiasing = 0x0002, ///< Antialiasing for drawing text
SmoothImages = 0x0004, ///< Adjust images to the device space using smooth transformation (slower, but better image quality)
IgnoreOptionalContent = 0x0008, ///< Ignore optional content (so all is drawn ignoring settings of optional content)
};
Q_DECLARE_FLAGS(Features, Feature)
explicit PDFRenderer(const PDFDocument* document, const PDFFontCache* fontCache, const PDFOptionalContentActivity* optionalContentActivity, Features features);
/// Paints desired page onto the painter. Page is painted in the rectangle using best-fit method.
/// If the page doesn't exist, then error is returned. No exception is thrown. Rendering errors
/// are reported and returned in the error list. If no error occured, empty list is returned.
@ -56,6 +58,9 @@ public:
/// Rendering errors are reported and returned in the error list. If no error occured, empty list is returned.
QList<PDFRenderError> render(QPainter* painter, const QMatrix& matrix, size_t pageIndex) const;
/// Returns default renderer features
static constexpr Features getDefaultFeatures() { return Antialiasing | TextAntialiasing; }
private:
const PDFDocument* m_document;
const PDFFontCache* m_fontCache;

View File

@ -45,6 +45,7 @@ namespace pdfviewer
PDFViewerMainWindow::PDFViewerMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::PDFViewerMainWindow),
m_settings(new PDFViewerSettings(this)),
m_pdfWidget(nullptr),
m_optionalContentDockWidget(nullptr),
m_optionalContentTreeView(nullptr),
@ -97,11 +98,22 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget *parent) :
addDockWidget(Qt::LeftDockWidgetArea, m_optionalContentDockWidget);
m_optionalContentDockWidget->hide();
ui->actionRenderOptionAntialiasing->setData(pdf::PDFRenderer::Antialiasing);
ui->actionRenderOptionTextAntialiasing->setData(pdf::PDFRenderer::TextAntialiasing);
ui->actionRenderOptionSmoothPictures->setData(pdf::PDFRenderer::SmoothImages);
ui->actionRenderOptionIgnoreOptionalContentSettings->setData(pdf::PDFRenderer::IgnoreOptionalContent);
for (QAction* action : getRenderingOptionActions())
{
connect(action, &QAction::triggered, this, &PDFViewerMainWindow::onRenderingOptionTriggered);
}
ui->menuView->addSeparator();
ui->menuView->addAction(m_optionalContentDockWidget->toggleViewAction());
connect(m_pdfWidget->getDrawWidgetProxy(), &pdf::PDFDrawWidgetProxy::pageLayoutChanged, this, &PDFViewerMainWindow::updatePageLayoutActions);
connect(m_pdfWidget, &pdf::PDFWidget::pageRenderingErrorsChanged, this, &PDFViewerMainWindow::onPageRenderingErrorsChanged, Qt::QueuedConnection);
connect(m_settings, &PDFViewerSettings::settingsChanged, this, &PDFViewerMainWindow::onViewerSettingsChanged);
readSettings();
updatePageLayoutActions();
@ -114,7 +126,7 @@ PDFViewerMainWindow::~PDFViewerMainWindow()
void PDFViewerMainWindow::onActionOpenTriggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Select PDF document"), m_directory, tr("PDF document (*.pdf)"));
QString fileName = QFileDialog::getOpenFileName(this, tr("Select PDF document"), m_settings->getDirectory(), tr("PDF document (*.pdf)"));
if (!fileName.isEmpty())
{
openDocument(fileName);
@ -141,7 +153,7 @@ void PDFViewerMainWindow::onPageRenderingErrorsChanged(pdf::PDFInteger pageIndex
void PDFViewerMainWindow::readSettings()
{
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
if (geometry.isEmpty())
@ -162,15 +174,16 @@ void PDFViewerMainWindow::readSettings()
restoreState(state);
}
m_directory = settings.value("defaultDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
m_settings->readSettings(settings);
}
void PDFViewerMainWindow::writeSettings()
{
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
settings.setValue("geometry", saveGeometry());
settings.setValue("windowState", saveState());
settings.setValue("defaultDirectory", m_directory);
m_settings->writeSettings(settings);
}
void PDFViewerMainWindow::updateTitle()
@ -225,6 +238,31 @@ void PDFViewerMainWindow::updatePageLayoutActions()
}
}
void PDFViewerMainWindow::updateRenderingOptionActions()
{
const pdf::PDFRenderer::Features features = m_settings->getFeatures();
for (QAction* action : getRenderingOptionActions())
{
action->setChecked(features.testFlag(static_cast<pdf::PDFRenderer::Feature>(action->data().toInt())));
}
}
void PDFViewerMainWindow::onViewerSettingsChanged()
{
m_pdfWidget->getDrawWidgetProxy()->setFeatures(m_settings->getFeatures());
updateRenderingOptionActions();
}
void PDFViewerMainWindow::onRenderingOptionTriggered(bool checked)
{
QAction* action = qobject_cast<QAction*>(sender());
Q_ASSERT(action);
pdf::PDFRenderer::Features features = m_settings->getFeatures();
features.setFlag(static_cast<pdf::PDFRenderer::Feature>(action->data().toInt()), checked);
m_settings->setFeatures(features);
}
void PDFViewerMainWindow::openDocument(const QString& fileName)
{
// First close old document
@ -240,7 +278,7 @@ void PDFViewerMainWindow::openDocument(const QString& fileName)
{
// Mark current directory as this
QFileInfo fileInfo(fileName);
m_directory = fileInfo.dir().absolutePath();
m_settings->setDirectory(fileInfo.dir().absolutePath());
m_currentFile = fileInfo.fileName();
m_pdfDocument.reset(new pdf::PDFDocument(std::move(document)));
@ -297,6 +335,11 @@ void PDFViewerMainWindow::setPageLayout(pdf::PageLayout pageLayout)
m_pdfWidget->getDrawWidgetProxy()->setPageLayout(pageLayout);
}
std::vector<QAction*> PDFViewerMainWindow::getRenderingOptionActions() const
{
return { ui->actionRenderOptionAntialiasing, ui->actionRenderOptionTextAntialiasing, ui->actionRenderOptionSmoothPictures, ui->actionRenderOptionIgnoreOptionalContentSettings };
}
void PDFViewerMainWindow::closeEvent(QCloseEvent* event)
{
writeSettings();
@ -393,4 +436,50 @@ void PDFViewerMainWindow::on_actionGenerateCMAPrepository_triggered()
}
}
void PDFViewerSettings::readSettings(QSettings& settings)
{
settings.beginGroup("ViewerSettings");
m_directory = settings.value("defaultDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
m_features = static_cast<pdf::PDFRenderer::Features>(settings.value("rendererFeatures", static_cast<int>(pdf::PDFRenderer::getDefaultFeatures())).toInt());
settings.endGroup();
emit settingsChanged();
}
void PDFViewerSettings::writeSettings(QSettings& settings)
{
settings.beginGroup("ViewerSettings");
settings.setValue("defaultDirectory", m_directory);
settings.setValue("rendererFeatures", static_cast<int>(m_features));
settings.endGroup();
}
QString PDFViewerSettings::getDirectory() const
{
return m_directory;
}
void PDFViewerSettings::setDirectory(const QString& directory)
{
if (m_directory != directory)
{
m_directory = directory;
emit settingsChanged();
}
}
pdf::PDFRenderer::Features PDFViewerSettings::getFeatures() const
{
return m_features;
}
void PDFViewerSettings::setFeatures(const pdf::PDFRenderer::Features& features)
{
if (m_features != features)
{
m_features = features;
emit settingsChanged();
}
}
} // namespace pdfviewer

View File

@ -25,6 +25,8 @@
#include <QMainWindow>
#include <QSharedPointer>
class QSettings;
namespace Ui
{
class PDFViewerMainWindow;
@ -40,6 +42,35 @@ class PDFOptionalContentTreeItemModel;
namespace pdfviewer
{
class PDFViewerSettings : public QObject
{
Q_OBJECT
public:
inline explicit PDFViewerSettings(QObject* parent) :
QObject(parent),
m_features(pdf::PDFRenderer::Antialiasing | pdf::PDFRenderer::TextAntialiasing)
{
}
void readSettings(QSettings& settings);
void writeSettings(QSettings& settings);
QString getDirectory() const;
void setDirectory(const QString& directory);
pdf::PDFRenderer::Features getFeatures() const;
void setFeatures(const pdf::PDFRenderer::Features& features);
signals:
void settingsChanged();
private:
pdf::PDFRenderer::Features m_features;
QString m_directory;
};
class PDFViewerMainWindow : public QMainWindow
{
Q_OBJECT
@ -58,7 +89,6 @@ private slots:
void on_actionFirstPageOnRightSide_triggered();
void on_actionRendering_Errors_triggered();
void on_actionGenerateCMAPrepository_triggered();
private:
@ -72,6 +102,10 @@ private:
void updateTitle();
void updatePageLayoutActions();
void updateRenderingOptionActions();
void onViewerSettingsChanged();
void onRenderingOptionTriggered(bool checked);
void openDocument(const QString& fileName);
void setDocument(const pdf::PDFDocument* document);
@ -79,10 +113,12 @@ private:
void setPageLayout(pdf::PageLayout pageLayout);
std::vector<QAction*> getRenderingOptionActions() const;
Ui::PDFViewerMainWindow* ui;
PDFViewerSettings* m_settings;
pdf::PDFWidget* m_pdfWidget;
QSharedPointer<pdf::PDFDocument> m_pdfDocument;
QString m_directory;
QString m_currentFile;
QDockWidget* m_optionalContentDockWidget;
QTreeView* m_optionalContentTreeView;

View File

@ -51,7 +51,17 @@
<addaction name="separator"/>
<addaction name="actionFirstPageOnRightSide"/>
</widget>
<widget class="QMenu" name="menuRendering_Options">
<property name="title">
<string>Rendering Options</string>
</property>
<addaction name="actionRenderOptionAntialiasing"/>
<addaction name="actionRenderOptionTextAntialiasing"/>
<addaction name="actionRenderOptionSmoothPictures"/>
<addaction name="actionRenderOptionIgnoreOptionalContentSettings"/>
</widget>
<addaction name="menuPage_Layout"/>
<addaction name="menuRendering_Options"/>
</widget>
<widget class="QMenu" name="menuTools">
<property name="title">
@ -166,6 +176,38 @@
<string>Generate CMAP repository</string>
</property>
</action>
<action name="actionRenderOptionAntialiasing">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Antialiasing</string>
</property>
</action>
<action name="actionRenderOptionTextAntialiasing">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Text Antialiasing</string>
</property>
</action>
<action name="actionRenderOptionSmoothPictures">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Smooth Pictures</string>
</property>
</action>
<action name="actionRenderOptionIgnoreOptionalContentSettings">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Ignore Optional Content Settings</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>