Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkPDFGraphicState.cpp
Go to the documentation of this file.
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
9
10#include "include/core/SkData.h"
16#include "src/pdf/SkPDFUtils.h"
17
18static const char* as_pdf_blend_mode_name(SkBlendMode mode) {
19 const char* name = SkPDFUtils::BlendModeName(mode);
21 return name;
22}
23
24static int to_stroke_cap(uint8_t cap) {
25 // PDF32000.book section 8.4.3.3 "Line Cap Style"
26 switch ((SkPaint::Cap)cap) {
27 case SkPaint::kButt_Cap: return 0;
28 case SkPaint::kRound_Cap: return 1;
29 case SkPaint::kSquare_Cap: return 2;
30 default: SkASSERT(false); return 0;
31 }
32}
33
34static int to_stroke_join(uint8_t join) {
35 // PDF32000.book section 8.4.3.4 "Line Join Style"
36 switch ((SkPaint::Join)join) {
37 case SkPaint::kMiter_Join: return 0;
38 case SkPaint::kRound_Join: return 1;
39 case SkPaint::kBevel_Join: return 2;
40 default: SkASSERT(false); return 0;
41 }
42}
43
44// If a SkBlendMode is unsupported in PDF, this function returns
45// SrcOver, otherwise, it returns the blend mode.
46static uint8_t pdf_blend_mode(SkBlendMode mode) {
48 || SkBlendMode::kXor == mode
49 || SkBlendMode::kPlus == mode)
50 {
52 }
53 return SkToU8((unsigned)mode);
54}
55
57 const SkPaint& p) {
58 SkASSERT(doc);
59 const SkBlendMode mode = p.getBlendMode_or(SkBlendMode::kSrcOver);
60
61 if (SkPaint::kFill_Style == p.getStyle()) {
62 SkPDFFillGraphicState fillKey = {p.getColor4f().fA, pdf_blend_mode(mode)};
63 auto& fillMap = doc->fFillGSMap;
64 if (SkPDFIndirectReference* statePtr = fillMap.find(fillKey)) {
65 return *statePtr;
66 }
68 state.reserve(2);
69 state.insertColorComponentF("ca", fillKey.fAlpha);
70 state.insertName("BM", as_pdf_blend_mode_name((SkBlendMode)fillKey.fBlendMode));
72 fillMap.set(fillKey, ref);
73 return ref;
74 } else {
75 SkPDFStrokeGraphicState strokeKey = {
76 p.getStrokeWidth(),
77 p.getStrokeMiter(),
78 p.getColor4f().fA,
79 SkToU8(p.getStrokeCap()),
80 SkToU8(p.getStrokeJoin()),
81 pdf_blend_mode(mode)
82 };
83 auto& sMap = doc->fStrokeGSMap;
84 if (SkPDFIndirectReference* statePtr = sMap.find(strokeKey)) {
85 return *statePtr;
86 }
88 state.reserve(8);
89 state.insertColorComponentF("CA", strokeKey.fAlpha);
90 state.insertColorComponentF("ca", strokeKey.fAlpha);
91 state.insertInt("LC", to_stroke_cap(strokeKey.fStrokeCap));
92 state.insertInt("LJ", to_stroke_join(strokeKey.fStrokeJoin));
93 state.insertScalar("LW", strokeKey.fStrokeWidth);
94 state.insertScalar("ML", strokeKey.fStrokeMiter);
95 state.insertBool("SA", true); // SA = Auto stroke adjustment.
96 state.insertName("BM", as_pdf_blend_mode_name((SkBlendMode)strokeKey.fBlendMode));
98 sMap.set(strokeKey, ref);
99 return ref;
100 }
101}
102
103////////////////////////////////////////////////////////////////////////////////
104
106 // Acrobat crashes if we use a type 0 function, kpdf crashes if we use
107 // a type 2 function, so we use a type 4 function.
108 static const char psInvert[] = "{1 exch sub}";
109 // Do not copy the trailing '\0' into the SkData.
110 auto invertFunction = SkData::MakeWithoutCopy(psInvert, strlen(psInvert));
111
112 std::unique_ptr<SkPDFDict> dict = SkPDFMakeDict();
113 dict->insertInt("FunctionType", 4);
114 dict->insertObject("Domain", SkPDFMakeArray(0, 1));
115 dict->insertObject("Range", SkPDFMakeArray(0, 1));
116 return SkPDFStreamOut(std::move(dict), SkMemoryStream::Make(std::move(invertFunction)), doc);
117}
118
120 bool invert,
121 SkPDFSMaskMode sMaskMode,
122 SkPDFDocument* doc) {
123 // The practical chances of using the same mask more than once are unlikely
124 // enough that it's not worth canonicalizing.
125 auto sMaskDict = SkPDFMakeDict("Mask");
126 if (sMaskMode == kAlpha_SMaskMode) {
127 sMaskDict->insertName("S", "Alpha");
128 } else if (sMaskMode == kLuminosity_SMaskMode) {
129 sMaskDict->insertName("S", "Luminosity");
130 }
131 sMaskDict->insertRef("G", sMask);
132 if (invert) {
133 // let the doc deduplicate this object.
136 }
137 sMaskDict->insertRef("TR", doc->fInvertFunction);
138 }
139 SkPDFDict result("ExtGState");
140 result.insertObject("SMask", std::move(sMaskDict));
141 return doc->emit(result);
142}
#define SkASSERT(cond)
Definition SkAssert.h:116
SkBlendMode
Definition SkBlendMode.h:38
@ kPlus
r = min(s + d, 1)
@ kSrcOver
r = s + (1-sa)*d
@ kXor
r = s*(1-da) + d*(1-sa)
static int to_stroke_cap(uint8_t cap)
static SkPDFIndirectReference make_invert_function(SkPDFDocument *doc)
static uint8_t pdf_blend_mode(SkBlendMode mode)
static int to_stroke_join(uint8_t join)
static const char * as_pdf_blend_mode_name(SkBlendMode mode)
SkPDFIndirectReference SkPDFStreamOut(std::unique_ptr< SkPDFDict > dict, std::unique_ptr< SkStreamAsset > content, SkPDFDocument *doc, SkPDFSteamCompressionEnabled compress)
static std::unique_ptr< SkPDFDict > SkPDFMakeDict(const char *type=nullptr)
Definition SkPDFTypes.h:195
static std::unique_ptr< SkPDFArray > SkPDFMakeArray(Args... args)
Definition SkPDFTypes.h:135
constexpr uint8_t SkToU8(S x)
Definition SkTo.h:22
static sk_sp< SkData > MakeWithoutCopy(const void *data, size_t length)
Definition SkData.h:116
static std::unique_ptr< SkMemoryStream > Make(sk_sp< SkData > data)
Definition SkStream.cpp:314
void reserve(int n)
skia_private::THashMap< SkPDFStrokeGraphicState, SkPDFIndirectReference, SkPDFStrokeGraphicState::Hash > fStrokeGSMap
SkPDFIndirectReference emit(const SkPDFObject &, SkPDFIndirectReference)
skia_private::THashMap< SkPDFFillGraphicState, SkPDFIndirectReference, SkPDFFillGraphicState::Hash > fFillGSMap
SkPDFIndirectReference fInvertFunction
@ kRound_Cap
adds circle
Definition SkPaint.h:335
@ kButt_Cap
no stroke extension
Definition SkPaint.h:334
@ kSquare_Cap
adds square
Definition SkPaint.h:336
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
@ kRound_Join
adds circle
Definition SkPaint.h:360
@ kMiter_Join
extends to miter limit
Definition SkPaint.h:359
@ kBevel_Join
connects outside edges
Definition SkPaint.h:361
AtkStateType state
gboolean invert
GAsyncResult * result
const char * name
Definition fuchsia.cc:50
SkPDFIndirectReference GetGraphicStateForPaint(SkPDFDocument *, const SkPaint &)
SkPDFIndirectReference GetSMaskGraphicState(SkPDFIndirectReference sMask, bool invert, SkPDFSMaskMode sMaskMode, SkPDFDocument *doc)
const char * BlendModeName(SkBlendMode)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition switches.h:228