blob: e5ec11cd2fd6df9efac5910796966e8c16f021f6 [file] [log] [blame]
halcanaryf12a1672015-09-23 12:45:49 -07001/*
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 Lubicka74a9882023-09-06 12:58:33 +00007#include "include/core/SkTypes.h"
8
9#ifdef SK_SUPPORT_PDF
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkData.h"
Kevin Lubick8c73a592022-10-17 15:25:35 -040011#include "include/core/SkDocument.h"
12#include "include/core/SkRefCnt.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkStream.h"
Kevin Lubick8c73a592022-10-17 15:25:35 -040014#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/docs/SkPDFDocument.h"
Kevin Lubicka74a9882023-09-06 12:58:33 +000016#include "src/pdf/SkPDFUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "tests/Test.h"
halcanaryf12a1672015-09-23 12:45:49 -070018
Kevin Lubick8c73a592022-10-17 15:25:35 -040019#include <cstdint>
20#include <cstring>
21
halcanary4b656662016-04-27 07:45:18 -070022DEF_TEST(SkPDF_Metadata, r) {
23 REQUIRE_PDF_DOCUMENT(SkPDF_Metadata, r);
Kevin Lubicka74a9882023-09-06 12:58:33 +000024 SkPDF::DateTime now;
25 SkPDFUtils::GetDateTime(&now);
Hal Canary23564b92018-09-07 14:33:14 -040026 SkPDF::Metadata metadata;
halcanary4b656662016-04-27 07:45:18 -070027 metadata.fTitle = "A1";
28 metadata.fAuthor = "A2";
29 metadata.fSubject = "A3";
30 metadata.fKeywords = "A4";
31 metadata.fCreator = "A5";
Hal Canary23564b92018-09-07 14:33:14 -040032 metadata.fCreation = now;
33 metadata.fModified = now;
halcanary4b656662016-04-27 07:45:18 -070034
35 SkDynamicMemoryWStream pdf;
Hal Canary3026d4b2019-01-07 10:00:48 -050036 auto doc = SkPDF::MakeDocument(&pdf, metadata);
halcanaryf12a1672015-09-23 12:45:49 -070037 doc->beginPage(612.0f, 792.0f);
38 doc->close();
reed42943c82016-09-12 12:01:44 -070039 sk_sp<SkData> data = pdf.detachAsData();
halcanaryf12a1672015-09-23 12:45:49 -070040 static const char* expectations[] = {
41 "/Title (A1)",
42 "/Author (A2)",
43 "/Subject (A3)",
44 "/Keywords (A4)",
45 "/Creator (A5)",
halcanaryffe54002016-03-29 09:09:29 -070046 "/Producer (Skia/PDF ",
halcanaryf12a1672015-09-23 12:45:49 -070047 "/CreationDate (D:",
48 "/ModDate (D:"
49 };
halcanary4b656662016-04-27 07:45:18 -070050 const uint8_t* bytes = data->bytes();
halcanaryf12a1672015-09-23 12:45:49 -070051 for (const char* expectation : expectations) {
halcanary4b656662016-04-27 07:45:18 -070052 size_t len = strlen(expectation);
halcanaryf12a1672015-09-23 12:45:49 -070053 bool found = false;
halcanary4b656662016-04-27 07:45:18 -070054 size_t N = 1 + data->size() - len;
halcanaryf12a1672015-09-23 12:45:49 -070055 for (size_t i = 0; i < N; ++i) {
halcanary4b656662016-04-27 07:45:18 -070056 if (0 == memcmp(bytes + i, expectation, len)) {
halcanaryf12a1672015-09-23 12:45:49 -070057 found = true;
58 break;
59 }
60 }
61 if (!found) {
62 ERRORF(r, "expectation missing: '%s'.", expectation);
63 }
64 }
65}
Kevin Lubicka74a9882023-09-06 12:58:33 +000066#endif