halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
Kevin Lubick | a74a988 | 2023-09-06 12:58:33 +0000 | [diff] [blame] | 7 | #include "include/core/SkTypes.h" |
| 8 | |
| 9 | #ifdef SK_SUPPORT_PDF |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkData.h" |
Kevin Lubick | 8c73a59 | 2022-10-17 15:25:35 -0400 | [diff] [blame] | 11 | #include "include/core/SkDocument.h" |
| 12 | #include "include/core/SkRefCnt.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkStream.h" |
Kevin Lubick | 8c73a59 | 2022-10-17 15:25:35 -0400 | [diff] [blame] | 14 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/docs/SkPDFDocument.h" |
Kevin Lubick | a74a988 | 2023-09-06 12:58:33 +0000 | [diff] [blame] | 16 | #include "src/pdf/SkPDFUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "tests/Test.h" |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 18 | |
Kevin Lubick | 8c73a59 | 2022-10-17 15:25:35 -0400 | [diff] [blame] | 19 | #include <cstdint> |
| 20 | #include <cstring> |
| 21 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 22 | DEF_TEST(SkPDF_Metadata, r) { |
| 23 | REQUIRE_PDF_DOCUMENT(SkPDF_Metadata, r); |
Kevin Lubick | a74a988 | 2023-09-06 12:58:33 +0000 | [diff] [blame] | 24 | SkPDF::DateTime now; |
| 25 | SkPDFUtils::GetDateTime(&now); |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 26 | SkPDF::Metadata metadata; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 27 | metadata.fTitle = "A1"; |
| 28 | metadata.fAuthor = "A2"; |
| 29 | metadata.fSubject = "A3"; |
| 30 | metadata.fKeywords = "A4"; |
| 31 | metadata.fCreator = "A5"; |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 32 | metadata.fCreation = now; |
| 33 | metadata.fModified = now; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 34 | |
| 35 | SkDynamicMemoryWStream pdf; |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 36 | auto doc = SkPDF::MakeDocument(&pdf, metadata); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 37 | doc->beginPage(612.0f, 792.0f); |
| 38 | doc->close(); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 39 | sk_sp<SkData> data = pdf.detachAsData(); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 40 | static const char* expectations[] = { |
| 41 | "/Title (A1)", |
| 42 | "/Author (A2)", |
| 43 | "/Subject (A3)", |
| 44 | "/Keywords (A4)", |
| 45 | "/Creator (A5)", |
halcanary | ffe5400 | 2016-03-29 09:09:29 -0700 | [diff] [blame] | 46 | "/Producer (Skia/PDF ", |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 47 | "/CreationDate (D:", |
| 48 | "/ModDate (D:" |
| 49 | }; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 50 | const uint8_t* bytes = data->bytes(); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 51 | for (const char* expectation : expectations) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 52 | size_t len = strlen(expectation); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 53 | bool found = false; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 54 | size_t N = 1 + data->size() - len; |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 55 | for (size_t i = 0; i < N; ++i) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 56 | if (0 == memcmp(bytes + i, expectation, len)) { |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 57 | found = true; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | if (!found) { |
| 62 | ERRORF(r, "expectation missing: '%s'.", expectation); |
| 63 | } |
| 64 | } |
| 65 | } |
Kevin Lubick | a74a988 | 2023-09-06 12:58:33 +0000 | [diff] [blame] | 66 | #endif |