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 "SkPdfNativeObject.h" | 8 #include "SkPdfNativeObject.h" |
9 | 9 |
| 10 #include "SkBitmap.h" |
| 11 #include "SkFlate.h" |
| 12 #include "SkPdfFont.h" |
| 13 #include "SkPdfNativeTokenizer.h" |
| 14 #include "SkPdfReporter.h" |
| 15 #include "SkStream.h" |
| 16 |
10 // TODO(edisonn): mac builder does not find the header ... but from headers is o
k | 17 // TODO(edisonn): mac builder does not find the header ... but from headers is o
k |
11 //#include "SkPdfStreamCommonDictionary_autogen.h" | 18 //#include "SkPdfStreamCommonDictionary_autogen.h" |
12 #include "SkPdfHeaders_autogen.h" | 19 #include "SkPdfHeaders_autogen.h" |
13 | 20 |
14 #include "SkFlate.h" | |
15 #include "SkStream.h" | |
16 #include "SkPdfNativeTokenizer.h" | |
17 | |
18 #include "SkBitmap.h" | |
19 #include "SkPdfFont.h" | |
20 | |
21 #include "SkPdfReporter.h" | |
22 | 21 |
23 SkPdfNativeObject SkPdfNativeObject::kNull = SkPdfNativeObject::makeNull(); | 22 SkPdfNativeObject SkPdfNativeObject::kNull = SkPdfNativeObject::makeNull(); |
24 | 23 |
25 bool SkPdfNativeObject::applyFlateDecodeFilter() { | 24 bool SkPdfNativeObject::applyFlateDecodeFilter() { |
26 if (!SkFlate::HaveFlate()) { | 25 if (!SkFlate::HaveFlate()) { |
27 SkPdfReport(kIgnoreError_SkPdfIssueSeverity, kNoFlateLibrary_SkPdfIssue,
"forgot to link with flate library?", NULL, NULL); | 26 SkPdfReport(kIgnoreError_SkPdfIssueSeverity, kNoFlateLibrary_SkPdfIssue, |
| 27 "forgot to link with flate library?", NULL, NULL); |
28 return false; | 28 return false; |
29 } | 29 } |
30 | 30 |
31 const unsigned char* old = fStr.fBuffer; | 31 const unsigned char* old = fStr.fBuffer; |
32 bool deleteOld = isStreamOwned(); | 32 bool deleteOld = isStreamOwned(); |
33 | 33 |
34 SkMemoryStream skstream(fStr.fBuffer, fStr.fBytes >> 2, false); | 34 SkMemoryStream skstream(fStr.fBuffer, fStr.fBytes >> 2, false); |
35 SkDynamicMemoryWStream uncompressedData; | 35 SkDynamicMemoryWStream uncompressedData; |
36 | 36 |
37 if (SkFlate::Inflate(&skstream, &uncompressedData)) { | 37 if (SkFlate::Inflate(&skstream, &uncompressedData)) { |
38 fStr.fBytes = (uncompressedData.bytesWritten() << 2) + kOwnedStreamBit +
kUnfilteredStreamBit; | 38 fStr.fBytes = (uncompressedData.bytesWritten() << 2) + kOwnedStreamBit + |
| 39 kUnfilteredStreamBit; |
39 fStr.fBuffer = (const unsigned char*)new unsigned char[uncompressedData.
bytesWritten()]; | 40 fStr.fBuffer = (const unsigned char*)new unsigned char[uncompressedData.
bytesWritten()]; |
40 uncompressedData.copyTo((void*)fStr.fBuffer); | 41 uncompressedData.copyTo((void*)fStr.fBuffer); |
41 | 42 |
42 if (deleteOld) { | 43 if (deleteOld) { |
43 delete[] old; | 44 delete[] old; |
44 } | 45 } |
45 | 46 |
46 return true; | 47 return true; |
47 } else { | 48 } else { |
48 SkPdfReport(kIgnoreError_SkPdfIssueSeverity, kBadStream_SkPdfIssue, "inf
late failed", this, NULL); | 49 SkPdfReport(kIgnoreError_SkPdfIssueSeverity, kBadStream_SkPdfIssue, "inf
late failed", this, NULL); |
49 return false; | 50 return false; |
50 } | 51 } |
51 } | 52 } |
52 | 53 |
53 bool SkPdfNativeObject::applyDCTDecodeFilter() { | 54 bool SkPdfNativeObject::applyDCTDecodeFilter() { |
54 // this would fail, and it won't allow any more filters. | 55 // applyDCTDecodeFilter will fail, and it won't allow any more filters. |
55 // technically, it would be possible, but not a real world scenario | 56 // technically, it would be possible, but not a real world scenario. |
56 // TODO(edisonn): or get the image here and store it for fast retrieval? | 57 // in this way we create the image from the DCT stream directly. |
57 return false; | 58 return false; |
58 } | 59 } |
59 | 60 |
60 bool SkPdfNativeObject::applyFilter(const char* name) { | 61 bool SkPdfNativeObject::applyFilter(const char* name) { |
61 if (strcmp(name, "FlateDecode") == 0) { | 62 if (strcmp(name, "FlateDecode") == 0) { |
62 return applyFlateDecodeFilter(); | 63 return applyFlateDecodeFilter(); |
63 } else if (strcmp(name, "DCTDecode") == 0) { | 64 } else if (strcmp(name, "DCTDecode") == 0) { |
64 return applyDCTDecodeFilter(); | 65 return applyDCTDecodeFilter(); |
65 } | 66 } |
66 SkPdfReport(kCodeWarning_SkPdfIssueSeverity, kNYI_SkPdfIssue, "filter not su
pported", this, NULL); | 67 SkPdfReport(kCodeWarning_SkPdfIssueSeverity, kNYI_SkPdfIssue, "filter not su
pported", this, |
| 68 NULL); |
67 return false; | 69 return false; |
68 } | 70 } |
69 | 71 |
70 bool SkPdfNativeObject::filterStream() { | 72 bool SkPdfNativeObject::filterStream() { |
71 SkPdfMarkObjectUsed(); | 73 SkPdfMarkObjectUsed(); |
72 | 74 |
73 if (!hasStream()) { | 75 if (!hasStream()) { |
74 SkPdfReport(kIgnoreError_SkPdfIssueSeverity, kBadStream_SkPdfIssue, "No
Stream", this, NULL); | 76 SkPdfReport(kIgnoreError_SkPdfIssueSeverity, kBadStream_SkPdfIssue, "No
Stream", this, |
| 77 NULL); |
75 return false; | 78 return false; |
76 } | 79 } |
77 | 80 |
78 if (isStreamFiltered()) { | 81 if (isStreamFiltered()) { |
79 return true; | 82 return true; |
80 } | 83 } |
81 | 84 |
82 SkPdfStreamCommonDictionary* stream = (SkPdfStreamCommonDictionary*)this; | 85 SkPdfStreamCommonDictionary* stream = (SkPdfStreamCommonDictionary*)this; |
83 | 86 |
84 if (!stream->has_Filter()) { | 87 if (!stream->has_Filter()) { |
85 fStr.fBytes = ((fStr.fBytes >> 1) << 1) + kFilteredStreamBit; | 88 fStr.fBytes = ((fStr.fBytes >> 1) << 1) + kFilteredStreamBit; |
86 } else if (stream->isFilterAName(NULL)) { | 89 } else if (stream->isFilterAName(NULL)) { |
87 SkString filterName = stream->getFilterAsName(NULL); | 90 SkString filterName = stream->getFilterAsName(NULL); |
88 applyFilter(filterName.c_str()); | 91 applyFilter(filterName.c_str()); |
89 } else if (stream->isFilterAArray(NULL)) { | 92 } else if (stream->isFilterAArray(NULL)) { |
90 const SkPdfArray* filters = stream->getFilterAsArray(NULL); | 93 const SkPdfArray* filters = stream->getFilterAsArray(NULL); |
91 int cnt = filters->size(); | 94 int cnt = filters->size(); |
92 for (int i = cnt - 1; i >= 0; i--) { | 95 for (int i = cnt - 1; i >= 0; i--) { |
93 const SkPdfNativeObject* filterName = filters->objAtAIndex(i); | 96 const SkPdfNativeObject* filterName = filters->objAtAIndex(i); |
94 if (filterName != NULL && filterName->isName()) { | 97 if (filterName != NULL && filterName->isName()) { |
95 if (!applyFilter(filterName->nameValue())) { | 98 if (!applyFilter(filterName->nameValue())) { |
96 break; | 99 break; |
97 } | 100 } |
98 } else { | 101 } else { |
99 SkPdfReport(kIgnoreError_SkPdfIssueSeverity, kIncositentSyntax_S
kPdfIssue, "filter name should be a Name", this, NULL); | 102 SkPdfReport(kIgnoreError_SkPdfIssueSeverity, kIncositentSyntax_S
kPdfIssue, |
| 103 "filter name should be a Name", this, NULL); |
100 } | 104 } |
101 } | 105 } |
102 } | 106 } |
103 | 107 |
104 return true; | 108 return true; |
105 } | 109 } |
106 | 110 |
107 void SkPdfNativeObject::releaseData() { | 111 void SkPdfNativeObject::releaseData() { |
108 #ifdef PDF_TRACK_OBJECT_USAGE | 112 #ifdef PDF_TRACK_OBJECT_USAGE |
109 SkPdfReportIf(!fUsed, kInfo_SkPdfIssueSeverity, kNoIssue_SkPdfIssue, "Unused
object in rendering", this, NULL); | 113 SkPdfReportIf(!fUsed, kInfo_SkPdfIssueSeverity, kNoIssue_SkPdfIssue, |
| 114 "Unused object in rendering", this, NULL); |
110 #endif // PDF_TRACK_OBJECT_USAGE | 115 #endif // PDF_TRACK_OBJECT_USAGE |
111 | 116 |
112 SkPdfMarkObjectUnused(); | 117 SkPdfMarkObjectUnused(); |
113 | 118 |
114 if (fData) { | 119 if (fData) { |
115 switch (fDataType) { | 120 switch (fDataType) { |
116 case kFont_Data: | 121 case kFont_Data: |
117 delete (SkPdfFont*)fData; | 122 delete (SkPdfFont*)fData; |
118 break; | 123 break; |
119 case kBitmap_Data: | 124 case kBitmap_Data: |
120 delete (SkBitmap*)fData; | 125 delete (SkBitmap*)fData; |
121 break; | 126 break; |
122 default: | 127 default: |
123 SkASSERT(false); | 128 SkASSERT(false); |
124 break; | 129 break; |
125 } | 130 } |
126 } | 131 } |
127 fData = NULL; | 132 fData = NULL; |
128 fDataType = kEmpty_Data; | 133 fDataType = kEmpty_Data; |
129 } | 134 } |
OLD | NEW |