OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 The Android Open Source Project | 2 * Copyright 2010 The Android Open Source Project |
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 | 8 |
9 #include "SkPDFFormXObject.h" | 9 #include "SkPDFFormXObject.h" |
10 | 10 |
11 #include "SkMatrix.h" | 11 sk_sp<SkPDFObject> SkPDFMakeFormXObject(std::unique_ptr<SkStreamAsset> content, |
12 #include "SkPDFDevice.h" | 12 sk_sp<SkPDFArray> mediaBox, |
13 #include "SkPDFUtils.h" | 13 sk_sp<SkPDFDict> resourceDict, |
14 #include "SkStream.h" | 14 const char* colorSpace) { |
15 #include "SkTypes.h" | 15 auto form = sk_make_sp<SkPDFStream>(std::move(content)); |
| 16 form->insertName("Type", "XObject"); |
| 17 form->insertName("Subtype", "Form"); |
| 18 form->insertObject("Resources", std::move(resourceDict)); |
| 19 form->insertObject("BBox", std::move(mediaBox)); |
16 | 20 |
17 SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) { | 21 // Right now FormXObject is only used for saveLayer, which implies |
18 // We don't want to keep around device because we'd have two copies | |
19 // of content, so reference or copy everything we need (content and | |
20 // resources). | |
21 auto resourceDict = device->makeResourceDict(); | |
22 | |
23 this->setData(device->content()); | |
24 | |
25 sk_sp<SkPDFArray> bboxArray(device->copyMediaBox()); | |
26 this->init(nullptr, resourceDict.get(), bboxArray.get()); | |
27 | |
28 // We invert the initial transform and apply that to the xobject so that | |
29 // it doesn't get applied twice. We can't just undo it because it's | |
30 // embedded in things like shaders and images. | |
31 if (!device->initialTransform().isIdentity()) { | |
32 SkMatrix inverse; | |
33 if (!device->initialTransform().invert(&inverse)) { | |
34 // The initial transform should be invertible. | |
35 SkASSERT(false); | |
36 inverse.reset(); | |
37 } | |
38 this->insertObject("Matrix", SkPDFUtils::MatrixToArray(inverse)); | |
39 } | |
40 } | |
41 | |
42 /** | |
43 * Creates a FormXObject from a content stream and associated resources. | |
44 */ | |
45 SkPDFFormXObject::SkPDFFormXObject(std::unique_ptr<SkStreamAsset> content, | |
46 SkRect bbox, | |
47 SkPDFDict* resourceDict) { | |
48 this->setData(std::move(content)); | |
49 sk_sp<SkPDFArray> bboxArray(SkPDFUtils::RectToArray(bbox)); | |
50 this->init("DeviceRGB", resourceDict, bboxArray.get()); | |
51 } | |
52 | |
53 /** | |
54 * Common initialization code. | |
55 * Note that bbox is unreferenced here, so calling code does not need worry. | |
56 */ | |
57 void SkPDFFormXObject::init(const char* colorSpace, | |
58 SkPDFDict* resourceDict, SkPDFArray* bbox) { | |
59 this->insertName("Type", "XObject"); | |
60 this->insertName("Subtype", "Form"); | |
61 this->insertObject("Resources", sk_ref_sp(resourceDict)); | |
62 this->insertObject("BBox", sk_ref_sp(bbox)); | |
63 | |
64 // Right now SkPDFFormXObject is only used for saveLayer, which implies | |
65 // isolated blending. Do this conditionally if that changes. | 22 // isolated blending. Do this conditionally if that changes. |
| 23 // TODO(halcanary): Is this comment obsolete, since we use it for |
| 24 // alpha masks? |
66 auto group = sk_make_sp<SkPDFDict>("Group"); | 25 auto group = sk_make_sp<SkPDFDict>("Group"); |
67 group->insertName("S", "Transparency"); | 26 group->insertName("S", "Transparency"); |
68 | |
69 if (colorSpace != nullptr) { | 27 if (colorSpace != nullptr) { |
70 group->insertName("CS", colorSpace); | 28 group->insertName("CS", colorSpace); |
71 } | 29 } |
72 group->insertBool("I", true); // Isolated. | 30 group->insertBool("I", true); // Isolated. |
73 this->insertObject("Group", std::move(group)); | 31 form->insertObject("Group", std::move(group)); |
| 32 return form; |
74 } | 33 } |
75 | |
76 SkPDFFormXObject::~SkPDFFormXObject() {} | |
OLD | NEW |