Debian Codegraph @

src/pdf/SkPDFDevice.h

raw
1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkPDFDevice_DEFINED
9 #define SkPDFDevice_DEFINED
10
11 #include "include/core/SkCPURecorder.h"
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSamplingOptions.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSpan.h"
18 #include "include/core/SkStream.h"
19 #include "src/core/SkClipStack.h"
20 #include "src/core/SkClipStackDevice.h"
21 #include "src/core/SkTHash.h"
22 #include "src/pdf/SkKeyedImage.h"
23 #include "src/pdf/SkPDFGraphicStackState.h"
24 #include "src/pdf/SkPDFTag.h"
25 #include "src/pdf/SkPDFTypes.h"
26
27 #include <memory>
28
29 class SkBitmap;
30 class SkBlender;
31 class SkData;
32 class SkDevice;
33 class SkImage;
34 class SkMesh;
35 class SkPaint;
36 class SkPath;
37 class SkPDFDocument;
38 class SkRecorder;
39 class SkRRect;
40 class SkSpecialImage;
41 class SkSurface;
42 class SkSurfaceProps;
43 class SkVertices;
44 enum class SkBlendMode;
45 struct SkIRect;
46 struct SkISize;
47 struct SkImageInfo;
48 struct SkPoint;
49 struct SkRect;
50
51 namespace sktext {
52 class GlyphRun;
53 class GlyphRunList;
54 }
55
56 /**
57 * \class SkPDFDevice
58 *
59 * An SkPDFDevice is the drawing context for a page or layer of PDF
60 * content.
61 */
62 class SkPDFDevice final : public SkClipStackDevice {
63 public:
64 /**
65 * @param pageSize Page size in point units.
66 * 1 point == 127/360 mm == 1/72 inch
67 * @param document A non-null pointer back to the
68 * PDFDocument object. The document is responsible for
69 * de-duplicating across pages (via the SkPDFDocument) and
70 * for early serializing of large immutable objects, such
71 * as images (via SkPDFDocument::serialize()).
72 * @param initialTransform Transform to be applied to the entire page.
73 */
74 SkPDFDevice(SkISize pageSize, SkPDFDocument* document,
75 const SkMatrix& initialTransform = SkMatrix::I());
76
77 sk_sp<SkPDFDevice> makeCongruentDevice() {
78 return sk_make_sp<SkPDFDevice>(this->size(), fDocument);
79 }
80
81 ~SkPDFDevice() override;
82
83 /**
84 * These are called inside the per-device-layer loop for each draw call.
85 * When these are called, we have already applied any saveLayer
86 * operations, and are handling any looping from the paint.
87 */
88 void drawPaint(const SkPaint& paint) override;
89 void drawPoints(SkCanvas::PointMode, SkSpan<const SkPoint>, const SkPaint&) override;
90 void drawRect(const SkRect& r, const SkPaint& paint) override;
91 void drawOval(const SkRect& oval, const SkPaint& paint) override;
92 void drawRRect(const SkRRect& rr, const SkPaint& paint) override;
93 void drawPath(const SkPath& origpath, const SkPaint& paint) override;
94
95 void drawImageRect(const SkImage*,
96 const SkRect* src,
97 const SkRect& dst,
98 const SkSamplingOptions&,
99 const SkPaint&,
100 SkCanvas::SrcRectConstraint) override;
101
102 void drawVertices(const SkVertices*, sk_sp<SkBlender>, const SkPaint&, bool) override;
103 void drawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) override;
104
105 void drawAnnotation(const SkRect&, const char key[], SkData* value) override;
106
107 void drawDevice(SkDevice*, const SkSamplingOptions&, const SkPaint&) override;
108 void drawSpecial(SkSpecialImage*, const SkMatrix&, const SkSamplingOptions&,
109 const SkPaint&, SkCanvas::SrcRectConstraint) override;
110
111 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
112 sk_sp<SkDevice> createDevice(const CreateInfo&, const SkPaint*) override;
113
114 // PDF specific methods.
115 void drawSprite(const SkBitmap& bitmap, int x, int y,
116 const SkPaint& paint);
117
118 /** Create the resource dictionary for this device. Destructive. */
119 std::unique_ptr<SkPDFDict> makeResourceDict();
120
121 /** Returns a SkStream with the page contents.
122 */
123 std::unique_ptr<SkStreamAsset> content();
124
125 const SkMatrix& initialTransform() const { return fInitialTransform; }
126
127 SkRecorder* baseRecorder() const override {
128 // TODO(kjlubick) the creation of this should likely involve a CPU context.
129 return skcpu::Recorder::TODO();
130 }
131
132 private:
133 // TODO(vandebo): push most of SkPDFDevice's state into a core object in
134 // order to get the right access levels without using friend.
135 friend class ScopedContentEntry;
136
137 SkMatrix fInitialTransform;
138
139 skia_private::THashSet<SkPDFIndirectReference> fGraphicStateResources;
140 skia_private::THashSet<SkPDFIndirectReference> fXObjectResources;
141 skia_private::THashSet<SkPDFIndirectReference> fShaderResources;
142 skia_private::THashSet<SkPDFIndirectReference> fFontResources;
143
144 class MarkedContentManager {
145 public:
146 MarkedContentManager(SkPDFDocument* document, SkDynamicMemoryWStream* out);
147 ~MarkedContentManager();
148
149 // Sets the current element identifier. Associate future draws with the structure element
150 // with the given element identifier. Element identifier 0 is reserved to mean no structure
151 // element.
152 void setNextMarksElemId(int nextMarksElemId);
153
154 // The current element identifier.
155 int elemId() const;
156
157 // Starts a marked-content sequence for a content item for the structure element with the
158 // current element identifier. If there is an active marked-content sequence associated with
159 // a different element identifier the active marked-content sequence will first be closed.
160 // If there is no structure element with the current element identifier then the
161 // marked-content sequence will not be started.
162 void beginMark();
163
164 // Tests if there is an active marked-content sequence.
165 bool hasActiveMark() const;
166
167 // Accumulates an upper left location for the active mark. The point is in PDF page space
168 // and so is y-up. Only use if this.hasActiveMark()
169 void accumulate(const SkPoint& p);
170
171 // Tests if this marked content manager made any marks.
172 bool madeMarks() const { return fMadeMarks; }
173
174 private:
175 SkPDFDocument* fDoc;
176 SkDynamicMemoryWStream* fOut;
177 SkPDFStructTree::Mark fCurrentlyActiveMark;
178 int fNextMarksElemId;
179 int fCurrentMarksElemId;
180 bool fMadeMarks;
181 } fMarkManager;
182
183 SkDynamicMemoryWStream fContent;
184 SkDynamicMemoryWStream fContentBuffer;
185 bool fNeedsExtraSave = false;
186 SkPDFGraphicStackState fActiveStackState;
187 SkPDFDocument* fDocument;
188
189 ////////////////////////////////////////////////////////////////////////////
190
191 void onDrawGlyphRunList(SkCanvas*, const sktext::GlyphRunList&, const SkPaint& paint) override;
192
193 // Set alpha to true if making a transparency group form x-objects.
194 SkPDFIndirectReference makeFormXObjectFromDevice(bool alpha = false);
195 SkPDFIndirectReference makeFormXObjectFromDevice(SkIRect bbox, bool alpha = false);
196
197 void drawFormXObjectWithMask(SkPDFIndirectReference xObject,
198 SkPDFIndirectReference sMask,
199 SkBlendMode,
200 bool invertClip);
201
202 // If the paint or clip is such that we shouldn't draw anything, this
203 // returns nullptr and does not create a content entry.
204 // setUpContentEntry and finishContentEntry can be used directly, but
205 // the preferred method is to use the ScopedContentEntry helper class.
206 SkDynamicMemoryWStream* setUpContentEntry(const SkClipStack* clipStack,
207 const SkMatrix& matrix,
208 const SkPaint& paint,
209 SkScalar,
210 SkPDFIndirectReference* dst);
211 void finishContentEntry(const SkClipStack*, SkBlendMode, SkPDFIndirectReference, SkPath*);
212 bool isContentEmpty();
213
214 void internalDrawGlyphRun(
215 const sktext::GlyphRun& glyphRun, SkPoint offset, const SkPaint& runPaint);
216 void drawGlyphRunAsPath(
217 const sktext::GlyphRun& glyphRun, SkPoint offset, const SkPaint& runPaint);
218
219 void internalDrawImageRect(SkKeyedImage,
220 const SkRect* src,
221 const SkRect& dst,
222 const SkSamplingOptions&,
223 const SkPaint&,
224 const SkMatrix& canvasTransformationMatrix);
225
226 void internalDrawPath(const SkClipStack&,
227 const SkMatrix&,
228 const SkPath&,
229 const SkPaint&,
230 bool pathIsMutable);
231
232 void internalDrawPathWithFilter(const SkClipStack& clipStack,
233 const SkMatrix& ctm,
234 const SkPath& origPath,
235 const SkPaint& paint);
236
237 bool handleInversePath(const SkPath& origPath, const SkPaint& paint, bool pathIsMutable);
238
239 void clearMaskOnGraphicState(SkDynamicMemoryWStream*);
240 void setGraphicState(SkPDFIndirectReference gs, SkDynamicMemoryWStream*);
241 void drawFormXObject(SkPDFIndirectReference xObject, SkDynamicMemoryWStream*, SkPath* shape);
242
243 bool hasEmptyClip() const { return this->cs().isEmpty(this->bounds()); }
244
245 void reset();
246 };
247
248 #endif