Flutter Engine
The Flutter Engine
Functions
SkPDFTypes.cpp File Reference
#include "src/pdf/SkPDFTypes.h"
#include "include/core/SkExecutor.h"
#include "include/core/SkStream.h"
#include "include/core/SkString.h"
#include "include/docs/SkPDFDocument.h"
#include "include/private/base/SkDebug.h"
#include "include/private/base/SkTo.h"
#include "src/base/SkUTF.h"
#include "src/base/SkUtils.h"
#include "src/core/SkStreamPriv.h"
#include "src/pdf/SkDeflate.h"
#include "src/pdf/SkPDFDocumentPriv.h"
#include "src/pdf/SkPDFUnion.h"
#include "src/pdf/SkPDFUtils.h"
#include <cstring>
#include <functional>
#include <new>

Go to the source code of this file.

Functions

static void write_name_escaped (SkWStream *o, const char *name)
 
static void write_literal_byte_string (SkWStream *wStream, const char *cin, size_t len)
 
static void write_hex_byte_string (SkWStream *wStream, const char *cin, size_t len)
 
static void write_optimized_byte_string (SkWStream *wStream, const char *cin, size_t len, size_t literalExtras)
 
static void write_byte_string (SkWStream *wStream, const char *cin, size_t len)
 
static void write_text_string (SkWStream *wStream, const char *cin, size_t len)
 
void SkPDFWriteTextString (SkWStream *wStream, const char *cin, size_t len)
 
void SkPDFWriteByteString (SkWStream *wStream, const char *cin, size_t len)
 
static void serialize_stream (SkPDFDict *origDict, SkStreamAsset *stream, SkPDFSteamCompressionEnabled compress, SkPDFDocument *doc, SkPDFIndirectReference ref)
 
SkPDFIndirectReference SkPDFStreamOut (std::unique_ptr< SkPDFDict > dict, std::unique_ptr< SkStreamAsset > content, SkPDFDocument *doc, SkPDFSteamCompressionEnabled compress)
 

Function Documentation

◆ serialize_stream()

static void serialize_stream ( SkPDFDict origDict,
SkStreamAsset stream,
SkPDFSteamCompressionEnabled  compress,
SkPDFDocument doc,
SkPDFIndirectReference  ref 
)
static

Definition at line 544 of file SkPDFTypes.cpp.

548 {
549 // Code assumes that the stream starts at the beginning.
550 SkASSERT(stream && stream->hasLength());
551
552 std::unique_ptr<SkStreamAsset> tmp;
553 SkPDFDict tmpDict;
554 SkPDFDict& dict = origDict ? *origDict : tmpDict;
555 static const size_t kMinimumSavings = strlen("/Filter_/FlateDecode_");
558 stream->getLength() > kMinimumSavings)
559 {
560 SkDynamicMemoryWStream compressedData;
561 SkDeflateWStream deflateWStream(&compressedData,SkToInt(doc->metadata().fCompressionLevel));
562 SkStreamCopy(&deflateWStream, stream);
563 deflateWStream.finalize();
564 #ifdef SK_PDF_BASE85_BINARY
565 {
566 SkPDFUtils::Base85Encode(compressedData.detachAsStream(), &compressedData);
567 tmp = compressedData.detachAsStream();
568 stream = tmp.get();
569 auto filters = SkPDFMakeArray();
570 filters->appendName("ASCII85Decode");
571 filters->appendName("FlateDecode");
572 dict.insertObject("Filter", std::move(filters));
573 }
574 #else
575 if (stream->getLength() > compressedData.bytesWritten() + kMinimumSavings) {
576 tmp = compressedData.detachAsStream();
577 stream = tmp.get();
578 dict.insertName("Filter", "FlateDecode");
579 } else {
580 SkAssertResult(stream->rewind());
581 }
582 #endif
583
584 }
585 dict.insertInt("Length", stream->getLength());
586 doc->emitStream(dict,
587 [stream](SkWStream* dst) { dst->writeStream(stream, stream->getLength()); },
588 ref);
589}
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
#define SkASSERT(cond)
Definition: SkAssert.h:116
static std::unique_ptr< SkPDFArray > SkPDFMakeArray(Args... args)
Definition: SkPDFTypes.h:125
bool SkStreamCopy(SkWStream *out, SkStream *input)
Definition: SkStream.cpp:954
constexpr int SkToInt(S x)
Definition: SkTo.h:29
size_t bytesWritten() const override
Definition: SkStream.cpp:526
std::unique_ptr< SkStreamAsset > detachAsStream()
Definition: SkStream.cpp:876
void insertName(const char key[], const char nameValue[])
Definition: SkPDFTypes.cpp:512
void insertObject(const char key[], std::unique_ptr< SkPDFObject > &&)
Definition: SkPDFTypes.cpp:484
void insertInt(const char key[], int32_t value)
Definition: SkPDFTypes.cpp:496
const SkPDF::Metadata & metadata() const
void emitStream(const SkPDFDict &dict, T writeStream, SkPDFIndirectReference ref)
dst
Definition: cp.py:12
enum SkPDF::Metadata::CompressionLevel fCompressionLevel

