blob: 6947ae4df968f0036f47350b0abef7ba51696e00 [file] [log] [blame]
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +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
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000017#include "SkFlate.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000018#include "SkPDFCatalog.h"
19#include "SkPDFStream.h"
20#include "SkStream.h"
21
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000022SkPDFStream::SkPDFStream(SkStream* stream) {
23 if (SkFlate::HaveFlate()) {
24 SkAssertResult(SkFlate::Deflate(stream, &fCompressedData));
25 fLength = fCompressedData.getOffset();
26
27 SkRefPtr<SkPDFName> flateFilter = new SkPDFName("FlateDecode");
28 flateFilter->unref(); // SkRefPtr and new both took a reference.
29 fDict.insert("Filter", flateFilter.get());
30 } else {
31 fPlainData = stream;
32 fLength = fPlainData->getLength();
33 }
34
35 SkRefPtr<SkPDFInt> lenValue = new SkPDFInt(fLength);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000036 lenValue->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000037 fDict.insert("Length", lenValue.get());
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000038}
39
40SkPDFStream::~SkPDFStream() {
41}
42
43void SkPDFStream::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
44 bool indirect) {
45 if (indirect)
46 return emitIndirectObject(stream, catalog);
47
48 fDict.emitObject(stream, catalog, false);
49 stream->writeText(" stream\n");
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000050 if (fPlainData.get())
51 stream->write(fPlainData->getMemoryBase(), fLength);
52 else
53 stream->write(fCompressedData.getStream(), fLength);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000054 stream->writeText("\nendstream");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000055}
56
57size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) {
58 if (indirect)
59 return getIndirectOutputSize(catalog);
60
61 return fDict.getOutputSize(catalog, false) +
vandebo@chromium.orga09ef972010-12-01 22:17:20 +000062 strlen(" stream\n\nendstream") + fLength;
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000063}
64
65void SkPDFStream::insert(SkPDFName* key, SkPDFObject* value) {
66 fDict.insert(key, value);
67}
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000068
69void SkPDFStream::insert(const char key[], SkPDFObject* value) {
70 fDict.insert(key, value);
71}