SkPDF: debugging flag - SK_PDF_BASE85_BINARY

If this is defined at compile time, binary date embedded in PDFs are
encoded n base85 to make them more inspectable.  This is only useful to
anyone hacking on SkPDF.

If the flag is unset, there is no change from this CL.

Change-Id: Ib07dc7ef7cedbff17a5fde0ffbeccd24dbeb6a8e
Reviewed-on: https://skia-review.googlesource.com/c/179250
Reviewed-by: Hal Canary <halcanary@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>
This commit is contained in:
Hal Canary 2018-12-19 13:49:23 -05:00 committed by Skia Commit-Bot
parent bf3e486a5c
commit fde1875f50
5 changed files with 87 additions and 0 deletions

View File

@ -82,7 +82,14 @@ static void emit_dict(SkWStream* stream, SkISize size, const char* colorSpace,
pdfDict.insertRef("SMask", *smask);
}
pdfDict.insertInt("BitsPerComponent", 8);
#ifdef SK_PDF_BASE85_BINARY
auto filters = SkPDFMakeArray();
filters->appendName("ASCII85Decode");
filters->appendName("FlateDecode");
pdfDict.insertObject("Filter", std::move(filters));
#else
pdfDict.insertName("Filter", "FlateDecode");
#endif
pdfDict.insertInt("Length", length);
pdfDict.emitObject(stream);
}
@ -114,6 +121,10 @@ static SkPDFIndirectReference do_deflated_alpha(const SkPixmap& pm, SkPDFDocumen
deflateWStream.write(byteBuffer, dst - byteBuffer);
}
deflateWStream.finalize();
#ifdef SK_PDF_BASE85_BINARY
SkPDFUtils::Base85Encode(&buffer);
#endif
SkWStream* stream = doc->beginObject(ref);
emit_dict(stream, pm.info().dimensions(), "DeviceGray", nullptr, buffer.bytesWritten());
emit_stream(&buffer, stream);
@ -169,6 +180,9 @@ static void do_deflated_image(const SkPixmap& pm,
deflateWStream.write(byteBuffer, dst - byteBuffer);
}
deflateWStream.finalize();
#ifdef SK_PDF_BASE85_BINARY
SkPDFUtils::Base85Encode(&buffer);
#endif
SkWStream* stream = doc->beginObject(ref);
emit_dict(stream, pm.info().dimensions(), colorSpace,
sMask.fValue != -1 ? &sMask : nullptr,

View File

@ -420,6 +420,8 @@ void SkPDFDict::insertString(const char key[], SkString value) {
////////////////////////////////////////////////////////////////////////////////
static void serialize_stream(const SkPDFDict* origDict,
SkStreamAsset* stream,
bool deflate,
@ -436,6 +438,17 @@ static void serialize_stream(const SkPDFDict* origDict,
SkDeflateWStream deflateWStream(&compressedData);
SkStreamCopy(&deflateWStream, stream);
deflateWStream.finalize();
#ifdef SK_PDF_BASE85_BINARY
{
SkPDFUtils::Base85Encode(&compressedData);
tmp = compressedData.detachAsStream();
stream = tmp.get();
auto filters = SkPDFMakeArray();
filters->appendName("ASCII85Decode");
filters->appendName("FlateDecode");
dict.insertObject("Filter", std::move(filters));
}
#else
if (stream->getLength() > compressedData.bytesWritten() + kMinimumSavings) {
tmp = compressedData.detachAsStream();
stream = tmp.get();
@ -443,6 +456,8 @@ static void serialize_stream(const SkPDFDict* origDict,
} else {
SkAssertResult(stream->rewind());
}
#endif
}
dict.insertInt("Length", stream->getLength());

View File

@ -131,6 +131,8 @@ inline void SkPDFArray_Append(SkPDFArray* a, T v, Args... args) {
SkPDFArray_Append(a, args...);
}
static inline void SkPDFArray_Append(SkPDFArray* a) {}
template <typename... Args>
inline sk_sp<SkPDFArray> SkPDFMakeArray(Args... args) {
auto ret = sk_make_sp<SkPDFArray>();

View File

@ -335,3 +335,54 @@ bool SkPDFUtils::ToBitmap(const SkImage* img, SkBitmap* dst) {
}
return false;
}
#ifdef SK_PDF_BASE85_BINARY
static void base85_fourbytes(uint32_t v, char dst[5]) {
for (int n = 4; n > 0; --n) {
dst[n] = (v % 85) + '!';
v /= 85;
}
dst[0] = v + '!';
}
static uint32_t big_end(const uint8_t s[4]) {
return ((uint32_t)s[0] << 24) | ((uint32_t)s[1] << 16) | ((uint32_t)s[2] << 8) | s[3];
}
void SkPDFUtils::Base85Encode(SkDynamicMemoryWStream* dst) {
SkASSERT(dst);
std::unique_ptr<SkStreamAsset> stream = dst->detachAsStream();
SkASSERT(src);
size_t remaining = stream->getLength();
dst->writeText("\n");
int line = 0;
while (remaining > 3) {
uint8_t src[4];
(void)stream->read(src, 4);
remaining -= 4;
uint32_t v = big_end(src);
if (v != 0) {
char buffer[5];
base85_fourbytes(v, buffer);
dst->write(buffer, 5);
line += 5;
} else {
dst->writeText("z");
line += 1;
}
if (line > 74) {
dst->writeText("\n");
line = 0;
}
}
if (remaining > 0) {
SkASSERT(remaining < 4);
uint8_t src[4] = {0, 0, 0, 0};
(void)stream->read(src, remaining);
char buffer[5];
base85_fourbytes(big_end(src), buffer);
dst->write(buffer, remaining + 1);
}
dst->writeText("~>\n");
}
#endif // SK_PDF_BASE85_BINARY

View File

@ -125,6 +125,11 @@ void PopulateTilingPatternDict(SkPDFDict* pattern,
const SkMatrix& matrix);
bool ToBitmap(const SkImage* img, SkBitmap* dst);
#ifdef SK_PDF_BASE85_BINARY
void Base85Encode(SkDynamicMemoryWStream*);
#endif // SK_PDF_BASE85_BINARY
} // namespace SkPDFUtils
#endif