John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <limits.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | #include <list> |
| 11 | #include <string> |
| 12 | #include <utility> |
| 13 | |
John Abd-El-Malek | 197fd8d | 2014-05-23 19:27:48 -0700 | [diff] [blame] | 14 | #include "../fpdfsdk/include/fpdf_dataavail.h" |
| 15 | #include "../fpdfsdk/include/fpdf_ext.h" |
| 16 | #include "../fpdfsdk/include/fpdfformfill.h" |
| 17 | #include "../fpdfsdk/include/fpdftext.h" |
| 18 | #include "../fpdfsdk/include/fpdfview.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 19 | #include "v8/include/v8.h" |
| 20 | |
| 21 | #ifdef _WIN32 |
| 22 | #define snprintf _snprintf |
| 23 | /* in Windows, rb for open and read binary file */ |
| 24 | #define FOPEN_READ "rb" |
| 25 | #else |
| 26 | #define FOPEN_READ "r" |
| 27 | #endif |
| 28 | |
| 29 | static void WritePpm(const char* pdf_name, int num, |
| 30 | const char* buffer, int stride, int width, int height) { |
| 31 | if (stride < 0 || width < 0 || height < 0) |
| 32 | return; |
| 33 | if (height > 0 && width > INT_MAX / height) |
| 34 | return; |
| 35 | int out_len = width * height; |
| 36 | if (out_len > INT_MAX / 3) |
| 37 | return; |
| 38 | out_len *= 3; |
| 39 | |
| 40 | char filename[256]; |
| 41 | snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num); |
| 42 | FILE* fp = fopen(filename, "w"); |
| 43 | if (!fp) |
| 44 | return; |
| 45 | fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height); |
| 46 | // Source data is B, G, R, unused. |
| 47 | // Dest data is R, G, B. |
| 48 | char* result = new char[out_len]; |
| 49 | if (result) { |
| 50 | for (int h = 0; h < height; ++h) { |
| 51 | const char* src_line = buffer + (stride * h); |
| 52 | char* dest_line = result + (width * h * 3); |
| 53 | for (int w = 0; w < width; ++w) { |
| 54 | // R |
| 55 | dest_line[w * 3] = src_line[(w * 4) + 2]; |
| 56 | // G |
| 57 | dest_line[(w * 3) + 1] = src_line[(w * 4) + 1]; |
| 58 | // B |
| 59 | dest_line[(w * 3) + 2] = src_line[w * 4]; |
| 60 | } |
| 61 | } |
| 62 | fwrite(result, out_len, 1, fp); |
| 63 | delete [] result; |
| 64 | } |
| 65 | fclose(fp); |
| 66 | } |
| 67 | |
| 68 | int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) { |
| 69 | printf("Form_Alert called.\n"); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | void Unsupported_Handler(UNSUPPORT_INFO*, int type) { |
| 74 | std::string feature = "Unknown"; |
| 75 | switch (type) { |
| 76 | case FPDF_UNSP_DOC_XFAFORM: |
| 77 | feature = "XFA"; |
| 78 | break; |
| 79 | case FPDF_UNSP_DOC_PORTABLECOLLECTION: |
| 80 | feature = "Portfolios_Packages"; |
| 81 | break; |
| 82 | case FPDF_UNSP_DOC_ATTACHMENT: |
| 83 | case FPDF_UNSP_ANNOT_ATTACHMENT: |
| 84 | feature = "Attachment"; |
| 85 | break; |
| 86 | case FPDF_UNSP_DOC_SECURITY: |
| 87 | feature = "Rights_Management"; |
| 88 | break; |
| 89 | case FPDF_UNSP_DOC_SHAREDREVIEW: |
| 90 | feature = "Shared_Review"; |
| 91 | break; |
| 92 | case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT: |
| 93 | case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM: |
| 94 | case FPDF_UNSP_DOC_SHAREDFORM_EMAIL: |
| 95 | feature = "Shared_Form"; |
| 96 | break; |
| 97 | case FPDF_UNSP_ANNOT_3DANNOT: |
| 98 | feature = "3D"; |
| 99 | break; |
| 100 | case FPDF_UNSP_ANNOT_MOVIE: |
| 101 | feature = "Movie"; |
| 102 | break; |
| 103 | case FPDF_UNSP_ANNOT_SOUND: |
| 104 | feature = "Sound"; |
| 105 | break; |
| 106 | case FPDF_UNSP_ANNOT_SCREEN_MEDIA: |
| 107 | case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: |
| 108 | feature = "Screen"; |
| 109 | break; |
| 110 | case FPDF_UNSP_ANNOT_SIG: |
| 111 | feature = "Digital_Signature"; |
| 112 | break; |
| 113 | } |
| 114 | printf("Unsupported feature: %s.\n", feature.c_str()); |
| 115 | } |
| 116 | |
| 117 | bool ParseCommandLine(int argc, const char* argv[], bool* write_images, |
| 118 | std::list<const char*>* files) { |
| 119 | *write_images = false; |
| 120 | files->clear(); |
| 121 | |
| 122 | int cur_arg = 1; |
| 123 | if (cur_arg < argc && |
| 124 | strcmp(argv[cur_arg], "--write_images") == 0) { |
| 125 | *write_images = true; |
| 126 | cur_arg++; |
| 127 | } |
| 128 | |
| 129 | if (cur_arg >= argc) |
| 130 | return false; |
| 131 | |
| 132 | for (int i = cur_arg; i < argc; i++) |
| 133 | files->push_back(argv[i]); |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | class TestLoader { |
| 139 | public: |
| 140 | TestLoader(const char* pBuf, size_t len); |
| 141 | |
| 142 | const char* m_pBuf; |
| 143 | size_t m_Len; |
| 144 | }; |
| 145 | |
| 146 | TestLoader::TestLoader(const char* pBuf, size_t len) |
| 147 | : m_pBuf(pBuf), m_Len(len) { |
| 148 | } |
| 149 | |
| 150 | int Get_Block(void* param, unsigned long pos, unsigned char* pBuf, |
| 151 | unsigned long size) { |
| 152 | TestLoader* pLoader = (TestLoader*) param; |
| 153 | if (pos + size < pos || pos + size > pLoader->m_Len) return 0; |
| 154 | memcpy(pBuf, pLoader->m_pBuf + pos, size); |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { |
| 163 | } |
| 164 | |
| 165 | void RenderPdf(const char* name, const char* pBuf, size_t len, |
| 166 | bool write_images) { |
| 167 | printf("Rendering PDF file %s.\n", name); |
| 168 | |
| 169 | IPDF_JSPLATFORM platform_callbacks; |
| 170 | memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
| 171 | platform_callbacks.version = 1; |
| 172 | platform_callbacks.app_alert = Form_Alert; |
| 173 | |
| 174 | FPDF_FORMFILLINFO form_callbacks; |
| 175 | memset(&form_callbacks, '\0', sizeof(form_callbacks)); |
| 176 | form_callbacks.version = 1; |
| 177 | form_callbacks.m_pJsPlatform = &platform_callbacks; |
| 178 | |
| 179 | TestLoader loader(pBuf, len); |
| 180 | |
| 181 | FPDF_FILEACCESS file_access; |
| 182 | memset(&file_access, '\0', sizeof(file_access)); |
John Abd-El-Malek | 7dc5172 | 2014-05-26 12:54:31 -0700 | [diff] [blame] | 183 | file_access.m_FileLen = static_cast<unsigned long>(len); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 184 | file_access.m_GetBlock = Get_Block; |
| 185 | file_access.m_Param = &loader; |
| 186 | |
| 187 | FX_FILEAVAIL file_avail; |
| 188 | memset(&file_avail, '\0', sizeof(file_avail)); |
| 189 | file_avail.version = 1; |
| 190 | file_avail.IsDataAvail = Is_Data_Avail; |
| 191 | |
| 192 | FX_DOWNLOADHINTS hints; |
| 193 | memset(&hints, '\0', sizeof(hints)); |
| 194 | hints.version = 1; |
| 195 | hints.AddSegment = Add_Segment; |
| 196 | |
| 197 | FPDF_DOCUMENT doc; |
| 198 | FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); |
| 199 | |
| 200 | (void) FPDFAvail_IsDocAvail(pdf_avail, &hints); |
| 201 | |
| 202 | if (!FPDFAvail_IsLinearized(pdf_avail)) { |
| 203 | printf("Non-linearized path...\n"); |
| 204 | doc = FPDF_LoadCustomDocument(&file_access, NULL); |
| 205 | } else { |
| 206 | printf("Linearized path...\n"); |
| 207 | doc = FPDFAvail_GetDocument(pdf_avail, NULL); |
| 208 | } |
| 209 | |
| 210 | (void) FPDF_GetDocPermissions(doc); |
| 211 | (void) FPDFAvail_IsFormAvail(pdf_avail, &hints); |
| 212 | |
| 213 | FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnviroument(doc, &form_callbacks); |
| 214 | FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); |
| 215 | FPDF_SetFormFieldHighlightAlpha(form, 100); |
| 216 | |
| 217 | int first_page = FPDFAvail_GetFirstPageNum(doc); |
| 218 | (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints); |
| 219 | |
| 220 | int page_count = FPDF_GetPageCount(doc); |
| 221 | for (int i = 0; i < page_count; ++i) { |
| 222 | (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints); |
| 223 | } |
| 224 | |
| 225 | FORM_DoDocumentJSAction(form); |
| 226 | FORM_DoDocumentOpenAction(form); |
| 227 | |
| 228 | for (int i = 0; i < page_count; ++i) { |
| 229 | FPDF_PAGE page = FPDF_LoadPage(doc, i); |
| 230 | FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); |
| 231 | FORM_OnAfterLoadPage(page, form); |
| 232 | FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); |
| 233 | |
| 234 | int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 235 | int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 236 | FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); |
| 237 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 255, 255, 255, 255); |
| 238 | |
| 239 | FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); |
| 240 | FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); |
| 241 | if (write_images) { |
| 242 | const char* buffer = reinterpret_cast<const char*>( |
| 243 | FPDFBitmap_GetBuffer(bitmap)); |
| 244 | int stride = FPDFBitmap_GetStride(bitmap); |
| 245 | WritePpm(name, i, buffer, stride, width, height); |
| 246 | } |
| 247 | |
| 248 | FPDFBitmap_Destroy(bitmap); |
| 249 | |
| 250 | FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 251 | FORM_OnBeforeClosePage(page, form); |
| 252 | FPDFText_ClosePage(text_page); |
| 253 | FPDF_ClosePage(page); |
| 254 | } |
| 255 | |
| 256 | FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
| 257 | FPDFDOC_ExitFormFillEnviroument(form); |
| 258 | FPDF_CloseDocument(doc); |
| 259 | FPDFAvail_Destroy(pdf_avail); |
| 260 | |
| 261 | printf("Loaded, parsed and rendered %d pages.\n", page_count); |
| 262 | } |
| 263 | |
| 264 | int main(int argc, const char* argv[]) { |
| 265 | v8::V8::InitializeICU(); |
| 266 | bool write_images = false; |
| 267 | std::list<const char*> files; |
| 268 | if (!ParseCommandLine(argc, argv, &write_images, &files)) { |
| 269 | printf("Usage is: test [--write_images] /path/to/pdf\n"); |
| 270 | printf("--write_images - to write page images <pdf-name>.<page-number>.ppm\n"); |
| 271 | return 1; |
| 272 | } |
| 273 | |
| 274 | FPDF_InitLibrary(NULL); |
| 275 | |
| 276 | UNSUPPORT_INFO unsuppored_info; |
| 277 | memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); |
| 278 | unsuppored_info.version = 1; |
| 279 | unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; |
| 280 | |
| 281 | FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
| 282 | |
| 283 | while (!files.empty()) { |
| 284 | const char* filename = files.front(); |
| 285 | files.pop_front(); |
| 286 | FILE* file = fopen(filename, FOPEN_READ); |
| 287 | if (!file) { |
| 288 | fprintf(stderr, "Failed to open: %s\n", filename); |
| 289 | continue; |
| 290 | } |
| 291 | (void) fseek(file, 0, SEEK_END); |
| 292 | size_t len = ftell(file); |
| 293 | (void) fseek(file, 0, SEEK_SET); |
| 294 | char* pBuf = (char*) malloc(len); |
| 295 | size_t ret = fread(pBuf, 1, len, file); |
| 296 | (void) fclose(file); |
| 297 | if (ret != len) { |
| 298 | fprintf(stderr, "Failed to read: %s\n", filename); |
| 299 | } else { |
| 300 | RenderPdf(filename, pBuf, len, write_images); |
| 301 | } |
| 302 | free(pBuf); |
| 303 | } |
| 304 | |
| 305 | FPDF_DestroyLibrary(); |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |