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