◆ SkPDFStreamOut()

SkPDFIndirectReference SkPDFStreamOut ( std::unique_ptr< SkPDFDict dict,
std::unique_ptr< SkStreamAsset content,
SkPDFDocument doc,
SkPDFSteamCompressionEnabled  compress 
)

Definition at line 591 of file SkPDFTypes.cpp.

594 {
596 if (SkExecutor* executor = doc->executor()) {
597 SkPDFDict* dictPtr = dict.release();
598 SkStreamAsset* contentPtr = content.release();
599 // Pass ownership of both pointers into a std::function, which should
600 // only be executed once.
601 doc->incrementJobCount();
602 executor->add([dictPtr, contentPtr, compress, doc, ref]() {
603 serialize_stream(dictPtr, contentPtr, compress, doc, ref);
604 delete dictPtr;
605 delete contentPtr;
606 doc->signalJobComplete();
607 });
608 return ref;
609 }
610 serialize_stream(dict.get(), content.get(), compress, doc, ref);
611 return ref;
612}
static void serialize_stream(SkPDFDict *origDict, SkStreamAsset *stream, SkPDFSteamCompressionEnabled compress, SkPDFDocument *doc, SkPDFIndirectReference ref)
Definition: SkPDFTypes.cpp:544
SkExecutor * executor() const
void signalJobComplete()
void incrementJobCount()
SkPDFIndirectReference reserveRef()
union flutter::testing::@2836::KeyboardChange::@76 content

◆ SkPDFWriteByteString()

void SkPDFWriteByteString ( SkWStream wStream,
const char *  cin,
size_t  len 
)

Definition at line 248 of file SkPDFTypes.cpp.

248 {
249 write_byte_string(wStream, cin, len);
250}
static void write_byte_string(SkWStream *wStream, const char *cin, size_t len)
Definition: SkPDFTypes.cpp:176

◆ SkPDFWriteTextString()

void SkPDFWriteTextString ( SkWStream wStream,
const char *  cin,
size_t  len 
)

Definition at line 245 of file SkPDFTypes.cpp.

245 {
246 write_text_string(wStream, cin, len);
247}
static void write_text_string(SkWStream *wStream, const char *cin, size_t len)
Definition: SkPDFTypes.cpp:194

◆ write_byte_string()

static void write_byte_string ( SkWStream wStream,
const char *  cin,
size_t  len 
)
static

Definition at line 176 of file SkPDFTypes.cpp.

176 {
177 SkDEBUGCODE(static const size_t kMaxLen = 65535;)
178 SkASSERT(len <= kMaxLen);
179
180 size_t literalExtras = 0;
181 {
182 for (size_t i = 0; i < len; i++) {
183 uint8_t c = static_cast<uint8_t>(cin[i]);
184 if (c < ' ' || '~' < c) {
185 literalExtras += 3;
186 } else if (c == '\\' || c == '(' || c == ')') {
187 ++literalExtras;
188 }
189 }
190 }
191 write_optimized_byte_string(wStream, cin, len, literalExtras);
192}
static void write_optimized_byte_string(SkWStream *wStream, const char *cin, size_t len, size_t literalExtras)
Definition: SkPDFTypes.cpp:165
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()

◆ write_hex_byte_string()

static void write_hex_byte_string ( SkWStream wStream,
const char *  cin,
size_t  len 
)
static

Definition at line 151 of file SkPDFTypes.cpp.

