2020-02-19 18:59:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
#include "tests/Test.h"
|
|
|
|
|
2020-02-20 15:47:29 +00:00
|
|
|
#ifdef SK_SUPPORT_PDF
|
|
|
|
|
2020-02-19 18:59:29 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/docs/SkPDFDocument.h"
|
|
|
|
|
|
|
|
using PDFTag = SkPDF::StructureElementNode;
|
|
|
|
|
|
|
|
// Test building a tagged PDF containing a table.
|
|
|
|
// Add this to args.gn to output the PDF to a file:
|
|
|
|
// extra_cflags = [ "-DSK_PDF_TEST_TAGS_OUTPUT_PATH=\"/tmp/table.pdf\"" ]
|
|
|
|
DEF_TEST(SkPDF_tagged_table, r) {
|
|
|
|
REQUIRE_PDF_DOCUMENT(SkPDF_tagged, r);
|
|
|
|
#ifdef SK_PDF_TEST_TAGS_OUTPUT_PATH
|
|
|
|
SkFILEWStream outputStream(SK_PDF_TEST_TAGS_OUTPUT_PATH);
|
|
|
|
#else
|
|
|
|
SkDynamicMemoryWStream outputStream;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SkSize pageSize = SkSize::Make(612, 792); // U.S. Letter
|
|
|
|
|
|
|
|
SkPDF::Metadata metadata;
|
|
|
|
metadata.fTitle = "Example Tagged Table PDF";
|
|
|
|
metadata.fCreator = "Skia";
|
|
|
|
SkTime::DateTime now;
|
|
|
|
SkTime::GetDateTime(&now);
|
|
|
|
metadata.fCreation = now;
|
|
|
|
metadata.fModified = now;
|
|
|
|
|
|
|
|
constexpr int kRowCount = 5;
|
|
|
|
constexpr int kColCount = 4;
|
|
|
|
const char* cellData[kRowCount * kColCount] = {
|
|
|
|
"Car", "Engine", "City MPG", "Highway MPG",
|
|
|
|
"Mitsubishi Mirage ES", "Gas", "28", "47",
|
|
|
|
"Toyota Prius Three", "Hybrid", "43", "59",
|
|
|
|
"Nissan Leaf SL", "Electric", "N/A", nullptr,
|
|
|
|
"Tesla Model 3", nullptr, "N/A", nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
// The document tag.
|
2020-02-20 19:34:24 +00:00
|
|
|
auto root = std::make_unique<PDFTag>();
|
|
|
|
root->fNodeId = 1;
|
|
|
|
root->fTypeString = "Document";
|
|
|
|
root->fLang = "en-US";
|
2020-02-19 18:59:29 +00:00
|
|
|
|
|
|
|
// Heading.
|
2020-02-20 19:34:24 +00:00
|
|
|
auto h1 = std::make_unique<PDFTag>();
|
|
|
|
h1->fNodeId = 2;
|
|
|
|
h1->fTypeString = "H1";
|
|
|
|
h1->fAlt = "Tagged PDF Table Alt Text";
|
|
|
|
root->fChildVector.push_back(std::move(h1));
|
2020-02-19 18:59:29 +00:00
|
|
|
|
|
|
|
// Table.
|
2020-02-20 19:34:24 +00:00
|
|
|
auto table = std::make_unique<PDFTag>();
|
|
|
|
table->fNodeId = 3;
|
|
|
|
table->fTypeString = "Table";
|
|
|
|
auto& rows = table->fChildVector;
|
|
|
|
table->fAttributes.appendFloatArray("Layout", "BBox", {72, 72, 360, 360});
|
2020-02-19 18:59:29 +00:00
|
|
|
|
|
|
|
for (int rowIndex = 0; rowIndex < kRowCount; rowIndex++) {
|
2020-02-20 19:34:24 +00:00
|
|
|
auto row = std::make_unique<PDFTag>();
|
|
|
|
row->fNodeId = 4 + rowIndex;
|
|
|
|
row->fTypeString = "TR";
|
|
|
|
auto& cells = row->fChildVector;
|
2020-02-19 18:59:29 +00:00
|
|
|
for (int colIndex = 0; colIndex < kColCount; colIndex++) {
|
2020-02-20 19:34:24 +00:00
|
|
|
auto cell = std::make_unique<PDFTag>();
|
2020-02-19 18:59:29 +00:00
|
|
|
int cellIndex = rowIndex * kColCount + colIndex;
|
2020-02-20 19:34:24 +00:00
|
|
|
cell->fNodeId = 10 + cellIndex;
|
|
|
|
if (!cellData[cellIndex]) {
|
|
|
|
cell->fTypeString = "NonStruct";
|
|
|
|
} else if (rowIndex == 0 || colIndex == 0) {
|
|
|
|
cell->fTypeString = "TH";
|
|
|
|
} else {
|
|
|
|
cell->fTypeString = "TD";
|
|
|
|
}
|
|
|
|
cell->fChildCount = 0;
|
2020-02-19 18:59:29 +00:00
|
|
|
|
|
|
|
if (cellIndex == 13) {
|
2020-02-20 19:34:24 +00:00
|
|
|
cell->fAttributes.appendInt("Table", "RowSpan", 2);
|
2020-02-19 18:59:29 +00:00
|
|
|
} else if (cellIndex == 14 || cellIndex == 18) {
|
2020-02-20 19:34:24 +00:00
|
|
|
cell->fAttributes.appendInt("Table", "ColSpan", 2);
|
|
|
|
} else if (rowIndex == 0 || colIndex == 0) {
|
|
|
|
cell->fAttributes.appendString(
|
2020-02-19 18:59:29 +00:00
|
|
|
"Table", "Scope", rowIndex == 0 ? "Column" : "Row");
|
|
|
|
}
|
2020-02-20 19:34:24 +00:00
|
|
|
cells.push_back(std::move(cell));
|
2020-02-19 18:59:29 +00:00
|
|
|
}
|
2020-02-20 19:34:24 +00:00
|
|
|
rows.push_back(std::move(row));
|
2020-02-19 18:59:29 +00:00
|
|
|
}
|
2020-02-20 19:34:24 +00:00
|
|
|
root->fChildVector.push_back(std::move(table));
|
2020-02-19 18:59:29 +00:00
|
|
|
|
2020-02-20 19:34:24 +00:00
|
|
|
metadata.fStructureElementTreeRoot = root.get();
|
2020-02-19 18:59:29 +00:00
|
|
|
sk_sp<SkDocument> document = SkPDF::MakeDocument(
|
|
|
|
&outputStream, metadata);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
|
|
|
|
SkCanvas* canvas =
|
|
|
|
document->beginPage(pageSize.width(),
|
|
|
|
pageSize.height());
|
|
|
|
SkPDF::SetNodeId(canvas, 2);
|
|
|
|
SkFont font(nullptr, 36);
|
|
|
|
canvas->drawString("Tagged PDF Table", 72, 72, font, paint);
|
|
|
|
|
|
|
|
font.setSize(14);
|
|
|
|
for (int rowIndex = 0; rowIndex < kRowCount; rowIndex++) {
|
|
|
|
for (int colIndex = 0; colIndex < kColCount; colIndex++) {
|
|
|
|
int cellIndex = rowIndex * kColCount + colIndex;
|
|
|
|
const char* str = cellData[cellIndex];
|
|
|
|
if (!str)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int x = 72 + colIndex * 108 + (colIndex > 0 ? 72 : 0);
|
|
|
|
int y = 144 + rowIndex * 48;
|
|
|
|
|
|
|
|
SkPDF::SetNodeId(canvas, 10 + cellIndex);
|
|
|
|
canvas->drawString(str, x, y, font, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document->endPage();
|
|
|
|
document->close();
|
|
|
|
outputStream.flush();
|
|
|
|
}
|
2020-02-20 15:47:29 +00:00
|
|
|
|
|
|
|
#endif
|