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 "SkLazyPtr.h" | 9 #include "SkOncePtr.h" |
10 #include "SkPDFCanon.h" | 10 #include "SkPDFCanon.h" |
11 #include "SkPDFFormXObject.h" | 11 #include "SkPDFFormXObject.h" |
12 #include "SkPDFGraphicState.h" | 12 #include "SkPDFGraphicState.h" |
13 #include "SkPDFUtils.h" | 13 #include "SkPDFUtils.h" |
14 #include "SkTypes.h" | 14 #include "SkTypes.h" |
15 | 15 |
16 static const char* as_blend_mode(SkXfermode::Mode mode) { | 16 static const char* as_blend_mode(SkXfermode::Mode mode) { |
17 switch (mode) { | 17 switch (mode) { |
18 case SkXfermode::kSrcOver_Mode: | 18 case SkXfermode::kSrcOver_Mode: |
19 return "Normal"; | 19 return "Normal"; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 // since the emitObject() interface is non-const. But We | 119 // since the emitObject() interface is non-const. But We |
120 // promise that there is no way to mutate this object from | 120 // promise that there is no way to mutate this object from |
121 // here on out. | 121 // here on out. |
122 return SkRef(const_cast<SkPDFGraphicState*>(canonGS)); | 122 return SkRef(const_cast<SkPDFGraphicState*>(canonGS)); |
123 } | 123 } |
124 SkPDFGraphicState* pdfGraphicState = new SkPDFGraphicState(paint); | 124 SkPDFGraphicState* pdfGraphicState = new SkPDFGraphicState(paint); |
125 canon->addGraphicState(pdfGraphicState); | 125 canon->addGraphicState(pdfGraphicState); |
126 return pdfGraphicState; | 126 return pdfGraphicState; |
127 } | 127 } |
128 | 128 |
129 namespace { | 129 static SkPDFObject* create_invert_function() { |
130 SkPDFObject* create_invert_function() { | |
131 // Acrobat crashes if we use a type 0 function, kpdf crashes if we use | 130 // 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. | 131 // a type 2 function, so we use a type 4 function. |
133 SkAutoTUnref<SkPDFArray> domainAndRange(new SkPDFArray); | 132 SkAutoTUnref<SkPDFArray> domainAndRange(new SkPDFArray); |
134 domainAndRange->reserve(2); | 133 domainAndRange->reserve(2); |
135 domainAndRange->appendInt(0); | 134 domainAndRange->appendInt(0); |
136 domainAndRange->appendInt(1); | 135 domainAndRange->appendInt(1); |
137 | 136 |
138 static const char psInvert[] = "{1 exch sub}"; | 137 static const char psInvert[] = "{1 exch sub}"; |
139 // Do not copy the trailing '\0' into the SkData. | 138 // Do not copy the trailing '\0' into the SkData. |
140 SkAutoTUnref<SkData> psInvertStream( | 139 SkAutoTUnref<SkData> psInvertStream( |
141 SkData::NewWithoutCopy(psInvert, strlen(psInvert))); | 140 SkData::NewWithoutCopy(psInvert, strlen(psInvert))); |
142 | 141 |
143 SkPDFStream* invertFunction = new SkPDFStream(psInvertStream.get()); | 142 SkPDFStream* invertFunction = new SkPDFStream(psInvertStream.get()); |
144 invertFunction->insertInt("FunctionType", 4); | 143 invertFunction->insertInt("FunctionType", 4); |
145 invertFunction->insertObject("Domain", SkRef(domainAndRange.get())); | 144 invertFunction->insertObject("Domain", SkRef(domainAndRange.get())); |
146 invertFunction->insertObject("Range", domainAndRange.detach()); | 145 invertFunction->insertObject("Range", domainAndRange.detach()); |
147 return invertFunction; | 146 return invertFunction; |
148 } | 147 } |
149 | 148 |
150 template <typename T> void unref(T* ptr) { ptr->unref(); } | 149 SK_DECLARE_STATIC_ONCE_PTR(SkPDFObject, invertFunction); |
151 } // namespace | |
152 | |
153 SK_DECLARE_STATIC_LAZY_PTR(SkPDFObject, | |
154 invertFunction, | |
155 create_invert_function, | |
156 unref<SkPDFObject>); | |
157 | 150 |
158 // static | 151 // static |
159 SkPDFDict* SkPDFGraphicState::GetSMaskGraphicState(SkPDFFormXObject* sMask, | 152 SkPDFDict* SkPDFGraphicState::GetSMaskGraphicState(SkPDFFormXObject* sMask, |
160 bool invert, | 153 bool invert, |
161 SkPDFSMaskMode sMaskMode) { | 154 SkPDFSMaskMode sMaskMode) { |
162 // The practical chances of using the same mask more than once are unlikely | 155 // The practical chances of using the same mask more than once are unlikely |
163 // enough that it's not worth canonicalizing. | 156 // enough that it's not worth canonicalizing. |
164 SkAutoTUnref<SkPDFDict> sMaskDict(new SkPDFDict("Mask")); | 157 SkAutoTUnref<SkPDFDict> sMaskDict(new SkPDFDict("Mask")); |
165 if (sMaskMode == kAlpha_SMaskMode) { | 158 if (sMaskMode == kAlpha_SMaskMode) { |
166 sMaskDict->insertName("S", "Alpha"); | 159 sMaskDict->insertName("S", "Alpha"); |
167 } else if (sMaskMode == kLuminosity_SMaskMode) { | 160 } else if (sMaskMode == kLuminosity_SMaskMode) { |
168 sMaskDict->insertName("S", "Luminosity"); | 161 sMaskDict->insertName("S", "Luminosity"); |
169 } | 162 } |
170 sMaskDict->insertObjRef("G", SkRef(sMask)); | 163 sMaskDict->insertObjRef("G", SkRef(sMask)); |
171 if (invert) { | 164 if (invert) { |
172 sMaskDict->insertObjRef("TR", SkRef(invertFunction.get())); | 165 sMaskDict->insertObjRef("TR", SkRef(invertFunction.get(create_invert_fun
ction))); |
173 } | 166 } |
174 | 167 |
175 SkPDFDict* result = new SkPDFDict("ExtGState"); | 168 SkPDFDict* result = new SkPDFDict("ExtGState"); |
176 result->insertObject("SMask", sMaskDict.detach()); | 169 result->insertObject("SMask", sMaskDict.detach()); |
177 return result; | 170 return result; |
178 } | 171 } |
179 | 172 |
180 namespace { | 173 static SkPDFDict* create_no_smask_graphic_state() { |
181 SkPDFDict* create_no_smask_graphic_state() { | |
182 SkPDFDict* noSMaskGS = new SkPDFDict("ExtGState"); | 174 SkPDFDict* noSMaskGS = new SkPDFDict("ExtGState"); |
183 noSMaskGS->insertName("SMask", "None"); | 175 noSMaskGS->insertName("SMask", "None"); |
184 return noSMaskGS; | 176 return noSMaskGS; |
185 } | 177 } |
186 } // namespace | 178 SK_DECLARE_STATIC_ONCE_PTR(SkPDFDict, noSMaskGraphicState); |
187 SK_DECLARE_STATIC_LAZY_PTR(SkPDFDict, | |
188 noSMaskGraphicState, | |
189 create_no_smask_graphic_state, | |
190 unref<SkPDFDict>); | |
191 | 179 |
192 // static | 180 // static |
193 SkPDFDict* SkPDFGraphicState::GetNoSMaskGraphicState() { | 181 SkPDFDict* SkPDFGraphicState::GetNoSMaskGraphicState() { |
194 return SkRef(noSMaskGraphicState.get()); | 182 return SkRef(noSMaskGraphicState.get(create_no_smask_graphic_state)); |
195 } | 183 } |
196 | 184 |
197 void SkPDFGraphicState::emitObject( | 185 void SkPDFGraphicState::emitObject( |
198 SkWStream* stream, | 186 SkWStream* stream, |
199 const SkPDFObjNumMap& objNumMap, | 187 const SkPDFObjNumMap& objNumMap, |
200 const SkPDFSubstituteMap& substitutes) const { | 188 const SkPDFSubstituteMap& substitutes) const { |
201 SkAutoTUnref<SkPDFDict> dict(new SkPDFDict("ExtGState")); | 189 SkAutoTUnref<SkPDFDict> dict(new SkPDFDict("ExtGState")); |
202 dict->insertName("Type", "ExtGState"); | 190 dict->insertName("Type", "ExtGState"); |
203 | 191 |
204 SkScalar alpha = SkIntToScalar(fAlpha) / 0xFF; | 192 SkScalar alpha = SkIntToScalar(fAlpha) / 0xFF; |
(...skipping 17 matching lines...) Expand all Loading... |
222 static_assert(SkPaint::kJoinCount == 3, "paint_join_mismatch"); | 210 static_assert(SkPaint::kJoinCount == 3, "paint_join_mismatch"); |
223 SkASSERT(strokeJoin >= 0 && strokeJoin <= 2); | 211 SkASSERT(strokeJoin >= 0 && strokeJoin <= 2); |
224 dict->insertInt("LJ", strokeJoin); | 212 dict->insertInt("LJ", strokeJoin); |
225 | 213 |
226 dict->insertScalar("LW", fStrokeWidth); | 214 dict->insertScalar("LW", fStrokeWidth); |
227 dict->insertScalar("ML", fStrokeMiter); | 215 dict->insertScalar("ML", fStrokeMiter); |
228 dict->insertBool("SA", true); // SA = Auto stroke adjustment. | 216 dict->insertBool("SA", true); // SA = Auto stroke adjustment. |
229 dict->insertName("BM", as_blend_mode(xferMode)); | 217 dict->insertName("BM", as_blend_mode(xferMode)); |
230 dict->emitObject(stream, objNumMap, substitutes); | 218 dict->emitObject(stream, objNumMap, substitutes); |
231 } | 219 } |
OLD | NEW |