151 {
152 SkDEBUGCODE(static const size_t kMaxLen = 65535;)
153 SkASSERT(len <= kMaxLen);
154
155 wStream->writeText("<");
156 for (size_t i = 0; i < len; i++) {
157 uint8_t c = static_cast<uint8_t>(cin[i]);
158 char hexValue[2] = { SkHexadecimalDigits::gUpper[c >> 4],
160 wStream->write(hexValue, 2);
161 }
162 wStream->writeText(">");
163}
virtual bool write(const void *buffer, size_t size)=0
bool writeText(const char text[])
Definition: SkStream.h:247
const char gUpper[16]
Definition: SkUtils.cpp:10

◆ write_literal_byte_string()

static void write_literal_byte_string ( SkWStream wStream,
const char *  cin,
size_t  len 
)
static

Definition at line 131 of file SkPDFTypes.cpp.

131 {
132 wStream->writeText("(");
133 for (size_t i = 0; i < len; i++) {
134 uint8_t c = static_cast<uint8_t>(cin[i]);
135 if (c < ' ' || '~' < c) {
136 uint8_t octal[4] = { '\\',
137 (uint8_t)('0' | ( c >> 6 )),
138 (uint8_t)('0' | ((c >> 3) & 0x07)),
139 (uint8_t)('0' | ( c & 0x07)) };
140 wStream->write(octal, 4);
141 } else {
142 if (c == '\\' || c == '(' || c == ')') {
143 wStream->writeText("\\");
144 }
145 wStream->write(&c, 1);
146 }
147 }
148 wStream->writeText(")");
149}

◆ write_name_escaped()

static void write_name_escaped ( SkWStream o,
const char *  name 
)
static

Definition at line 116 of file SkPDFTypes.cpp.

116 {
117 static const char kToEscape[] = "#/%()<>[]{}";
118 for (const uint8_t* n = reinterpret_cast<const uint8_t*>(name); *n; ++n) {
119 uint8_t v = *n;
120 if (v < '!' || v > '~' || strchr(kToEscape, v)) {
121 char buffer[3] = {'#',
124 o->write(buffer, sizeof(buffer));
125 } else {
126 o->write(n, 1);
127 }
128 }
129}
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126

◆ write_optimized_byte_string()

static void write_optimized_byte_string ( SkWStream wStream,
const char *  cin,
size_t  len,
size_t  literalExtras 
)
static

Definition at line 165 of file SkPDFTypes.cpp.

166 {
167 const size_t hexLength = 2 + 2*len;
168 const size_t literalLength = 2 + len + literalExtras;
169 if (literalLength <= hexLength) {
170 write_literal_byte_string(wStream, cin, len);
171 } else {
172 write_hex_byte_string(wStream, cin, len);
173 }
174}
static void write_hex_byte_string(SkWStream *wStream, const char *cin, size_t len)
Definition: SkPDFTypes.cpp:151
static void write_literal_byte_string(SkWStream *wStream, const char *cin, size_t len)
Definition: SkPDFTypes.cpp:131

◆ write_text_string()

static void write_text_string ( SkWStream wStream,
const char *  cin,
size_t  len 
)
static

Definition at line 194 of file SkPDFTypes.cpp.

194 {
195 SkDEBUGCODE(static const size_t kMaxLen = 65535;)
196 SkASSERT(len <= kMaxLen);
197
198 bool inputIsValidUTF8 = true;
199 bool inputIsPDFDocEncoding = true;
200 size_t literalExtras = 0;
201 {
202 const char* textPtr = cin;
203 const char* textEnd = cin + len;
204 while (textPtr < textEnd) {
205 SkUnichar unichar = SkUTF::NextUTF8(&textPtr, textEnd);
206 if (unichar < 0) {
207 inputIsValidUTF8 = false;
208 break;
209 }
210 // See Table D.2 (PDFDocEncoding Character Set) in the PDF3200_2008 spec.
211 // Could convert from UTF-8 to PDFDocEncoding and, if successful, use that.
212 if ((0x15 < unichar && unichar < 0x20) || 0x7E < unichar) {
213 inputIsPDFDocEncoding = false;
214 break;
215 }
216 if (unichar < ' ' || '~' < unichar) {
217 literalExtras += 3;
218 } else if (unichar == '\\' || unichar == '(' || unichar == ')') {
219 ++literalExtras;
220 }
221 }
222 }
223
224 if (!inputIsValidUTF8) {
225 SkDebugf("Invalid UTF8: %.*s\n", (int)len, cin);
226 wStream->writeText("<>");
227 return;
228 }
229
230 if (inputIsPDFDocEncoding) {
231 write_optimized_byte_string(wStream, cin, len, literalExtras);
232 return;
233 }
234
235 wStream->writeText("<FEFF");
236 const char* textPtr = cin;
237 const char* textEnd = cin + len;
238 while (textPtr < textEnd) {
239 SkUnichar unichar = SkUTF::NextUTF8(&textPtr, textEnd);
240 SkPDFUtils::WriteUTF16beHex(wStream, unichar);
241 }
242 wStream->writeText(">");
243}
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
int32_t SkUnichar
Definition: SkTypes.h:175
void WriteUTF16beHex(SkWStream *wStream, SkUnichar utf32)
Definition: SkPDFUtils.h:119
SK_SPI SkUnichar NextUTF8(const char **ptr, const char *end)
Definition: SkUTF.cpp:118