1 2 /* 3 * Copyright 2010 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #include "SkPDFFormXObject.h" 11 12 #include "SkMatrix.h" 13 #include "SkPDFCatalog.h" 14 #include "SkPDFDevice.h" 15 #include "SkPDFUtils.h" 16 #include "SkStream.h" 17 #include "SkTypes.h" 18 19 SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) { 20 // We don't want to keep around device because we'd have two copies 21 // of content, so reference or copy everything we need (content and 22 // resources). 23 device->getResources(&fResources, false); 24 25 // Fail fast if in the tree of resources a child references a parent. 26 // If there is an issue, getResources will end up consuming all memory. 27 // TODO: A better approach might be for all SkPDFObject to keep track 28 // of possible cycles. 29 #ifdef SK_DEBUG 30 SkTDArray<SkPDFObject*> dummy_resourceList; 31 getResources(&dummy_resourceList); 32 #endif 33 34 SkAutoTUnref<SkStream> content(device->content()); 35 setData(content.get()); 36 37 insertName("Type", "XObject"); 38 insertName("Subtype", "Form"); 39 SkSafeUnref(this->insert("BBox", device->copyMediaBox())); 40 insert("Resources", device->getResourceDict()); 41 42 // We invert the initial transform and apply that to the xobject so that 43 // it doesn't get applied twice. We can't just undo it because it's 44 // embedded in things like shaders and images. 45 if (!device->initialTransform().isIdentity()) { 46 SkMatrix inverse; 47 if (!device->initialTransform().invert(&inverse)) { 48 // The initial transform should be invertible. 49 SkASSERT(false); 50 inverse.reset(); 51 } 52 insert("Matrix", SkPDFUtils::MatrixToArray(inverse))->unref(); 53 } 54 55 // Right now SkPDFFormXObject is only used for saveLayer, which implies 56 // isolated blending. Do this conditionally if that changes. 57 SkAutoTUnref<SkPDFDict> group(new SkPDFDict("Group")); 58 group->insertName("S", "Transparency"); 59 group->insert("I", new SkPDFBool(true))->unref(); // Isolated. 60 insert("Group", group.get()); 61 } 62 63 SkPDFFormXObject::~SkPDFFormXObject() { 64 fResources.unrefAll(); 65 } 66 67 void SkPDFFormXObject::getResources(SkTDArray<SkPDFObject*>* resourceList) { 68 GetResourcesHelper(&fResources, resourceList); 69 } 70