From 0770044da6d61dcbc8d9673fed8dd92460faa314 Mon Sep 17 00:00:00 2001 From: "reed@google.com" Date: Mon, 20 Dec 2010 19:46:07 +0000 Subject: [PATCH] add option to write PDFs from gm fix some compile warnings (reorder initializers, init local ptr) git-svn-id: http://skia.googlecode.com/svn/trunk@642 2bbb7eff-a529-9590-31e7-b0007b416f81 --- Makefile | 17 ++++++++++++++- gm/gmmain.cpp | 41 ++++++++++++++++++++++++++++++----- include/config/SkUserConfig.h | 2 +- src/pdf/SkPDFDevice.cpp | 6 ++--- src/pdf/SkPDFImage.cpp | 2 +- 5 files changed, 57 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index dc6fd30cb6..38f28915dd 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ GPP := g++ C_INCLUDES := -Iinclude/config -Iinclude/core -Iinclude/effects -Iinclude/images -Iinclude/utils CFLAGS := -Wall -O2 CFLAGS_SSE2 = $(CFLAGS) -msse2 -LINKER_OPTS := -lpthread +LINKER_OPTS := -lpthread -lz DEFINES := -DSK_CAN_USE_FLOAT HIDE = @ @@ -24,6 +24,10 @@ endif DEFINES += -DSK_SUPPORT_LCDTEXT +ifeq ($(SKIA_PDF_SUPPORT),true) + DEFINES += -DSK_SUPPORT_PDF +endif + # start with the core (required) include src/core/core_files.mk SRC_LIST := $(addprefix src/core/, $(SOURCE)) @@ -200,6 +204,17 @@ gm: $(GM_OBJS) out/libskia.a @echo "linking gm..." $(HIDE)$(GPP) $(GM_OBJS) out/libskia.a -o out/gm/gm $(LINKER_OPTS) +SAMPLEPDF_SRCS := samplepdf.cpp + +SAMPLEPDF_SRCS := $(addprefix tools/, $(SAMPLEPDF_SRCS)) + +SAMPLEPDF_OBJS := $(SAMPLEPDF_SRCS:.cpp=.o) +SAMPLEPDF_OBJS := $(addprefix out/, $(SAMPLEPDF_OBJS)) + +samplepdf: $(SAMPLEPDF_OBJS) out/libskia.a + @echo "linking samplepdf..." + $(HIDE)$(GPP) $(SAMPLEPDF_OBJS) out/libskia.a -o out/tools/samplepdf $(LINKER_OPTS) + ############################################################################## .PHONY: all diff --git a/gm/gmmain.cpp b/gm/gmmain.cpp index 0be08e0386..3bcf43746c 100644 --- a/gm/gmmain.cpp +++ b/gm/gmmain.cpp @@ -3,6 +3,13 @@ #include "SkGraphics.h" #include "SkImageDecoder.h" #include "SkImageEncoder.h" +#include "SkStream.h" +#include "SkRefCnt.h" + +#ifdef SK_SUPPORT_PDF + #include "SkPDFDevice.h" + #include "SkPDFDocument.h" +#endif using namespace skiagm; @@ -44,12 +51,12 @@ static SkString make_name(const char shortName[], const char configName[]) { return name; } -static SkString make_filename(const char path[], const SkString& name) { +static SkString make_filename(const char path[], const SkString& name, const char suffix[]) { SkString filename(path); if (filename.size() && filename[filename.size() - 1] != '/') { filename.append("/"); } - filename.appendf("%s.png", name.c_str()); + filename.appendf("%s.%s", name.c_str(), suffix); return filename; } @@ -109,6 +116,29 @@ static void compare(const SkBitmap& target, const SkBitmap& base, } } +static void write_pdf(GM* gm, const char writePath[]) { +#ifdef SK_SUPPORT_PDF + SkISize size = gm->getISize(); + SkPDFDevice* dev = new SkPDFDevice(size.width(), size.height()); + SkAutoUnref aur(dev); + + { + SkCanvas c(dev); + gm->draw(&c); + } + + SkDynamicMemoryWStream output; + SkPDFDocument doc; + doc.appendPage(dev); + doc.emitPDF(&output); + + SkString shortName(gm->shortName()); + SkString path = make_filename(writePath, shortName, "pdf"); + SkFILEWStream stream(path.c_str()); + stream.write(output.getStream(), output.getOffset()); +#endif +} + static const struct { SkBitmap::Config fConfig; bool fUsePicture; @@ -162,17 +192,18 @@ int main (int argc, char * const argv[]) { SkCanvas canvas(bitmap); gm->draw(&canvas); - + SkString name = make_name(gm->shortName(), gRec[i].fName); if (writePath) { - SkString path = make_filename(writePath, name); + SkString path = make_filename(writePath, name, "png"); bool success = write_bitmap(path, bitmap); if (!success) { fprintf(stderr, "FAILED to write %s\n", path.c_str()); } + write_pdf(gm, writePath); } else if (readPath) { - SkString path = make_filename(readPath, name); + SkString path = make_filename(readPath, name, "png"); SkBitmap orig; bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig, SkBitmap::kARGB_8888_Config, diff --git a/include/config/SkUserConfig.h b/include/config/SkUserConfig.h index a2df7456e3..721f5f76c9 100644 --- a/include/config/SkUserConfig.h +++ b/include/config/SkUserConfig.h @@ -121,7 +121,7 @@ algorithm (used in PDF generation), define SK_ZLIB_INCLUDE to be the include path. */ -//#define SK_ZLIB_INCLUDE +#define SK_ZLIB_INCLUDE /* If SK_DEBUG is defined, then you can optionally define SK_SUPPORT_UNITTEST which will run additional self-tests at startup. These can take a long time, diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp index 9aefd0a258..fec9a4f883 100644 --- a/src/pdf/SkPDFDevice.cpp +++ b/src/pdf/SkPDFDevice.cpp @@ -131,10 +131,10 @@ static inline SkBitmap makeABitmap(int width, int height) { } SkPDFDevice::SkPDFDevice(int width, int height) - : fWidth(width), + : SkDevice(NULL, makeABitmap(width, height), false), + fWidth(width), fHeight(height), - fGraphicStackIndex(0), - SkDevice(NULL, makeABitmap(width, height), false) { + fGraphicStackIndex(0) { fGraphicStack[0].fColor = SK_ColorBLACK; fGraphicStack[0].fTextSize = SK_ScalarNaN; // This has no default value. fGraphicStack[0].fTextScaleX = SK_Scalar1; diff --git a/src/pdf/SkPDFImage.cpp b/src/pdf/SkPDFImage.cpp index 65b9b24514..2162eaec66 100644 --- a/src/pdf/SkPDFImage.cpp +++ b/src/pdf/SkPDFImage.cpp @@ -29,7 +29,7 @@ namespace { SkMemoryStream* extractImageData(const SkBitmap& bitmap) { - SkMemoryStream* result; + SkMemoryStream* result = NULL; bitmap.lockPixels(); switch (bitmap.getConfig()) {