vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
halcanary | 67ec1f8 | 2014-06-27 11:36:20 -0700 | [diff] [blame] | 8 | #include "SkData.h" |
halcanary | e6ea244 | 2015-01-21 13:13:22 -0800 | [diff] [blame] | 9 | #include "SkLazyPtr.h" |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 10 | #include "SkPDFCanon.h" |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 11 | #include "SkPDFFormXObject.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 12 | #include "SkPDFGraphicState.h" |
vandebo@chromium.org | f71b210 | 2011-04-04 19:46:31 +0000 | [diff] [blame] | 13 | #include "SkPDFUtils.h" |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 14 | #include "SkTypes.h" |
| 15 | |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 16 | static const char* as_blend_mode(SkXfermode::Mode mode) { |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 17 | switch (mode) { |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 18 | 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.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 50 | |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 51 | // These are handled in SkPDFDevice::setUpContentEntry. |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 52 | 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.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 60 | case SkXfermode::kSrcATop_Mode: |
| 61 | case SkXfermode::kDstATop_Mode: |
| 62 | case SkXfermode::kModulate_Mode: |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 63 | return "Normal"; |
| 64 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 65 | // TODO(vandebo): Figure out if we can support more of these modes. |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 66 | case SkXfermode::kXor_Mode: |
| 67 | case SkXfermode::kPlus_Mode: |
| 68 | return NULL; |
| 69 | } |
| 70 | return NULL; |
| 71 | } |
| 72 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 73 | // If a SkXfermode is unsupported in PDF, this function returns |
| 74 | // SrcOver, otherwise, it returns that Xfermode as a Mode. |
| 75 | static SkXfermode::Mode mode_for_pdf(const SkXfermode* xfermode) { |
| 76 | SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode; |
| 77 | if (xfermode) { |
| 78 | xfermode->asMode(&mode); |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 79 | } |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 80 | 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. |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 101 | } |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 102 | } |
| 103 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 104 | SkPDFGraphicState::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.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 111 | |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 112 | // static |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 113 | SkPDFGraphicState* SkPDFGraphicState::GetGraphicStateForPaint( |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 114 | SkPDFCanon* canon, const SkPaint& paint) { |
| 115 | SkASSERT(canon); |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 116 | 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.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 123 | } |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 124 | SkPDFGraphicState* pdfGraphicState = new SkPDFGraphicState(paint); |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 125 | canon->addGraphicState(pdfGraphicState); |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 126 | return pdfGraphicState; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 127 | } |
| 128 | |
halcanary | e6ea244 | 2015-01-21 13:13:22 -0800 | [diff] [blame] | 129 | namespace { |
| 130 | SkPDFObject* 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); |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 146 | invertFunction->insertObject("Domain", SkRef(domainAndRange.get())); |
| 147 | invertFunction->insertObject("Range", domainAndRange.detach()); |
halcanary | e6ea244 | 2015-01-21 13:13:22 -0800 | [diff] [blame] | 148 | return invertFunction; |
| 149 | } |
| 150 | |
| 151 | template <typename T> void unref(T* ptr) { ptr->unref(); } |
| 152 | } // namespace |
| 153 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 154 | SK_DECLARE_STATIC_LAZY_PTR(SkPDFObject, |
| 155 | invertFunction, |
| 156 | create_invert_function, |
| 157 | unref<SkPDFObject>); |
halcanary | e6ea244 | 2015-01-21 13:13:22 -0800 | [diff] [blame] | 158 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 159 | // static |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 160 | SkPDFDict* SkPDFGraphicState::GetSMaskGraphicState(SkPDFFormXObject* sMask, |
| 161 | bool invert, |
| 162 | SkPDFSMaskMode sMaskMode) { |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 163 | // The practical chances of using the same mask more than once are unlikely |
| 164 | // enough that it's not worth canonicalizing. |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 165 | SkAutoTUnref<SkPDFDict> sMaskDict(new SkPDFDict("Mask")); |
commit-bot@chromium.org | 93a2e21 | 2013-07-23 23:16:03 +0000 | [diff] [blame] | 166 | if (sMaskMode == kAlpha_SMaskMode) { |
| 167 | sMaskDict->insertName("S", "Alpha"); |
| 168 | } else if (sMaskMode == kLuminosity_SMaskMode) { |
| 169 | sMaskDict->insertName("S", "Luminosity"); |
| 170 | } |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 171 | sMaskDict->insertObjRef("G", SkRef(sMask)); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 172 | if (invert) { |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 173 | sMaskDict->insertObjRef("TR", SkRef(invertFunction.get())); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 174 | } |
| 175 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 176 | SkPDFDict* result = new SkPDFDict("ExtGState"); |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 177 | result->insertObject("SMask", sMaskDict.detach()); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 178 | return result; |
| 179 | } |
| 180 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 181 | namespace { |
| 182 | SkPDFDict* create_no_smask_graphic_state() { |
| 183 | SkPDFDict* noSMaskGS = new SkPDFDict("ExtGState"); |
halcanary | e6ea244 | 2015-01-21 13:13:22 -0800 | [diff] [blame] | 184 | noSMaskGS->insertName("SMask", "None"); |
| 185 | return noSMaskGS; |
| 186 | } |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 187 | } // namespace |
| 188 | SK_DECLARE_STATIC_LAZY_PTR(SkPDFDict, |
| 189 | noSMaskGraphicState, |
| 190 | create_no_smask_graphic_state, |
| 191 | unref<SkPDFDict>); |
halcanary | e6ea244 | 2015-01-21 13:13:22 -0800 | [diff] [blame] | 192 | |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 193 | // static |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 194 | SkPDFDict* SkPDFGraphicState::GetNoSMaskGraphicState() { |
halcanary | e6ea244 | 2015-01-21 13:13:22 -0800 | [diff] [blame] | 195 | return SkRef(noSMaskGraphicState.get()); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 196 | } |
| 197 | |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 198 | void SkPDFGraphicState::emitObject( |
| 199 | SkWStream* stream, |
| 200 | const SkPDFObjNumMap& objNumMap, |
| 201 | const SkPDFSubstituteMap& substitutes) const { |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 202 | SkAutoTUnref<SkPDFDict> dict(SkNEW_ARGS(SkPDFDict, ("ExtGState"))); |
| 203 | dict->insertName("Type", "ExtGState"); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 204 | |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 205 | SkScalar alpha = SkIntToScalar(fAlpha) / 0xFF; |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 206 | dict->insertScalar("CA", alpha); |
| 207 | dict->insertScalar("ca", alpha); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 208 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 209 | SkPaint::Cap strokeCap = (SkPaint::Cap)fStrokeCap; |
| 210 | SkPaint::Join strokeJoin = (SkPaint::Join)fStrokeJoin; |
| 211 | SkXfermode::Mode xferMode = (SkXfermode::Mode)fMode; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 212 | |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame^] | 213 | 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"); |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 217 | SkASSERT(strokeCap >= 0 && strokeCap <= 2); |
| 218 | dict->insertInt("LC", strokeCap); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 219 | |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame^] | 220 | 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"); |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 224 | SkASSERT(strokeJoin >= 0 && strokeJoin <= 2); |
| 225 | dict->insertInt("LJ", strokeJoin); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 226 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 227 | dict->insertScalar("LW", fStrokeWidth); |
| 228 | dict->insertScalar("ML", fStrokeMiter); |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 229 | dict->insertBool("SA", true); // SA = Auto stroke adjustment. |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 230 | dict->insertName("BM", as_blend_mode(xferMode)); |
| 231 | dict->emitObject(stream, objNumMap, substitutes); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 232 | } |