OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkPdfContext.h" | 8 #include "SkPdfContext.h" |
9 #include "SkPdfNativeTokenizer.h" | 9 #include "SkPdfNativeDoc.h" |
10 #include "SkPdfReporter.h" | |
11 #include "SkPdfTokenLooper.h" | |
12 | |
13 /////////////////////////////////////////////////////////////////////////////// | |
14 | |
15 class PdfMainLooper : public SkPdfTokenLooper { | |
16 public: | |
17 PdfMainLooper(SkPdfTokenLooper* parent, | |
18 SkPdfNativeTokenizer* tokenizer, | |
19 SkPdfContext* pdfContext, | |
20 SkCanvas* canvas) | |
21 : SkPdfTokenLooper(parent, tokenizer, pdfContext, canvas) {} | |
22 | |
23 virtual SkPdfResult consumeToken(PdfToken& token); | |
24 virtual void loop(); | |
25 }; | |
26 | |
27 /////////////////////////////////////////////////////////////////////////////// | |
10 | 28 |
11 SkPdfContext::SkPdfContext(SkPdfNativeDoc* doc) | 29 SkPdfContext::SkPdfContext(SkPdfNativeDoc* doc) |
12 : fPdfDoc(doc) | 30 : fPdfDoc(doc) |
13 , fTmpPageAllocator(new SkPdfAllocator()) { | 31 { |
32 SkASSERT(fPdfDoc != NULL); | |
14 } | 33 } |
15 | 34 |
16 SkPdfContext::~SkPdfContext() { | 35 void SkPdfContext::parseStream(SkPdfNativeObject* stream, SkCanvas* canvas) { |
17 delete fTmpPageAllocator; | 36 SkPdfNativeTokenizer* tokenizer = fPdfDoc->tokenizerOfStream(stream, &fTmpPa geAllocator); |
37 if (NULL == tokenizer) { | |
38 // Nothing to parse. | |
39 return; | |
40 } | |
41 PdfMainLooper looper(NULL, tokenizer, this, canvas); | |
42 looper.loop(); | |
43 // FIXME: SkDELETE, once tokenizerOfStream uses SkNEW. | |
mtklein
2013/11/22 14:40:20
Or really, SkAutoTDelete for the whole thing?
scroggo
2013/11/22 15:03:00
In a future CL, I put the object on the stack (cou
| |
44 delete tokenizer; | |
18 } | 45 } |
46 | |
47 /////////////////////////////////////////////////////////////////////////////// | |
48 | |
49 // FIXME (scroggo): This probably belongs in a debugging file. | |
50 // For reportRenderStats declaration. | |
51 #include "SkPdfRenderer.h" | |
52 | |
53 // Temp code to measure what operands fail. | |
54 template <typename T> class SkTDictWithDefaultConstructor : public SkTDict<T> { | |
55 public: | |
56 SkTDictWithDefaultConstructor() : SkTDict<T>(10) {} | |
57 }; | |
58 | |
59 SkTDictWithDefaultConstructor<int> gRenderStats[kCount_SkPdfResult]; | |
60 | |
61 const char* gRenderStatsNames[kCount_SkPdfResult] = { | |
62 "Success", | |
63 "Partially implemented", | |
64 "Not yet implemented", | |
65 "Ignore Error", | |
66 "Error", | |
67 "Unsupported/Unknown" | |
68 }; | |
69 | |
70 void reportPdfRenderStats() { | |
mtklein
2013/11/22 14:40:20
can we make this guy static and report_pdf_render_
scroggo
2013/11/22 15:03:00
It cannot be static, since we need to access it fr
| |
71 for (int i = 0 ; i < kCount_SkPdfResult; i++) { | |
72 SkTDict<int>::Iter iter(gRenderStats[i]); | |
73 const char* key; | |
74 int value = 0; | |
75 while ((key = iter.next(&value)) != NULL) { | |
76 printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value); | |
77 } | |
78 } | |
79 } | |
80 | |
81 #include "SkPdfOps.h" | |
82 | |
83 SkPdfResult PdfMainLooper::consumeToken(PdfToken& token) { | |
84 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256) | |
85 { | |
86 PdfOperatorRenderer pdfOperatorRenderer = NULL; | |
87 if (gPdfOps.find(token.fKeyword, token.fKeywordLength, &pdfOperatorRende rer) && | |
88 pdfOperatorRenderer) { | |
89 SkPdfTokenLooper* childLooper = NULL; | |
90 // Main work is done by pdfOperatorRenderer(...) | |
91 SkPdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &chil dLooper); | |
92 | |
93 int cnt = 0; | |
94 gRenderStats[result].find(token.fKeyword, token.fKeywordLength, &cnt ); | |
95 gRenderStats[result].set(token.fKeyword, token.fKeywordLength, cnt + 1); | |
96 if (childLooper) { | |
97 childLooper->setUp(this); | |
98 childLooper->loop(); | |
99 delete childLooper; | |
mtklein
2013/11/22 14:40:20
It'd be nice if we could get this to be an SkAutoT
scroggo
2013/11/22 15:03:00
Agreed. I added a fixme, so I can leave the code a
| |
100 } | |
101 } else { | |
102 int cnt = 0; | |
103 gRenderStats[kUnsupported_SkPdfResult].find(token.fKeyword, | |
104 token.fKeywordLength, | |
105 &cnt); | |
106 gRenderStats[kUnsupported_SkPdfResult].set(token.fKeyword, | |
107 token.fKeywordLength, | |
108 cnt + 1); | |
109 } | |
110 } | |
111 else if (token.fType == kObject_TokenType) | |
112 { | |
113 fPdfContext->fObjectStack.push( token.fObject ); | |
114 } | |
115 else { | |
116 // TODO(edisonn): store the keyword as a object, so we can track the loc ation in file, | |
117 // and report where the error was triggered | |
118 SkPdfReport(kCodeWarning_SkPdfIssueSeverity, kNYI_SkPdfIssue, token.fKey word, NULL, | |
119 fPdfContext); | |
120 return kIgnoreError_SkPdfResult; | |
121 } | |
122 return kOK_SkPdfResult; | |
123 } | |
124 | |
125 void PdfMainLooper::loop() { | |
126 PdfToken token; | |
127 // readToken defined in SkPdfTokenLooper.h | |
128 while (readToken(fTokenizer, &token)) { | |
mtklein
2013/11/22 14:40:20
hmm, it wants to be named ReadToken then right?
I
scroggo
2013/11/22 15:03:00
The next patch (https://codereview.chromium.org/80
| |
129 this->consumeToken(token); | |
130 } | |
131 } | |
OLD | NEW |