blob: a8a3290e2220b46b264cd89309004b138324046b [file] [log] [blame]
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +00001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SkPDFFormXObject.h"
18
19#include "SkMatrix.h"
20#include "SkPDFCatalog.h"
21#include "SkPDFDevice.h"
vandebo@chromium.org61d26782011-05-24 23:02:07 +000022#include "SkPDFUtils.h"
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000023#include "SkStream.h"
24#include "SkTypes.h"
25
vandebo@chromium.org79ac4fd2011-02-08 19:22:40 +000026SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) {
27 // We don't want to keep around device because we'd have two copies
28 // of content, so reference or copy everything we need (content and
29 // resources).
30 device->getResources(&fResources);
31
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +000032 SkRefPtr<SkStream> content = device->content();
33 content->unref(); // SkRefPtr and content() both took a reference.
vandebo@chromium.org421d6442011-07-20 17:39:01 +000034 setData(content.get());
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000035
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +000036 insert("Type", new SkPDFName("XObject"))->unref();
37 insert("Subtype", new SkPDFName("Form"))->unref();
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000038 insert("BBox", device->getMediaBox().get());
39 insert("Resources", device->getResourceDict().get());
vandebo@chromium.org48543272011-02-08 19:28:07 +000040
vandebo@chromium.org61d26782011-05-24 23:02:07 +000041 // We invert the initial transform and apply that to the xobject so that
42 // it doesn't get applied twice. We can't just undo it because it's
43 // embedded in things like shaders and images.
44 if (!device->initialTransform().isIdentity()) {
45 SkMatrix inverse;
46 inverse.reset();
47 device->initialTransform().invert(&inverse);
48 insert("Matrix", SkPDFUtils::MatrixToArray(inverse))->unref();
49 }
50
vandebo@chromium.org48543272011-02-08 19:28:07 +000051 // Right now SkPDFFormXObject is only used for saveLayer, which implies
52 // isolated blending. Do this conditionally if that changes.
53 SkRefPtr<SkPDFDict> group = new SkPDFDict("Group");
54 group->unref(); // SkRefPtr and new both took a reference.
55 group->insert("S", new SkPDFName("Transparency"))->unref();
56 group->insert("I", new SkPDFBool(true))->unref(); // Isolated.
57 insert("Group", group.get());
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000058}
59
vandebo@chromium.org79ac4fd2011-02-08 19:22:40 +000060SkPDFFormXObject::~SkPDFFormXObject() {
61 fResources.unrefAll();
62}
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000063
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000064void SkPDFFormXObject::getResources(SkTDArray<SkPDFObject*>* resourceList) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +000065 GetResourcesHelper(&fResources, resourceList);
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000066}