Flutter Engine
The Flutter Engine
PDFTaggedLinkTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 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 */
8
9#ifdef SK_SUPPORT_PDF
13#include "include/core/SkData.h"
15#include "include/core/SkFont.h"
17#include "include/core/SkRect.h"
20#include "include/core/SkSize.h"
25#include "src/pdf/SkPDFUtils.h"
26#include "tests/Test.h"
28
29#include <memory>
30#include <utility>
31#include <vector>
32
33using PDFTag = SkPDF::StructureElementNode;
34
35// Test building a tagged PDF with links.
36// Add this to args.gn to output the PDF to a file:
37// extra_cflags = [ "-DSK_PDF_TEST_TAGS_OUTPUT_PATH=\"/tmp/links.pdf\"" ]
38DEF_TEST(SkPDF_tagged_links, r) {
39 REQUIRE_PDF_DOCUMENT(SkPDF_tagged_links, r);
40#ifdef SK_PDF_TEST_TAGS_OUTPUT_PATH
41 SkFILEWStream outputStream(SK_PDF_TEST_TAGS_OUTPUT_PATH);
42#else
43 SkDynamicMemoryWStream outputStream;
44#endif
45
46 SkSize pageSize = SkSize::Make(612, 792); // U.S. Letter
47
48 SkPDF::Metadata metadata;
49 metadata.fTitle = "Example Tagged PDF With Links";
50 metadata.fCreator = "Skia";
53 metadata.fCreation = now;
54 metadata.fModified = now;
55
56 // The document tag.
57 auto root = std::make_unique<PDFTag>();
58 root->fNodeId = 1;
59 root->fTypeString = "Document";
60 root->fLang = "en-US";
61
62 // A link.
63 auto l1 = std::make_unique<PDFTag>();
64 l1->fNodeId = 2;
65 l1->fTypeString = "Link";
66 root->fChildVector.push_back(std::move(l1));
67
68 metadata.fStructureElementTreeRoot = root.get();
70 &outputStream, metadata);
71
73 paint.setColor(SK_ColorBLUE);
74
75 SkCanvas* canvas =
76 document->beginPage(pageSize.width(),
77 pageSize.height());
79
80 // The node ID should cover both the text and the annotation.
81 SkPDF::SetNodeId(canvas, 2);
82 canvas->drawString("Click to visit Google.com", 72, 72, font, paint);
83 SkRect linkRect = SkRect::MakeXYWH(
86 sk_sp<SkData> url(SkData::MakeWithCString("http://www.google.com"));
87 SkAnnotateRectWithURL(canvas, linkRect, url.get());
88
89 document->endPage();
90 document->close();
91 outputStream.flush();
92}
93
94#endif
SK_API void SkAnnotateRectWithURL(SkCanvas *, const SkRect &, SkData *)
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define DEF_TEST(name, reporter)
Definition: Test.h:312
#define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER)
Definition: Test.h:485
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
static sk_sp< SkData > MakeWithCString(const char cstr[])
Definition: SkData.cpp:195
Definition: SkFont.h:35
virtual void flush()
Definition: SkStream.cpp:77
const Paint & paint
Definition: color_source.cc:38
void GetDateTime(SkPDF::DateTime *)
Definition: SkPDFUtils.cpp:431
SK_API sk_sp< SkDocument > MakeDocument(SkWStream *stream, const Metadata &metadata)
SK_API void SetNodeId(SkCanvas *dst, int nodeID)
sk_sp< SkTypeface > DefaultTypeface()
font
Font Metadata and Metrics.
string root
Definition: scale_cpu.py:20
StructureElementNode * fStructureElementTreeRoot
DateTime fCreation
DateTime fModified
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
Definition: SkSize.h:52
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition: SkSize.h:56
SkScalar width() const
Definition: SkSize.h:76
SkScalar height() const
Definition: SkSize.h:77