PDFDocumentTest.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright 2013 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "tests/Test.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkExecutor.h"
  10. #include "include/core/SkStream.h"
  11. #include "include/docs/SkPDFDocument.h"
  12. #include "src/core/SkOSFile.h"
  13. #include "src/utils/SkOSPath.h"
  14. #include "tools/Resources.h"
  15. #include "tools/ToolUtils.h"
  16. static void test_empty(skiatest::Reporter* reporter) {
  17. SkDynamicMemoryWStream stream;
  18. auto doc = SkPDF::MakeDocument(&stream);
  19. doc->close();
  20. REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
  21. }
  22. static void test_abort(skiatest::Reporter* reporter) {
  23. SkDynamicMemoryWStream stream;
  24. auto doc = SkPDF::MakeDocument(&stream);
  25. SkCanvas* canvas = doc->beginPage(100, 100);
  26. canvas->drawColor(SK_ColorRED);
  27. doc->endPage();
  28. doc->abort();
  29. // Test that only the header is written, not the full document.
  30. REPORTER_ASSERT(reporter, stream.bytesWritten() < 256);
  31. }
  32. static void test_abortWithFile(skiatest::Reporter* reporter) {
  33. SkString tmpDir = skiatest::GetTmpDir();
  34. if (tmpDir.isEmpty()) {
  35. ERRORF(reporter, "missing tmpDir.");
  36. return;
  37. }
  38. SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
  39. if (!SkFILEWStream(path.c_str()).isValid()) {
  40. ERRORF(reporter, "unable to write to: %s", path.c_str());
  41. return;
  42. }
  43. // Make sure doc's destructor is called to flush.
  44. {
  45. SkFILEWStream stream(path.c_str());
  46. auto doc = SkPDF::MakeDocument(&stream);
  47. SkCanvas* canvas = doc->beginPage(100, 100);
  48. canvas->drawColor(SK_ColorRED);
  49. doc->endPage();
  50. doc->abort();
  51. }
  52. FILE* file = fopen(path.c_str(), "r");
  53. // Test that only the header is written, not the full document.
  54. char buffer[256];
  55. REPORTER_ASSERT(reporter, fread(buffer, 1, sizeof(buffer), file) < sizeof(buffer));
  56. fclose(file);
  57. }
  58. static void test_file(skiatest::Reporter* reporter) {
  59. SkString tmpDir = skiatest::GetTmpDir();
  60. if (tmpDir.isEmpty()) {
  61. ERRORF(reporter, "missing tmpDir.");
  62. return;
  63. }
  64. SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
  65. if (!SkFILEWStream(path.c_str()).isValid()) {
  66. ERRORF(reporter, "unable to write to: %s", path.c_str());
  67. return;
  68. }
  69. {
  70. SkFILEWStream stream(path.c_str());
  71. auto doc = SkPDF::MakeDocument(&stream);
  72. SkCanvas* canvas = doc->beginPage(100, 100);
  73. canvas->drawColor(SK_ColorRED);
  74. doc->endPage();
  75. doc->close();
  76. }
  77. FILE* file = fopen(path.c_str(), "r");
  78. REPORTER_ASSERT(reporter, file != nullptr);
  79. char header[100];
  80. REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0);
  81. REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
  82. fclose(file);
  83. }
  84. static void test_close(skiatest::Reporter* reporter) {
  85. SkDynamicMemoryWStream stream;
  86. auto doc = SkPDF::MakeDocument(&stream);
  87. SkCanvas* canvas = doc->beginPage(100, 100);
  88. canvas->drawColor(SK_ColorRED);
  89. doc->endPage();
  90. doc->close();
  91. REPORTER_ASSERT(reporter, stream.bytesWritten() != 0);
  92. }
  93. DEF_TEST(SkPDF_document_tests, reporter) {
  94. REQUIRE_PDF_DOCUMENT(document_tests, reporter);
  95. test_empty(reporter);
  96. test_abort(reporter);
  97. test_abortWithFile(reporter);
  98. test_file(reporter);
  99. test_close(reporter);
  100. }
  101. DEF_TEST(SkPDF_document_skbug_4734, r) {
  102. REQUIRE_PDF_DOCUMENT(SkPDF_document_skbug_4734, r);
  103. SkDynamicMemoryWStream stream;
  104. auto doc = SkPDF::MakeDocument(&stream);
  105. SkCanvas* canvas = doc->beginPage(64, 64);
  106. canvas->scale(10000.0f, 10000.0f);
  107. canvas->translate(20.0f, 10.0f);
  108. canvas->rotate(30.0f);
  109. const char text[] = "HELLO";
  110. canvas->drawString(text, 0, 0, SkFont(), SkPaint());
  111. }
  112. static bool contains(const uint8_t* result, size_t size, const char expectation[]) {
  113. size_t len = strlen(expectation);
  114. size_t N = 1 + size - len;
  115. for (size_t i = 0; i < N; ++i) {
  116. if (0 == memcmp(result + i, expectation, len)) {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. // verify that the PDFA flag does something.
  123. DEF_TEST(SkPDF_pdfa_document, r) {
  124. REQUIRE_PDF_DOCUMENT(SkPDF_pdfa_document, r);
  125. SkPDF::Metadata pdfMetadata;
  126. pdfMetadata.fTitle = "test document";
  127. pdfMetadata.fCreation = {0, 1999, 12, 5, 31, 23, 59, 59};
  128. pdfMetadata.fPDFA = true;
  129. SkDynamicMemoryWStream buffer;
  130. auto doc = SkPDF::MakeDocument(&buffer, pdfMetadata);
  131. doc->beginPage(64, 64)->drawColor(SK_ColorRED);
  132. doc->close();
  133. sk_sp<SkData> data(buffer.detachAsData());
  134. static const char* expectations[] = {
  135. "sRGB IEC61966-2.1",
  136. "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">test document",
  137. "<xmp:CreateDate>1999-12-31T23:59:59+00:00</xmp:CreateDate>",
  138. "/Subtype /XML",
  139. "/CreationDate (D:19991231235959+00'00')>>",
  140. };
  141. for (const char* expectation : expectations) {
  142. if (!contains(data->bytes(), data->size(), expectation)) {
  143. ERRORF(r, "PDFA expectation missing: '%s'.", expectation);
  144. }
  145. }
  146. pdfMetadata.fProducer = "phoney library";
  147. pdfMetadata.fPDFA = true;
  148. doc = SkPDF::MakeDocument(&buffer, pdfMetadata);
  149. doc->beginPage(64, 64)->drawColor(SK_ColorRED);
  150. doc->close();
  151. data = buffer.detachAsData();
  152. static const char* moreExpectations[] = {
  153. "/Producer (phoney library)",
  154. "/ProductionLibrary (Skia/PDF m",
  155. "<!-- <skia:ProductionLibrary>Skia/PDF m",
  156. "<pdf:Producer>phoney library</pdf:Producer>",
  157. };
  158. for (const char* expectation : moreExpectations) {
  159. if (!contains(data->bytes(), data->size(), expectation)) {
  160. ERRORF(r, "PDFA expectation missing: '%s'.", expectation);
  161. }
  162. }
  163. }
  164. DEF_TEST(SkPDF_unicode_metadata, r) {
  165. REQUIRE_PDF_DOCUMENT(SkPDF_unicode_metadata, r);
  166. SkPDF::Metadata pdfMetadata;
  167. pdfMetadata.fTitle = "𝓐𝓑𝓒𝓓𝓔 𝓕𝓖𝓗𝓘𝓙"; // Out of basic multilingual plane
  168. pdfMetadata.fAuthor = "ABCDE FGHIJ"; // ASCII
  169. pdfMetadata.fSubject = "αβγδε ζηθικ"; // inside basic multilingual plane
  170. pdfMetadata.fPDFA = true;
  171. SkDynamicMemoryWStream wStream;
  172. {
  173. auto doc = SkPDF::MakeDocument(&wStream, pdfMetadata);
  174. doc->beginPage(612, 792)->drawColor(SK_ColorCYAN);
  175. }
  176. sk_sp<SkData> data(wStream.detachAsData());
  177. static const char* expectations[] = {
  178. "<</Title <FEFFD835DCD0D835DCD1D835DCD2D835DCD3D835DCD40020"
  179. "D835DCD5D835DCD6D835DCD7D835DCD8D835DCD9>",
  180. "/Author (ABCDE FGHIJ)",
  181. "Subject <FEFF03B103B203B303B403B5002003B603B703B803B903BA>",
  182. };
  183. for (const char* expectation : expectations) {
  184. if (!contains(data->bytes(), data->size(), expectation)) {
  185. ERRORF(r, "PDF expectation missing: '%s'.", expectation);
  186. }
  187. }
  188. }
  189. // Make sure we excercise the multi-page functionality without problems.
  190. // Add this to args.gn to output the PDF to a file:
  191. // extra_cflags = [ "-DSK_PDF_TEST_MULTIPAGE=\"/tmp/skpdf_test_multipage.pdf\"" ]
  192. DEF_TEST(SkPDF_multiple_pages, r) {
  193. REQUIRE_PDF_DOCUMENT(SkPDF_multiple_pages, r);
  194. int n = 100;
  195. #ifdef SK_PDF_TEST_MULTIPAGE
  196. SkFILEWStream wStream(SK_PDF_TEST_MULTIPAGE);
  197. #else
  198. SkDynamicMemoryWStream wStream;
  199. #endif
  200. auto doc = SkPDF::MakeDocument(&wStream);
  201. for (int i = 0; i < n; ++i) {
  202. doc->beginPage(612, 792)->drawColor(
  203. SkColorSetARGB(0xFF, 0x00, (uint8_t)(255.0f * i / (n - 1)), 0x00));
  204. }
  205. }
  206. // Test to make sure that jobs launched by PDF backend don't cause a segfault
  207. // after calling abort().
  208. DEF_TEST(SkPDF_abort_jobs, rep) {
  209. REQUIRE_PDF_DOCUMENT(SkPDF_abort_jobs, rep);
  210. SkBitmap b;
  211. b.allocN32Pixels(612, 792);
  212. b.eraseColor(0x4F9643A0);
  213. SkPDF::Metadata metadata;
  214. std::unique_ptr<SkExecutor> executor = SkExecutor::MakeFIFOThreadPool();
  215. metadata.fExecutor = executor.get();
  216. SkNullWStream dst;
  217. auto doc = SkPDF::MakeDocument(&dst, metadata);
  218. doc->beginPage(612, 792)->drawBitmap(b, 0, 0);
  219. doc->abort();
  220. }