OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkData.h" | 8 #include "SkData.h" |
9 #include "SkOncePtr.h" | |
10 #include "SkPDFCanon.h" | 9 #include "SkPDFCanon.h" |
11 #include "SkPDFFormXObject.h" | 10 #include "SkPDFFormXObject.h" |
12 #include "SkPDFGraphicState.h" | 11 #include "SkPDFGraphicState.h" |
13 #include "SkPDFUtils.h" | 12 #include "SkPDFUtils.h" |
14 #include "SkTypes.h" | |
15 | 13 |
16 static const char* as_blend_mode(SkXfermode::Mode mode) { | 14 static const char* as_blend_mode(SkXfermode::Mode mode) { |
17 switch (mode) { | 15 switch (mode) { |
18 case SkXfermode::kSrcOver_Mode: | 16 case SkXfermode::kSrcOver_Mode: |
19 return "Normal"; | 17 return "Normal"; |
20 case SkXfermode::kMultiply_Mode: | 18 case SkXfermode::kMultiply_Mode: |
21 return "Multiply"; | 19 return "Multiply"; |
22 case SkXfermode::kScreen_Mode: | 20 case SkXfermode::kScreen_Mode: |
23 return "Screen"; | 21 return "Screen"; |
24 case SkXfermode::kOverlay_Mode: | 22 case SkXfermode::kOverlay_Mode: |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 // since the emitObject() interface is non-const. But We | 117 // since the emitObject() interface is non-const. But We |
120 // promise that there is no way to mutate this object from | 118 // promise that there is no way to mutate this object from |
121 // here on out. | 119 // here on out. |
122 return SkRef(const_cast<SkPDFGraphicState*>(canonGS)); | 120 return SkRef(const_cast<SkPDFGraphicState*>(canonGS)); |
123 } | 121 } |
124 SkPDFGraphicState* pdfGraphicState = new SkPDFGraphicState(paint); | 122 SkPDFGraphicState* pdfGraphicState = new SkPDFGraphicState(paint); |
125 canon->addGraphicState(pdfGraphicState); | 123 canon->addGraphicState(pdfGraphicState); |
126 return pdfGraphicState; | 124 return pdfGraphicState; |
127 } | 125 } |
128 | 126 |
129 static SkPDFStream* create_invert_function() { | 127 sk_sp<SkPDFStream> SkPDFGraphicState::MakeInvertFunction() { |
130 // Acrobat crashes if we use a type 0 function, kpdf crashes if we use | 128 // Acrobat crashes if we use a type 0 function, kpdf crashes if we use |
131 // a type 2 function, so we use a type 4 function. | 129 // a type 2 function, so we use a type 4 function. |
132 auto domainAndRange = sk_make_sp<SkPDFArray>(); | 130 auto domainAndRange = sk_make_sp<SkPDFArray>(); |
133 domainAndRange->reserve(2); | 131 domainAndRange->reserve(2); |
134 domainAndRange->appendInt(0); | 132 domainAndRange->appendInt(0); |
135 domainAndRange->appendInt(1); | 133 domainAndRange->appendInt(1); |
136 | 134 |
137 static const char psInvert[] = "{1 exch sub}"; | 135 static const char psInvert[] = "{1 exch sub}"; |
138 // Do not copy the trailing '\0' into the SkData. | 136 // Do not copy the trailing '\0' into the SkData. |
139 sk_sp<SkData> psInvertStream( | 137 sk_sp<SkData> psInvertStream( |
140 SkData::NewWithoutCopy(psInvert, strlen(psInvert))); | 138 SkData::NewWithoutCopy(psInvert, strlen(psInvert))); |
141 | 139 |
142 auto invertFunction = sk_make_sp<SkPDFStream>(psInvertStream.get()); | 140 auto invertFunction = sk_make_sp<SkPDFStream>(psInvertStream.get()); |
143 invertFunction->insertInt("FunctionType", 4); | 141 invertFunction->insertInt("FunctionType", 4); |
144 invertFunction->insertObject("Domain", domainAndRange); | 142 invertFunction->insertObject("Domain", domainAndRange); |
145 invertFunction->insertObject("Range", std::move(domainAndRange)); | 143 invertFunction->insertObject("Range", std::move(domainAndRange)); |
146 return invertFunction.release(); | 144 return invertFunction; |
147 } | 145 } |
148 | 146 |
149 SK_DECLARE_STATIC_ONCE_PTR(SkPDFStream, invertFunction); | 147 sk_sp<SkPDFDict> SkPDFGraphicState::GetSMaskGraphicState( |
150 | 148 SkPDFFormXObject* sMask, |
151 static sk_sp<SkPDFStream> make_invert_function() { | 149 bool invert, |
152 return sk_sp<SkPDFStream>( | 150 SkPDFSMaskMode sMaskMode, |
153 SkRef(invertFunction.get(create_invert_function))); | 151 SkPDFCanon* canon) { |
154 } | |
155 | |
156 // static | |
157 SkPDFDict* SkPDFGraphicState::GetSMaskGraphicState(SkPDFFormXObject* sMask, | |
158 bool invert, | |
159 SkPDFSMaskMode sMaskMode) { | |
160 // The practical chances of using the same mask more than once are unlikely | 152 // The practical chances of using the same mask more than once are unlikely |
161 // enough that it's not worth canonicalizing. | 153 // enough that it's not worth canonicalizing. |
162 auto sMaskDict = sk_make_sp<SkPDFDict>("Mask"); | 154 auto sMaskDict = sk_make_sp<SkPDFDict>("Mask"); |
163 if (sMaskMode == kAlpha_SMaskMode) { | 155 if (sMaskMode == kAlpha_SMaskMode) { |
164 sMaskDict->insertName("S", "Alpha"); | 156 sMaskDict->insertName("S", "Alpha"); |
165 } else if (sMaskMode == kLuminosity_SMaskMode) { | 157 } else if (sMaskMode == kLuminosity_SMaskMode) { |
166 sMaskDict->insertName("S", "Luminosity"); | 158 sMaskDict->insertName("S", "Luminosity"); |
167 } | 159 } |
168 sMaskDict->insertObjRef("G", sk_ref_sp(sMask)); | 160 sMaskDict->insertObjRef("G", sk_ref_sp(sMask)); |
169 if (invert) { | 161 if (invert) { |
170 sMaskDict->insertObjRef("TR", make_invert_function()); | 162 // Instead of calling SkPDFGraphicState::MakeInvertFunction, |
| 163 // let the canon deduplicate this object. |
| 164 sMaskDict->insertObjRef("TR", canon->makeInvertFunction()); |
171 } | 165 } |
172 | 166 |
173 auto result = sk_make_sp<SkPDFDict>("ExtGState"); | 167 auto result = sk_make_sp<SkPDFDict>("ExtGState"); |
174 result->insertObject("SMask", std::move(sMaskDict)); | 168 result->insertObject("SMask", std::move(sMaskDict)); |
175 return result.release(); | 169 return result; |
176 } | 170 } |
177 | 171 |
178 static SkPDFDict* create_no_smask_graphic_state() { | 172 sk_sp<SkPDFDict> SkPDFGraphicState::MakeNoSmaskGraphicState() { |
179 SkPDFDict* noSMaskGS = new SkPDFDict("ExtGState"); | 173 auto noSMaskGS = sk_make_sp<SkPDFDict>("ExtGState"); |
180 noSMaskGS->insertName("SMask", "None"); | 174 noSMaskGS->insertName("SMask", "None"); |
181 return noSMaskGS; | 175 return noSMaskGS; |
182 } | 176 } |
183 SK_DECLARE_STATIC_ONCE_PTR(SkPDFDict, noSMaskGraphicState); | |
184 | |
185 // static | |
186 SkPDFDict* SkPDFGraphicState::GetNoSMaskGraphicState() { | |
187 return SkRef(noSMaskGraphicState.get(create_no_smask_graphic_state)); | |
188 } | |
189 | 177 |
190 void SkPDFGraphicState::emitObject( | 178 void SkPDFGraphicState::emitObject( |
191 SkWStream* stream, | 179 SkWStream* stream, |
192 const SkPDFObjNumMap& objNumMap, | 180 const SkPDFObjNumMap& objNumMap, |
193 const SkPDFSubstituteMap& substitutes) const { | 181 const SkPDFSubstituteMap& substitutes) const { |
194 auto dict = sk_make_sp<SkPDFDict>("ExtGState"); | 182 auto dict = sk_make_sp<SkPDFDict>("ExtGState"); |
195 dict->insertName("Type", "ExtGState"); | 183 dict->insertName("Type", "ExtGState"); |
196 | 184 |
197 SkScalar alpha = SkIntToScalar(fAlpha) / 0xFF; | 185 SkScalar alpha = SkIntToScalar(fAlpha) / 0xFF; |
198 dict->insertScalar("CA", alpha); | 186 dict->insertScalar("CA", alpha); |
(...skipping 16 matching lines...) Expand all Loading... |
215 static_assert(SkPaint::kJoinCount == 3, "paint_join_mismatch"); | 203 static_assert(SkPaint::kJoinCount == 3, "paint_join_mismatch"); |
216 SkASSERT(strokeJoin >= 0 && strokeJoin <= 2); | 204 SkASSERT(strokeJoin >= 0 && strokeJoin <= 2); |
217 dict->insertInt("LJ", strokeJoin); | 205 dict->insertInt("LJ", strokeJoin); |
218 | 206 |
219 dict->insertScalar("LW", fStrokeWidth); | 207 dict->insertScalar("LW", fStrokeWidth); |
220 dict->insertScalar("ML", fStrokeMiter); | 208 dict->insertScalar("ML", fStrokeMiter); |
221 dict->insertBool("SA", true); // SA = Auto stroke adjustment. | 209 dict->insertBool("SA", true); // SA = Auto stroke adjustment. |
222 dict->insertName("BM", as_blend_mode(xferMode)); | 210 dict->insertName("BM", as_blend_mode(xferMode)); |
223 dict->emitObject(stream, objNumMap, substitutes); | 211 dict->emitObject(stream, objNumMap, substitutes); |
224 } | 212 } |
OLD | NEW |