5#include "samples/helpers/dump.h"
16#include "public/cpp/fpdf_scopers.h"
17#include "public/fpdf_doc.h"
18#include "public/fpdf_transformpage.h"
19#include "testing/fx_string_testhelpers.h"
22 std::function<
bool(FPDF_PAGE,
float*,
float*,
float*,
float*)>;
26std::wstring ConvertToWString(
const unsigned short* buf,
27 unsigned long buf_size) {
29 result.reserve(buf_size);
30 std::copy(buf, buf + buf_size,
std::back_inserter(result));
34void DumpBoxInfo(GetBoxInfoFunc func,
39 bool ret = func(page, &rect.left, &rect.bottom, &rect.right, &rect.top);
41 printf(
"Page %d: No %s.\n", page_idx, box_type);
44 printf(
"Page %d: %s: %0.2f %0.2f %0.2f %0.2f\n", page_idx, box_type,
45 rect.left, rect.bottom, rect.right, rect.top);
48void DumpStructureElementAttributes(FPDF_STRUCTELEMENT_ATTR attr,
int indent) {
49 static const size_t kBufSize = 1024;
51 for (
int i = 0; i < count; i++) {
52 char name[kBufSize] = {};
53 unsigned long len = ULONG_MAX;
54 if (!FPDF_StructElement_Attr_GetName(attr, i, name,
sizeof(name), &len)) {
55 printf(
"%*s FPDF_StructElement_Attr_GetName failed for %d\n", indent,
"",
60 FPDF_OBJECT_TYPE type = FPDF_StructElement_Attr_GetType(attr, name);
63 if (!FPDF_StructElement_Attr_GetBooleanValue(attr, name, &value)) {
64 printf(
"%*s %s: Failed FPDF_StructElement_Attr_GetBooleanValue\n",
68 printf(
"%*s %s: %d\n", indent,
"", name, value);
71 if (!FPDF_StructElement_Attr_GetNumberValue(attr, name, &value)) {
72 printf(
"%*s %s: Failed FPDF_StructElement_Attr_GetNumberValue\n",
76 printf(
"%*s %s: %f\n", indent,
"", name, value);
78 unsigned short buffer[kBufSize] = {};
79 if (!FPDF_StructElement_Attr_GetStringValue(attr, name, buffer,
80 sizeof(buffer), &len)) {
81 printf(
"%*s %s: Failed FPDF_StructElement_Attr_GetStringValue\n",
85 printf(
"%*s %s: %ls\n", indent,
"", name,
86 ConvertToWString(buffer, len).c_str());
88 printf(
"%*s %s: FPDF_OBJECT_UNKNOWN\n", indent,
"", name);
90 printf(
"%*s %s: NOT_YET_IMPLEMENTED: %d\n", indent,
"", name, type);
98 static const size_t kBufSize = 1024;
99 unsigned short buf[kBufSize];
100 unsigned long len = FPDF_StructElement_GetType(child, buf, kBufSize);
102 printf(
"%*s S: %ls\n", indent * 2,
"", ConvertToWString(buf, len).c_str());
106 for (
int i = 0; i < attr_count; i++) {
107 FPDF_STRUCTELEMENT_ATTR child_attr =
112 printf(
"%*s A[%d]:\n", indent * 2,
"", i);
113 DumpStructureElementAttributes(child_attr, indent * 2 + 2);
116 memset(buf, 0,
sizeof(buf));
117 len = FPDF_StructElement_GetActualText(child, buf, kBufSize);
119 printf(
"%*s ActualText: %ls\n", indent * 2,
"",
120 ConvertToWString(buf, len).c_str());
123 memset(buf, 0,
sizeof(buf));
124 len = FPDF_StructElement_GetAltText(child, buf, kBufSize);
126 printf(
"%*s AltText: %ls\n", indent * 2,
"",
127 ConvertToWString(buf, len).c_str());
130 memset(buf, 0,
sizeof(buf));
131 len = FPDF_StructElement_GetID(child, buf, kBufSize);
133 printf(
"%*s ID: %ls\n", indent * 2,
"", ConvertToWString(buf, len).c_str());
136 memset(buf, 0,
sizeof(buf));
137 len = FPDF_StructElement_GetLang(child, buf, kBufSize);
139 printf(
"%*s Lang: %ls\n", indent * 2,
"",
140 ConvertToWString(buf, len).c_str());
145 printf(
"%*s MCID: %d\n", indent * 2,
"", mcid);
150 memset(buf, 0,
sizeof(buf));
151 len = FPDF_StructElement_GetID(parent, buf, kBufSize);
153 printf(
"%*s Parent ID: %ls\n", indent * 2,
"",
154 ConvertToWString(buf, len).c_str());
158 memset(buf, 0,
sizeof(buf));
159 len = FPDF_StructElement_GetTitle(child, buf, kBufSize);
161 printf(
"%*s Title: %ls\n", indent * 2,
"",
162 ConvertToWString(buf, len).c_str());
165 memset(buf, 0,
sizeof(buf));
166 len = FPDF_StructElement_GetObjType(child, buf, kBufSize);
168 printf(
"%*s Type: %ls\n", indent * 2,
"",
169 ConvertToWString(buf, len).c_str());
193 ScopedFPDFStructTree tree(FPDF_StructTree_GetForPage(page));
195 fprintf(stderr,
"Failed to load struct tree for page %d\n", page_idx);
199 printf(
"Structure Tree for Page %d\n", page_idx);
200 for (
int i = 0; i < FPDF_StructTree_CountChildren(tree.get()); ++i) {
201 FPDF_STRUCTELEMENT child = FPDF_StructTree_GetChildAtIndex(tree.get(), i);
203 fprintf(stderr,
"Failed to load child %d for page %d\n", i, page_idx);
212 static constexpr const char* kMetaTags[] = {
213 "Title",
"Author",
"Subject",
"Keywords",
214 "Creator",
"Producer",
"CreationDate",
"ModDate"};
215 for (
const char* meta_tag : kMetaTags) {
216 char meta_buffer[4096];
223 auto* meta_string =
reinterpret_cast<
unsigned short*>(meta_buffer);
224 printf(
"%-12s = %ls (%lu bytes)\n", meta_tag,
void DumpPageStructure(FPDF_PAGE page, int page_idx)
void DumpMetaData(FPDF_DOCUMENT doc)
void DumpPageInfo(FPDF_PAGE page, int page_idx)
void DumpChildStructure(FPDF_STRUCTELEMENT child, int indent)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetMetaText(FPDF_DOCUMENT document, FPDF_BYTESTRING tag, void *buffer, unsigned long buflen)
FPDF_EXPORT FPDF_STRUCTELEMENT FPDF_CALLCONV FPDF_StructElement_GetParent(FPDF_STRUCTELEMENT struct_element)
FPDF_EXPORT int FPDF_CALLCONV FPDF_StructElement_GetAttributeCount(FPDF_STRUCTELEMENT struct_element)
FPDF_EXPORT int FPDF_CALLCONV FPDF_StructElement_GetMarkedContentID(FPDF_STRUCTELEMENT struct_element)
FPDF_EXPORT int FPDF_CALLCONV FPDF_StructElement_Attr_GetCount(FPDF_STRUCTELEMENT_ATTR struct_attribute)
FPDF_EXPORT FPDF_STRUCTELEMENT FPDF_CALLCONV FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element, int index)
FPDF_EXPORT FPDF_STRUCTELEMENT_ATTR FPDF_CALLCONV FPDF_StructElement_GetAttributeAtIndex(FPDF_STRUCTELEMENT struct_element, int index)
FPDF_EXPORT int FPDF_CALLCONV FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GetArtBox(FPDF_PAGE page, float *left, float *bottom, float *right, float *top)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GetTrimBox(FPDF_PAGE page, float *left, float *bottom, float *right, float *top)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GetMediaBox(FPDF_PAGE page, float *left, float *bottom, float *right, float *top)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GetCropBox(FPDF_PAGE page, float *left, float *bottom, float *right, float *top)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GetBleedBox(FPDF_PAGE page, float *left, float *bottom, float *right, float *top)
#define FPDF_OBJECT_NUMBER
#define FPDF_OBJECT_BOOLEAN
#define FPDF_OBJECT_UNKNOWN
#define FPDF_OBJECT_STRING
std::wstring GetPlatformWString(FPDF_WIDESTRING wstr)