blob: 70f166c1a9c60ee071040c64f0b401c3795570e2 [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"
22#include "SkStream.h"
23#include "SkTypes.h"
24
vandebo@chromium.org79ac4fd2011-02-08 19:22:40 +000025SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) {
26 // We don't want to keep around device because we'd have two copies
27 // of content, so reference or copy everything we need (content and
28 // resources).
29 device->getResources(&fResources);
30
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +000031 SkRefPtr<SkStream> content = device->content();
32 content->unref(); // SkRefPtr and content() both took a reference.
33 fStream = new SkPDFStream(content.get());
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000034 fStream->unref(); // SkRefPtr and new both took a reference.
35
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
41 // Right now SkPDFFormXObject is only used for saveLayer, which implies
42 // isolated blending. Do this conditionally if that changes.
43 SkRefPtr<SkPDFDict> group = new SkPDFDict("Group");
44 group->unref(); // SkRefPtr and new both took a reference.
45 group->insert("S", new SkPDFName("Transparency"))->unref();
46 group->insert("I", new SkPDFBool(true))->unref(); // Isolated.
47 insert("Group", group.get());
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000048}
49
vandebo@chromium.org79ac4fd2011-02-08 19:22:40 +000050SkPDFFormXObject::~SkPDFFormXObject() {
51 fResources.unrefAll();
52}
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000053
54void SkPDFFormXObject::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
55 bool indirect) {
56 if (indirect)
57 return emitIndirectObject(stream, catalog);
58
59 fStream->emitObject(stream, catalog, indirect);
60}
61
62size_t SkPDFFormXObject::getOutputSize(SkPDFCatalog* catalog, bool indirect) {
63 if (indirect)
64 return getIndirectOutputSize(catalog);
65
66 return fStream->getOutputSize(catalog, indirect);
67}
68
69void SkPDFFormXObject::getResources(SkTDArray<SkPDFObject*>* resourceList) {
vandebo@chromium.org79ac4fd2011-02-08 19:22:40 +000070 resourceList->setReserve(resourceList->count() + fResources.count());
71 for (int i = 0; i < fResources.count(); i++) {
72 resourceList->push(fResources[i]);
73 fResources[i]->ref();
74 }
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000075}
76
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +000077SkPDFObject* SkPDFFormXObject::insert(SkPDFName* key, SkPDFObject* value) {
78 return fStream->insert(key, value);
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000079}
80
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +000081SkPDFObject* SkPDFFormXObject::insert(const char key[], SkPDFObject* value) {
82 return fStream->insert(key, value);
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000083}