blob: c4b6c20fe443532af774cf3aefb27d345ead32df [file] [log] [blame]
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00006 */
7
halcanary67ec1f82014-06-27 11:36:20 -07008#include "SkData.h"
halcanarye6ea2442015-01-21 13:13:22 -08009#include "SkLazyPtr.h"
halcanaryfb62b3d2015-01-21 09:59:14 -080010#include "SkPDFCanon.h"
vandebo@chromium.org6112c212011-05-13 03:50:38 +000011#include "SkPDFFormXObject.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000012#include "SkPDFGraphicState.h"
vandebo@chromium.orgf71b2102011-04-04 19:46:31 +000013#include "SkPDFUtils.h"
vandebo@chromium.org48543272011-02-08 19:28:07 +000014#include "SkTypes.h"
15
halcanaryfb62b3d2015-01-21 09:59:14 -080016static const char* as_blend_mode(SkXfermode::Mode mode) {
vandebo@chromium.org48543272011-02-08 19:28:07 +000017 switch (mode) {
halcanaryfb62b3d2015-01-21 09:59:14 -080018 case SkXfermode::kSrcOver_Mode:
19 return "Normal";
20 case SkXfermode::kMultiply_Mode:
21 return "Multiply";
22 case SkXfermode::kScreen_Mode:
23 return "Screen";
24 case SkXfermode::kOverlay_Mode:
25 return "Overlay";
26 case SkXfermode::kDarken_Mode:
27 return "Darken";
28 case SkXfermode::kLighten_Mode:
29 return "Lighten";
30 case SkXfermode::kColorDodge_Mode:
31 return "ColorDodge";
32 case SkXfermode::kColorBurn_Mode:
33 return "ColorBurn";
34 case SkXfermode::kHardLight_Mode:
35 return "HardLight";
36 case SkXfermode::kSoftLight_Mode:
37 return "SoftLight";
38 case SkXfermode::kDifference_Mode:
39 return "Difference";
40 case SkXfermode::kExclusion_Mode:
41 return "Exclusion";
42 case SkXfermode::kHue_Mode:
43 return "Hue";
44 case SkXfermode::kSaturation_Mode:
45 return "Saturation";
46 case SkXfermode::kColor_Mode:
47 return "Color";
48 case SkXfermode::kLuminosity_Mode:
49 return "Luminosity";
vandebo@chromium.org48543272011-02-08 19:28:07 +000050
vandebo@chromium.org25adce82011-05-09 08:05:01 +000051 // These are handled in SkPDFDevice::setUpContentEntry.
vandebo@chromium.org48543272011-02-08 19:28:07 +000052 case SkXfermode::kClear_Mode:
53 case SkXfermode::kSrc_Mode:
54 case SkXfermode::kDst_Mode:
55 case SkXfermode::kDstOver_Mode:
56 case SkXfermode::kSrcIn_Mode:
57 case SkXfermode::kDstIn_Mode:
58 case SkXfermode::kSrcOut_Mode:
59 case SkXfermode::kDstOut_Mode:
vandebo@chromium.org3b416212013-10-30 20:48:05 +000060 case SkXfermode::kSrcATop_Mode:
61 case SkXfermode::kDstATop_Mode:
62 case SkXfermode::kModulate_Mode:
vandebo@chromium.org6112c212011-05-13 03:50:38 +000063 return "Normal";
64
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000065 // TODO(vandebo): Figure out if we can support more of these modes.
vandebo@chromium.org48543272011-02-08 19:28:07 +000066 case SkXfermode::kXor_Mode:
67 case SkXfermode::kPlus_Mode:
68 return NULL;
69 }
70 return NULL;
71}
72
halcanarybe27a112015-04-01 13:31:19 -070073// If a SkXfermode is unsupported in PDF, this function returns
74// SrcOver, otherwise, it returns that Xfermode as a Mode.
75static SkXfermode::Mode mode_for_pdf(const SkXfermode* xfermode) {
76 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
77 if (xfermode) {
78 xfermode->asMode(&mode);
halcanaryfb62b3d2015-01-21 09:59:14 -080079 }
halcanarybe27a112015-04-01 13:31:19 -070080 switch (mode) {
81 case SkXfermode::kSrcOver_Mode:
82 case SkXfermode::kMultiply_Mode:
83 case SkXfermode::kScreen_Mode:
84 case SkXfermode::kOverlay_Mode:
85 case SkXfermode::kDarken_Mode:
86 case SkXfermode::kLighten_Mode:
87 case SkXfermode::kColorDodge_Mode:
88 case SkXfermode::kColorBurn_Mode:
89 case SkXfermode::kHardLight_Mode:
90 case SkXfermode::kSoftLight_Mode:
91 case SkXfermode::kDifference_Mode:
92 case SkXfermode::kExclusion_Mode:
93 case SkXfermode::kHue_Mode:
94 case SkXfermode::kSaturation_Mode:
95 case SkXfermode::kColor_Mode:
96 case SkXfermode::kLuminosity_Mode:
97 // Mode is suppported and handled by pdf graphics state.
98 return mode;
99 default:
100 return SkXfermode::kSrcOver_Mode; // Default mode.
halcanaryfb62b3d2015-01-21 09:59:14 -0800101 }
halcanaryfb62b3d2015-01-21 09:59:14 -0800102}
103
halcanarybe27a112015-04-01 13:31:19 -0700104SkPDFGraphicState::SkPDFGraphicState(const SkPaint& p)
105 : fStrokeWidth(p.getStrokeWidth())
106 , fStrokeMiter(p.getStrokeMiter())
107 , fAlpha(p.getAlpha())
108 , fStrokeCap(SkToU8(p.getStrokeCap()))
109 , fStrokeJoin(SkToU8(p.getStrokeJoin()))
110 , fMode(SkToU8(mode_for_pdf(p.getXfermode()))) {}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000111
vandebo@chromium.org48543272011-02-08 19:28:07 +0000112// static
halcanaryfb62b3d2015-01-21 09:59:14 -0800113SkPDFGraphicState* SkPDFGraphicState::GetGraphicStateForPaint(
halcanary792c80f2015-02-20 07:21:05 -0800114 SkPDFCanon* canon, const SkPaint& paint) {
115 SkASSERT(canon);
halcanarybe27a112015-04-01 13:31:19 -0700116 SkPDFGraphicState key(paint);
117 if (const SkPDFGraphicState* canonGS = canon->findGraphicState(key)) {
118 // The returned SkPDFGraphicState must be made non-const,
119 // since the emitObject() interface is non-const. But We
120 // promise that there is no way to mutate this object from
121 // here on out.
122 return SkRef(const_cast<SkPDFGraphicState*>(canonGS));
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000123 }
halcanarybe27a112015-04-01 13:31:19 -0700124 SkPDFGraphicState* pdfGraphicState = new SkPDFGraphicState(paint);
halcanary792c80f2015-02-20 07:21:05 -0800125 canon->addGraphicState(pdfGraphicState);
halcanaryfb62b3d2015-01-21 09:59:14 -0800126 return pdfGraphicState;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000127}
128
halcanarye6ea2442015-01-21 13:13:22 -0800129namespace {
130SkPDFObject* create_invert_function() {
131 // Acrobat crashes if we use a type 0 function, kpdf crashes if we use
132 // a type 2 function, so we use a type 4 function.
133 SkAutoTUnref<SkPDFArray> domainAndRange(new SkPDFArray);
134 domainAndRange->reserve(2);
135 domainAndRange->appendInt(0);
136 domainAndRange->appendInt(1);
137
138 static const char psInvert[] = "{1 exch sub}";
139 // Do not copy the trailing '\0' into the SkData.
140 SkAutoTUnref<SkData> psInvertStream(
141 SkData::NewWithoutCopy(psInvert, strlen(psInvert)));
142
143 SkPDFStream* invertFunction = SkNEW_ARGS(
144 SkPDFStream, (psInvertStream.get()));
145 invertFunction->insertInt("FunctionType", 4);
halcanarya25b3372015-04-27 14:00:09 -0700146 invertFunction->insertObject("Domain", SkRef(domainAndRange.get()));
147 invertFunction->insertObject("Range", domainAndRange.detach());
halcanarye6ea2442015-01-21 13:13:22 -0800148 return invertFunction;
149}
150
151template <typename T> void unref(T* ptr) { ptr->unref(); }
152} // namespace
153
halcanarybe27a112015-04-01 13:31:19 -0700154SK_DECLARE_STATIC_LAZY_PTR(SkPDFObject,
155 invertFunction,
156 create_invert_function,
157 unref<SkPDFObject>);
halcanarye6ea2442015-01-21 13:13:22 -0800158
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000159// static
halcanarybe27a112015-04-01 13:31:19 -0700160SkPDFDict* SkPDFGraphicState::GetSMaskGraphicState(SkPDFFormXObject* sMask,
161 bool invert,
162 SkPDFSMaskMode sMaskMode) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000163 // The practical chances of using the same mask more than once are unlikely
164 // enough that it's not worth canonicalizing.
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000165 SkAutoTUnref<SkPDFDict> sMaskDict(new SkPDFDict("Mask"));
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000166 if (sMaskMode == kAlpha_SMaskMode) {
167 sMaskDict->insertName("S", "Alpha");
168 } else if (sMaskMode == kLuminosity_SMaskMode) {
169 sMaskDict->insertName("S", "Luminosity");
170 }
halcanarya25b3372015-04-27 14:00:09 -0700171 sMaskDict->insertObjRef("G", SkRef(sMask));
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000172 if (invert) {
halcanarya25b3372015-04-27 14:00:09 -0700173 sMaskDict->insertObjRef("TR", SkRef(invertFunction.get()));
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000174 }
175
halcanarybe27a112015-04-01 13:31:19 -0700176 SkPDFDict* result = new SkPDFDict("ExtGState");
halcanarya25b3372015-04-27 14:00:09 -0700177 result->insertObject("SMask", sMaskDict.detach());
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000178 return result;
179}
180
halcanarybe27a112015-04-01 13:31:19 -0700181namespace {
182SkPDFDict* create_no_smask_graphic_state() {
183 SkPDFDict* noSMaskGS = new SkPDFDict("ExtGState");
halcanarye6ea2442015-01-21 13:13:22 -0800184 noSMaskGS->insertName("SMask", "None");
185 return noSMaskGS;
186}
halcanarybe27a112015-04-01 13:31:19 -0700187} // namespace
188SK_DECLARE_STATIC_LAZY_PTR(SkPDFDict,
189 noSMaskGraphicState,
190 create_no_smask_graphic_state,
191 unref<SkPDFDict>);
halcanarye6ea2442015-01-21 13:13:22 -0800192
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000193// static
halcanarybe27a112015-04-01 13:31:19 -0700194SkPDFDict* SkPDFGraphicState::GetNoSMaskGraphicState() {
halcanarye6ea2442015-01-21 13:13:22 -0800195 return SkRef(noSMaskGraphicState.get());
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000196}
197
halcanarya060eba2015-08-19 12:26:46 -0700198void SkPDFGraphicState::emitObject(
199 SkWStream* stream,
200 const SkPDFObjNumMap& objNumMap,
201 const SkPDFSubstituteMap& substitutes) const {
halcanarybe27a112015-04-01 13:31:19 -0700202 SkAutoTUnref<SkPDFDict> dict(SkNEW_ARGS(SkPDFDict, ("ExtGState")));
203 dict->insertName("Type", "ExtGState");
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000204
reed80ea19c2015-05-12 10:37:34 -0700205 SkScalar alpha = SkIntToScalar(fAlpha) / 0xFF;
halcanarya25b3372015-04-27 14:00:09 -0700206 dict->insertScalar("CA", alpha);
207 dict->insertScalar("ca", alpha);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000208
halcanarybe27a112015-04-01 13:31:19 -0700209 SkPaint::Cap strokeCap = (SkPaint::Cap)fStrokeCap;
210 SkPaint::Join strokeJoin = (SkPaint::Join)fStrokeJoin;
211 SkXfermode::Mode xferMode = (SkXfermode::Mode)fMode;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000212
bungeman99fe8222015-08-20 07:57:51 -0700213 static_assert(SkPaint::kButt_Cap == 0, "paint_cap_mismatch");
214 static_assert(SkPaint::kRound_Cap == 1, "paint_cap_mismatch");
215 static_assert(SkPaint::kSquare_Cap == 2, "paint_cap_mismatch");
216 static_assert(SkPaint::kCapCount == 3, "paint_cap_mismatch");
halcanarybe27a112015-04-01 13:31:19 -0700217 SkASSERT(strokeCap >= 0 && strokeCap <= 2);
218 dict->insertInt("LC", strokeCap);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000219
bungeman99fe8222015-08-20 07:57:51 -0700220 static_assert(SkPaint::kMiter_Join == 0, "paint_join_mismatch");
221 static_assert(SkPaint::kRound_Join == 1, "paint_join_mismatch");
222 static_assert(SkPaint::kBevel_Join == 2, "paint_join_mismatch");
223 static_assert(SkPaint::kJoinCount == 3, "paint_join_mismatch");
halcanarybe27a112015-04-01 13:31:19 -0700224 SkASSERT(strokeJoin >= 0 && strokeJoin <= 2);
225 dict->insertInt("LJ", strokeJoin);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000226
halcanarybe27a112015-04-01 13:31:19 -0700227 dict->insertScalar("LW", fStrokeWidth);
228 dict->insertScalar("ML", fStrokeMiter);
halcanarya25b3372015-04-27 14:00:09 -0700229 dict->insertBool("SA", true); // SA = Auto stroke adjustment.
halcanarybe27a112015-04-01 13:31:19 -0700230 dict->insertName("BM", as_blend_mode(xferMode));
231 dict->emitObject(stream, objNumMap, substitutes);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000232}