Flutter Engine
The Flutter Engine
PDFTaggedPruningTest.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/SkFont.h"
15#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
22#include "src/pdf/SkPDFUtils.h"
23#include "tests/Test.h"
25
26#include <memory>
27#include <utility>
28#include <vector>
29
30using PDFTag = SkPDF::StructureElementNode;
31
32// Test building a tagged PDF where nodes are pruned.
33// Add this to args.gn to output the PDF to a file:
34// extra_cflags = [ "-DSK_PDF_TEST_TAGS_OUTPUT_PATH=\"/tmp/pruning.pdf\"" ]
35DEF_TEST(SkPDF_tagged_pruning, r) {
36 REQUIRE_PDF_DOCUMENT(SkPDF_tagged, r);
37#ifdef SK_PDF_TEST_TAGS_OUTPUT_PATH
38 SkFILEWStream outputStream(SK_PDF_TEST_TAGS_OUTPUT_PATH);
39#else
40 SkDynamicMemoryWStream outputStream;
41#endif
42
43 SkSize pageSize = SkSize::Make(612, 792); // U.S. Letter
44
45 SkPDF::Metadata metadata;
46 metadata.fTitle = "Example Tagged PDF";
47 metadata.fCreator = "Skia";
50 metadata.fCreation = now;
51 metadata.fModified = now;
52
53 // The document tag.
54 auto root = std::make_unique<PDFTag>();
55 root->fNodeId = 1;
56 root->fTypeString = "Document";
57 root->fLang = "en-US";
58
59 // First paragraph.
60 auto p1 = std::make_unique<PDFTag>();
61 p1->fNodeId = 2;
62 p1->fAdditionalNodeIds = {3, 4};
63 p1->fTypeString = "P";
64 root->fChildVector.push_back(std::move(p1));
65
66 // Second paragraph.
67 auto p2 = std::make_unique<PDFTag>();
68 p2->fNodeId = 5;
69 p2->fAdditionalNodeIds = {6, 7};
70 p2->fTypeString = "P";
71 root->fChildVector.push_back(std::move(p2));
72
73 metadata.fStructureElementTreeRoot = root.get();
75 &outputStream, metadata);
76
78 paint.setColor(SK_ColorBLACK);
79
80 SkCanvas* canvas =
81 document->beginPage(pageSize.width(),
82 pageSize.height());
84 SkPDF::SetNodeId(canvas, 3);
85 canvas->drawString("First paragraph line 1", 72, 72, font, paint);
86 SkPDF::SetNodeId(canvas, 4);
87 canvas->drawString("First paragraph line 2", 72, 108, font, paint);
88 SkPDF::SetNodeId(canvas, 6);
89 canvas->drawString("Second paragraph line 1", 72, 180, font, paint);
90 SkPDF::SetNodeId(canvas, 7);
91 canvas->drawString("Second paragraph line 2", 72, 216, font, paint);
92
93 document->endPage();
94 document->close();
95 outputStream.flush();
96}
97
98// Similar to SkPDF_tagged_pruning but never actually writes out anything annotated.
99// Ensures that nothing goes wring when there are no annotations on an annotated PDF.
100DEF_TEST(SkPDF_tagged_pruning_empty, r) {
101 REQUIRE_PDF_DOCUMENT(SkPDF_tagged, r);
102#ifdef SK_PDF_TEST_TAGS_OUTPUT_PATH
103 SkFILEWStream outputStream(SK_PDF_TEST_TAGS_OUTPUT_PATH);
104#else
105 SkDynamicMemoryWStream outputStream;
106#endif
107
108 SkSize pageSize = SkSize::Make(612, 792); // U.S. Letter
109
110 SkPDF::Metadata metadata;
111 metadata.fTitle = "Example Tagged PDF";
112 metadata.fCreator = "Skia";
113 SkPDF::DateTime now;
115 metadata.fCreation = now;
116 metadata.fModified = now;
117
118 // The document tag.
119 auto root = std::make_unique<PDFTag>();
120 root->fNodeId = 1;
121 root->fTypeString = "Document";
122 root->fLang = "en-US";
123
124 // First paragraph.
125 auto p1 = std::make_unique<PDFTag>();
126 p1->fNodeId = 2;
127 p1->fAdditionalNodeIds = {3, 4};
128 p1->fTypeString = "P";
129 root->fChildVector.push_back(std::move(p1));
130
131 // Second paragraph.
132 auto p2 = std::make_unique<PDFTag>();
133 p2->fNodeId = 5;
134 p2->fAdditionalNodeIds = {6, 7};
135 p2->fTypeString = "P";
136 root->fChildVector.push_back(std::move(p2));
137
138 metadata.fStructureElementTreeRoot = root.get();
140 &outputStream, metadata);
141
143 paint.setColor(SK_ColorBLACK);
144
145 SkCanvas* canvas =
146 document->beginPage(pageSize.width(),
147 pageSize.height());
148
149 canvas->drawRect(SkRect::MakeXYWH(10, 10, 100, 100), paint);
150
151 document->endPage();
152 document->close();
153 outputStream.flush();
154}
155
156#endif
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
#define DEF_TEST(name, reporter)
Definition: Test.h:312
#define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER)
Definition: Test.h:485
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
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