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 #include "src/pdf/SkPDFGraphicState.h"
9
10 #include "include/core/SkData.h"
11 #include "include/core/SkPaint.h"
12 #include "include/docs/SkPDFDocument.h"
13 #include "include/private/SkTo.h"
14 #include "src/pdf/SkPDFDocumentPriv.h"
15 #include "src/pdf/SkPDFFormXObject.h"
16 #include "src/pdf/SkPDFUtils.h"
17
as_pdf_blend_mode_name(SkBlendMode mode)18 static const char* as_pdf_blend_mode_name(SkBlendMode mode) {
19 const char* name = SkPDFUtils::BlendModeName(mode);
20 SkASSERT(name);
21 return name;
22 }
23
to_stroke_cap(uint8_t cap)24 static 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
to_stroke_join(uint8_t join)34 static 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 SkXfermode is unsupported in PDF, this function returns
45 // SrcOver, otherwise, it returns that Xfermode as a Mode.
pdf_blend_mode(SkBlendMode mode)46 static uint8_t pdf_blend_mode(SkBlendMode mode) {
47 if (!SkPDFUtils::BlendModeName(mode)
48 || SkBlendMode::kXor == mode
49 || SkBlendMode::kPlus == mode)
50 {
51 mode = SkBlendMode::kSrcOver;
52 }
53 return SkToU8((unsigned)mode);
54 }
55
GetGraphicStateForPaint(SkPDFDocument * doc,const SkPaint & p)56 SkPDFIndirectReference SkPDFGraphicState::GetGraphicStateForPaint(SkPDFDocument* doc,
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 }
67 SkPDFDict state;
68 state.reserve(2);
69 state.insertColorComponentF("ca", fillKey.fAlpha);
70 state.insertName("BM", as_pdf_blend_mode_name((SkBlendMode)fillKey.fBlendMode));
71 SkPDFIndirectReference ref = doc->emit(state);
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 }
87 SkPDFDict state;
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));
97 SkPDFIndirectReference ref = doc->emit(state);
98 sMap.set(strokeKey, ref);
99 return ref;
100 }
101 }
102
103 ////////////////////////////////////////////////////////////////////////////////
104
make_invert_function(SkPDFDocument * doc)105 static SkPDFIndirectReference make_invert_function(SkPDFDocument* doc) {
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
GetSMaskGraphicState(SkPDFIndirectReference sMask,bool invert,SkPDFSMaskMode sMaskMode,SkPDFDocument * doc)119 SkPDFIndirectReference SkPDFGraphicState::GetSMaskGraphicState(SkPDFIndirectReference sMask,
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.
134 if (doc->fInvertFunction == SkPDFIndirectReference()) {
135 doc->fInvertFunction = make_invert_function(doc);
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 }
143