aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental
diff options
context:
space:
mode:
authorGravatar edisonn@google.com <edisonn@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-07-23 17:43:18 +0000
committerGravatar edisonn@google.com <edisonn@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-07-23 17:43:18 +0000
commit2ccc3afa474f9485c39c2e863252ddaa3f35724b (patch)
treea0656156a32a57c109e6dc0ed8063b41b24ddcdc /experimental
parent0ab790bef3f4cc018015fce58b12265b5511afad (diff)
pdfviewer: improve memory usage, improve parse time (by 30-50%) and don't allocate extra buffers (more to do, but low priority now), and put the page specific memory in an allocator.
Review URL: https://codereview.chromium.org/19793011 git-svn-id: http://skia.googlecode.com/svn/trunk@10282 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'experimental')
-rw-r--r--experimental/PdfViewer/SkPdfBasics.cpp12
-rw-r--r--experimental/PdfViewer/SkPdfBasics.h13
-rw-r--r--experimental/PdfViewer/SkPdfConfig.h21
-rw-r--r--experimental/PdfViewer/SkPdfFont.cpp8
-rw-r--r--experimental/PdfViewer/SkPdfRenderer.cpp274
-rw-r--r--experimental/PdfViewer/pdf_viewer_main.cpp1
-rw-r--r--experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp82
-rw-r--r--experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.h17
-rw-r--r--experimental/PdfViewer/pdfparser/native/SkPdfNativeTokenizer.cpp312
-rw-r--r--experimental/PdfViewer/pdfparser/native/SkPdfNativeTokenizer.h16
-rw-r--r--experimental/PdfViewer/pdfparser/native/SkPdfObject.cpp39
-rw-r--r--experimental/PdfViewer/pdfparser/native/SkPdfObject.h99
12 files changed, 558 insertions, 336 deletions
diff --git a/experimental/PdfViewer/SkPdfBasics.cpp b/experimental/PdfViewer/SkPdfBasics.cpp
index e69de29bb2..e345f48a45 100644
--- a/experimental/PdfViewer/SkPdfBasics.cpp
+++ b/experimental/PdfViewer/SkPdfBasics.cpp
@@ -0,0 +1,12 @@
+#include "SkPdfBasics.h"
+#include "SkPdfNativeTokenizer.h"
+
+PdfContext::PdfContext(SkNativeParsedPDF* doc)
+ : fPdfDoc(doc)
+ , fTmpPageAllocator(new SkPdfAllocator()) {
+}
+
+PdfContext::~PdfContext() {
+ delete fTmpPageAllocator;
+}
+
diff --git a/experimental/PdfViewer/SkPdfBasics.h b/experimental/PdfViewer/SkPdfBasics.h
index 747c32a3d4..31fc2cd071 100644
--- a/experimental/PdfViewer/SkPdfBasics.h
+++ b/experimental/PdfViewer/SkPdfBasics.h
@@ -16,11 +16,12 @@ class SkPdfObject;
class SkPdfResourceDictionary;
class SkNativeParsedPDF;
+class SkPdfAllocator;
// TODO(edisonn): better class design.
struct SkPdfColorOperator {
// does not own the char*
- const char* fColorSpace; // TODO(edisonn): use SkString, or even char*
+ NotOwnedString fColorSpace;
SkColor fColor;
double fOpacity; // ca or CA
// TODO(edisonn): add here other color space options.
@@ -30,7 +31,9 @@ struct SkPdfColorOperator {
fColor = color;
}
// TODO(edisonn): double check the default values for all fields.
- SkPdfColorOperator() : fColorSpace(NULL), fColor(SK_ColorBLACK), fOpacity(1) {}
+ SkPdfColorOperator() : fColor(SK_ColorBLACK), fOpacity(1) {
+ NotOwnedString::init(&fColorSpace);
+ }
void applyGraphicsState(SkPaint* paint) {
paint->setColor(SkColorSetA(fColor, fOpacity * 255));
@@ -117,12 +120,14 @@ struct PdfContext {
std::stack<SkPdfGraphicsState> fStateStack;
SkPdfGraphicsState fGraphicsState;
SkNativeParsedPDF* fPdfDoc;
+ // TODO(edisonn): the allocator, could be freed after the page is done drawing.
+ SkPdfAllocator* fTmpPageAllocator;
SkMatrix fOriginalMatrix;
SkPdfInlineImage fInlineImage;
- PdfContext(SkNativeParsedPDF* doc) : fPdfDoc(doc) {}
-
+ PdfContext(SkNativeParsedPDF* doc);
+ ~PdfContext();
};
// TODO(edisonn): temporary code, to report how much of the PDF we actually think we rendered.
diff --git a/experimental/PdfViewer/SkPdfConfig.h b/experimental/PdfViewer/SkPdfConfig.h
index 1092de17e2..3d24a86ff7 100644
--- a/experimental/PdfViewer/SkPdfConfig.h
+++ b/experimental/PdfViewer/SkPdfConfig.h
@@ -2,6 +2,7 @@
#define __DEFINED__SkPdfConfig
//#define PDF_TRACE
+//#define PDF_TRACE_READ_TOKEN
//#define PDF_TRACE_DIFF_IN_PNG
//#define PDF_DEBUG_NO_CLIPING
//#define PDF_DEBUG_NO_PAGE_CLIPING
@@ -16,4 +17,24 @@ void SkTraceRect(const SkRect& rect, const char* sz);
#define SkTraceRect(a,b)
#endif
+struct NotOwnedString {
+ const unsigned char* fBuffer;
+ size_t fBytes;
+
+ static void init(NotOwnedString* str) {
+ str->fBuffer = NULL;
+ str->fBytes = 0;
+ }
+
+ static void init(NotOwnedString* str, const char* sz) {
+ str->fBuffer = (const unsigned char*)sz;
+ str->fBytes = strlen(sz);
+ }
+
+ bool equals(const char* sz) {
+ return strncmp((const char*)fBuffer, sz, fBytes) == 0 && fBytes == strlen(sz);
+
+ }
+};
+
#endif // __DEFINED__SkPdfConfig
diff --git a/experimental/PdfViewer/SkPdfFont.cpp b/experimental/PdfViewer/SkPdfFont.cpp
index 2574ce4165..0fec460bf5 100644
--- a/experimental/PdfViewer/SkPdfFont.cpp
+++ b/experimental/PdfViewer/SkPdfFont.cpp
@@ -164,12 +164,12 @@ SkPdfFont* SkPdfFont::fontFromFontDescriptor(SkNativeParsedPDF* doc, SkPdfFontDe
}
}
- unsigned char* uncompressedStream = NULL;
+ const unsigned char* uncompressedStream = NULL;
size_t uncompressedStreamLength = 0;
// TODO(edisonn): report warning to be used in testing.
if (!pdfStream ||
- !pdfStream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength, doc->allocator()) ||
+ !pdfStream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength) ||
!uncompressedStream ||
!uncompressedStreamLength) {
return NULL;
@@ -321,7 +321,9 @@ SkPdfToUnicode::SkPdfToUnicode(SkNativeParsedPDF* parsed, SkPdfStream* stream) :
fCMapEncodingFlag = NULL;
if (stream) {
- SkPdfNativeTokenizer* tokenizer = fParsed->tokenizerOfStream(stream);
+ // Since font will be cached, the font has to sit in the per doc allocator, not to be
+ // freed after the page is done drawing.
+ SkPdfNativeTokenizer* tokenizer = fParsed->tokenizerOfStream(stream, parsed->allocator());
PdfToken token;
fCMapEncoding = new unsigned short[256 * 256];
diff --git a/experimental/PdfViewer/SkPdfRenderer.cpp b/experimental/PdfViewer/SkPdfRenderer.cpp
index 1ace7ca5ae..6e86fcb6a5 100644
--- a/experimental/PdfViewer/SkPdfRenderer.cpp
+++ b/experimental/PdfViewer/SkPdfRenderer.cpp
@@ -16,11 +16,10 @@
#include "SkStream.h"
#include "SkTypeface.h"
#include "SkTArray.h"
+#include "SkTDict.h"
#include "SkPdfBasics.h"
#include "SkPdfNativeTokenizer.h"
-
-#include <iostream>
#include <cstdio>
#include <stack>
#include <set>
@@ -78,7 +77,18 @@ __SK_FORCE_IMAGE_DECODER_LINKING;
using namespace std;
+NotOwnedString strings_DeviceRGB;
+NotOwnedString strings_DeviceCMYK;
+class StringsInit {
+public:
+ StringsInit() {
+ NotOwnedString::init(&strings_DeviceRGB, "DeviceRGB");
+ NotOwnedString::init(&strings_DeviceCMYK, "DeviceCMYK");
+ }
+};
+
+StringsInit gStringsInit;
// TODO(edisonn): Document PdfTokenLooper and subclasses.
class PdfTokenLooper {
@@ -150,16 +160,16 @@ static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color
}
// TODO(edisonn): synonyms? DeviceRGB and RGB ...
-static int GetColorSpaceComponents(const std::string& colorSpace) {
- if (colorSpace == "DeviceCMYK") {
+static int GetColorSpaceComponents(NotOwnedString& colorSpace) {
+ if (colorSpace.equals("DeviceCMYK")) {
return 4;
- } else if (colorSpace == "DeviceGray" ||
- colorSpace == "CalGray" ||
- colorSpace == "Indexed") {
+ } else if (colorSpace.equals("DeviceGray") ||
+ colorSpace.equals("CalGray") ||
+ colorSpace.equals("Indexed")) {
return 1;
- } else if (colorSpace == "DeviceRGB" ||
- colorSpace == "CalRGB" ||
- colorSpace == "Lab") {
+ } else if (colorSpace.equals("DeviceRGB") ||
+ colorSpace.equals("CalRGB") ||
+ colorSpace.equals("Lab")) {
return 3;
} else {
return 0;
@@ -317,9 +327,15 @@ static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) {
typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**);
-map<std::string, PdfOperatorRenderer> gPdfOps;
+SkTDict<PdfOperatorRenderer> gPdfOps(100);
-map<std::string, int> gRenderStats[kCount_PdfResult];
+
+template <typename T> class SkTDictWithDefaultConstructor : public SkTDict<T> {
+public:
+ SkTDictWithDefaultConstructor() : SkTDict<T>(10) {}
+};
+
+SkTDictWithDefaultConstructor<int> gRenderStats[kCount_PdfResult];
const char* gRenderStatsNames[kCount_PdfResult] = {
"Success",
@@ -419,7 +435,7 @@ static SkColorTable* getGrayColortable() {
return grayColortable;
}
-static SkBitmap transferImageStreamToBitmap(unsigned char* uncompressedStream, size_t uncompressedStreamLength,
+static SkBitmap transferImageStreamToBitmap(const unsigned char* uncompressedStream, size_t uncompressedStreamLength,
int width, int height, int bytesPerLine,
int bpc, const std::string& colorSpace,
bool transparencyMask) {
@@ -508,12 +524,12 @@ static SkBitmap getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary*
}
*/
- unsigned char* uncompressedStream = NULL;
+ const unsigned char* uncompressedStream = NULL;
size_t uncompressedStreamLength = 0;
SkPdfStream* stream = (SkPdfStream*)image;
- if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength, pdfContext->fPdfDoc->allocator()) ||
+ if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength) ||
uncompressedStream == NULL || uncompressedStreamLength == 0) {
// TODO(edisonn): report warning to be used in testing.
return SkBitmap();
@@ -654,7 +670,8 @@ static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfT
SkPdfStream* stream = (SkPdfStream*)skobj;
- SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream);
+ SkPdfNativeTokenizer* tokenizer =
+ pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
if (tokenizer != NULL) {
PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
looper.loop();
@@ -702,7 +719,8 @@ PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObjec
SkPdfStream* stream = (SkPdfStream*)skobj;
- SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream);
+ SkPdfNativeTokenizer* tokenizer =
+ pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
if (tokenizer != NULL) {
PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
looper.loop();
@@ -1151,10 +1169,10 @@ static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLoop
//size; they must be specified explicitly using Tf before any text is shown.
static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
pdfContext->fGraphicsState.fCurFontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
- const char* fontName = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
+ SkPdfObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
#ifdef PDF_TRACE
- printf("font name: %s\n", fontName);
+ printf("font name: %s\n", fontName->nameValue2().c_str());
#endif
if (pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
@@ -1263,7 +1281,7 @@ static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLoop
}
static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
- colorOperator->fColorSpace = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
+ colorOperator->fColorSpace = pdfContext->fObjectStack.top()->strRef(); pdfContext->fObjectStack.pop();
return kOK_PdfResult;
}
@@ -1282,12 +1300,12 @@ static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColo
int n = GetColorSpaceComponents(colorOperator->fColorSpace);
bool doubles = true;
- if (strcmp(colorOperator->fColorSpace, "Indexed") == 0) {
+ if (colorOperator->fColorSpace.equals("Indexed")) {
doubles = false;
}
#ifdef PDF_TRACE
- printf("color space = %s, N = %i\n", colorOperator->fColorSpace, n);
+ printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n);
#endif
for (int i = n - 1; i >= 0 ; i--) {
@@ -1301,7 +1319,7 @@ static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColo
// TODO(edisonn): Now, set that color. Only DeviceRGB supported.
// TODO(edisonn): do possible field values to enum at parsing time!
// TODO(edisonn): support also abreviations /DeviceRGB == /RGB
- if (strcmp(colorOperator->fColorSpace, "DeviceRGB") == 0 || strcmp(colorOperator->fColorSpace, "RGB") == 0) {
+ if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) {
colorOperator->setRGBColor(SkColorSetRGB(255*c[0], 255*c[1], 255*c[2]));
}
return kPartial_PdfResult;
@@ -1354,7 +1372,7 @@ static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColo
double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
- colorOperator->fColorSpace = "DeviceRGB";
+ colorOperator->fColorSpace = strings_DeviceRGB;
colorOperator->setRGBColor(SkColorSetRGB(255*r, 255*g, 255*b));
return kOK_PdfResult;
}
@@ -1374,7 +1392,7 @@ static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorO
/*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
/*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
- colorOperator->fColorSpace = "DeviceCMYK";
+ colorOperator->fColorSpace = strings_DeviceCMYK;
// TODO(edisonn): Set color.
return kNYI_PdfResult;
}
@@ -1504,7 +1522,7 @@ static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLoope
//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
//the name of a graphics state parameter dictionary in the ExtGState subdictionary of the current resource dictionary (see the next section).
static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
- const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
+ SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
#ifdef PDF_TRACE
std::string str;
@@ -1622,7 +1640,7 @@ static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLoop
//name Do
static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
- const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
+ SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
@@ -1693,94 +1711,94 @@ static void initPdfOperatorRenderes() {
return;
}
- gPdfOps["q"] = PdfOp_q;
- gPdfOps["Q"] = PdfOp_Q;
- gPdfOps["cm"] = PdfOp_cm;
-
- gPdfOps["TD"] = PdfOp_TD;
- gPdfOps["Td"] = PdfOp_Td;
- gPdfOps["Tm"] = PdfOp_Tm;
- gPdfOps["T*"] = PdfOp_T_star;
-
- gPdfOps["m"] = PdfOp_m;
- gPdfOps["l"] = PdfOp_l;
- gPdfOps["c"] = PdfOp_c;
- gPdfOps["v"] = PdfOp_v;
- gPdfOps["y"] = PdfOp_y;
- gPdfOps["h"] = PdfOp_h;
- gPdfOps["re"] = PdfOp_re;
-
- gPdfOps["S"] = PdfOp_S;
- gPdfOps["s"] = PdfOp_s;
- gPdfOps["f"] = PdfOp_f;
- gPdfOps["F"] = PdfOp_F;
- gPdfOps["f*"] = PdfOp_f_star;
- gPdfOps["B"] = PdfOp_B;
- gPdfOps["B*"] = PdfOp_B_star;
- gPdfOps["b"] = PdfOp_b;
- gPdfOps["b*"] = PdfOp_b_star;
- gPdfOps["n"] = PdfOp_n;
-
- gPdfOps["BT"] = PdfOp_BT;
- gPdfOps["ET"] = PdfOp_ET;
-
- gPdfOps["Tj"] = PdfOp_Tj;
- gPdfOps["'"] = PdfOp_quote;
- gPdfOps["\""] = PdfOp_doublequote;
- gPdfOps["TJ"] = PdfOp_TJ;
-
- gPdfOps["CS"] = PdfOp_CS;
- gPdfOps["cs"] = PdfOp_cs;
- gPdfOps["SC"] = PdfOp_SC;
- gPdfOps["SCN"] = PdfOp_SCN;
- gPdfOps["sc"] = PdfOp_sc;
- gPdfOps["scn"] = PdfOp_scn;
- gPdfOps["G"] = PdfOp_G;
- gPdfOps["g"] = PdfOp_g;
- gPdfOps["RG"] = PdfOp_RG;
- gPdfOps["rg"] = PdfOp_rg;
- gPdfOps["K"] = PdfOp_K;
- gPdfOps["k"] = PdfOp_k;
-
- gPdfOps["W"] = PdfOp_W;
- gPdfOps["W*"] = PdfOp_W_star;
-
- gPdfOps["BX"] = PdfOp_BX;
- gPdfOps["EX"] = PdfOp_EX;
-
- gPdfOps["BI"] = PdfOp_BI;
- gPdfOps["ID"] = PdfOp_ID;
- gPdfOps["EI"] = PdfOp_EI;
-
- gPdfOps["w"] = PdfOp_w;
- gPdfOps["J"] = PdfOp_J;
- gPdfOps["j"] = PdfOp_j;
- gPdfOps["M"] = PdfOp_M;
- gPdfOps["d"] = PdfOp_d;
- gPdfOps["ri"] = PdfOp_ri;
- gPdfOps["i"] = PdfOp_i;
- gPdfOps["gs"] = PdfOp_gs;
-
- gPdfOps["Tc"] = PdfOp_Tc;
- gPdfOps["Tw"] = PdfOp_Tw;
- gPdfOps["Tz"] = PdfOp_Tz;
- gPdfOps["TL"] = PdfOp_TL;
- gPdfOps["Tf"] = PdfOp_Tf;
- gPdfOps["Tr"] = PdfOp_Tr;
- gPdfOps["Ts"] = PdfOp_Ts;
-
- gPdfOps["d0"] = PdfOp_d0;
- gPdfOps["d1"] = PdfOp_d1;
-
- gPdfOps["sh"] = PdfOp_sh;
-
- gPdfOps["Do"] = PdfOp_Do;
-
- gPdfOps["MP"] = PdfOp_MP;
- gPdfOps["DP"] = PdfOp_DP;
- gPdfOps["BMC"] = PdfOp_BMC;
- gPdfOps["BDC"] = PdfOp_BDC;
- gPdfOps["EMC"] = PdfOp_EMC;
+ gPdfOps.set("q", PdfOp_q);
+ gPdfOps.set("Q", PdfOp_Q);
+ gPdfOps.set("cm", PdfOp_cm);
+
+ gPdfOps.set("TD", PdfOp_TD);
+ gPdfOps.set("Td", PdfOp_Td);
+ gPdfOps.set("Tm", PdfOp_Tm);
+ gPdfOps.set("T*", PdfOp_T_star);
+
+ gPdfOps.set("m", PdfOp_m);
+ gPdfOps.set("l", PdfOp_l);
+ gPdfOps.set("c", PdfOp_c);
+ gPdfOps.set("v", PdfOp_v);
+ gPdfOps.set("y", PdfOp_y);
+ gPdfOps.set("h", PdfOp_h);
+ gPdfOps.set("re", PdfOp_re);
+
+ gPdfOps.set("S", PdfOp_S);
+ gPdfOps.set("s", PdfOp_s);
+ gPdfOps.set("f", PdfOp_f);
+ gPdfOps.set("F", PdfOp_F);
+ gPdfOps.set("f*", PdfOp_f_star);
+ gPdfOps.set("B", PdfOp_B);
+ gPdfOps.set("B*", PdfOp_B_star);
+ gPdfOps.set("b", PdfOp_b);
+ gPdfOps.set("b*", PdfOp_b_star);
+ gPdfOps.set("n", PdfOp_n);
+
+ gPdfOps.set("BT", PdfOp_BT);
+ gPdfOps.set("ET", PdfOp_ET);
+
+ gPdfOps.set("Tj", PdfOp_Tj);
+ gPdfOps.set("'", PdfOp_quote);
+ gPdfOps.set("\"", PdfOp_doublequote);
+ gPdfOps.set("TJ", PdfOp_TJ);
+
+ gPdfOps.set("CS", PdfOp_CS);
+ gPdfOps.set("cs", PdfOp_cs);
+ gPdfOps.set("SC", PdfOp_SC);
+ gPdfOps.set("SCN", PdfOp_SCN);
+ gPdfOps.set("sc", PdfOp_sc);
+ gPdfOps.set("scn", PdfOp_scn);
+ gPdfOps.set("G", PdfOp_G);
+ gPdfOps.set("g", PdfOp_g);
+ gPdfOps.set("RG", PdfOp_RG);
+ gPdfOps.set("rg", PdfOp_rg);
+ gPdfOps.set("K", PdfOp_K);
+ gPdfOps.set("k", PdfOp_k);
+
+ gPdfOps.set("W", PdfOp_W);
+ gPdfOps.set("W*", PdfOp_W_star);
+
+ gPdfOps.set("BX", PdfOp_BX);
+ gPdfOps.set("EX", PdfOp_EX);
+
+ gPdfOps.set("BI", PdfOp_BI);
+ gPdfOps.set("ID", PdfOp_ID);
+ gPdfOps.set("EI", PdfOp_EI);
+
+ gPdfOps.set("w", PdfOp_w);
+ gPdfOps.set("J", PdfOp_J);
+ gPdfOps.set("j", PdfOp_j);
+ gPdfOps.set("M", PdfOp_M);
+ gPdfOps.set("d", PdfOp_d);
+ gPdfOps.set("ri", PdfOp_ri);
+ gPdfOps.set("i", PdfOp_i);
+ gPdfOps.set("gs", PdfOp_gs);
+
+ gPdfOps.set("Tc", PdfOp_Tc);
+ gPdfOps.set("Tw", PdfOp_Tw);
+ gPdfOps.set("Tz", PdfOp_Tz);
+ gPdfOps.set("TL", PdfOp_TL);
+ gPdfOps.set("Tf", PdfOp_Tf);
+ gPdfOps.set("Tr", PdfOp_Tr);
+ gPdfOps.set("Ts", PdfOp_Ts);
+
+ gPdfOps.set("d0", PdfOp_d0);
+ gPdfOps.set("d1", PdfOp_d1);
+
+ gPdfOps.set("sh", PdfOp_sh);
+
+ gPdfOps.set("Do", PdfOp_Do);
+
+ gPdfOps.set("MP", PdfOp_MP);
+ gPdfOps.set("DP", PdfOp_DP);
+ gPdfOps.set("BMC", PdfOp_BMC);
+ gPdfOps.set("BDC", PdfOp_BDC);
+ gPdfOps.set("EMC", PdfOp_EMC);
gInitialized = true;
}
@@ -1798,8 +1816,11 @@ void reportPdfRenderStats() {
std::map<std::string, int>::iterator iter;
for (int i = 0 ; i < kCount_PdfResult; i++) {
- for (iter = gRenderStats[i].begin(); iter != gRenderStats[i].end(); ++iter) {
- printf("%s: %s -> count %i\n", gRenderStatsNames[i], iter->first.c_str(), iter->second);
+ SkTDict<int>::Iter iter(gRenderStats[i]);
+ const char* key;
+ int value = 0;
+ while ((key = iter.next(&value)) != NULL) {
+ printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value);
}
}
}
@@ -1812,11 +1833,15 @@ PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
strncpy(keyword, token.fKeyword, token.fKeywordLength);
keyword[token.fKeywordLength] = '\0';
// TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
- PdfOperatorRenderer pdfOperatorRenderer = gPdfOps[keyword];
- if (pdfOperatorRenderer) {
+ PdfOperatorRenderer pdfOperatorRenderer = NULL;
+ if (gPdfOps.find(keyword, &pdfOperatorRenderer) && pdfOperatorRenderer) {
// caller, main work is done by pdfOperatorRenderer(...)
PdfTokenLooper* childLooper = NULL;
- gRenderStats[pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper)][keyword]++;
+ PdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper);
+
+ int cnt = 0;
+ gRenderStats[result].find(keyword, &cnt);
+ gRenderStats[result].set(keyword, cnt + 1);
if (childLooper) {
childLooper->setUp(this);
@@ -1824,7 +1849,9 @@ PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
delete childLooper;
}
} else {
- gRenderStats[kUnsupported_PdfResult][keyword]++;
+ int cnt = 0;
+ gRenderStats[kUnsupported_PdfResult].find(keyword, &cnt);
+ gRenderStats[kUnsupported_PdfResult].set(keyword, cnt + 1);
}
}
else if (token.fType == kObject_TokenType)
@@ -1919,9 +1946,10 @@ bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) co
return false;
}
- SkPdfNativeTokenizer* tokenizer = fPdfDoc->tokenizerOfPage(page);
-
PdfContext pdfContext(fPdfDoc);
+
+ SkPdfNativeTokenizer* tokenizer = fPdfDoc->tokenizerOfPage(page, pdfContext.fTmpPageAllocator);
+
pdfContext.fOriginalMatrix = SkMatrix::I();
pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
diff --git a/experimental/PdfViewer/pdf_viewer_main.cpp b/experimental/PdfViewer/pdf_viewer_main.cpp
index f08fa510fd..7541cdfae6 100644
--- a/experimental/PdfViewer/pdf_viewer_main.cpp
+++ b/experimental/PdfViewer/pdf_viewer_main.cpp
@@ -199,6 +199,7 @@ static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
return false;
} else {
+ printf("renders = %i\n", FLAGS_benchRender);
for (int i = 0; i < FLAGS_benchRender + 1; i++) {
// TODO(edisonn) if (i == 1) start timer
if (strcmp(FLAGS_pages[0], "all") == 0) {
diff --git a/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp b/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp
index fe5d0ee830..ac543040b0 100644
--- a/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp
+++ b/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp
@@ -23,14 +23,14 @@ static long getFileSize(const char* filename)
return rc == 0 ? (long)stat_buf.st_size : -1;
}
-static unsigned char* lineHome(unsigned char* start, unsigned char* current) {
+static const unsigned char* lineHome(const unsigned char* start, const unsigned char* current) {
while (current > start && !isPdfEOL(*(current - 1))) {
current--;
}
return current;
}
-static unsigned char* previousLineHome(unsigned char* start, unsigned char* current) {
+static const unsigned char* previousLineHome(const unsigned char* start, const unsigned char* current) {
if (current > start && isPdfEOL(*(current - 1))) {
current--;
}
@@ -47,7 +47,7 @@ static unsigned char* previousLineHome(unsigned char* start, unsigned char* curr
return current;
}
-static unsigned char* ignoreLine(unsigned char* current, unsigned char* end) {
+static const unsigned char* ignoreLine(const unsigned char* current, const unsigned char* end) {
while (current < end && !isPdfEOL(*current)) {
current++;
}
@@ -74,9 +74,10 @@ SkNativeParsedPDF::SkNativeParsedPDF(const char* path)
gDoc = this;
FILE* file = fopen(path, "r");
fContentLength = getFileSize(path);
- fFileContent = new unsigned char[fContentLength + 1];
- bool ok = (0 != fread(fFileContent, fContentLength, 1, file));
- fFileContent[fContentLength] = '\0';
+ unsigned char* content = new unsigned char[fContentLength + 1];
+ bool ok = (0 != fread(content, fContentLength, 1, file));
+ content[fContentLength] = '\0';
+ fFileContent = content;
fclose(file);
file = NULL;
@@ -85,9 +86,9 @@ SkNativeParsedPDF::SkNativeParsedPDF(const char* path)
return; // Doc will have 0 pages
}
- unsigned char* eofLine = lineHome(fFileContent, fFileContent + fContentLength - 1);
- unsigned char* xrefByteOffsetLine = previousLineHome(fFileContent, eofLine);
- unsigned char* xrefstartKeywordLine = previousLineHome(fFileContent, xrefByteOffsetLine);
+ const unsigned char* eofLine = lineHome(fFileContent, fFileContent + fContentLength - 1);
+ const unsigned char* xrefByteOffsetLine = previousLineHome(fFileContent, eofLine);
+ const unsigned char* xrefstartKeywordLine = previousLineHome(fFileContent, xrefByteOffsetLine);
if (strcmp((char*)xrefstartKeywordLine, "startxref") != 0) {
// TODO(edisonn): report/issue
@@ -97,7 +98,7 @@ SkNativeParsedPDF::SkNativeParsedPDF(const char* path)
bool storeCatalog = true;
while (xrefByteOffset >= 0) {
- unsigned char* trailerStart = readCrossReferenceSection(fFileContent + xrefByteOffset, xrefstartKeywordLine);
+ const unsigned char* trailerStart = readCrossReferenceSection(fFileContent + xrefByteOffset, xrefstartKeywordLine);
xrefByteOffset = readTrailer(trailerStart, xrefstartKeywordLine, storeCatalog);
storeCatalog = false;
}
@@ -129,21 +130,21 @@ SkNativeParsedPDF::~SkNativeParsedPDF() {
delete fAllocator;
}
-unsigned char* SkNativeParsedPDF::readCrossReferenceSection(unsigned char* xrefStart, unsigned char* trailerEnd) {
- unsigned char* current = ignoreLine(xrefStart, trailerEnd); // TODO(edisonn): verify next keyord is "xref", use nextObject here
+const unsigned char* SkNativeParsedPDF::readCrossReferenceSection(const unsigned char* xrefStart, const unsigned char* trailerEnd) {
+ const unsigned char* current = ignoreLine(xrefStart, trailerEnd); // TODO(edisonn): verify next keyord is "xref", use nextObject here
SkPdfObject token;
while (current < trailerEnd) {
token.reset();
- unsigned char* previous = current;
- current = nextObject(current, trailerEnd, &token, NULL, NULL);
+ const unsigned char* previous = current;
+ current = nextObject(0, current, trailerEnd, &token, NULL, NULL);
if (!token.isInteger()) {
return previous;
}
int startId = (int)token.intValue();
token.reset();
- current = nextObject(current, trailerEnd, &token, NULL, NULL);
+ current = nextObject(0, current, trailerEnd, &token, NULL, NULL);
if (!token.isInteger()) {
// TODO(edisonn): report/warning
@@ -154,7 +155,7 @@ unsigned char* SkNativeParsedPDF::readCrossReferenceSection(unsigned char* xrefS
for (int i = 0; i < entries; i++) {
token.reset();
- current = nextObject(current, trailerEnd, &token, NULL, NULL);
+ current = nextObject(0, current, trailerEnd, &token, NULL, NULL);
if (!token.isInteger()) {
// TODO(edisonn): report/warning
return current;
@@ -162,7 +163,7 @@ unsigned char* SkNativeParsedPDF::readCrossReferenceSection(unsigned char* xrefS
int offset = (int)token.intValue();
token.reset();
- current = nextObject(current, trailerEnd, &token, NULL, NULL);
+ current = nextObject(0, current, trailerEnd, &token, NULL, NULL);
if (!token.isInteger()) {
// TODO(edisonn): report/warning
return current;
@@ -170,7 +171,7 @@ unsigned char* SkNativeParsedPDF::readCrossReferenceSection(unsigned char* xrefS
int generation = (int)token.intValue();
token.reset();
- current = nextObject(current, trailerEnd, &token, NULL, NULL);
+ current = nextObject(0, current, trailerEnd, &token, NULL, NULL);
if (!token.isKeyword() || token.len() != 1 || (*token.c_str() != 'f' && *token.c_str() != 'n')) {
// TODO(edisonn): report/warning
return current;
@@ -183,11 +184,21 @@ unsigned char* SkNativeParsedPDF::readCrossReferenceSection(unsigned char* xrefS
return current;
}
-long SkNativeParsedPDF::readTrailer(unsigned char* trailerStart, unsigned char* trailerEnd, bool storeCatalog) {
- unsigned char* current = ignoreLine(trailerStart, trailerEnd); // TODO(edisonn): verify next keyord is "trailer" use nextObject here
+long SkNativeParsedPDF::readTrailer(const unsigned char* trailerStart, const unsigned char* trailerEnd, bool storeCatalog) {
+ SkPdfObject trailerKeyword;
+ // TODO(edisonn): use null allocator, and let it just fail if memory
+ // needs allocated (but no crash)!
+ const unsigned char* current =
+ nextObject(0, trailerStart, trailerEnd, &trailerKeyword, fAllocator, NULL);
+
+ if (strlen("trailer") != trailerKeyword.len() &&
+ strncmp(trailerKeyword.c_str(), "trailer", strlen("trailer")) != 0) {
+ // TODO(edisonn): report warning, rebuild trailer from objects.
+ return -1;
+ }
SkPdfObject token;
- current = nextObject(current, trailerEnd, &token, fAllocator, NULL);
+ current = nextObject(0, current, trailerEnd, &token, fAllocator, NULL);
if (!token.isDictionary()) {
return -1;
}
@@ -230,8 +241,8 @@ SkPdfObject* SkNativeParsedPDF::readObject(int id/*, int expectedGeneration*/) {
// to decrease memory usage, we wither need to be smart and know where objects end, and we will
// alocate only the chancks needed, or the tokenizer will not make copies, but then it needs to
// cache the results so it does not go twice on the same buffer
- unsigned char* current = fFileContent + startOffset;
- unsigned char* end = fFileContent + fContentLength;
+ const unsigned char* current = fFileContent + startOffset;
+ const unsigned char* end = fFileContent + fContentLength;
SkPdfNativeTokenizer tokenizer(current, end - current, fMapper, fAllocator, this);
@@ -240,19 +251,19 @@ SkPdfObject* SkNativeParsedPDF::readObject(int id/*, int expectedGeneration*/) {
SkPdfObject objKeyword;
SkPdfObject* dict = fAllocator->allocObject();
- current = nextObject(current, end, &idObj, NULL, NULL);
+ current = nextObject(0, current, end, &idObj, NULL, NULL);
if (current >= end) {
// TODO(edisonn): report warning/error
return NULL;
}
- current = nextObject(current, end, &generationObj, NULL, NULL);
+ current = nextObject(0, current, end, &generationObj, NULL, NULL);
if (current >= end) {
// TODO(edisonn): report warning/error
return NULL;
}
- current = nextObject(current, end, &objKeyword, NULL, NULL);
+ current = nextObject(0, current, end, &objKeyword, NULL, NULL);
if (current >= end) {
// TODO(edisonn): report warning/error
return NULL;
@@ -266,7 +277,7 @@ SkPdfObject* SkNativeParsedPDF::readObject(int id/*, int expectedGeneration*/) {
// TODO(edisonn): report warning/error
}
- current = nextObject(current, end, dict, fAllocator, this);
+ current = nextObject(1, current, end, dict, fAllocator, this);
// TODO(edisonn): report warning/error - verify last token is endobj
@@ -313,9 +324,10 @@ SkRect SkNativeParsedPDF::MediaBox(int page) {
}
// TODO(edisonn): stream or array ... ? for now only array
-SkPdfNativeTokenizer* SkNativeParsedPDF::tokenizerOfPage(int page) {
+SkPdfNativeTokenizer* SkNativeParsedPDF::tokenizerOfPage(int page,
+ SkPdfAllocator* allocator) {
if (fPages[page]->isContentsAStream(this)) {
- return tokenizerOfStream(fPages[page]->getContentsAsStream(this));
+ return tokenizerOfStream(fPages[page]->getContentsAsStream(this), allocator);
} else {
// TODO(edisonn): NYI, we need to concatenate all streams in the array or make the tokenizer smart
// so we don't allocate new memory
@@ -323,19 +335,21 @@ SkPdfNativeTokenizer* SkNativeParsedPDF::tokenizerOfPage(int page) {
}
}
-SkPdfNativeTokenizer* SkNativeParsedPDF::tokenizerOfStream(SkPdfObject* stream) {
+SkPdfNativeTokenizer* SkNativeParsedPDF::tokenizerOfStream(SkPdfObject* stream,
+ SkPdfAllocator* allocator) {
if (stream == NULL) {
return NULL;
}
- return new SkPdfNativeTokenizer(stream, fMapper, fAllocator, this);
+ return new SkPdfNativeTokenizer(stream, fMapper, allocator, this);
}
// TODO(edisonn): NYI
-SkPdfNativeTokenizer* SkNativeParsedPDF::tokenizerOfBuffer(unsigned char* buffer, size_t len) {
+SkPdfNativeTokenizer* SkNativeParsedPDF::tokenizerOfBuffer(const unsigned char* buffer, size_t len,
+ SkPdfAllocator* allocator) {
// warning does not track two calls in the same buffer! the buffer is updated!
// make a clean copy if needed!
- return new SkPdfNativeTokenizer(buffer, len, fMapper, fAllocator, this);
+ return new SkPdfNativeTokenizer(buffer, len, fMapper, allocator, this);
}
size_t SkNativeParsedPDF::objects() const {
@@ -374,7 +388,7 @@ SkPdfInteger* SkNativeParsedPDF::createInteger(int value) const {
return (SkPdfInteger*)obj;
}
-SkPdfString* SkNativeParsedPDF::createString(unsigned char* sz, size_t len) const {
+SkPdfString* SkNativeParsedPDF::createString(const unsigned char* sz, size_t len) const {
SkPdfObject* obj = fAllocator->allocObject();
SkPdfObject::makeString(sz, len, obj);
return (SkPdfString*)obj;
diff --git a/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.h b/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.h
index b91d574f26..c520b0394a 100644
--- a/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.h
+++ b/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.h
@@ -41,10 +41,11 @@ public:
int pages() const;
SkPdfResourceDictionary* pageResources(int page);
SkRect MediaBox(int page);
- SkPdfNativeTokenizer* tokenizerOfPage(int n);
+ SkPdfNativeTokenizer* tokenizerOfPage(int n, SkPdfAllocator* allocator);
- SkPdfNativeTokenizer* tokenizerOfStream(SkPdfObject* stream);
- SkPdfNativeTokenizer* tokenizerOfBuffer(unsigned char* buffer, size_t len);
+ SkPdfNativeTokenizer* tokenizerOfStream(SkPdfObject* stream, SkPdfAllocator* allocator);
+ SkPdfNativeTokenizer* tokenizerOfBuffer(const unsigned char* buffer, size_t len,
+ SkPdfAllocator* allocator);
size_t objects() const;
SkPdfObject* object(int i);
@@ -55,9 +56,7 @@ public:
SkPdfReal* createReal(double value) const;
SkPdfInteger* createInteger(int value) const;
// the string does not own the char*
- SkPdfString* createString(unsigned char* sz, size_t len) const;
-
- void drawPage(int page, SkCanvas* canvas);
+ SkPdfString* createString(const unsigned char* sz, size_t len) const;
SkPdfObject* resolveReference(const SkPdfObject* ref);
@@ -66,8 +65,8 @@ public:
private:
- unsigned char* readCrossReferenceSection(unsigned char* xrefStart, unsigned char* trailerEnd);
- long readTrailer(unsigned char* trailerStart, unsigned char* trailerEnd, bool storeCatalog);
+ const unsigned char* readCrossReferenceSection(const unsigned char* xrefStart, const unsigned char* trailerEnd);
+ long readTrailer(const unsigned char* trailerStart, const unsigned char* trailerEnd, bool storeCatalog);
// TODO(edisonn): updates not supported right now, generation ignored
void addCrossSectionInfo(int id, int generation, int offset, bool isFreed);
@@ -84,7 +83,7 @@ private:
// private fields
SkPdfAllocator* fAllocator;
SkPdfMapper* fMapper;
- unsigned char* fFileContent;
+ const unsigned char* fFileContent;
size_t fContentLength;
const SkPdfObject* fRootCatalogRef;
SkPdfCatalogDictionary* fRootCatalog;
diff --git a/experimental/PdfViewer/pdfparser/native/SkPdfNativeTokenizer.cpp b/experimental/PdfViewer/pdfparser/native/SkPdfNativeTokenizer.cpp
index babfedfb05..4798031808 100644
--- a/experimental/PdfViewer/pdfparser/native/SkPdfNativeTokenizer.cpp
+++ b/experimental/PdfViewer/pdfparser/native/SkPdfNativeTokenizer.cpp
@@ -28,17 +28,74 @@ static char* strrstrk(char* hayStart, char* hayEnd, const char* needle) {
return NULL;
}
+#ifdef PDF_TRACE
+static void TRACE_INDENT(int level, const char* type) {
+ static int id = 0;
+ id++;
+ if (478613 == id) {
+ printf("break;\n");
+ }
+ // all types should have 2 letters, so the text is alligned nicely
+ printf("\n%10i %15s: ", id, type);
+ for (int i = 0 ; i < level; i++) {
+ printf(" ");
+ }
+}
+
+static void TRACE_COMMENT(char ch) {
+ printf("%c", ch);
+}
+
+static void TRACE_TK(char ch) {
+ printf("%c", ch);
+}
+
+static void TRACE_NAME(const unsigned char* start, const unsigned char* end) {
+ while (start < end) {
+ printf("%c", *start);
+ start++;
+ }
+ printf("\n");
+}
-static unsigned char* skipPdfWhiteSpaces(unsigned char* start, unsigned char* end) {
+static void TRACE_STRING(const unsigned char* start, const unsigned char* end) {
+ while (start < end) {
+ printf("%c", *start);
+ start++;
+ }
+ printf("\n");
+}
+
+static void TRACE_HEXSTRING(const unsigned char* start, const unsigned char* end) {
+ while (start < end) {
+ printf("%c", *start);
+ start++;
+ }
+ printf("\n");
+}
+
+#else
+#define TRACE_INDENT(level,type)
+#define TRACE_COMMENT(ch)
+#define TRACE_TK(ch)
+#define TRACE_NAME(start,end)
+#define TRACE_STRING(start,end)
+#define TRACE_HEXSTRING(start,end)
+#endif
+
+static const unsigned char* skipPdfWhiteSpaces(int level, const unsigned char* start, const unsigned char* end) {
+ TRACE_INDENT(level, "White Space");
while (start < end && isPdfWhiteSpace(*start)) {
+ TRACE_COMMENT(*start);
if (*start == kComment_PdfDelimiter) {
// skip the comment until end of line
while (start < end && !isPdfEOL(*start)) {
- *start = '\0';
+ //*start = '\0';
start++;
+ TRACE_COMMENT(*start);
}
} else {
- *start = '\0';
+ //*start = '\0';
start++;
}
}
@@ -46,30 +103,34 @@ static unsigned char* skipPdfWhiteSpaces(unsigned char* start, unsigned char* en
}
// TODO(edisonn) '(' can be used, will it break the string a delimiter or space inside () ?
-static unsigned char* endOfPdfToken(unsigned char* start, unsigned char* end) {
+static const unsigned char* endOfPdfToken(int level, const unsigned char* start, const unsigned char* end) {
//int opened brackets
//TODO(edisonn): what out for special chars, like \n, \032
+ TRACE_INDENT(level, "Token");
SkASSERT(!isPdfWhiteSpace(*start));
if (start < end && isPdfDelimiter(*start)) {
+ TRACE_TK(*start);
start++;
return start;
}
while (start < end && !isPdfWhiteSpaceOrPdfDelimiter(*start)) {
+ TRACE_TK(*start);
start++;
}
return start;
}
// last elem has to be ]
-static unsigned char* readArray(unsigned char* start, unsigned char* end, SkPdfObject* array, SkPdfAllocator* allocator, SkNativeParsedPDF* doc) {
+static const unsigned char* readArray(int level, const unsigned char* start, const unsigned char* end, SkPdfObject* array, SkPdfAllocator* allocator, SkNativeParsedPDF* doc) {
+ TRACE_INDENT(level, "Array");
while (start < end) {
// skip white spaces
- start = skipPdfWhiteSpaces(start, end);
+ start = skipPdfWhiteSpaces(level + 1, start, end);
- unsigned char* endOfToken = endOfPdfToken(start, end);
+ const unsigned char* endOfToken = endOfPdfToken(level + 1, start, end);
if (endOfToken == start) {
// TODO(edisonn): report error in pdf file (end of stream with ] for end of aray
@@ -81,7 +142,7 @@ static unsigned char* readArray(unsigned char* start, unsigned char* end, SkPdfO
}
SkPdfObject* newObj = allocator->allocObject();
- start = nextObject(start, end, newObj, allocator, doc);
+ start = nextObject(level + 1, start, end, newObj, allocator, doc);
// TODO(edisonn): perf/memory: put the variables on the stack, and flush them on the array only when
// we are sure they are not references!
if (newObj->isKeywordReference() && array->size() >= 2 && array->objAtAIndex(array->size() - 1)->isInteger() && array->objAtAIndex(array->size() - 2)->isInteger()) {
@@ -101,38 +162,47 @@ static unsigned char* readArray(unsigned char* start, unsigned char* end, SkPdfO
// When we read strings we will rewrite the string so we will reuse the memory
// when we start to read the string, we already consumed the opened bracket
-static unsigned char* readString(unsigned char* start, unsigned char* end, SkPdfObject* str) {
- unsigned char* out = start;
- unsigned char* in = start;
- int openRoundBrackets = 0;
- while (in < end && (*in != kClosedRoundBracket_PdfDelimiter || openRoundBrackets > 0)) {
+// TODO(edisonn): space: add paramater, taht would report if we need to allocate new buffer, or we can reuse the one we have
+
+static const unsigned char* readString(int level, const unsigned char* start, const unsigned char* end, unsigned char* out) {
+ TRACE_INDENT(level, "String");
+ const unsigned char* in = start;
+ bool hasOut = (out != NULL);
+
+ int openRoundBrackets = 1;
+ while (in < end) {
openRoundBrackets += ((*in) == kOpenedRoundBracket_PdfDelimiter);
openRoundBrackets -= ((*in) == kClosedRoundBracket_PdfDelimiter);
+ if (openRoundBrackets == 0) {
+ in++; // consumed )
+ break;
+ }
+
if (*in == kEscape_PdfSpecial) {
if (in + 1 < end) {
switch (in[1]) {
case 'n':
- *out = kLF_PdfWhiteSpace;
+ if (hasOut) { *out = kLF_PdfWhiteSpace; }
out++;
in += 2;
break;
case 'r':
- *out = kCR_PdfWhiteSpace;
+ if (hasOut) { *out = kCR_PdfWhiteSpace; }
out++;
in += 2;
break;
case 't':
- *out = kHT_PdfWhiteSpace;
+ if (hasOut) { *out = kHT_PdfWhiteSpace; }
out++;
in += 2;
break;
case 'b':
// TODO(edisonn): any special meaning to backspace?
- *out = kBackspace_PdfSpecial;
+ if (hasOut) { *out = kBackspace_PdfSpecial; }
out++;
in += 2;
break;
@@ -144,19 +214,19 @@ static unsigned char* readString(unsigned char* start, unsigned char* end, SkPdf
break;
case kOpenedRoundBracket_PdfDelimiter:
- *out = kOpenedRoundBracket_PdfDelimiter;
+ if (hasOut) { *out = kOpenedRoundBracket_PdfDelimiter; }
out++;
in += 2;
break;
case kClosedRoundBracket_PdfDelimiter:
- *out = kClosedRoundBracket_PdfDelimiter;
+ if (hasOut) { *out = kClosedRoundBracket_PdfDelimiter; }
out++;
in += 2;
break;
case kEscape_PdfSpecial:
- *out = kEscape_PdfSpecial;
+ if (hasOut) { *out = kEscape_PdfSpecial; }
out++;
in += 2;
break;
@@ -179,13 +249,13 @@ static unsigned char* readString(unsigned char* start, unsigned char* end, SkPdf
i++;
in++;
if (i == 3) {
- *out = code & 0xff;
+ if (hasOut) { *out = code & 0xff; }
out++;
i = 0;
}
}
if (i > 0) {
- *out = code & 0xff;
+ if (hasOut) { *out = code & 0xff; }
out++;
}
}
@@ -203,20 +273,37 @@ static unsigned char* readString(unsigned char* start, unsigned char* end, SkPdf
// TODO(edisonn): perf, avoid copy into itself, maybe first do a simple scan until found backslash ?
// we could have one look that first just inc current, and when we find the backslash
// we go to this loop
- *in = *out;
+ if (hasOut) { *out = *in; }
in++;
out++;
}
}
+ if (hasOut) {
+ return in; // consumed already ) at the end of the string
+ } else {
+ return start + (out - (const unsigned char*)NULL); // return where the string would end if we reuse the string
+ }
+}
+
+static int readStringLength(int level, const unsigned char* start, const unsigned char* end) {
+ return readString(level, start, end, NULL) - start;
+}
- SkPdfObject::makeString(start, out, str);
- return in + 1; // consume ) at the end of the string
+static const unsigned char* readString(int level, const unsigned char* start, const unsigned char* end, SkPdfObject* str, SkPdfAllocator* allocator) {
+ int outLength = readStringLength(level, start, end);
+ // TODO(edisonn): optimize the allocation, don't allocate new string, but put it in a preallocated buffer
+ unsigned char* out = (unsigned char*)allocator->alloc(outLength);
+ start = readString(level, start, end, out);
+ SkPdfObject::makeString(out, out + outLength, str);
+ TRACE_STRING(out, out + outLength);
+ return start; // consumed already ) at the end of the string
}
-static unsigned char* readHexString(unsigned char* start, unsigned char* end, SkPdfObject* str) {
- unsigned char* out = start;
- unsigned char* in = start;
+static const unsigned char* readHexString(int level, const unsigned char* start, const unsigned char* end, unsigned char* out) {
+ TRACE_INDENT(level, "HexString");
+ bool hasOut = (out != NULL);
+ const unsigned char* in = start;
unsigned char code = 0;
@@ -226,8 +313,8 @@ static unsigned char* readHexString(unsigned char* start, unsigned char* end, Sk
}
if (*in == kClosedInequityBracket_PdfDelimiter) {
- *in = '\0';
- in++;
+ //*in = '\0';
+ in++; // consume >
// normal exit
break;
}
@@ -282,13 +369,13 @@ static unsigned char* readHexString(unsigned char* start, unsigned char* end, Sk
// TODO(edisonn): report error
if (in >= end) {
- *out = code;
+ if (hasOut) { *out = code; }
out++;
break;
}
if (*in == kClosedInequityBracket_PdfDelimiter) {
- *out = code;
+ if (hasOut) { *out = code; }
out++;
break;
}
@@ -330,23 +417,37 @@ static unsigned char* readHexString(unsigned char* start, unsigned char* end, Sk
break;
}
- *out = code;
+ if (hasOut) { *out = code; }
out++;
in++;
}
- if (out < in) {
- *out = '\0';
+ if (hasOut) {
+ return in; // consumed already > at the end of the string
+ } else {
+ return start + (out - (const unsigned char*)NULL); // return where the string would end if we reuse the string
}
+}
- SkPdfObject::makeHexString(start, out, str);
- return in; // consume > at the end of the string
+static int readHexStringLength(int level, const unsigned char* start, const unsigned char* end) {
+ return readHexString(level, start, end, NULL) - start;
+}
+
+static const unsigned char* readHexString(int level, const unsigned char* start, const unsigned char* end, SkPdfObject* str, SkPdfAllocator* allocator) {
+ int outLength = readHexStringLength(level, start, end);
+ // TODO(edisonn): optimize the allocation, don't allocate new string, but put it in a preallocated buffer
+ unsigned char* out = (unsigned char*)allocator->alloc(outLength);
+ start = readHexString(level, start, end, out);
+ SkPdfObject::makeHexString(out, out + outLength, str);
+ TRACE_HEXSTRING(out, out + outLength);
+ return start; // consumed already > at the end of the string
}
// TODO(edisonn): before PDF 1.2 name could not have special characters, add version parameter
-static unsigned char* readName(unsigned char* start, unsigned char* end, SkPdfObject* name) {
- unsigned char* out = start;
- unsigned char* in = start;
+static const unsigned char* readName(int level, const unsigned char* start, const unsigned char* end, unsigned char* out) {
+ TRACE_INDENT(level, "Name");
+ bool hasOut = (out != NULL);
+ const unsigned char* in = start;
unsigned char code = 0;
@@ -433,18 +534,35 @@ static unsigned char* readName(unsigned char* start, unsigned char* end, SkPdfOb
break;
}
- *out = code;
+ if (hasOut) { *out = code; }
out++;
in++;
} else {
- *out = *in;
+ if (hasOut) { *out = *in; }
out++;
in++;
}
}
- SkPdfObject::makeName(start, out, name);
- return in;
+ if (hasOut) {
+ return in;
+ } else {
+ return start + (out - (const unsigned char*)NULL); // return where the string would end if we reuse the string
+ }
+}
+
+static int readNameLength(int level, const unsigned char* start, const unsigned char* end) {
+ return readName(level, start, end, NULL) - start;
+}
+
+static const unsigned char* readName(int level, const unsigned char* start, const unsigned char* end, SkPdfObject* name, SkPdfAllocator* allocator) {
+ int outLength = readNameLength(level, start, end);
+ // TODO(edisonn): optimize the allocation, don't allocate new string, but put it in a preallocated buffer
+ unsigned char* out = (unsigned char*)allocator->alloc(outLength);
+ start = readName(level, start, end, out);
+ SkPdfObject::makeName(out, out + outLength, name);
+ TRACE_NAME(out, out + outLength);
+ return start;
}
// TODO(edisonn): pdf spec let Length to be an indirect object define after the stream
@@ -471,8 +589,9 @@ and it could get worse, with multiple object like this
// right now implement the silly algorithm that assumes endstream is finishing the stream
-static unsigned char* readStream(unsigned char* start, unsigned char* end, SkPdfObject* dict, SkNativeParsedPDF* doc) {
- start = skipPdfWhiteSpaces(start, end);
+static const unsigned char* readStream(int level, const unsigned char* start, const unsigned char* end, SkPdfObject* dict, SkNativeParsedPDF* doc) {
+ TRACE_INDENT(level, "Stream");
+ start = skipPdfWhiteSpaces(level, start, end);
if (!(start[0] == 's' && start[1] == 't' && start[2] == 'r' && start[3] == 'e' && start[4] == 'a' && start[5] == 'm')) {
// no stream. return.
return start;
@@ -505,7 +624,7 @@ static unsigned char* readStream(unsigned char* start, unsigned char* end, SkPdf
if (length < 0) {
// scan the buffer, until we find first endstream
// TODO(edisonn): all buffers must have a 0 at the end now,
- unsigned char* endstream = (unsigned char*)strrstrk((char*)start, (char*)end, "endstream");
+ const unsigned char* endstream = (const unsigned char*)strrstrk((char*)start, (char*)end, "endstream");
if (endstream) {
length = endstream - start;
@@ -514,7 +633,7 @@ static unsigned char* readStream(unsigned char* start, unsigned char* end, SkPdf
}
}
if (length >= 0) {
- unsigned char* endstream = start + length;
+ const unsigned char* endstream = start + length;
if (endstream[0] == kCR_PdfWhiteSpace && endstream[1] == kLF_PdfWhiteSpace) {
endstream += 2;
@@ -532,7 +651,8 @@ static unsigned char* readStream(unsigned char* start, unsigned char* end, SkPdf
return start;
}
-static unsigned char* readInlineImageStream(unsigned char* start, unsigned char* end, SkPdfImageDictionary* inlineImage, SkNativeParsedPDF* doc) {
+static const unsigned char* readInlineImageStream(int level, const unsigned char* start, const unsigned char* end, SkPdfImageDictionary* inlineImage, SkNativeParsedPDF* doc) {
+ TRACE_INDENT(level, "Inline Image");
// We already processed ID keyword, and we should be positioned immediately after it
// TODO(edisonn): security: read after end check, or make buffers with extra 2 bytes
@@ -547,8 +667,8 @@ static unsigned char* readInlineImageStream(unsigned char* start, unsigned char*
// TODO(edisonn): warning?
}
- unsigned char* endstream = (unsigned char*)strrstrk((char*)start, (char*)end, "EI");
- unsigned char* endEI = endstream ? endstream + 2 : NULL; // 2 == strlen("EI")
+ const unsigned char* endstream = (const unsigned char*)strrstrk((char*)start, (char*)end, "EI");
+ const unsigned char* endEI = endstream ? endstream + 2 : NULL; // 2 == strlen("EI")
if (endstream) {
int length = endstream - start;
@@ -563,32 +683,34 @@ static unsigned char* readInlineImageStream(unsigned char* start, unsigned char*
return endEI;
}
-static unsigned char* readDictionary(unsigned char* start, unsigned char* end, SkPdfObject* dict, SkPdfAllocator* allocator, SkNativeParsedPDF* doc) {
+static const unsigned char* readDictionary(int level, const unsigned char* start, const unsigned char* end, SkPdfObject* dict, SkPdfAllocator* allocator, SkNativeParsedPDF* doc) {
+ TRACE_INDENT(level, "Dictionary");
SkPdfObject::makeEmptyDictionary(dict);
- start = skipPdfWhiteSpaces(start, end);
+ start = skipPdfWhiteSpaces(level, start, end);
+ SkPdfAllocator tmpStorage; // keys will be stored in dict, we can free them immediately after set.
while (start < end && *start == kNamed_PdfDelimiter) {
SkPdfObject key;
- *start = '\0';
+ //*start = '\0';
start++;
- start = readName(start, end, &key);
- start = skipPdfWhiteSpaces(start, end);
+ start = readName(level + 1, start, end, &key, &tmpStorage);
+ start = skipPdfWhiteSpaces(level + 1, start, end);
if (start < end) {
SkPdfObject* value = allocator->allocObject();
- start = nextObject(start, end, value, allocator, doc);
+ start = nextObject(level + 1, start, end, value, allocator, doc);
- start = skipPdfWhiteSpaces(start, end);
+ start = skipPdfWhiteSpaces(level + 1, start, end);
if (start < end) {
// seems we have an indirect reference
if (isPdfDigit(*start)) {
SkPdfObject generation;
- start = nextObject(start, end, &generation, allocator, doc);
+ start = nextObject(level + 1, start, end, &generation, allocator, doc);
SkPdfObject keywordR;
- start = nextObject(start, end, &keywordR, allocator, doc);
+ start = nextObject(level + 1, start, end, &keywordR, allocator, doc);
if (value->isInteger() && generation.isInteger() && keywordR.isKeywordReference()) {
int64_t id = value->intValue();
@@ -608,7 +730,7 @@ static unsigned char* readDictionary(unsigned char* start, unsigned char* end, S
dict->set(&key, value);
return end;
}
- start = skipPdfWhiteSpaces(start, end);
+ start = skipPdfWhiteSpaces(level + 1, start, end);
} else {
dict->set(&key, &SkPdfObject::kNull);
return end;
@@ -618,30 +740,30 @@ static unsigned char* readDictionary(unsigned char* start, unsigned char* end, S
// TODO(edisonn): options to ignore these errors
// now we should expect >>
- start = skipPdfWhiteSpaces(start, end);
+ start = skipPdfWhiteSpaces(level, start, end);
if (*start != kClosedInequityBracket_PdfDelimiter) {
// TODO(edisonn): report/warning
}
- *start = '\0';
+ //*start = '\0';
start++; // skip >
if (*start != kClosedInequityBracket_PdfDelimiter) {
// TODO(edisonn): report/warning
}
- *start = '\0';
+ //*start = '\0';
start++; // skip >
- start = readStream(start, end, dict, doc);
+ start = readStream(level, start, end, dict, doc);
return start;
}
-unsigned char* nextObject(unsigned char* start, unsigned char* end, SkPdfObject* token, SkPdfAllocator* allocator, SkNativeParsedPDF* doc) {
- unsigned char* current;
+const unsigned char* nextObject(int level, const unsigned char* start, const unsigned char* end, SkPdfObject* token, SkPdfAllocator* allocator, SkNativeParsedPDF* doc) {
+ const unsigned char* current;
// skip white spaces
- start = skipPdfWhiteSpaces(start, end);
+ start = skipPdfWhiteSpaces(level, start, end);
- current = endOfPdfToken(start, end);
+ current = endOfPdfToken(level, start, end);
// no token, len would be 0
if (current == start) {
@@ -654,27 +776,27 @@ unsigned char* nextObject(unsigned char* start, unsigned char* end, SkPdfObject*
// start array
switch (*start) {
case kOpenedSquareBracket_PdfDelimiter:
- *start = '\0';
+ //*start = '\0';
SkPdfObject::makeEmptyArray(token);
- return readArray(current, end, token, allocator, doc);
+ return readArray(level + 1, current, end, token, allocator, doc);
case kOpenedRoundBracket_PdfDelimiter:
- *start = '\0';
- return readString(start, end, token);
+ //*start = '\0';
+ return readString(level, start + 1, end, token, allocator);
case kOpenedInequityBracket_PdfDelimiter:
- *start = '\0';
+ //*start = '\0';
if (end > start + 1 && start[1] == kOpenedInequityBracket_PdfDelimiter) {
- start[1] = '\0'; // optional
+ //start[1] = '\0'; // optional
// TODO(edisonn): pass here the length somehow?
- return readDictionary(start + 2, end, token, allocator, doc); // skip <<
+ return readDictionary(level + 1, start + 2, end, token, allocator, doc); // skip <<
} else {
- return readHexString(start + 1, end, token); // skip <
+ return readHexString(level, start + 1, end, token, allocator); // skip <
}
case kNamed_PdfDelimiter:
- *start = '\0';
- return readName(start + 1, end, token);
+ //*start = '\0';
+ return readName(level, start + 1, end, token, allocator);
// TODO(edisonn): what to do curly brackets? read spec!
case kOpenedCurlyBracket_PdfDelimiter:
@@ -746,28 +868,26 @@ SkPdfObject* SkPdfAllocator::allocObject() {
// TODO(edisonn): perf: do no copy the buffers, but use them, and mark cache the result, so there is no need of a second pass
SkPdfNativeTokenizer::SkPdfNativeTokenizer(SkPdfObject* objWithStream, const SkPdfMapper* mapper, SkPdfAllocator* allocator, SkNativeParsedPDF* doc) : fDoc(doc), fMapper(mapper), fAllocator(allocator), fUncompressedStream(NULL), fUncompressedStreamEnd(NULL), fEmpty(false), fHasPutBack(false) {
- unsigned char* buffer = NULL;
+ const unsigned char* buffer = NULL;
size_t len = 0;
- objWithStream->GetFilteredStreamRef(&buffer, &len, fAllocator);
+ objWithStream->GetFilteredStreamRef(&buffer, &len);
// TODO(edisonn): hack, find end of object
char* endobj = strrstrk((char*)buffer, (char*)buffer + len, "endobj");
if (endobj) {
len = endobj - (char*)buffer + strlen("endobj");
}
- fUncompressedStreamStart = fUncompressedStream = (unsigned char*)fAllocator->alloc(len);
+ fUncompressedStreamStart = fUncompressedStream = buffer;
fUncompressedStreamEnd = fUncompressedStream + len;
- memcpy(fUncompressedStream, buffer, len);
}
-SkPdfNativeTokenizer::SkPdfNativeTokenizer(unsigned char* buffer, int len, const SkPdfMapper* mapper, SkPdfAllocator* allocator, SkNativeParsedPDF* doc) : fDoc(doc), fMapper(mapper), fAllocator(allocator), fEmpty(false), fHasPutBack(false) {
+SkPdfNativeTokenizer::SkPdfNativeTokenizer(const unsigned char* buffer, int len, const SkPdfMapper* mapper, SkPdfAllocator* allocator, SkNativeParsedPDF* doc) : fDoc(doc), fMapper(mapper), fAllocator(allocator), fEmpty(false), fHasPutBack(false) {
// TODO(edisonn): hack, find end of object
char* endobj = strrstrk((char*)buffer, (char*)buffer + len, "endobj");
if (endobj) {
len = endobj - (char*)buffer + strlen("endobj");
}
- fUncompressedStreamStart = fUncompressedStream = (unsigned char*)fAllocator->alloc(len);
+ fUncompressedStreamStart = fUncompressedStream = buffer;
fUncompressedStreamEnd = fUncompressedStream + len;
- memcpy(fUncompressedStream, buffer, len);
}
SkPdfNativeTokenizer::~SkPdfNativeTokenizer() {
@@ -777,13 +897,13 @@ bool SkPdfNativeTokenizer::readTokenCore(PdfToken* token) {
token->fKeyword = NULL;
token->fObject = NULL;
- fUncompressedStream = skipPdfWhiteSpaces(fUncompressedStream, fUncompressedStreamEnd);
+ fUncompressedStream = skipPdfWhiteSpaces(0, fUncompressedStream, fUncompressedStreamEnd);
if (fUncompressedStream >= fUncompressedStreamEnd) {
return false;
}
SkPdfObject obj;
- fUncompressedStream = nextObject(fUncompressedStream, fUncompressedStreamEnd, &obj, fAllocator, fDoc);
+ fUncompressedStream = nextObject(0, fUncompressedStream, fUncompressedStreamEnd, &obj, fAllocator, fDoc);
// If it is a keyword, we will only get the pointer of the string
if (obj.type() == SkPdfObject::kKeyword_PdfObjectType) {
@@ -797,7 +917,7 @@ bool SkPdfNativeTokenizer::readTokenCore(PdfToken* token) {
token->fType = kObject_TokenType;
}
-#ifdef PDF_TRACE
+#ifdef PDF_TRACE_READ_TOKEN
static int read_op = 0;
read_op++;
if (548 == read_op) {
@@ -813,7 +933,7 @@ void SkPdfNativeTokenizer::PutBack(PdfToken token) {
SkASSERT(!fHasPutBack);
fHasPutBack = true;
fPutBack = token;
-#ifdef PDF_TRACE
+#ifdef PDF_TRACE_READ_TOKEN
printf("PUT_BACK %s %s\n", token.fType == kKeyword_TokenType ? "Keyword" : "Object", token.fKeyword ? std::string(token.fKeyword, token.fKeywordLength).c_str(): token.fObject->toString().c_str());
#endif
}
@@ -822,14 +942,14 @@ bool SkPdfNativeTokenizer::readToken(PdfToken* token) {
if (fHasPutBack) {
*token = fPutBack;
fHasPutBack = false;
-#ifdef PDF_TRACE
+#ifdef PDF_TRACE_READ_TOKEN
printf("READ_BACK %s %s\n", token->fType == kKeyword_TokenType ? "Keyword" : "Object", token->fKeyword ? std::string(token->fKeyword, token->fKeywordLength).c_str() : token->fObject->toString().c_str());
#endif
return true;
}
if (fEmpty) {
-#ifdef PDF_TRACE
+#ifdef PDF_TRACE_READ_TOKEN
printf("EMPTY TOKENIZER\n");
#endif
return false;
@@ -911,7 +1031,7 @@ static SkPdfObject* inlineImageValueAbbreviationExpand(SkPdfObject* value) {
SkPdfImageDictionary* SkPdfNativeTokenizer::readInlineImage() {
// BI already processed
- fUncompressedStream = skipPdfWhiteSpaces(fUncompressedStream, fUncompressedStreamEnd);
+ fUncompressedStream = skipPdfWhiteSpaces(0, fUncompressedStream, fUncompressedStreamEnd);
if (fUncompressedStream >= fUncompressedStreamEnd) {
return NULL;
}
@@ -921,14 +1041,14 @@ SkPdfImageDictionary* SkPdfNativeTokenizer::readInlineImage() {
while (fUncompressedStream < fUncompressedStreamEnd) {
SkPdfObject* key = fAllocator->allocObject();
- fUncompressedStream = nextObject(fUncompressedStream, fUncompressedStreamEnd, key, fAllocator, fDoc);
+ fUncompressedStream = nextObject(0, fUncompressedStream, fUncompressedStreamEnd, key, fAllocator, fDoc);
if (key->isKeyword() && key->len() == 2 && key->c_str()[0] == 'I' && key->c_str()[1] == 'D') { // ID
- fUncompressedStream = readInlineImageStream(fUncompressedStream, fUncompressedStreamEnd, inlineImage, fDoc);
+ fUncompressedStream = readInlineImageStream(0, fUncompressedStream, fUncompressedStreamEnd, inlineImage, fDoc);
return inlineImage;
} else {
SkPdfObject* obj = fAllocator->allocObject();
- fUncompressedStream = nextObject(fUncompressedStream, fUncompressedStreamEnd, obj, fAllocator, fDoc);
+ fUncompressedStream = nextObject(0, fUncompressedStream, fUncompressedStreamEnd, obj, fAllocator, fDoc);
// TODO(edisonn): perf maybe we should not expand abreviation like this
inlineImage->set(inlineImageKeyAbbreviationExpand(key),
inlineImageValueAbbreviationExpand(obj));
diff --git a/experimental/PdfViewer/pdfparser/native/SkPdfNativeTokenizer.h b/experimental/PdfViewer/pdfparser/native/SkPdfNativeTokenizer.h
index ad425d8e02..2884937aaf 100644
--- a/experimental/PdfViewer/pdfparser/native/SkPdfNativeTokenizer.h
+++ b/experimental/PdfViewer/pdfparser/native/SkPdfNativeTokenizer.h
@@ -62,9 +62,9 @@ class SkPdfImageDictionary;
#define isPdfDigit(ch) ((ch)>='0'&&(ch)<='9')
#define isPdfNumeric(ch) (isPdfDigit(ch)||(ch)=='+'||(ch)=='-')
-unsigned char* skipPdfWhiteSpaces(unsigned char* buffer, size_t len);
-unsigned char* endOfPdfToken(unsigned char* start, size_t len);
-unsigned char* skipPdfComment(unsigned char* start, size_t len);
+const unsigned char* skipPdfWhiteSpaces(int level, const unsigned char* buffer, size_t len);
+const unsigned char* endOfPdfToken(int level, const unsigned char* start, size_t len);
+const unsigned char* skipPdfComment(int level, const unsigned char* start, size_t len);
// TODO(edisonn): typedef read and integer tyepes? make less readable...
//typedef double SkPdfReal;
@@ -111,7 +111,7 @@ public:
};
class SkNativeParsedPDF;
-unsigned char* nextObject(unsigned char* start, unsigned char* end, SkPdfObject* token, SkPdfAllocator* allocator, SkNativeParsedPDF* doc);
+const unsigned char* nextObject(int level, const unsigned char* start, const unsigned char* end, SkPdfObject* token, SkPdfAllocator* allocator, SkNativeParsedPDF* doc);
enum SkPdfTokenType {
kKeyword_TokenType,
@@ -130,7 +130,7 @@ struct PdfToken {
class SkPdfNativeTokenizer {
public:
SkPdfNativeTokenizer(SkPdfObject* objWithStream, const SkPdfMapper* mapper, SkPdfAllocator* allocator, SkNativeParsedPDF* doc);
- SkPdfNativeTokenizer(unsigned char* buffer, int len, const SkPdfMapper* mapper, SkPdfAllocator* allocator, SkNativeParsedPDF* doc);
+ SkPdfNativeTokenizer(const unsigned char* buffer, int len, const SkPdfMapper* mapper, SkPdfAllocator* allocator, SkNativeParsedPDF* doc);
virtual ~SkPdfNativeTokenizer();
@@ -144,9 +144,9 @@ private:
const SkPdfMapper* fMapper;
SkPdfAllocator* fAllocator;
- unsigned char* fUncompressedStreamStart;
- unsigned char* fUncompressedStream;
- unsigned char* fUncompressedStreamEnd;
+ const unsigned char* fUncompressedStreamStart;
+ const unsigned char* fUncompressedStream;
+ const unsigned char* fUncompressedStreamEnd;
bool fEmpty;
bool fHasPutBack;
diff --git a/experimental/PdfViewer/pdfparser/native/SkPdfObject.cpp b/experimental/PdfViewer/pdfparser/native/SkPdfObject.cpp
index 57e9abb368..eb342af730 100644
--- a/experimental/PdfViewer/pdfparser/native/SkPdfObject.cpp
+++ b/experimental/PdfViewer/pdfparser/native/SkPdfObject.cpp
@@ -8,19 +8,27 @@
SkPdfObject SkPdfObject::kNull = SkPdfObject::makeNull();
-bool SkPdfObject::applyFlateDecodeFilter(SkPdfAllocator* allocator) {
+bool SkPdfObject::applyFlateDecodeFilter() {
if (!SkFlate::HaveFlate()) {
// TODO(edisonn): warn, make callers handle it
return false;
}
- SkMemoryStream skstream(fStr.fBuffer, fStr.fBytes >> 1, false);
+ const unsigned char* old = fStr.fBuffer;
+ bool deleteOld = isStreamOwned();
+
+ SkMemoryStream skstream(fStr.fBuffer, fStr.fBytes >> 2, false);
SkDynamicMemoryWStream uncompressedData;
if (SkFlate::Inflate(&skstream, &uncompressedData)) {
- fStr.fBytes = (uncompressedData.bytesWritten() << 1) + kUnfilteredStreamBit;
- fStr.fBuffer = (unsigned char*)allocator->alloc(uncompressedData.bytesWritten());
- uncompressedData.copyTo(fStr.fBuffer);
+ fStr.fBytes = (uncompressedData.bytesWritten() << 2) + kOwnedStreamBit + kUnfilteredStreamBit;
+ fStr.fBuffer = (const unsigned char*)new unsigned char[uncompressedData.bytesWritten()];
+ uncompressedData.copyTo((void*)fStr.fBuffer);
+
+ if (deleteOld) {
+ delete[] old;
+ }
+
return true;
} else {
// TODO(edisonn): warn, make callers handle it
@@ -28,24 +36,24 @@ bool SkPdfObject::applyFlateDecodeFilter(SkPdfAllocator* allocator) {
}
}
-bool SkPdfObject::applyDCTDecodeFilter(SkPdfAllocator* allocator) {
+bool SkPdfObject::applyDCTDecodeFilter() {
// this would fail, and it won't allow any more filters.
// technically, it would be possible, but not a real world scenario
// TODO(edisonn): or get the image here and store it for fast retrieval?
return false;
}
-bool SkPdfObject::applyFilter(const char* name, SkPdfAllocator* allocator) {
+bool SkPdfObject::applyFilter(const char* name) {
if (strcmp(name, "FlateDecode") == 0) {
- return applyFlateDecodeFilter(allocator);
+ return applyFlateDecodeFilter();
} else if (strcmp(name, "DCTDecode") == 0) {
- return applyDCTDecodeFilter(allocator);
+ return applyDCTDecodeFilter();
}
// TODO(edisonn): allert, not supported, but should be implemented asap
return false;
}
-bool SkPdfObject::filterStream(SkPdfAllocator* allocator) {
+bool SkPdfObject::filterStream() {
if (!hasStream()) {
return false;
}
@@ -58,19 +66,16 @@ bool SkPdfObject::filterStream(SkPdfAllocator* allocator) {
if (!stream->has_Filter()) {
fStr.fBytes = ((fStr.fBytes >> 1) << 1) + kFilteredStreamBit;
- return true;
- }
-
- if (stream->isFilterAName(NULL)) {
+ } else if (stream->isFilterAName(NULL)) {
std::string filterName = stream->getFilterAsName(NULL);
- applyFilter(filterName.c_str(), allocator);
+ applyFilter(filterName.c_str());
} else if (stream->isFilterAArray(NULL)) {
const SkPdfArray* filters = stream->getFilterAsArray(NULL);
int cnt = filters->size();
for (int i = cnt - 1; i >= 0; i--) {
const SkPdfObject* filterName = filters->objAtAIndex(i);
if (filterName != NULL && filterName->isName()) {
- if (!applyFilter(filterName->nameValue(), allocator)) {
+ if (!applyFilter(filterName->nameValue())) {
break;
}
} else {
@@ -79,7 +84,5 @@ bool SkPdfObject::filterStream(SkPdfAllocator* allocator) {
}
}
- fStr.fBytes = ((fStr.fBytes >> 1) << 1) + kFilteredStreamBit;
-
return true;
}
diff --git a/experimental/PdfViewer/pdfparser/native/SkPdfObject.h b/experimental/PdfViewer/pdfparser/native/SkPdfObject.h
index d496670bc1..f7171191c8 100644
--- a/experimental/PdfViewer/pdfparser/native/SkPdfObject.h
+++ b/experimental/PdfViewer/pdfparser/native/SkPdfObject.h
@@ -23,7 +23,7 @@ SkMatrix SkMatrixFromPdfMatrix(double array[6]);
#define kFilteredStreamBit 0
#define kUnfilteredStreamBit 1
-
+#define kOwnedStreamBit 2
class SkPdfObject {
public:
@@ -50,11 +50,6 @@ class SkPdfObject {
};
private:
- struct NotOwnedString {
- unsigned char* fBuffer;
- size_t fBytes;
- };
-
struct Reference {
unsigned int fId;
unsigned int fGen;
@@ -108,6 +103,11 @@ public:
case kDictionary_PdfObjectType:
delete fMap;
+ if (isStreamOwned()) {
+ delete[] fStr.fBuffer;
+ fStr.fBuffer = NULL;
+ fStr.fBytes = 0;
+ }
break;
default:
@@ -212,13 +212,13 @@ public:
static SkPdfObject kNull;
- static void makeNumeric(unsigned char* start, unsigned char* end, SkPdfObject* obj) {
+ static void makeNumeric(const unsigned char* start, const unsigned char* end, SkPdfObject* obj) {
SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);
// TODO(edisonn): NYI properly
// if has dot (impl), or exceeds max int, is real, otherwise is int
bool isInt = true;
- for (unsigned char* current = start; current < end; current++) {
+ for (const unsigned char* current = start; current < end; current++) {
if (*current == '.') {
isInt = false;
break;
@@ -241,54 +241,54 @@ public:
}
- static void makeString(unsigned char* start, SkPdfObject* obj) {
+ static void makeString(const unsigned char* start, SkPdfObject* obj) {
makeStringCore(start, strlen((const char*)start), obj, kString_PdfObjectType);
}
- static void makeString(unsigned char* start, unsigned char* end, SkPdfObject* obj) {
+ static void makeString(const unsigned char* start, const unsigned char* end, SkPdfObject* obj) {
makeStringCore(start, end - start, obj, kString_PdfObjectType);
}
- static void makeString(unsigned char* start, size_t bytes, SkPdfObject* obj) {
+ static void makeString(const unsigned char* start, size_t bytes, SkPdfObject* obj) {
makeStringCore(start, bytes, obj, kString_PdfObjectType);
}
- static void makeHexString(unsigned char* start, SkPdfObject* obj) {
+ static void makeHexString(const unsigned char* start, SkPdfObject* obj) {
makeStringCore(start, strlen((const char*)start), obj, kHexString_PdfObjectType);
}
- static void makeHexString(unsigned char* start, unsigned char* end, SkPdfObject* obj) {
+ static void makeHexString(const unsigned char* start, const unsigned char* end, SkPdfObject* obj) {
makeStringCore(start, end - start, obj, kHexString_PdfObjectType);
}
- static void makeHexString(unsigned char* start, size_t bytes, SkPdfObject* obj) {
+ static void makeHexString(const unsigned char* start, size_t bytes, SkPdfObject* obj) {
makeStringCore(start, bytes, obj, kHexString_PdfObjectType);
}
- static void makeName(unsigned char* start, SkPdfObject* obj) {
+ static void makeName(const unsigned char* start, SkPdfObject* obj) {
makeStringCore(start, strlen((const char*)start), obj, kName_PdfObjectType);
}
- static void makeName(unsigned char* start, unsigned char* end, SkPdfObject* obj) {
+ static void makeName(const unsigned char* start, const unsigned char* end, SkPdfObject* obj) {
makeStringCore(start, end - start, obj, kName_PdfObjectType);
}
- static void makeName(unsigned char* start, size_t bytes, SkPdfObject* obj) {
+ static void makeName(const unsigned char* start, size_t bytes, SkPdfObject* obj) {
makeStringCore(start, bytes, obj, kName_PdfObjectType);
}
- static void makeKeyword(unsigned char* start, SkPdfObject* obj) {
+ static void makeKeyword(const unsigned char* start, SkPdfObject* obj) {
makeStringCore(start, strlen((const char*)start), obj, kKeyword_PdfObjectType);
}
- static void makeKeyword(unsigned char* start, unsigned char* end, SkPdfObject* obj) {
+ static void makeKeyword(const unsigned char* start, const unsigned char* end, SkPdfObject* obj) {
makeStringCore(start, end - start, obj, kKeyword_PdfObjectType);
}
- static void makeKeyword(unsigned char* start, size_t bytes, SkPdfObject* obj) {
+ static void makeKeyword(const unsigned char* start, size_t bytes, SkPdfObject* obj) {
makeStringCore(start, bytes, obj, kKeyword_PdfObjectType);
}
@@ -381,8 +381,8 @@ public:
return false;
}
- // we rewrite all delimiters and white spaces with '\0', so we expect the end of name to be '\0'
- SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0');
+ //// we rewrite all delimiters and white spaces with '\0', so we expect the end of name to be '\0'
+ //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0');
return set(key->fStr.fBuffer, key->fStr.fBytes, value);
}
@@ -411,7 +411,7 @@ public:
return NULL;
}
- SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0');
+ //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0');
return get(key->fStr.fBuffer, key->fStr.fBytes);
}
@@ -441,7 +441,7 @@ public:
return NULL;
}
- SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0');
+ //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0');
return get(key->fStr.fBuffer, key->fStr.fBytes);
}
@@ -637,6 +637,19 @@ public:
return (const char*)fStr.fBuffer;
}
+ inline NotOwnedString strRef() {
+ switch (fObjectType) {
+ case kString_PdfObjectType:
+ case kHexString_PdfObjectType:
+ case kKeyword_PdfObjectType:
+ return fStr;
+
+ default:
+ // TODO(edisonn): report/warning
+ return NotOwnedString();
+ }
+ }
+
// TODO(edisonn): nameValue2 and stringValue2 are used to make code generation easy,
// but it is not a performat way to do it, since it will create an extra copy
// remove these functions and make code generated faster
@@ -647,7 +660,7 @@ public:
// TODO(edisonn): log err
return "";
}
- return (const char*)fStr.fBuffer;
+ return std::string((const char*)fStr.fBuffer, fStr.fBytes);
}
inline std::string stringValue2() const {
@@ -657,7 +670,7 @@ public:
// TODO(edisonn): log err
return "";
}
- return (const char*)fStr.fBuffer;
+ return std::string((const char*)fStr.fBuffer, fStr.fBytes);
}
inline bool boolValue() const {
@@ -713,23 +726,23 @@ public:
return SkMatrixFromPdfMatrix(array);
}
- bool filterStream(SkPdfAllocator* allocator);
+ bool filterStream();
- bool GetFilteredStreamRef(unsigned char** buffer, size_t* len, SkPdfAllocator* allocator) {
+ bool GetFilteredStreamRef(unsigned char const** buffer, size_t* len) {
// TODO(edisonn): add params that couls let the last filter in place if it is jpeg or png to fast load images
if (!hasStream()) {
return false;
}
- filterStream(allocator);
+ filterStream();
if (buffer) {
*buffer = fStr.fBuffer;
}
if (len) {
- *len = fStr.fBytes >> 1; // last bit
+ *len = fStr.fBytes >> 2; // last 2 bits
}
return true;
@@ -739,7 +752,11 @@ public:
return hasStream() && ((fStr.fBytes & 1) == kFilteredStreamBit);
}
- bool GetUnfilteredStreamRef(unsigned char** buffer, size_t* len) const {
+ bool isStreamOwned() const {
+ return hasStream() && ((fStr.fBytes & 2) == kOwnedStreamBit);
+ }
+
+ bool GetUnfilteredStreamRef(unsigned char const** buffer, size_t* len) const {
if (isStreamFiltered()) {
return false;
}
@@ -753,13 +770,13 @@ public:
}
if (len) {
- *len = fStr.fBytes >> 1; // remove slast bit
+ *len = fStr.fBytes >> 2; // remove last 2 bits
}
return true;
}
- bool addStream(unsigned char* buffer, size_t len) {
+ bool addStream(const unsigned char* buffer, size_t len) {
SkASSERT(!hasStream());
SkASSERT(isDictionary());
@@ -849,15 +866,15 @@ public:
}
private:
- static void makeStringCore(unsigned char* start, SkPdfObject* obj, ObjectType type) {
+ static void makeStringCore(const unsigned char* start, SkPdfObject* obj, ObjectType type) {
makeStringCore(start, strlen((const char*)start), obj, type);
}
- static void makeStringCore(unsigned char* start, unsigned char* end, SkPdfObject* obj, ObjectType type) {
+ static void makeStringCore(const unsigned char* start, const unsigned char* end, SkPdfObject* obj, ObjectType type) {
makeStringCore(start, end - start, obj, type);
}
- static void makeStringCore(unsigned char* start, size_t bytes, SkPdfObject* obj, ObjectType type) {
+ static void makeStringCore(const unsigned char* start, size_t bytes, SkPdfObject* obj, ObjectType type) {
SkASSERT(obj->fObjectType == kInvalid_PdfObjectType);
obj->fObjectType = type;
@@ -865,9 +882,9 @@ private:
obj->fStr.fBytes = bytes;
}
- bool applyFilter(const char* name, SkPdfAllocator* allocator);
- bool applyFlateDecodeFilter(SkPdfAllocator* allocator);
- bool applyDCTDecodeFilter(SkPdfAllocator* allocator);
+ bool applyFilter(const char* name);
+ bool applyFlateDecodeFilter();
+ bool applyDCTDecodeFilter();
};
class SkPdfStream : public SkPdfObject {};
@@ -880,11 +897,11 @@ class SkPdfNumber : public SkPdfObject {};
class SkPdfName : public SkPdfObject {
SkPdfName() : SkPdfObject() {
- SkPdfObject::makeName((unsigned char*)"", this);
+ SkPdfObject::makeName((const unsigned char*)"", this);
}
public:
SkPdfName(char* name) : SkPdfObject() {
- this->makeName((unsigned char*)name, this);
+ this->makeName((const unsigned char*)name, this);
}
};