• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
7 #include "include/core/SkTypes.h"
8 
9 #ifdef SK_SUPPORT_PDF
10 #include "include/core/SkAnnotation.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkData.h"
14 #include "include/core/SkDocument.h"
15 #include "include/core/SkFont.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkStream.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkTime.h"
24 #include "include/core/SkTypeface.h"
25 #include "include/docs/SkPDFDocument.h"
26 #include "tests/Test.h"
27 
28 #include <memory>
29 #include <utility>
30 #include <vector>
31 
32 using PDFTag = SkPDF::StructureElementNode;
33 
34 // Test building a tagged PDF with links.
35 // Add this to args.gn to output the PDF to a file:
36 //   extra_cflags = [ "-DSK_PDF_TEST_TAGS_OUTPUT_PATH=\"/tmp/links.pdf\"" ]
DEF_TEST(SkPDF_tagged_links,r)37 DEF_TEST(SkPDF_tagged_links, r) {
38     REQUIRE_PDF_DOCUMENT(SkPDF_tagged_links, r);
39 #ifdef SK_PDF_TEST_TAGS_OUTPUT_PATH
40     SkFILEWStream outputStream(SK_PDF_TEST_TAGS_OUTPUT_PATH);
41 #else
42     SkDynamicMemoryWStream outputStream;
43 #endif
44 
45     SkSize pageSize = SkSize::Make(612, 792);  // U.S. Letter
46 
47     SkPDF::Metadata metadata;
48     metadata.fTitle = "Example Tagged PDF With Links";
49     metadata.fCreator = "Skia";
50     SkTime::DateTime now;
51     SkTime::GetDateTime(&now);
52     metadata.fCreation = now;
53     metadata.fModified = now;
54 
55     // The document tag.
56     auto root = std::make_unique<PDFTag>();
57     root->fNodeId = 1;
58     root->fTypeString = "Document";
59     root->fLang = "en-US";
60 
61     // A link.
62     auto l1 = std::make_unique<PDFTag>();
63     l1->fNodeId = 2;
64     l1->fTypeString = "Link";
65     root->fChildVector.push_back(std::move(l1));
66 
67     metadata.fStructureElementTreeRoot = root.get();
68     sk_sp<SkDocument> document = SkPDF::MakeDocument(
69         &outputStream, metadata);
70 
71     SkPaint paint;
72     paint.setColor(SK_ColorBLUE);
73 
74     SkCanvas* canvas =
75             document->beginPage(pageSize.width(),
76                                 pageSize.height());
77     SkFont font(nullptr, 20);
78 
79     // The node ID should cover both the text and the annotation.
80     SkPDF::SetNodeId(canvas, 2);
81     canvas->drawString("Click to visit Google.com", 72, 72, font, paint);
82     SkRect linkRect = SkRect::MakeXYWH(
83         SkIntToScalar(72), SkIntToScalar(54),
84         SkIntToScalar(218), SkIntToScalar(24));
85     sk_sp<SkData> url(SkData::MakeWithCString("http://www.google.com"));
86     SkAnnotateRectWithURL(canvas, linkRect, url.get());
87 
88     document->endPage();
89     document->close();
90     outputStream.flush();
91 }
92 
93 #endif
94