mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Precompiled pages
This commit is contained in:
@ -19,6 +19,7 @@
|
||||
#define PDFPAINTER_H
|
||||
|
||||
#include "pdfutils.h"
|
||||
#include "pdfpattern.h"
|
||||
#include "pdfrenderer.h"
|
||||
#include "pdfpagecontentprocessor.h"
|
||||
|
||||
@ -28,12 +29,76 @@
|
||||
namespace pdf
|
||||
{
|
||||
|
||||
/// Base painter, encapsulating common functionality for all PDF painters (for example,
|
||||
/// direct painter, or painter, which generates list of graphic commands).
|
||||
class PDFPainterBase : public PDFPageContentProcessor
|
||||
{
|
||||
using BaseClass = PDFPageContentProcessor;
|
||||
|
||||
public:
|
||||
explicit PDFPainterBase(PDFRenderer::Features features,
|
||||
const PDFPage* page,
|
||||
const PDFDocument* document,
|
||||
const PDFFontCache* fontCache,
|
||||
const PDFOptionalContentActivity* optionalContentActivity,
|
||||
QMatrix pagePointToDevicePointMatrix,
|
||||
const PDFMeshQualitySettings& meshQualitySettings);
|
||||
|
||||
protected:
|
||||
virtual void performUpdateGraphicsState(const PDFPageContentProcessorState& state) override;
|
||||
virtual void performBeginTransparencyGroup(ProcessOrder order, const PDFTransparencyGroup& transparencyGroup);
|
||||
virtual void performEndTransparencyGroup(ProcessOrder order, const PDFTransparencyGroup& transparencyGroup);
|
||||
virtual bool isContentSuppressedByOC(PDFObjectReference ocgOrOcmd) override;
|
||||
virtual void setWorldMatrix(const QMatrix& matrix) = 0;
|
||||
virtual void setCompositionMode(QPainter::CompositionMode mode) = 0;
|
||||
|
||||
/// Returns current pen
|
||||
const QPen& getCurrentPen() { return m_currentPen.get(this, &PDFPainterBase::getCurrentPenImpl); }
|
||||
|
||||
/// Returns current brush
|
||||
const QBrush& getCurrentBrush() { return m_currentBrush.get(this, &PDFPainterBase::getCurrentBrushImpl); }
|
||||
|
||||
/// Returns effective stroking alpha from transparency groups and current graphic state
|
||||
PDFReal getEffectiveStrokingAlpha() const;
|
||||
|
||||
/// Returns effective filling alpha from transparency groups and current graphic state
|
||||
PDFReal getEffectiveFillingAlpha() const;
|
||||
|
||||
/// Returns true, if blend mode can be set according the transparency group stack
|
||||
bool canSetBlendMode(BlendMode mode) const;
|
||||
|
||||
/// Returns, if feature is turned on
|
||||
bool hasFeature(PDFRenderer::Feature feature) const { return m_features.testFlag(feature); }
|
||||
|
||||
private:
|
||||
/// Returns current pen (implementation)
|
||||
QPen getCurrentPenImpl() const;
|
||||
|
||||
/// Returns current brush (implementation)
|
||||
QBrush getCurrentBrushImpl() const;
|
||||
|
||||
struct PDFTransparencyGroupPainterData
|
||||
{
|
||||
PDFTransparencyGroup group;
|
||||
PDFReal alphaStroke = 1.0;
|
||||
PDFReal alphaFill = 1.0;
|
||||
BlendMode blendMode = BlendMode::Normal;
|
||||
};
|
||||
|
||||
PDFRenderer::Features m_features;
|
||||
PDFCachedItem<QPen> m_currentPen;
|
||||
PDFCachedItem<QBrush> m_currentBrush;
|
||||
std::vector<PDFTransparencyGroupPainterData> m_transparencyGroupDataStack;
|
||||
};
|
||||
|
||||
/// Processor, which processes PDF's page commands on the QPainter. It works with QPainter
|
||||
/// and with transformation matrix, which translates page points to the device points.
|
||||
/// Only basic transparency is supported, advanced transparency, such as transparency groups,
|
||||
/// are not supported. Painter will try to emulate them so painting will not fail completely.
|
||||
class PDFPainter : public PDFPageContentProcessor
|
||||
class PDFPainter : public PDFPainterBase
|
||||
{
|
||||
using BaseClass = PDFPainterBase;
|
||||
|
||||
public:
|
||||
/// Constructs new PDFPainter object, with default parameters.
|
||||
/// \param painter Painter, on which page content is drawn
|
||||
@ -59,48 +124,182 @@ protected:
|
||||
virtual void performClipping(const QPainterPath& path, Qt::FillRule fillRule) override;
|
||||
virtual void performImagePainting(const QImage& image) override;
|
||||
virtual void performMeshPainting(const PDFMesh& mesh) override;
|
||||
virtual void performUpdateGraphicsState(const PDFPageContentProcessorState& state) override;
|
||||
virtual void performSaveGraphicState(ProcessOrder order) override;
|
||||
virtual void performRestoreGraphicState(ProcessOrder order) override;
|
||||
virtual void performBeginTransparencyGroup(ProcessOrder order, const PDFTransparencyGroup& transparencyGroup);
|
||||
virtual void performEndTransparencyGroup(ProcessOrder order, const PDFTransparencyGroup& transparencyGroup);
|
||||
virtual bool isContentSuppressedByOC(PDFObjectReference ocgOrOcmd) override;
|
||||
virtual void setWorldMatrix(const QMatrix& matrix) override;
|
||||
virtual void setCompositionMode(QPainter::CompositionMode mode) override;
|
||||
|
||||
private:
|
||||
/// Returns current pen
|
||||
const QPen& getCurrentPen() { return m_currentPen.get(this, &PDFPainter::getCurrentPenImpl); }
|
||||
QPainter* m_painter;
|
||||
};
|
||||
|
||||
/// Returns current brush
|
||||
const QBrush& getCurrentBrush() { return m_currentBrush.get(this, &PDFPainter::getCurrentBrushImpl); }
|
||||
/// Precompiled page contains precompiled graphic instructions of a PDF page to draw it quickly
|
||||
/// on the target painter. It enables very fast drawing, because instructions are not decoded
|
||||
/// and interpreted from the PDF stream, but they are just "played" on the painter.
|
||||
class PDFPrecompiledPage
|
||||
{
|
||||
public:
|
||||
|
||||
/// Returns current pen (implementation)
|
||||
QPen getCurrentPenImpl() const;
|
||||
|
||||
/// Returns current brush (implementation)
|
||||
QBrush getCurrentBrushImpl() const;
|
||||
|
||||
/// Returns effective stroking alpha from transparency groups and current graphic state
|
||||
PDFReal getEffectiveStrokingAlpha() const;
|
||||
|
||||
/// Returns effective filling alpha from transparency groups and current graphic state
|
||||
PDFReal getEffectiveFillingAlpha() const;
|
||||
|
||||
/// Returns true, if blend mode can be set according the transparency group stack
|
||||
bool canSetBlendMode(BlendMode mode) const;
|
||||
|
||||
struct PDFTransparencyGroupPainterData
|
||||
enum class InstructionType
|
||||
{
|
||||
PDFTransparencyGroup group;
|
||||
PDFReal alphaStroke = 1.0;
|
||||
PDFReal alphaFill = 1.0;
|
||||
BlendMode blendMode = BlendMode::Normal;
|
||||
Invalid,
|
||||
DrawPath,
|
||||
DrawImage,
|
||||
DrawMesh,
|
||||
Clip,
|
||||
SaveGraphicState,
|
||||
RestoreGraphicState,
|
||||
SetWorldMatrix,
|
||||
SetCompositionMode
|
||||
};
|
||||
|
||||
QPainter* m_painter;
|
||||
PDFRenderer::Features m_features;
|
||||
PDFCachedItem<QPen> m_currentPen;
|
||||
PDFCachedItem<QBrush> m_currentBrush;
|
||||
std::vector<PDFTransparencyGroupPainterData> m_transparencyGroupDataStack;
|
||||
struct Instruction
|
||||
{
|
||||
inline Instruction() = default;
|
||||
inline Instruction(InstructionType type, size_t dataIndex) :
|
||||
type(type),
|
||||
dataIndex(dataIndex)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
InstructionType type = InstructionType::Invalid;
|
||||
size_t dataIndex = 0;
|
||||
};
|
||||
|
||||
/// Paints page onto the painter using matrix
|
||||
/// \param painter Painter, onto which is page drawn
|
||||
/// \param cropBox Page's crop box
|
||||
/// \param pagePointToDevicePointMatrix Page point to device point transformation matrix
|
||||
/// \param features Renderer features
|
||||
void draw(QPainter* painter, const QRectF& cropBox, const QMatrix& pagePointToDevicePointMatrix, PDFRenderer::Features features) const;
|
||||
|
||||
void addPath(QPen pen, QBrush brush, QPainterPath path, bool isText);
|
||||
void addClip(QPainterPath path);
|
||||
void addImage(QImage image);
|
||||
void addMesh(PDFMesh mesh, PDFReal alpha);
|
||||
void addSaveGraphicState() { m_instructions.emplace_back(InstructionType::SaveGraphicState, 0); }
|
||||
void addRestoreGraphicState() { m_instructions.emplace_back(InstructionType::RestoreGraphicState, 0); }
|
||||
void addSetWorldMatrix(const QMatrix& matrix);
|
||||
void addSetCompositionMode(QPainter::CompositionMode compositionMode);
|
||||
|
||||
/// Optimizes page memory allocation to contain less space
|
||||
void optimize();
|
||||
|
||||
/// Finalizes precompiled page
|
||||
/// \param compilingTimeNS Compiling time in nanoseconds
|
||||
/// \param errors List of rendering errors
|
||||
void finalize(qint64 compilingTimeNS, QList<PDFRenderError> errors);
|
||||
|
||||
/// Returns compiling time in nanoseconds
|
||||
qint64 getCompilingTimeNS() const { return m_compilingTimeNS; }
|
||||
|
||||
/// Returns a list of rendering errors
|
||||
const QList<PDFRenderError>& getErrors() const { return m_errors; }
|
||||
|
||||
/// Returns true, if page is valid (i.e. has nonzero instruction count)
|
||||
bool isValid() const { return !m_instructions.empty(); }
|
||||
|
||||
private:
|
||||
struct PathPaintData
|
||||
{
|
||||
inline PathPaintData() = default;
|
||||
inline PathPaintData(QPen pen, QBrush brush, QPainterPath path, bool isText) :
|
||||
pen(qMove(pen)),
|
||||
brush(qMove(brush)),
|
||||
path(qMove(path)),
|
||||
isText(isText)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QPen pen;
|
||||
QBrush brush;
|
||||
QPainterPath path;
|
||||
bool isText = false;
|
||||
};
|
||||
|
||||
struct ClipData
|
||||
{
|
||||
inline ClipData() = default;
|
||||
inline ClipData(QPainterPath path) :
|
||||
clipPath(qMove(path))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QPainterPath clipPath;
|
||||
};
|
||||
|
||||
struct ImageData
|
||||
{
|
||||
inline ImageData() = default;
|
||||
inline ImageData(QImage image) :
|
||||
image(qMove(image))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QImage image;
|
||||
};
|
||||
|
||||
struct MeshPaintData
|
||||
{
|
||||
inline MeshPaintData() = default;
|
||||
inline MeshPaintData(PDFMesh mesh, PDFReal alpha) :
|
||||
mesh(qMove(mesh)),
|
||||
alpha(alpha)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
PDFMesh mesh;
|
||||
PDFReal alpha = 1.0;
|
||||
};
|
||||
|
||||
qint64 m_compilingTimeNS = 0;
|
||||
std::vector<Instruction> m_instructions;
|
||||
std::vector<PathPaintData> m_paths;
|
||||
std::vector<ClipData> m_clips;
|
||||
std::vector<ImageData> m_images;
|
||||
std::vector<MeshPaintData> m_meshes;
|
||||
std::vector<QMatrix> m_matrices;
|
||||
std::vector<QPainter::CompositionMode> m_compositionModes;
|
||||
QList<PDFRenderError> m_errors;
|
||||
};
|
||||
|
||||
/// Processor, which processes PDF's page commands and writes them to the precompiled page.
|
||||
/// Precompiled page then can be used to execute these commands on QPainter.
|
||||
class PDFPrecompiledPageGenerator : public PDFPainterBase
|
||||
{
|
||||
using BaseClass = PDFPainterBase;
|
||||
|
||||
public:
|
||||
explicit inline PDFPrecompiledPageGenerator(PDFPrecompiledPage* precompiledPage,
|
||||
PDFRenderer::Features features,
|
||||
const PDFPage* page,
|
||||
const PDFDocument* document,
|
||||
const PDFFontCache* fontCache,
|
||||
const PDFOptionalContentActivity* optionalContentActivity,
|
||||
const PDFMeshQualitySettings& meshQualitySettings) :
|
||||
BaseClass(features, page, document, fontCache, optionalContentActivity, QMatrix(), meshQualitySettings),
|
||||
m_precompiledPage(precompiledPage)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void performPathPainting(const QPainterPath& path, bool stroke, bool fill, bool text, Qt::FillRule fillRule) override;
|
||||
virtual void performClipping(const QPainterPath& path, Qt::FillRule fillRule) override;
|
||||
virtual void performImagePainting(const QImage& image) override;
|
||||
virtual void performMeshPainting(const PDFMesh& mesh) override;
|
||||
virtual void performSaveGraphicState(ProcessOrder order) override;
|
||||
virtual void performRestoreGraphicState(ProcessOrder order) override;
|
||||
virtual void setWorldMatrix(const QMatrix& matrix) override;
|
||||
virtual void setCompositionMode(QPainter::CompositionMode mode) override;
|
||||
|
||||
private:
|
||||
PDFPrecompiledPage* m_precompiledPage;
|
||||
};
|
||||
|
||||
} // namespace pdf
|
||||
|
Reference in New Issue
Block a user