1 /* 2 * Copyright (C) 2010 Google Inc. 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 "SkFlate.h" 18 #include "SkPDFCatalog.h" 19 #include "SkPDFStream.h" 20 #include "SkStream.h" 21 22 SkPDFStream::SkPDFStream(SkStream* stream) { 23 if (SkFlate::HaveFlate()) 24 SkAssertResult(SkFlate::Deflate(stream, &fCompressedData)); 25 26 if (SkFlate::HaveFlate() && 27 fCompressedData.getOffset() < stream->getLength()) { 28 fLength = fCompressedData.getOffset(); 29 insert("Filter", new SkPDFName("FlateDecode"))->unref(); 30 } else { 31 fCompressedData.reset(); 32 fPlainData = stream; 33 fLength = fPlainData->getLength(); 34 } 35 insert("Length", new SkPDFInt(fLength))->unref(); 36 } 37 38 SkPDFStream::~SkPDFStream() { 39 } 40 41 void SkPDFStream::emitObject(SkWStream* stream, SkPDFCatalog* catalog, 42 bool indirect) { 43 if (indirect) 44 return emitIndirectObject(stream, catalog); 45 46 this->INHERITED::emitObject(stream, catalog, false); 47 stream->writeText(" stream\n"); 48 if (fPlainData.get()) 49 stream->write(fPlainData->getMemoryBase(), fLength); 50 else 51 stream->write(fCompressedData.getStream(), fLength); 52 stream->writeText("\nendstream"); 53 } 54 55 size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) { 56 if (indirect) 57 return getIndirectOutputSize(catalog); 58 59 return this->INHERITED::getOutputSize(catalog, false) + 60 strlen(" stream\n\nendstream") + fLength; 61 } 62