aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pdf
diff options
context:
space:
mode:
authorGravatar Mike Reed <reed@google.com>2017-12-14 13:25:04 -0500
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-12-14 21:20:27 +0000
commita4daf193196c6da63b1c16e3490cad3c5ca6bf8b (patch)
treeda83a9b8ffeb0b07ffb5fdc87705d25c242c5b1c /src/pdf
parentdbd43481f1da3c9c87f5d913660f216ec531870a (diff)
Moving extra options/parameters into PDFMetadata
Bug: skia: Change-Id: I29aa69e5765a7f8ba05b0361912d1f5276d77de3 Reviewed-on: https://skia-review.googlesource.com/84501 Reviewed-by: Hal Canary <halcanary@google.com> Commit-Queue: Mike Reed <reed@google.com>
Diffstat (limited to 'src/pdf')
-rw-r--r--src/pdf/SkPDFBitmap.cpp29
-rw-r--r--src/pdf/SkPDFBitmap.h7
-rw-r--r--src/pdf/SkPDFCanon.h4
-rw-r--r--src/pdf/SkPDFDevice.cpp5
-rw-r--r--src/pdf/SkPDFDocument.cpp87
-rw-r--r--src/pdf/SkPDFDocument.h26
6 files changed, 106 insertions, 52 deletions
diff --git a/src/pdf/SkPDFBitmap.cpp b/src/pdf/SkPDFBitmap.cpp
index 8c312963da..f97eeff484 100644
--- a/src/pdf/SkPDFBitmap.cpp
+++ b/src/pdf/SkPDFBitmap.cpp
@@ -408,16 +408,15 @@ void PDFJpegBitmap::emitObject(SkWStream* stream,
////////////////////////////////////////////////////////////////////////////////
sk_sp<SkPDFObject> SkPDFCreateBitmapObject(sk_sp<SkImage> image,
- SkPixelSerializer* pixelSerializer) {
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
+ SkPixelSerializer* pixelSerializer,
+#endif
+ int encodingQuality) {
SkASSERT(image);
+ SkASSERT(encodingQuality >= 0);
sk_sp<SkData> data = image->refEncodedData();
SkJFIFInfo info;
- if (data && SkIsJFIF(data.get(), &info) &&
- (!pixelSerializer ||
- pixelSerializer->useEncodedData(data->data(), data->size()))) {
- // If there is a SkPixelSerializer, give it a chance to
- // re-encode the JPEG with more compression by returning false
- // from useEncodedData.
+ if (data && SkIsJFIF(data.get(), &info)) {
bool yuv = info.fType == SkJFIFInfo::kYCbCr;
if (info.fSize == image->dimensions()) { // Sanity check.
// hold on to data, not image.
@@ -428,6 +427,7 @@ sk_sp<SkPDFObject> SkPDFCreateBitmapObject(sk_sp<SkImage> image,
}
}
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
if (pixelSerializer) {
SkBitmap bm;
SkPixmap pmap;
@@ -441,9 +441,22 @@ sk_sp<SkPDFObject> SkPDFCreateBitmapObject(sk_sp<SkImage> image,
}
}
}
+#endif
+
+ const bool isOpaque = image_compute_is_opaque(image.get());
+
+ if (encodingQuality <= 100 && isOpaque) {
+ data = image->encodeToData(SkEncodedImageFormat::kJPEG, encodingQuality);
+ if (data && SkIsJFIF(data.get(), &info)) {
+ bool yuv = info.fType == SkJFIFInfo::kYCbCr;
+ if (info.fSize == image->dimensions()) { // Sanity check.
+ return sk_make_sp<PDFJpegBitmap>(info.fSize, data.get(), yuv);
+ }
+ }
+ }
sk_sp<SkPDFObject> smask;
- if (!image_compute_is_opaque(image.get())) {
+ if (!isOpaque) {
smask = sk_make_sp<PDFAlphaBitmap>(image);
}
#ifdef SK_PDF_IMAGE_STATS
diff --git a/src/pdf/SkPDFBitmap.h b/src/pdf/SkPDFBitmap.h
index 8de796c70b..a4e8ec5ccb 100644
--- a/src/pdf/SkPDFBitmap.h
+++ b/src/pdf/SkPDFBitmap.h
@@ -10,7 +10,9 @@
#include "SkRefCnt.h"
class SkImage;
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
class SkPixelSerializer;
+#endif
class SkPDFObject;
/**
@@ -19,6 +21,9 @@ class SkPDFObject;
* the image, and its emitObject() does not cache any data.
*/
sk_sp<SkPDFObject> SkPDFCreateBitmapObject(sk_sp<SkImage>,
- SkPixelSerializer*);
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
+ SkPixelSerializer*,
+#endif
+ int encodingQuality);
#endif // SkPDFBitmap_DEFINED
diff --git a/src/pdf/SkPDFCanon.h b/src/pdf/SkPDFCanon.h
index 8edd06e391..d68e471289 100644
--- a/src/pdf/SkPDFCanon.h
+++ b/src/pdf/SkPDFCanon.h
@@ -11,7 +11,9 @@
#include "SkPDFGradientShader.h"
#include "SkPDFGraphicState.h"
#include "SkPDFShader.h"
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
#include "SkPixelSerializer.h"
+#endif
#include "SkTDArray.h"
#include "SkTHash.h"
#include "SkTypeface.h"
@@ -43,7 +45,9 @@ public:
SkTHashMap<SkPDFStrokeGraphicState, sk_sp<SkPDFDict>> fStrokeGSMap;
SkTHashMap<SkPDFFillGraphicState, sk_sp<SkPDFDict>> fFillGSMap;
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
sk_sp<SkPixelSerializer> fPixelSerializer;
+#endif
sk_sp<SkPDFStream> fInvertFunction;
sk_sp<SkPDFDict> fNoSmaskGraphicState;
sk_sp<SkPDFArray> fRangeObject;
diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp
index aae792bb8b..582a44a963 100644
--- a/src/pdf/SkPDFDevice.cpp
+++ b/src/pdf/SkPDFDevice.cpp
@@ -2512,7 +2512,10 @@ void SkPDFDevice::internalDrawImageRect(SkKeyedImage imageSubset,
if (!pdfimage) {
SkASSERT(imageSubset);
pdfimage = SkPDFCreateBitmapObject(imageSubset.release(),
- fDocument->canon()->fPixelSerializer.get());
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
+ fDocument->canon()->fPixelSerializer.get(),
+#endif
+ fDocument->metadata().fEncodingQuality);
if (!pdfimage) {
return;
}
diff --git a/src/pdf/SkPDFDocument.cpp b/src/pdf/SkPDFDocument.cpp
index 9b23798a3f..f7c35ca2ac 100644
--- a/src/pdf/SkPDFDocument.cpp
+++ b/src/pdf/SkPDFDocument.cpp
@@ -12,6 +12,9 @@
#include "SkPDFCanon.h"
#include "SkPDFDevice.h"
#include "SkPDFUtils.h"
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
+#include "SkPixelSerializer.h"
+#endif
#include "SkStream.h"
SkPDFObjectSerializer::SkPDFObjectSerializer() : fBaseOffset(0), fNextToBeSerialized(0) {}
@@ -173,15 +176,16 @@ static sk_sp<SkPDFDict> generate_page_tree(SkTArray<sk_sp<SkPDFDict>>* pages) {
SkPDFDocument::SkPDFDocument(SkWStream* stream,
void (*doneProc)(SkWStream*, bool),
- SkScalar rasterDpi,
- const SkDocument::PDFMetadata& metadata,
- sk_sp<SkPixelSerializer> jpegEncoder,
- bool pdfa)
+ const SkDocument::PDFMetadata& metadata
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
+ , sk_sp<SkPixelSerializer> jpegEncoder
+#endif
+ )
: SkDocument(stream, doneProc)
- , fRasterDpi(rasterDpi)
- , fMetadata(metadata)
- , fPDFA(pdfa) {
+ , fMetadata(metadata) {
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
fCanon.fPixelSerializer = std::move(jpegEncoder);
+#endif
}
SkPDFDocument::~SkPDFDocument() {
@@ -200,7 +204,7 @@ SkCanvas* SkPDFDocument::onBeginPage(SkScalar width, SkScalar height) {
// if this is the first page if the document.
fObjectSerializer.serializeHeader(this->getStream(), fMetadata);
fDests = sk_make_sp<SkPDFDict>();
- if (fPDFA) {
+ if (fMetadata.fPDFA) {
SkPDFMetadata::UUID uuid = SkPDFMetadata::CreateUUID(fMetadata);
// We use the same UUID for Document ID and Instance ID since this
// is the first revision of this document (and Skia does not
@@ -404,7 +408,7 @@ void SkPDFDocument::onClose(SkWStream* stream) {
return;
}
auto docCatalog = sk_make_sp<SkPDFDict>("Catalog");
- if (fPDFA) {
+ if (fMetadata.fPDFA) {
SkASSERT(fXMP);
docCatalog->insertObjRef("Metadata", fXMP);
// Don't specify OutputIntents if we are not in PDF/A mode since
@@ -432,37 +436,58 @@ void SkPDFDocument::onClose(SkWStream* stream) {
sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream,
void (*proc)(SkWStream*, bool),
- SkScalar dpi,
- const SkDocument::PDFMetadata& metadata,
- sk_sp<SkPixelSerializer> jpeg,
- bool pdfa) {
- if (dpi <= 0) {
- dpi = 72.0f;
+ const SkDocument::PDFMetadata& metadata
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
+ , sk_sp<SkPixelSerializer> jpeg
+#endif
+ ) {
+ SkDocument::PDFMetadata meta = metadata;
+ if (meta.fRasterDPI <= 0) {
+ meta.fRasterDPI = 72.0f;
+ }
+ if (meta.fEncodingQuality < 0) {
+ meta.fEncodingQuality = 0;
}
- return stream ? sk_make_sp<SkPDFDocument>(stream, proc, dpi, metadata,
- std::move(jpeg), pdfa)
+ return stream ? sk_make_sp<SkPDFDocument>(stream, proc, meta
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
+ , std::move(jpeg)
+#endif
+ )
: nullptr;
}
-sk_sp<SkDocument> SkDocument::MakePDF(const char path[], SkScalar dpi) {
- auto delete_wstream = [](SkWStream* stream, bool) { delete stream; };
- auto stream = skstd::make_unique<SkFILEWStream>(path);
- return stream->isValid()
- ? SkPDFMakeDocument(stream.release(), delete_wstream, dpi,
- SkDocument::PDFMetadata(), nullptr,
- false)
- : nullptr;
+sk_sp<SkDocument> SkDocument::MakePDF(SkWStream* stream, const PDFMetadata& metadata) {
+ return SkPDFMakeDocument(stream, nullptr, metadata);
}
+sk_sp<SkDocument> SkDocument::MakePDF(SkWStream* stream) {
+ return SkPDFMakeDocument(stream, nullptr, PDFMetadata());
+}
+
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
sk_sp<SkDocument> SkDocument::MakePDF(SkWStream* stream,
SkScalar dpi,
- const SkDocument::PDFMetadata& metadata,
+ const PDFMetadata& metadata,
sk_sp<SkPixelSerializer> jpegEncoder,
bool pdfa) {
- return SkPDFMakeDocument(stream, nullptr, dpi, metadata,
- std::move(jpegEncoder), pdfa);
+ PDFMetadata meta = metadata;
+ meta.fRasterDPI = dpi;
+ meta.fPDFA = pdfa;
+ return SkPDFMakeDocument(stream, nullptr, meta, jpegEncoder);
}
-
-sk_sp<SkDocument> SkDocument::MakePDF(SkWStream* stream, const PDFMetadata& metadata) {
- return MakePDF(stream, SK_ScalarDefaultRasterDPI, metadata, nullptr, false);
+sk_sp<SkDocument> SkDocument::MakePDF(SkWStream* stream, SkScalar dpi) {
+ PDFMetadata meta;
+ meta.fRasterDPI = dpi;
+ return SkPDFMakeDocument(stream, nullptr, meta, nullptr);
+}
+sk_sp<SkDocument> SkDocument::MakePDF(const char path[], SkScalar dpi) {
+ auto delete_wstream = [](SkWStream* stream, bool) { delete stream; };
+ auto stream = skstd::make_unique<SkFILEWStream>(path);
+ if (!stream->isValid()) {
+ return nullptr;
+ }
+ PDFMetadata meta;
+ meta.fRasterDPI = dpi;
+ return SkPDFMakeDocument(stream.release(), delete_wstream, meta, nullptr);
}
+#endif
diff --git a/src/pdf/SkPDFDocument.h b/src/pdf/SkPDFDocument.h
index 3e3a50ae83..7cfd0bd1c3 100644
--- a/src/pdf/SkPDFDocument.h
+++ b/src/pdf/SkPDFDocument.h
@@ -10,6 +10,9 @@
#include "SkDocument.h"
#include "SkPDFCanon.h"
#include "SkPDFMetadata.h"
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
+#include "SkPixelSerializer.h"
+#endif
#include "SkPDFFont.h"
class SkPDFDevice;
@@ -26,10 +29,11 @@ class SkPDFDevice;
*/
sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream,
void (*doneProc)(SkWStream*, bool),
- SkScalar rasterDpi,
- const SkDocument::PDFMetadata&,
- sk_sp<SkPixelSerializer>,
- bool pdfa);
+ const SkDocument::PDFMetadata&
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
+ , sk_sp<SkPixelSerializer> = nullptr
+#endif
+ );
// Logically part of SkPDFDocument (like SkPDFCanon), but separate to
// keep similar functionality together.
@@ -56,10 +60,11 @@ class SkPDFDocument : public SkDocument {
public:
SkPDFDocument(SkWStream*,
void (*)(SkWStream*, bool),
- SkScalar,
- const SkDocument::PDFMetadata&,
- sk_sp<SkPixelSerializer>,
- bool);
+ const SkDocument::PDFMetadata&
+#ifdef SK_SUPPORT_LEGACY_PDF_PIXELSERIALIZER
+ , sk_sp<SkPixelSerializer> = nullptr
+#endif
+ );
~SkPDFDocument() override;
SkCanvas* onBeginPage(SkScalar, SkScalar) override;
void onEndPage() override;
@@ -77,8 +82,9 @@ public:
*/
void serialize(const sk_sp<SkPDFObject>&);
SkPDFCanon* canon() { return &fCanon; }
- SkScalar rasterDpi() const { return fRasterDpi; }
+ SkScalar rasterDpi() const { return fMetadata.fRasterDPI; }
void registerFont(SkPDFFont* f) { fFonts.add(f); }
+ const PDFMetadata& metadata() const { return fMetadata; }
private:
SkPDFObjectSerializer fObjectSerializer;
@@ -90,9 +96,7 @@ private:
std::unique_ptr<SkCanvas> fCanvas;
sk_sp<SkPDFObject> fID;
sk_sp<SkPDFObject> fXMP;
- SkScalar fRasterDpi;
SkDocument::PDFMetadata fMetadata;
- bool fPDFA;
void reset();
};