PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfContentStreamReader.h
1// SPDX-FileCopyrightText: 2022 Francesco Pretto <ceztko@gmail.com>
2// SPDX-License-Identifier: LGPL-2.0-or-later OR MPL-2.0
3
4#ifndef PDF_CONTENT_READER_H
5#define PDF_CONTENT_READER_H
6
7#include "PdfXObject.h"
8#include "PdfCanvas.h"
9#include "PdfData.h"
10#include "PdfDictionary.h"
11#include "PdfVariantStack.h"
12#include "PdfPostScriptTokenizer.h"
13
14namespace PoDoFo {
15
18{
19 Unknown = 0,
20 Operator,
22 ImageData,
23 DoXObject,
27};
28
37
39{
40 None = 0,
41 InvalidOperator = 1,
42 InvalidXObject = 2,
43 UnexpectedToken = 4,
44};
45
47class PODOFO_API PdfContent final
48{
49 friend class PdfContentStreamReader;
50public:
51 PdfContent();
52public:
53 struct Data
54 {
55 PdfVariantStack Stack;
56 PdfOperator Operator = PdfOperator::Unknown;
57 std::string_view Keyword;
58 PdfDictionary InlineImageDictionary;
59 charbuff InlineImageData;
60 const PdfName* Name = nullptr;
61 std::shared_ptr<PdfXObject> XObject;
62 };
63
64 const PdfVariantStack& GetStack() const;
65 PdfOperator GetOperator() const;
66 const std::string_view& GetKeyword() const;
67 const PdfDictionary& GetInlineImageDictionary() const;
68 const charbuff& GetInlineImageData() const;
69 const std::shared_ptr<const PdfXObject>& GetXObject() const;
70
71 bool HasWarnings() const;
72 bool HasErrors() const;
73
74 PdfContentType GetType() const { return Type; }
75 PdfContentWarnings GetWarnings() const { return Warnings; }
76 PdfContentErrors GetErrors() const { return Errors; }
77
79 const Data& operator*() const { return Data; }
80
82 Data& operator*() { return Data; }
83
85 const Data* operator->() const { return &Data; }
86
88 Data* operator->() { return &Data; }
89
90private:
91 void checkAccess(PdfContentType type) const;
92
93private:
94 PdfContentType Type;
95 bool ThrowOnWarnings;
96 PdfContentWarnings Warnings;
97 PdfContentErrors Errors;
98 struct Data Data;
99};
100
102{
103 None = 0,
104 ThrowOnWarnings = 1,
108};
109
114
115struct PODOFO_API PdfContentReaderArgs final
116{
117 PdfContentReaderFlags Flags = PdfContentReaderFlags::None;
118 PdfInlineImageHandler InlineImageHandler;
119};
120
123{
124public:
126
127 PdfContentStreamReader(std::shared_ptr<InputStreamDevice> device, nullable<const PdfContentReaderArgs&> args = { });
128
129private:
130 PdfContentStreamReader(std::shared_ptr<InputStreamDevice>&& device, const PdfCanvas* canvas,
132
133public:
134 bool TryReadNext(PdfContent& data);
135
136private:
137 void beforeReadReset(PdfContent& content);
138
139 void afterReadClear(PdfContent& content);
140
141 bool tryReadNextContent(PdfContent& content);
142
143 bool tryHandleOperator(PdfContent& content, bool& eof);
144
145 bool tryReadInlineImgDict(PdfContent& content);
146
147 bool tryReadInlineImgData(charbuff& data, const PdfDictionary& imageDict, bool skipSaveImage);
148
149 bool tryHandleXObject(PdfContent& content);
150
151 bool isCalledRecursively(const PdfObject* xobj);
152
153private:
154 struct Storage
155 {
157 std::string_view Keyword;
160 };
161
162 struct Input
163 {
164 std::shared_ptr<const PdfXObject> Form;
165 std::shared_ptr<InputStreamDevice> Device;
166 const PdfCanvas* Canvas;
167 };
168
169private:
170 std::vector<Input> m_inputs;
171 PdfContentReaderArgs m_args;
172 std::shared_ptr<charbuff> m_buffer;
173 PdfPostScriptTokenizer m_tokenizer;
174 bool m_readingInlineImgData; // A state of reading inline image data
175
176 // Temp storage
177 Storage m_temp;
178};
179
180};
181
182ENABLE_BITMASK_OPERATORS(PoDoFo::PdfContentReaderFlags);
183ENABLE_BITMASK_OPERATORS(PoDoFo::PdfContentWarnings);
184ENABLE_BITMASK_OPERATORS(PoDoFo::PdfContentErrors);
185
186#endif // PDF_CONTENT_READER_H
This class represents an input device It optionally supports peeking.
Definition InputDevice.h:19
An interface that provides the necessary features for a painter to draw onto a PdfObject.
Definition PdfCanvas.h:25
Reader class to read content streams.
Definition PdfContentStreamReader.h:123
Content as read from content streams.
Definition PdfContentStreamReader.h:48
Data & operator*()
Unchecked and mutable access to content data.
Definition PdfContentStreamReader.h:82
const Data * operator->() const
Unchecked access to content data.
Definition PdfContentStreamReader.h:85
Data * operator->()
Unchecked and mutable access to content data.
Definition PdfContentStreamReader.h:88
const Data & operator*() const
Unchecked access to content data.
Definition PdfContentStreamReader.h:79
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:77
This class represents a PdfName.
Definition PdfName.h:21
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:31
This class is a parser for general PostScript content in PDF documents.
Definition PdfPostScriptTokenizer.h:25
A variant data type which supports all data types supported by the PDF standard.
Definition PdfVariant.h:29
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:30
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
PdfContentReaderFlags
Definition PdfContentStreamReader.h:102
@ SkipFollowFormXObjects
Don't follow Form XObject.
@ SkipFetchInlineImages
Don't fetch inline images data, just skip it.
@ SkipHandleNonFormXObjects
Don't handle non Form XObjects (PdfImage, PdfXObjectPostScript). Doesn't influence traversing of Form...
PdfContentWarnings
Definition PdfContentStreamReader.h:30
@ MissingEndImage
Missing end inline image EI operator.
@ RecursiveXObject
Recursive XObject call detected. Applies to DoXObject.
@ InvalidImageDictionaryContent
Found invalid content while reading inline image dictionary. Applies to ImageDictionary.
@ SpuriousStackContent
Operand count for the operator are more than necessary.
PdfContentType
Type of the content read from a content stream.
Definition PdfContentStreamReader.h:18
@ ImageData
Raw inline image data found between ID and EI tags (see PDF ref section 4.8.6)
@ UnexpectedKeyword
An unexpected keyword that can be a custom operator or invalid PostScript content
@ DoXObject
Issued when a Do operator is found and it is handled by the reader. NOTE: for Form XObjects BeginForm...
@ EndFormXObject
Issued when a Form XObject has just been followed.
@ ImageDictionary
Inline image dictionary.
@ BeginFormXObject
Issued when a Form XObject is being followed.
@ Operator
The token is a PDF operator.
PdfPostScriptTokenType
An enum describing the type of a read token.
Definition PdfPostScriptTokenizer.h:15
@ Keyword
The token is a PDF keyword.
@ Variant
The token is a PDF variant. A variant is usually a parameter to a keyword.
@ Name
Name datatype. Names are used as keys in dictionary to reference values.
@ None
Do not add a default appearance.
PdfContentErrors
Definition PdfContentStreamReader.h:39
@ InvalidOperator
Unknown operator or insufficient operand count. Applies to Operator.
@ InvalidXObject
Invalid or not found XObject.
@ UnexpectedToken
Token encountered in an invalid context (e.g., misplaced PostScript brace delimiter '{' or '}')
std::function< bool(const PdfDictionary &imageDict, InputStreamDevice &device)> PdfInlineImageHandler
Custom handler for inline images.
Definition PdfContentStreamReader.h:113
PdfOperator
List of PDF stream content operators.
Definition PdfDeclarations.h:681