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