Skip to content

Commit 2a016ba

Browse files
minorninthSkia Commit-Bot
authored andcommitted
Allow passing multiple node IDs per PDF structure node.
At the time Chromium is painting, we're passing node IDs along with painting commands to enable tagging. However, this assumes that all nodes will end up in the structure tree, which we might not want. Instead, allow the client to prune the structure tree later before telling Skia to generate the PDF, but keep all of the node IDs to be matched up with. As an example, suppose the doc looks like this: root id=1 paragraph id=2 div id=3 text1 id=4 link id=5 text2 id=6 The pruned tree passed to Skia would look like this: root id=1 paragraph id=2 extra_ids=3,4 link id=5 extra_ids=6 We need to pass the extra node IDs into Skia so that when content is tagged with id=4, we know to map that to the paragraph node with id=2 instead. Note that the resulting PDF document will *not* have any of these extra IDs, they're all remapped and consolidated. While it's not strictly necessary that this is done in Skia, it's easiest to implement it here. Doing the same upstream would require replaying an SkPicture and rewriting all of the node IDs. Bug: chromium:607777 Change-Id: I0ecb62651e60b84cc5b9d053d7f7d3b9efda1470 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272462 Reviewed-by: Ben Wagner <bungeman@google.com> Commit-Queue: Dominic Mazzoni <dmazzoni@chromium.org>
1 parent 3fcf83a commit 2a016ba

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

gn/tests.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ tests_sources = [
169169
"$_tests/PDFMetadataAttributeTest.cpp",
170170
"$_tests/PDFOpaqueSrcModeToSrcOverTest.cpp",
171171
"$_tests/PDFPrimitivesTest.cpp",
172+
"$_tests/PDFTaggedPruningTest.cpp",
172173
"$_tests/PDFTaggedTableTest.cpp",
173174
"$_tests/PDFTaggedTest.cpp",
174175
"$_tests/PackBitsTest.cpp",

include/docs/SkPDFDocument.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct StructureElementNode {
111111
SkString fTypeString;
112112
std::vector<std::unique_ptr<StructureElementNode>> fChildVector;
113113
int fNodeId = 0;
114+
std::vector<int> fAdditionalNodeIds;
114115
AttributeList fAttributes;
115116
SkString fAlt;
116117
SkString fLang;

src/pdf/SkPDFTag.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ void SkPDFTagTree::Copy(SkPDF::StructureElementNode& node,
162162
SkArenaAlloc* arena,
163163
SkTHashMap<int, SkPDFTagNode*>* nodeMap) {
164164
nodeMap->set(node.fNodeId, dst);
165+
for (int nodeId : node.fAdditionalNodeIds) {
166+
SkASSERT(!nodeMap->find(nodeId));
167+
nodeMap->set(nodeId, dst);
168+
}
165169
dst->fNodeId = node.fNodeId;
166170
dst->fType = node.fType;
167171
dst->fTypeString = node.fTypeString;

tests/PDFTaggedPruningTest.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 "tests/Test.h"
8+
9+
#ifdef SK_SUPPORT_PDF
10+
11+
#include "include/core/SkCanvas.h"
12+
#include "include/core/SkFont.h"
13+
#include "include/core/SkStream.h"
14+
#include "include/docs/SkPDFDocument.h"
15+
16+
using PDFTag = SkPDF::StructureElementNode;
17+
18+
// Test building a tagged PDF where nodes are pruned.
19+
// Add this to args.gn to output the PDF to a file:
20+
// extra_cflags = [ "-DSK_PDF_TEST_TAGS_OUTPUT_PATH=\"/tmp/pruning.pdf\"" ]
21+
DEF_TEST(SkPDF_tagged_pruning, r) {
22+
REQUIRE_PDF_DOCUMENT(SkPDF_tagged, r);
23+
#ifdef SK_PDF_TEST_TAGS_OUTPUT_PATH
24+
SkFILEWStream outputStream(SK_PDF_TEST_TAGS_OUTPUT_PATH);
25+
#else
26+
SkDynamicMemoryWStream outputStream;
27+
#endif
28+
29+
SkSize pageSize = SkSize::Make(612, 792); // U.S. Letter
30+
31+
SkPDF::Metadata metadata;
32+
metadata.fTitle = "Example Tagged PDF";
33+
metadata.fCreator = "Skia";
34+
SkTime::DateTime now;
35+
SkTime::GetDateTime(&now);
36+
metadata.fCreation = now;
37+
metadata.fModified = now;
38+
39+
// The document tag.
40+
auto root = std::make_unique<PDFTag>();
41+
root->fNodeId = 1;
42+
root->fTypeString = "Document";
43+
root->fLang = "en-US";
44+
45+
// First paragraph.
46+
auto p1 = std::make_unique<PDFTag>();
47+
p1->fNodeId = 2;
48+
p1->fAdditionalNodeIds = {3, 4};
49+
p1->fTypeString = "P";
50+
root->fChildVector.push_back(std::move(p1));
51+
52+
// Second paragraph.
53+
auto p2 = std::make_unique<PDFTag>();
54+
p2->fNodeId = 5;
55+
p2->fAdditionalNodeIds = {6, 7};
56+
p2->fTypeString = "P";
57+
root->fChildVector.push_back(std::move(p2));
58+
59+
metadata.fStructureElementTreeRoot = root.get();
60+
sk_sp<SkDocument> document = SkPDF::MakeDocument(
61+
&outputStream, metadata);
62+
63+
SkPaint paint;
64+
paint.setColor(SK_ColorBLACK);
65+
66+
SkCanvas* canvas =
67+
document->beginPage(pageSize.width(),
68+
pageSize.height());
69+
SkFont font(nullptr, 20);
70+
SkPDF::SetNodeId(canvas, 3);
71+
canvas->drawString("First paragraph line 1", 72, 72, font, paint);
72+
SkPDF::SetNodeId(canvas, 4);
73+
canvas->drawString("First paragraph line 2", 72, 108, font, paint);
74+
SkPDF::SetNodeId(canvas, 6);
75+
canvas->drawString("Second paragraph line 1", 72, 180, font, paint);
76+
SkPDF::SetNodeId(canvas, 7);
77+
canvas->drawString("Second paragraph line 2", 72, 216, font, paint);
78+
79+
document->endPage();
80+
document->close();
81+
outputStream.flush();
82+
}
83+
84+
#endif

0 commit comments

Comments
 (0)