Debian Codegraph @

src/pdf/SkPDFGraphicStackState.cpp

raw
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
4 #include "src/pdf/SkPDFGraphicStackState.h"
5
6 #include "include/core/SkClipOp.h"
7 #include "include/core/SkPaint.h"
8 #include "include/core/SkPath.h"
9 #include "include/core/SkPathTypes.h"
10 #include "include/core/SkRect.h"
11 #include "include/core/SkStream.h"
12 #include "include/pathops/SkPathOps.h"
13 #include "include/private/base/SkAssert.h"
14 #include "src/pdf/SkPDFUtils.h"
15 #include "src/utils/SkClipStackUtils.h"
16
17 static void emit_pdf_color(SkColor4f color, SkWStream* result) {
18 SkASSERT(color.fA == 1); // We handle alpha elsewhere.
19 SkPDFUtils::AppendColorComponentF(color.fR, result);
20 result->writeText(" ");
21 SkPDFUtils::AppendColorComponentF(color.fG, result);
22 result->writeText(" ");
23 SkPDFUtils::AppendColorComponentF(color.fB, result);
24 result->writeText(" ");
25 }
26
27 static SkRect rect_intersect(SkRect u, SkRect v) {
28 if (u.isEmpty() || v.isEmpty()) { return {0, 0, 0, 0}; }
29 return u.intersect(v) ? u : SkRect{0, 0, 0, 0};
30 }
31
32 // Test to see if the clipstack is a simple rect, If so, we can avoid all PathOps code
33 // and speed thing up.
34 static bool is_rect(const SkClipStack& clipStack, const SkRect& bounds, SkRect* dst) {
35 SkRect currentClip = bounds;
36 SkClipStack::Iter iter(clipStack, SkClipStack::Iter::kBottom_IterStart);
37 while (const SkClipStack::Element* element = iter.next()) {
38 SkRect elementRect{0, 0, 0, 0};
39 switch (element->getDeviceSpaceType()) {
40 case SkClipStack::Element::DeviceSpaceType::kEmpty:
41 break;
42 case SkClipStack::Element::DeviceSpaceType::kRect:
43 elementRect = element->getDeviceSpaceRect();
44 break;
45 default:
46 return false;
47 }
48 if (element->isReplaceOp()) {
49 currentClip = rect_intersect(bounds, elementRect);
50 } else if (element->getOp() == SkClipOp::kIntersect) {
51 currentClip = rect_intersect(currentClip, elementRect);
52 } else {
53 return false;
54 }
55 }
56 *dst = currentClip;
57 return true;
58 }
59
60 // TODO: When there's no expanding clip ops, this function may not be necessary anymore.
61 static bool is_complex_clip(const SkClipStack& stack) {
62 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
63 while (const SkClipStack::Element* element = iter.next()) {
64 if (element->isReplaceOp()) {
65 return true;
66 }
67 }
68 return false;
69 }
70
71 template <typename F>
72 static void apply_clip(const SkClipStack& stack, const SkRect& outerBounds, F fn) {
73 // assumes clipstack is not complex.
74 constexpr SkRect kHuge{-30000, -30000, 30000, 30000};
75 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
76 SkRect bounds = outerBounds;
77 while (const SkClipStack::Element* element = iter.next()) {
78 SkPath operand = element->asDeviceSpacePath();
79 SkPathOp op;
80 switch (element->getOp()) {
81 case SkClipOp::kDifference: op = kDifference_SkPathOp; break;
82 case SkClipOp::kIntersect: op = kIntersect_SkPathOp; break;
83 }
84 if (op == kDifference_SkPathOp ||
85 operand.isInverseFillType() ||
86 !kHuge.contains(operand.getBounds()))
87 {
88 if (auto result = Op(SkPath::Rect(bounds), operand, op)) {
89 operand = *result;
90 }
91 }
92 SkASSERT(!operand.isInverseFillType());
93 fn(operand);
94 if (!bounds.intersect(operand.getBounds())) {
95 return; // return early;
96 }
97 }
98 }
99
100 static void append_clip_path(const SkPath& clipPath, SkWStream* wStream) {
101 using SkPDFUtils::EmptyPath, SkPDFUtils::EmptyVerb;
102 if (!SkPDFUtils::EmitPath(clipPath, SkPaint::kFill_Style,
103 EmptyPath::Preserve, EmptyVerb::Discard, wStream))
104 {
105 return;
106 }
107 SkPathFillType clipFill = clipPath.getFillType();
108 NOT_IMPLEMENTED(clipFill == SkPathFillType::kInverseEvenOdd, false);
109 NOT_IMPLEMENTED(clipFill == SkPathFillType::kInverseWinding, false);
110 if (clipFill == SkPathFillType::kEvenOdd) {
111 wStream->writeText("W* n\n");
112 } else {
113 wStream->writeText("W n\n");
114 }
115 }
116
117 static void append_clip(const SkClipStack& clipStack,
118 const SkIRect& bounds,
119 SkWStream* wStream) {
120 // The bounds are slightly outset to ensure this is correct in the
121 // face of floating-point accuracy and possible SkRegion bitmap
122 // approximations.
123 SkRect outsetBounds = SkRect::Make(bounds.makeOutset(1, 1));
124
125 SkRect clipStackRect;
126 if (is_rect(clipStack, outsetBounds, &clipStackRect)) {
127 SkPDFUtils::AppendRectangle(clipStackRect, wStream);
128 wStream->writeText("W* n\n");
129 return;
130 }
131
132 if (is_complex_clip(clipStack)) {
133 if (auto clipPath = Op(SkClipStack_AsPath(clipStack), SkPath::Rect(outsetBounds),
134 kIntersect_SkPathOp)) {
135 append_clip_path(*clipPath, wStream);
136 }
137 // If Op() fails (pathological case; e.g. input values are
138 // extremely large or NaN), emit no clip at all.
139 } else {
140 apply_clip(clipStack, outsetBounds, [wStream](const SkPath& path) {
141 append_clip_path(path, wStream);
142 });
143 }
144 }
145
146 ////////////////////////////////////////////////////////////////////////////////
147
148 void SkPDFGraphicStackState::updateClip(const SkClipStack* clipStack, const SkIRect& bounds) {
149 uint32_t clipStackGenID = clipStack ? clipStack->getTopmostGenID()
150 : SkClipStack::kWideOpenGenID;
151 if (clipStackGenID == currentEntry()->fClipStackGenID) {
152 return;
153 }
154 while (fStackDepth > 0) {
155 this->pop();
156 if (clipStackGenID == currentEntry()->fClipStackGenID) {
157 return;
158 }
159 }
160 SkASSERT(currentEntry()->fClipStackGenID == SkClipStack::kWideOpenGenID);
161 if (clipStackGenID != SkClipStack::kWideOpenGenID) {
162 SkASSERT(clipStack);
163 this->push();
164
165 currentEntry()->fClipStackGenID = clipStackGenID;
166 append_clip(*clipStack, bounds, fContentStream);
167 }
168 }
169
170
171 void SkPDFGraphicStackState::updateMatrix(const SkMatrix& matrix) {
172 if (matrix == currentEntry()->fMatrix) {
173 return;
174 }
175
176 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
177 SkASSERT(fStackDepth > 0);
178 SkASSERT(fEntries[fStackDepth].fClipStackGenID ==
179 fEntries[fStackDepth -1].fClipStackGenID);
180 this->pop();
181
182 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
183 }
184 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
185 return;
186 }
187
188 this->push();
189 SkPDFUtils::AppendTransform(matrix, fContentStream);
190 currentEntry()->fMatrix = matrix;
191 }
192
193 void SkPDFGraphicStackState::updateDrawingState(const SkPDFGraphicStackState::Entry& state) {
194 // PDF treats a shader as a color, so we only set one or the other.
195 if (state.fShaderIndex >= 0) {
196 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
197 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
198 currentEntry()->fShaderIndex = state.fShaderIndex;
199 }
200 } else if (state.fColor != currentEntry()->fColor || currentEntry()->fShaderIndex >= 0) {
201 emit_pdf_color(state.fColor, fContentStream);
202 fContentStream->writeText("RG ");
203 emit_pdf_color(state.fColor, fContentStream);
204 fContentStream->writeText("rg\n");
205 currentEntry()->fColor = state.fColor;
206 currentEntry()->fShaderIndex = -1;
207 }
208
209 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
210 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
211 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
212 }
213
214 if (state.fTextScaleX) {
215 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
216 SkScalar pdfScale = state.fTextScaleX * 100;
217 SkPDFUtils::AppendScalar(pdfScale, fContentStream);
218 fContentStream->writeText(" Tz\n");
219 currentEntry()->fTextScaleX = state.fTextScaleX;
220 }
221 }
222 }
223
224 void SkPDFGraphicStackState::push() {
225 SkASSERT(fStackDepth < kMaxStackDepth);
226 fContentStream->writeText("q\n");
227 ++fStackDepth;
228 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
229 }
230
231 void SkPDFGraphicStackState::pop() {
232 SkASSERT(fStackDepth > 0);
233 fContentStream->writeText("Q\n");
234 fEntries[fStackDepth] = SkPDFGraphicStackState::Entry();
235 --fStackDepth;
236 }
237
238 void SkPDFGraphicStackState::drainStack() {
239 if (fContentStream) {
240 while (fStackDepth) {
241 this->pop();
242 }
243 }
244 SkASSERT(fStackDepth == 0);
245 }