Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkPDFGraphicStackState.cpp
Go to the documentation of this file.
1// Copyright 2019 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
5
10
12 SkASSERT(color.fA == 1); // We handle alpha elsewhere.
14 result->writeText(" ");
16 result->writeText(" ");
18 result->writeText(" ");
19}
20
22 if (u.isEmpty() || v.isEmpty()) { return {0, 0, 0, 0}; }
23 return u.intersect(v) ? u : SkRect{0, 0, 0, 0};
24}
25
26// Test to see if the clipstack is a simple rect, If so, we can avoid all PathOps code
27// and speed thing up.
28static bool is_rect(const SkClipStack& clipStack, const SkRect& bounds, SkRect* dst) {
29 SkRect currentClip = bounds;
31 while (const SkClipStack::Element* element = iter.next()) {
32 SkRect elementRect{0, 0, 0, 0};
33 switch (element->getDeviceSpaceType()) {
35 break;
37 elementRect = element->getDeviceSpaceRect();
38 break;
39 default:
40 return false;
41 }
42 if (element->isReplaceOp()) {
43 currentClip = rect_intersect(bounds, elementRect);
44 } else if (element->getOp() == SkClipOp::kIntersect) {
45 currentClip = rect_intersect(currentClip, elementRect);
46 } else {
47 return false;
48 }
49 }
50 *dst = currentClip;
51 return true;
52}
53
54// TODO: When there's no expanding clip ops, this function may not be necessary anymore.
55static bool is_complex_clip(const SkClipStack& stack) {
57 while (const SkClipStack::Element* element = iter.next()) {
58 if (element->isReplaceOp() ||
59 (element->getOp() != SkClipOp::kDifference &&
60 element->getOp() != SkClipOp::kIntersect)) {
61 return true;
62 }
63 }
64 return false;
65}
66
67template <typename F>
68static void apply_clip(const SkClipStack& stack, const SkRect& outerBounds, F fn) {
69 // assumes clipstack is not complex.
70 constexpr SkRect kHuge{-30000, -30000, 30000, 30000};
72 SkRect bounds = outerBounds;
73 while (const SkClipStack::Element* element = iter.next()) {
74 SkPath operand;
75 element->asDeviceSpacePath(&operand);
76 SkPathOp op;
77 switch (element->getOp()) {
80 default: SkASSERT(false); return;
81 }
82 if (op == kDifference_SkPathOp ||
83 operand.isInverseFillType() ||
84 !kHuge.contains(operand.getBounds()))
85 {
86 Op(SkPath::Rect(bounds), operand, op, &operand);
87 }
88 SkASSERT(!operand.isInverseFillType());
89 fn(operand);
90 if (!bounds.intersect(operand.getBounds())) {
91 return; // return early;
92 }
93 }
94}
95
96static void append_clip_path(const SkPath& clipPath, SkWStream* wStream) {
98 SkPathFillType clipFill = clipPath.getFillType();
101 if (clipFill == SkPathFillType::kEvenOdd) {
102 wStream->writeText("W* n\n");
103 } else {
104 wStream->writeText("W n\n");
105 }
106}
107
108static void append_clip(const SkClipStack& clipStack,
109 const SkIRect& bounds,
110 SkWStream* wStream) {
111 // The bounds are slightly outset to ensure this is correct in the
112 // face of floating-point accuracy and possible SkRegion bitmap
113 // approximations.
114 SkRect outsetBounds = SkRect::Make(bounds.makeOutset(1, 1));
115
116 SkRect clipStackRect;
117 if (is_rect(clipStack, outsetBounds, &clipStackRect)) {
118 SkPDFUtils::AppendRectangle(clipStackRect, wStream);
119 wStream->writeText("W* n\n");
120 return;
121 }
122
123 if (is_complex_clip(clipStack)) {
124 SkPath clipPath;
125 SkClipStack_AsPath(clipStack, &clipPath);
126 if (Op(clipPath, SkPath::Rect(outsetBounds), kIntersect_SkPathOp, &clipPath)) {
127 append_clip_path(clipPath, wStream);
128 }
129 // If Op() fails (pathological case; e.g. input values are
130 // extremely large or NaN), emit no clip at all.
131 } else {
132 apply_clip(clipStack, outsetBounds, [wStream](const SkPath& path) {
133 append_clip_path(path, wStream);
134 });
135 }
136}
137
138////////////////////////////////////////////////////////////////////////////////
139
140void SkPDFGraphicStackState::updateClip(const SkClipStack* clipStack, const SkIRect& bounds) {
141 uint32_t clipStackGenID = clipStack ? clipStack->getTopmostGenID()
143 if (clipStackGenID == currentEntry()->fClipStackGenID) {
144 return;
145 }
146 while (fStackDepth > 0) {
147 this->pop();
148 if (clipStackGenID == currentEntry()->fClipStackGenID) {
149 return;
150 }
151 }
153 if (clipStackGenID != SkClipStack::kWideOpenGenID) {
154 SkASSERT(clipStack);
155 this->push();
156
157 currentEntry()->fClipStackGenID = clipStackGenID;
158 append_clip(*clipStack, bounds, fContentStream);
159 }
160}
161
162
164 if (matrix == currentEntry()->fMatrix) {
165 return;
166 }
167
168 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
170 SkASSERT(fEntries[fStackDepth].fClipStackGenID ==
171 fEntries[fStackDepth -1].fClipStackGenID);
172 this->pop();
173
175 }
176 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
177 return;
178 }
179
180 this->push();
182 currentEntry()->fMatrix = matrix;
183}
184
186 // PDF treats a shader as a color, so we only set one or the other.
187 if (state.fShaderIndex >= 0) {
188 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
190 currentEntry()->fShaderIndex = state.fShaderIndex;
191 }
192 } else if (state.fColor != currentEntry()->fColor || currentEntry()->fShaderIndex >= 0) {
196 fContentStream->writeText("rg\n");
197 currentEntry()->fColor = state.fColor;
199 }
200
201 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
203 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
204 }
205
206 if (state.fTextScaleX) {
207 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
208 SkScalar pdfScale = state.fTextScaleX * 100;
210 fContentStream->writeText(" Tz\n");
211 currentEntry()->fTextScaleX = state.fTextScaleX;
212 }
213 }
214}
215
222
229
231 if (fContentStream) {
232 while (fStackDepth) {
233 this->pop();
234 }
235 }
236 SkASSERT(fStackDepth == 0);
237}
SkColor4f color
#define SkASSERT(cond)
Definition SkAssert.h:116
void SkClipStack_AsPath(const SkClipStack &cs, SkPath *path)
static void apply_clip(const SkClipStack &stack, const SkRect &outerBounds, F fn)
static void emit_pdf_color(SkColor4f color, SkWStream *result)
static bool is_rect(const SkClipStack &clipStack, const SkRect &bounds, SkRect *dst)
static SkRect rect_intersect(SkRect u, SkRect v)
static bool is_complex_clip(const SkClipStack &stack)
static void append_clip_path(const SkPath &clipPath, SkWStream *wStream)
static void append_clip(const SkClipStack &clipStack, const SkIRect &bounds, SkWStream *wStream)
#define NOT_IMPLEMENTED(condition, assert)
Definition SkPDFUtils.h:39
SkPathOp
Definition SkPathOps.h:22
@ kDifference_SkPathOp
subtract the op path from the first path
Definition SkPathOps.h:23
@ kIntersect_SkPathOp
intersect the two paths
Definition SkPathOps.h:24
SkPathFillType
Definition SkPathTypes.h:11
@ kEmpty
This element makes the clip empty (regardless of previous elements).
@ kRect
This element combines a device space round-rect with the current clip.
const Element * next()
uint32_t getTopmostGenID() const
static const uint32_t kWideOpenGenID
@ kIdentity_Mask
identity SkMatrix; all bits clear
Definition SkMatrix.h:192
TypeMask getType() const
Definition SkMatrix.h:207
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
bool isInverseFillType() const
Definition SkPath.h:244
static SkPath Rect(const SkRect &, SkPathDirection=SkPathDirection::kCW, unsigned startIndex=0)
Definition SkPath.cpp:3518
const SkRect & getBounds() const
Definition SkPath.cpp:420
bool writeText(const char text[])
Definition SkStream.h:247
float SkScalar
Definition extension.cpp:12
AtkStateType state
GAsyncResult * result
void ApplyGraphicState(int objectIndex, SkWStream *content)
void EmitPath(const SkPath &path, SkPaint::Style paintStyle, bool doConsumeDegerates, SkWStream *content, SkScalar tolerance=0.25f)
void AppendRectangle(const SkRect &rect, SkWStream *content)
void AppendScalar(SkScalar value, SkWStream *stream)
Definition SkPDFUtils.h:86
void AppendTransform(const SkMatrix &, SkWStream *)
void ApplyPattern(int objectIndex, SkWStream *content)
void AppendColorComponentF(float value, SkWStream *wStream)
Definition SkPDFUtils.h:80
Definition SkMD5.cpp:120
Entry fEntries[kMaxStackDepth+1]
void updateMatrix(const SkMatrix &matrix)
void updateDrawingState(const Entry &state)
SkDynamicMemoryWStream * fContentStream
static constexpr int kMaxStackDepth
void updateClip(const SkClipStack *clipStack, const SkIRect &bounds)
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
bool intersect(const SkRect &r)
Definition SkRect.cpp:114
bool isEmpty() const
Definition SkRect.h:693