From 57f228d7527594de55951fa05e6082ba62382c57 Mon Sep 17 00:00:00 2001 From: Jane Liu Date: Thu, 13 Jul 2017 18:10:30 -0400 Subject: Updated pdfium_test's WriteAnnot() Updated pdfium_test's WriteAnnot() (corresponding to the --annot flag) to output more annotation info using the new APIs. Also fixed some nits in the annotation API code. Bug=pdfium:737 Change-Id: I3f40e83279ec82529f732eb94f309ab7d4992d3c Reviewed-on: https://pdfium-review.googlesource.com/7791 Reviewed-by: dsinclair Commit-Queue: Jane Liu --- fpdfsdk/fpdfannot.cpp | 4 +-- public/fpdf_annot.h | 6 ++-- samples/pdfium_test.cc | 90 ++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 80 insertions(+), 20 deletions(-) diff --git a/fpdfsdk/fpdfannot.cpp b/fpdfsdk/fpdfannot.cpp index faa4f4c8fb..412c80b5de 100644 --- a/fpdfsdk/fpdfannot.cpp +++ b/fpdfsdk/fpdfannot.cpp @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - #include "public/fpdf_annot.h" #include @@ -390,7 +388,7 @@ DLLEXPORT int STDCALL FPDFAnnot_GetObjectCount(FPDF_ANNOTATION annot) { pAnnot->SetForm(pStream); } - return pAnnot->GetForm()->GetPageObjectList()->size(); + return pdfium::CollectionSize(*pAnnot->GetForm()->GetPageObjectList()); } DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFAnnot_GetObject(FPDF_ANNOTATION annot, diff --git a/public/fpdf_annot.h b/public/fpdf_annot.h index c42b7059dc..e6c33a59eb 100644 --- a/public/fpdf_annot.h +++ b/public/fpdf_annot.h @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - #ifndef PUBLIC_FPDF_ANNOT_H_ #define PUBLIC_FPDF_ANNOT_H_ @@ -318,7 +316,7 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_HasKey(FPDF_ANNOTATION annot, FPDF_WIDESTRING key); // Experimental API. -// Get the type of the value corresponding to |key| in |annot|'s dictioanry. +// Get the type of the value corresponding to |key| in |annot|'s dictionary. // // annot - handle to an annotation. // key - the key to look for. @@ -330,7 +328,7 @@ DLLEXPORT FPDF_OBJECT_TYPE STDCALL FPDFAnnot_GetValueType(FPDF_ANNOTATION annot, // Experimental API. // Set the string value corresponding to |key| in |annot|'s dictionary, // overwriting the existing value if any. The value type would be -// FPDF_OBJECT_STRING after this function call. +// FPDF_OBJECT_STRING after this function call succeeds. // // annot - handle to an annotation. // key - the key to the dictionary entry to be set, encoded in UTF16-LE. diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc index 93821d963a..223a29f1f4 100644 --- a/samples/pdfium_test.cc +++ b/samples/pdfium_test.cc @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -265,6 +266,44 @@ std::string AnnotSubtypeToString(FPDF_ANNOTATION_SUBTYPE subtype) { return ""; } +std::string AnnotFlagsToString(int flags) { + std::string str = ""; + if (flags & FPDF_ANNOT_FLAG_INVISIBLE) + str += "Invisible"; + if (flags & FPDF_ANNOT_FLAG_HIDDEN) + str += std::string(str.empty() ? "" : ", ") + "Hidden"; + if (flags & FPDF_ANNOT_FLAG_PRINT) + str += std::string(str.empty() ? "" : ", ") + "Print"; + if (flags & FPDF_ANNOT_FLAG_NOZOOM) + str += std::string(str.empty() ? "" : ", ") + "NoZoom"; + if (flags & FPDF_ANNOT_FLAG_NOROTATE) + str += std::string(str.empty() ? "" : ", ") + "NoRotate"; + if (flags & FPDF_ANNOT_FLAG_NOVIEW) + str += std::string(str.empty() ? "" : ", ") + "NoView"; + if (flags & FPDF_ANNOT_FLAG_READONLY) + str += std::string(str.empty() ? "" : ", ") + "ReadOnly"; + if (flags & FPDF_ANNOT_FLAG_LOCKED) + str += std::string(str.empty() ? "" : ", ") + "Locked"; + if (flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW) + str += std::string(str.empty() ? "" : ", ") + "ToggleNoView"; + return str; +} + +std::string PageObjectTypeToString(int type) { + if (type == FPDF_PAGEOBJ_TEXT) + return "Text"; + if (type == FPDF_PAGEOBJ_PATH) + return "Path"; + if (type == FPDF_PAGEOBJ_IMAGE) + return "Image"; + if (type == FPDF_PAGEOBJ_SHADING) + return "Shading"; + if (type == FPDF_PAGEOBJ_FORM) + return "Form"; + NOTREACHED(); + return ""; +} + void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) { // Open the output text file. char filename[256]; @@ -285,7 +324,7 @@ void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) { fprintf(fp, "Number of annotations: %d\n\n", annot_count); // Iterate through all annotations on this page. - for (int i = 0; i < annot_count; i++) { + for (int i = 0; i < annot_count; ++i) { // Retrieve the annotation object and its subtype. fprintf(fp, "Annotation #%d:\n", i + 1); FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, i); @@ -296,6 +335,24 @@ void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) { FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot); fprintf(fp, "Subtype: %s\n", AnnotSubtypeToString(subtype).c_str()); + // Retrieve the annotation flags. + fprintf(fp, "Flags set: %s\n", + AnnotFlagsToString(FPDFAnnot_GetFlags(annot)).c_str()); + + // Retrieve the annotation's object count and object types. + const int obj_count = FPDFAnnot_GetObjectCount(annot); + fprintf(fp, "Number of objects: %d\n", obj_count); + if (obj_count > 0) { + fprintf(fp, "Object types: "); + for (int j = 0; j < obj_count; ++j) { + fprintf(fp, "%s ", + PageObjectTypeToString( + FPDFPageObj_GetType(FPDFAnnot_GetObject(annot, j))) + .c_str()); + } + fprintf(fp, "\n"); + } + // Retrieve the annotation's color and interior color. unsigned int R; unsigned int G; @@ -336,15 +393,19 @@ void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) { // Retrieve the annotation's quadpoints if it is a markup annotation. if (FPDFAnnot_HasAttachmentPoints(annot)) { FS_QUADPOINTSF quadpoints = FPDFAnnot_GetAttachmentPoints(annot); - fprintf(fp, "Quadpoints: (%f, %f), (%f, %f), (%f, %f), (%f, %f)\n", + fprintf(fp, + "Quadpoints: (%.3f, %.3f), (%.3f, %.3f), (%.3f, %.3f), (%.3f, " + "%.3f)\n", quadpoints.x1, quadpoints.y1, quadpoints.x2, quadpoints.y2, quadpoints.x3, quadpoints.y3, quadpoints.x4, quadpoints.y4); } // Retrieve the annotation's rectangle coordinates. FS_RECTF rect = FPDFAnnot_GetRect(annot); - fprintf(fp, "Rectangle: l - %f, b - %f, r - %f, t - %f\n\n", rect.left, - rect.bottom, rect.right, rect.top); + fprintf(fp, "Rectangle: l - %.3f, b - %.3f, r - %.3f, t - %.3f\n\n", + rect.left, rect.bottom, rect.right, rect.top); + + FPDFPage_CloseAnnot(annot); } (void)fclose(fp); @@ -1227,18 +1288,21 @@ static const char kUsageString[] = " --scale= - scale output size by number (e.g. 0.5)\n" " --pages=(-) - only render the given 0-based page(s)\n" #ifdef _WIN32 - " --bmp - write page images ..bmp\n" - " --emf - write page meta files ..emf\n" - " --ps2 - write page raw PostScript (Lvl 2) ..ps\n" - " --ps3 - write page raw PostScript (Lvl 3) ..ps\n" + " --bmp - write page images ..bmp\n" + " --emf - write page meta files ..emf\n" + " --ps2 - write page raw PostScript (Lvl 2) " + "..ps\n" + " --ps3 - write page raw PostScript (Lvl 3) " + "..ps\n" #endif // _WIN32 - " --txt - write page text in UTF32-LE ..txt\n" - " --png - write page images ..png\n" - " --ppm - write page images ..ppm\n" + " --txt - write page text in UTF32-LE ..txt\n" + " --png - write page images ..png\n" + " --ppm - write page images ..ppm\n" + " --annot - write annotation info ..annot.txt\n" #ifdef PDF_ENABLE_SKIA - " --skp - write page images ..skp\n" + " --skp - write page images ..skp\n" #endif - " --md5 - write output image paths and their md5 hashes to stdout.\n" + " --md5 - write output image paths and their md5 hashes to stdout.\n" ""; int main(int argc, const char* argv[]) { -- cgit v1.2.3