blob: 11afa323214159f40a52727a710a0c57704f9a78 [file] [log] [blame]
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +00001/*
vandebo@chromium.orgd90c1412011-02-24 21:50:04 +00002 * Copyright (C) 2010 Google Inc.
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +00003 *
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
reed@google.com8a85d0c2011-06-24 19:12:12 +000017#include "SkData.h"
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000018#include "SkFlate.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000019#include "SkPDFCatalog.h"
20#include "SkPDFStream.h"
21#include "SkStream.h"
22
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000023SkPDFStream::SkPDFStream(SkStream* stream) {
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +000024 if (SkFlate::HaveFlate())
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000025 SkAssertResult(SkFlate::Deflate(stream, &fCompressedData));
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +000026
27 if (SkFlate::HaveFlate() &&
28 fCompressedData.getOffset() < stream->getLength()) {
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000029 fLength = fCompressedData.getOffset();
vandebo@chromium.orgd90c1412011-02-24 21:50:04 +000030 insert("Filter", new SkPDFName("FlateDecode"))->unref();
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000031 } else {
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +000032 fCompressedData.reset();
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000033 fPlainData = stream;
34 fLength = fPlainData->getLength();
35 }
vandebo@chromium.orgd90c1412011-02-24 21:50:04 +000036 insert("Length", new SkPDFInt(fLength))->unref();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000037}
38
39SkPDFStream::~SkPDFStream() {
40}
41
42void SkPDFStream::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
43 bool indirect) {
44 if (indirect)
45 return emitIndirectObject(stream, catalog);
46
vandebo@chromium.orgd90c1412011-02-24 21:50:04 +000047 this->INHERITED::emitObject(stream, catalog, false);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000048 stream->writeText(" stream\n");
reed@google.com8a85d0c2011-06-24 19:12:12 +000049 if (fPlainData.get()) {
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000050 stream->write(fPlainData->getMemoryBase(), fLength);
reed@google.com8a85d0c2011-06-24 19:12:12 +000051 } else {
52 SkAutoDataUnref data(fCompressedData.copyToData());
53 stream->write(data.data(), fLength);
54 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000055 stream->writeText("\nendstream");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000056}
57
58size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) {
59 if (indirect)
60 return getIndirectOutputSize(catalog);
61
vandebo@chromium.orgd90c1412011-02-24 21:50:04 +000062 return this->INHERITED::getOutputSize(catalog, false) +
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000063 strlen(" stream\n\nendstream") + fLength;
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000064}