1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "printing/common/metafile_utils.h"
#include "base/check.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "printing/buildflags/buildflags.h"
#include "third_party/skia/include/codec/SkPngDecoder.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
#include "third_party/skia/include/core/SkStream.h"
#include "third_party/skia/include/core/SkString.h"
#include "third_party/skia/include/core/SkTypeface.h"
#include "third_party/skia/include/docs/SkPDFDocument.h"
#include "third_party/skia/include/encode/SkPngEncoder.h"
#include "third_party/skia/include/private/chromium/SkImageChromium.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/accessibility/ax_tree.h"
#include "ui/accessibility/ax_tree_update.h"
#include <variant>
namespace {
// Table 333 in PDF 32000-1:2008 spec, section 14.8.4.2
const char kPDFStructureTypeDocument[] = "Document";
const char kPDFStructureTypeParagraph[] = "P";
const char kPDFStructureTypeDiv[] = "Div";
const char kPDFStructureTypeHeading[] = "H";
const char kPDFStructureTypeLink[] = "Link";
const char kPDFStructureTypeList[] = "L";
const char kPDFStructureTypeListItemLabel[] = "Lbl";
const char kPDFStructureTypeListItemBody[] = "LI";
const char kPDFStructureTypeTable[] = "Table";
const char kPDFStructureTypeTableRow[] = "TR";
const char kPDFStructureTypeTableHeader[] = "TH";
const char kPDFStructureTypeTableCell[] = "TD";
const char kPDFStructureTypeFigure[] = "Figure";
const char kPDFStructureTypeNonStruct[] = "NonStruct";
// Standard attribute owners from PDF 32000-1:2008 spec, section 14.8.5.2
// (Attribute owners are kind of like "categories" for structure node
// attributes.)
const char kPDFTableAttributeOwner[] = "Table";
// Table Attributes from PDF 32000-1:2008 spec, section 14.8.5.7
const char kPDFTableCellColSpanAttribute[] = "ColSpan";
const char kPDFTableCellHeadersAttribute[] = "Headers";
const char kPDFTableCellRowSpanAttribute[] = "RowSpan";
const char kPDFTableHeaderScopeAttribute[] = "Scope";
const char kPDFTableHeaderScopeColumn[] = "Column";
const char kPDFTableHeaderScopeRow[] = "Row";
SkString GetHeadingStructureType(int heading_level) {
// From Table 333 in PDF 32000-1:2008 spec, section 14.8.4.2,
// "H1"..."H6" are valid structure types.
if (heading_level >= 1 && heading_level <= 6)
return SkString(base::StringPrintf("H%d", heading_level).c_str());
// If we don't have a valid heading level, use the generic heading role.
return SkString(kPDFStructureTypeHeading);
}
SkPDF::DateTime TimeToSkTime(base::Time time) {
base::Time::Exploded exploded;
time.UTCExplode(&exploded);
return SkPDF::DateTime{
.fTimeZoneMinutes = 0,
.fYear = static_cast<uint16_t>(exploded.year),
.fMonth = static_cast<uint8_t>(exploded.month),
.fDayOfWeek = static_cast<uint8_t>(exploded.day_of_week),
.fDay = static_cast<uint8_t>(exploded.day_of_month),
.fHour = static_cast<uint8_t>(exploded.hour),
.fMinute = static_cast<uint8_t>(exploded.minute),
.fSecond = static_cast<uint8_t>(exploded.second)};
}
sk_sp<SkPicture> GetEmptyPicture() {
SkPictureRecorder rec;
SkCanvas* canvas = rec.beginRecording(100, 100);
// Add some ops whose net effects equal to a noop.
canvas->save();
canvas->restore();
return rec.finishRecordingAsPicture();
}
// Convert an AXNode into a SkPDF::StructureElementNode in order to make a
// tagged (accessible) PDF. Returns true on success and false if we don't
// have enough data to build a valid tree.
bool RecursiveBuildStructureTree(const ui::AXNode* ax_node,
SkPDF::StructureElementNode* tag) {
bool valid = false;
tag->fNodeId = ax_node->GetIntAttribute(ax::mojom::IntAttribute::kDOMNodeId);
switch (ax_node->GetRole()) {
case ax::mojom::Role::kRootWebArea:
tag->fTypeString = kPDFStructureTypeDocument;
break;
case ax::mojom::Role::kParagraph:
tag->fTypeString = kPDFStructureTypeParagraph;
break;
case ax::mojom::Role::kGenericContainer:
tag->fTypeString = kPDFStructureTypeDiv;
break;
case ax::mojom::Role::kHeading:
tag->fTypeString = GetHeadingStructureType(ax_node->GetIntAttribute(
ax::mojom::IntAttribute::kHierarchicalLevel));
break;
case ax::mojom::Role::kLink:
tag->fTypeString = kPDFStructureTypeLink;
break;
case ax::mojom::Role::kList:
tag->fTypeString = kPDFStructureTypeList;
break;
case ax::mojom::Role::kListMarker:
tag->fTypeString = kPDFStructureTypeListItemLabel;
break;
case ax::mojom::Role::kListItem:
tag->fTypeString = kPDFStructureTypeListItemBody;
break;
case ax::mojom::Role::kTable:
tag->fTypeString = kPDFStructureTypeTable;
break;
case ax::mojom::Role::kRow:
tag->fTypeString = kPDFStructureTypeTableRow;
break;
case ax::mojom::Role::kColumnHeader:
tag->fTypeString = kPDFStructureTypeTableHeader;
tag->fAttributes.appendName(kPDFTableAttributeOwner,
kPDFTableHeaderScopeAttribute,
kPDFTableHeaderScopeColumn);
break;
case ax::mojom::Role::kRowHeader:
tag->fTypeString = kPDFStructureTypeTableHeader;
tag->fAttributes.appendName(kPDFTableAttributeOwner,
kPDFTableHeaderScopeAttribute,
kPDFTableHeaderScopeRow);
break;
case ax::mojom::Role::kCell: {
tag->fTypeString = kPDFStructureTypeTableCell;
// Append an attribute consisting of the string IDs of all of the
// header cells that correspond to this table cell.
std::vector<ui::AXNode*> header_nodes;
ax_node->GetTableCellColHeaders(&header_nodes);
ax_node->GetTableCellRowHeaders(&header_nodes);
std::vector<int> header_ids;
header_ids.reserve(header_nodes.size());
for (ui::AXNode* header_node : header_nodes) {
header_ids.push_back(header_node->GetIntAttribute(
ax::mojom::IntAttribute::kDOMNodeId));
}
tag->fAttributes.appendNodeIdArray(
kPDFTableAttributeOwner, kPDFTableCellHeadersAttribute, header_ids);
break;
}
case ax::mojom::Role::kImage:
// TODO(thestig): Figure out if the `ax::mojom::Role::kFigure` case should
// share code with the `ax::mojom::Role::kImage` case, and if `valid`
// should be set.
valid = true;
[[fallthrough]];
case ax::mojom::Role::kFigure: {
tag->fTypeString = kPDFStructureTypeFigure;
std::string alt =
ax_node->GetStringAttribute(ax::mojom::StringAttribute::kName);
tag->fAlt = SkString(alt.c_str());
break;
}
case ax::mojom::Role::kStaticText:
tag->fTypeString = kPDFStructureTypeNonStruct;
valid = true;
break;
default:
tag->fTypeString = kPDFStructureTypeNonStruct;
break;
}
if (ui::IsCellOrTableHeader(ax_node->GetRole())) {
absl::optional<int> row_span = ax_node->GetTableCellRowSpan();
if (row_span.has_value()) {
tag->fAttributes.appendInt(kPDFTableAttributeOwner,
kPDFTableCellRowSpanAttribute,
row_span.value());
}
absl::optional<int> col_span = ax_node->GetTableCellColSpan();
if (col_span.has_value()) {
tag->fAttributes.appendInt(kPDFTableAttributeOwner,
kPDFTableCellColSpanAttribute,
col_span.value());
}
}
std::string lang = ax_node->GetLanguage();
std::string parent_lang =
ax_node->parent() ? ax_node->parent()->GetLanguage() : "";
if (!lang.empty() && lang != parent_lang)
tag->fLang = lang.c_str();
tag->fChildVector.resize(ax_node->GetUnignoredChildCount());
for (size_t i = 0; i < tag->fChildVector.size(); i++) {
tag->fChildVector[i] = std::make_unique<SkPDF::StructureElementNode>();
valid |= RecursiveBuildStructureTree(ax_node->GetUnignoredChildAtIndex(i),
tag->fChildVector[i].get());
}
return valid;
}
} // namespace
namespace printing {
sk_sp<SkDocument> MakePdfDocument(base::StringPiece creator,
const ui::AXTreeUpdate& accessibility_tree,
SkWStream* stream) {
SkPDF::Metadata metadata;
SkPDF::DateTime now = TimeToSkTime(base::Time::Now());
metadata.fCreation = now;
metadata.fModified = now;
// TODO(crbug.com/691162): Switch to SkString's string_view constructor when
// possible.
metadata.fCreator = creator.empty()
? SkString("Chromium")
: SkString(creator.data(), creator.size());
metadata.fRasterDPI = 300.0f;
SkPDF::StructureElementNode tag_root = {};
if (!accessibility_tree.nodes.empty()) {
ui::AXTree tree(accessibility_tree);
if (RecursiveBuildStructureTree(tree.root(), &tag_root))
metadata.fStructureElementTreeRoot = &tag_root;
}
return SkPDF::MakeDocument(stream, metadata);
}
sk_sp<SkData> SerializeOopPicture(SkPicture* pic, void* ctx) {
const auto* context = reinterpret_cast<const ContentToProxyTokenMap*>(ctx);
uint32_t pic_id = pic->uniqueID();
auto iter = context->find(pic_id);
if (iter == context->end())
return nullptr;
return SkData::MakeWithCopy(&pic_id, sizeof(pic_id));
}
sk_sp<SkPicture> DeserializeOopPicture(const void* data,
size_t length,
void* ctx) {
uint32_t pic_id;
if (length < sizeof(pic_id)) {
NOTREACHED(); // Should not happen if the content is as written.
return GetEmptyPicture();
}
memcpy(&pic_id, data, sizeof(pic_id));
auto* context = reinterpret_cast<PictureDeserializationContext*>(ctx);
auto iter = context->find(pic_id);
if (iter == context->end() || !iter->second) {
// When we don't have the out-of-process picture available, we return
// an empty picture. Returning a nullptr will cause the deserialization
// crash.
return GetEmptyPicture();
}
return iter->second;
}
sk_sp<SkData> SerializeOopTypeface(SkTypeface* typeface, void* ctx) {
auto* context = reinterpret_cast<TypefaceSerializationContext*>(ctx);
SkTypefaceID typeface_id = typeface->uniqueID();
bool data_included = context->insert(typeface_id).second;
// Need the typeface ID to identify the desired typeface. Include an
// indicator for when typeface data actually follows vs. when the typeface
// should already exist in a cache when deserializing.
SkDynamicMemoryWStream stream;
stream.write32(typeface_id);
stream.writeBool(data_included);
if (data_included) {
typeface->serialize(&stream, SkTypeface::SerializeBehavior::kDoIncludeData);
}
return stream.detachAsData();
}
sk_sp<SkTypeface> DeserializeOopTypeface(const void* data,
size_t length,
void* ctx) {
SkStream* stream = *(reinterpret_cast<SkStream**>(const_cast<void*>(data)));
if (length < sizeof(stream)) {
NOTREACHED(); // Should not happen if the content is as written.
return nullptr;
}
SkTypefaceID id;
if (!stream->readU32(&id)) {
return nullptr;
}
bool data_included;
if (!stream->readBool(&data_included)) {
return nullptr;
}
auto* context = reinterpret_cast<TypefaceDeserializationContext*>(ctx);
auto iter = context->find(id);
if (iter != context->end()) {
DCHECK(!data_included);
return iter->second;
}
// Typeface not encountered before, expect it to be present in the stream.
DCHECK(data_included);
sk_sp<SkTypeface> typeface = SkTypeface::MakeDeserialize(stream);
context->emplace(id, typeface);
return typeface;
}
sk_sp<SkData> SerializeRasterImage(SkImage* img, void*) {
if (!img) {
return nullptr;
}
// TODO(crbug.com/1486503) Convert texture-backed images to raster
// *before* they get this far if possible.
if (img->isTextureBacked()) {
GrDirectContext* ctx = SkImages::GetContext(img);
return SkPngEncoder::Encode(ctx, img, SkPngEncoder::Options{});
}
return SkPngEncoder::Encode(nullptr, img, SkPngEncoder::Options{});
}
sk_sp<SkImage> DeserializeRasterImage(const void* bytes, size_t length, void*) {
auto data = SkData::MakeWithoutCopy(bytes, length);
auto codec = SkPngDecoder::Decode(data, nullptr);
if (codec) {
return std::get<0>(codec->getImage());
}
return nullptr;
}
SkSerialProcs SerializationProcs(PictureSerializationContext* picture_ctx,
TypefaceSerializationContext* typeface_ctx) {
SkSerialProcs procs;
procs.fImageProc = SerializeRasterImage;
procs.fPictureProc = SerializeOopPicture;
procs.fPictureCtx = picture_ctx;
procs.fTypefaceProc = SerializeOopTypeface;
procs.fTypefaceCtx = typeface_ctx;
return procs;
}
SkDeserialProcs DeserializationProcs(
PictureDeserializationContext* picture_ctx,
TypefaceDeserializationContext* typeface_ctx) {
SkDeserialProcs procs;
procs.fImageProc = DeserializeRasterImage;
procs.fPictureProc = DeserializeOopPicture;
procs.fPictureCtx = picture_ctx;
procs.fTypefaceProc = DeserializeOopTypeface;
procs.fTypefaceCtx = typeface_ctx;
return procs;
}
} // namespace printing
|