Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <limits.h> | 5 #include <limits.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <wchar.h> | 9 #include <wchar.h> |
| 10 | 10 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 std::string scale_factor_as_string; | 48 std::string scale_factor_as_string; |
| 49 std::string exe_path; | 49 std::string exe_path; |
| 50 std::string bin_directory; | 50 std::string bin_directory; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 // Reads the entire contents of a file into a newly malloc'd buffer. | 53 // Reads the entire contents of a file into a newly malloc'd buffer. |
| 54 static char* GetFileContents(const char* filename, size_t* retlen) { | 54 static char* GetFileContents(const char* filename, size_t* retlen) { |
| 55 FILE* file = fopen(filename, "rb"); | 55 FILE* file = fopen(filename, "rb"); |
| 56 if (!file) { | 56 if (!file) { |
| 57 fprintf(stderr, "Failed to open: %s\n", filename); | 57 fprintf(stderr, "Failed to open: %s\n", filename); |
| 58 return NULL; | 58 return nullptr; |
| 59 } | 59 } |
| 60 (void) fseek(file, 0, SEEK_END); | 60 (void)fseek(file, 0, SEEK_END); |
| 61 size_t file_length = ftell(file); | 61 size_t file_length = ftell(file); |
| 62 if (!file_length) { | 62 if (!file_length) { |
| 63 return NULL; | 63 (void)fclose(file); |
| 64 return nullptr; | |
| 64 } | 65 } |
| 65 (void) fseek(file, 0, SEEK_SET); | 66 (void)fseek(file, 0, SEEK_SET); |
| 66 char* buffer = (char*) malloc(file_length); | 67 char* buffer = static_cast<char*>(malloc(file_length)); |
| 67 if (!buffer) { | 68 if (!buffer) { |
| 68 return NULL; | 69 (void)fclose(file); |
| 70 return nullptr; | |
| 69 } | 71 } |
| 70 size_t bytes_read = fread(buffer, 1, file_length, file); | 72 size_t bytes_read = fread(buffer, 1, file_length, file); |
| 71 (void) fclose(file); | 73 (void)fclose(file); |
| 72 if (bytes_read != file_length) { | 74 if (bytes_read != file_length) { |
| 73 fprintf(stderr, "Failed to read: %s\n", filename); | 75 fprintf(stderr, "Failed to read: %s\n", filename); |
| 74 free(buffer); | 76 free(buffer); |
| 75 return NULL; | 77 return nullptr; |
| 76 } | 78 } |
| 77 *retlen = bytes_read; | 79 *retlen = bytes_read; |
| 78 return buffer; | 80 return buffer; |
| 79 } | 81 } |
| 80 | 82 |
| 81 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 83 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 82 // Returns the full path for an external V8 data file based on either | 84 // Returns the full path for an external V8 data file based on either |
| 83 // the currect exectuable path or an explicit override. | 85 // the currect exectuable path or an explicit override. |
| 84 static std::string GetFullPathForSnapshotFile(const Options& options, | 86 static std::string GetFullPathForSnapshotFile(const Options& options, |
| 85 const std::string& filename) { | 87 const std::string& filename) { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 if (!fp) { | 190 if (!fp) { |
| 189 fprintf(stderr, "Failed to open %s for output\n", filename); | 191 fprintf(stderr, "Failed to open %s for output\n", filename); |
| 190 return; | 192 return; |
| 191 } | 193 } |
| 192 | 194 |
| 193 size_t bytes_written = fwrite( | 195 size_t bytes_written = fwrite( |
| 194 &png_encoding.front(), 1, png_encoding.size(), fp); | 196 &png_encoding.front(), 1, png_encoding.size(), fp); |
| 195 if (bytes_written != png_encoding.size()) | 197 if (bytes_written != png_encoding.size()) |
| 196 fprintf(stderr, "Failed to write to %s\n", filename); | 198 fprintf(stderr, "Failed to write to %s\n", filename); |
| 197 | 199 |
| 198 (void) fclose(fp); | 200 (void)fclose(fp); |
| 199 } | 201 } |
| 200 | 202 |
| 201 #ifdef _WIN32 | 203 #ifdef _WIN32 |
| 202 static void WriteBmp(const char* pdf_name, int num, const void* buffer, | 204 static void WriteBmp(const char* pdf_name, int num, const void* buffer, |
| 203 int stride, int width, int height) { | 205 int stride, int width, int height) { |
| 204 if (!CheckDimensions(stride, width, height)) | 206 if (!CheckDimensions(stride, width, height)) |
| 205 return; | 207 return; |
| 206 | 208 |
| 207 int out_len = stride * height; | 209 int out_len = stride * height; |
| 208 if (out_len > INT_MAX / 3) | 210 if (out_len > INT_MAX / 3) |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 234 fclose(fp); | 236 fclose(fp); |
| 235 } | 237 } |
| 236 | 238 |
| 237 void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) { | 239 void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) { |
| 238 int width = static_cast<int>(FPDF_GetPageWidth(page)); | 240 int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 239 int height = static_cast<int>(FPDF_GetPageHeight(page)); | 241 int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 240 | 242 |
| 241 char filename[256]; | 243 char filename[256]; |
| 242 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num); | 244 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num); |
| 243 | 245 |
| 244 HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL); | 246 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr); |
| 245 | 247 |
| 246 HRGN rgn = CreateRectRgn(0, 0, width, height); | 248 HRGN rgn = CreateRectRgn(0, 0, width, height); |
| 247 SelectClipRgn(dc, rgn); | 249 SelectClipRgn(dc, rgn); |
| 248 DeleteObject(rgn); | 250 DeleteObject(rgn); |
| 249 | 251 |
| 250 SelectObject(dc, GetStockObject(NULL_PEN)); | 252 SelectObject(dc, GetStockObject(NULL_PEN)); |
| 251 SelectObject(dc, GetStockObject(WHITE_BRUSH)); | 253 SelectObject(dc, GetStockObject(WHITE_BRUSH)); |
| 252 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less. | 254 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less. |
| 253 Rectangle(dc, 0, 0, width + 1, height + 1); | 255 Rectangle(dc, 0, 0, width + 1, height + 1); |
| 254 | 256 |
| 255 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, | 257 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, |
| 256 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); | 258 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); |
| 257 | 259 |
| 258 DeleteEnhMetaFile(CloseEnhMetaFile(dc)); | 260 DeleteEnhMetaFile(CloseEnhMetaFile(dc)); |
| 259 } | 261 } |
| 260 #endif | 262 #endif |
| 261 | 263 |
| 262 int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING, | 264 int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING, |
| 263 int, int) { | 265 int, int) { |
| 264 // Deal with differences between UTF16LE and wchar_t on this platform. | 266 // Deal with differences between UTF16LE and wchar_t on this platform. |
| 265 size_t characters = 0; | 267 size_t characters = 0; |
| 266 while (msg[characters]) { | 268 while (msg[characters]) { |
| 267 ++characters; | 269 ++characters; |
| 268 } | 270 } |
| 269 wchar_t* platform_string = | 271 wchar_t* platform_string = |
| 270 (wchar_t*)malloc((characters + 1) * sizeof(wchar_t)); | 272 static_cast<wchar_t*>(malloc((characters + 1) * sizeof(wchar_t))); |
| 271 for (size_t i = 0; i < characters + 1; ++i) { | 273 for (size_t i = 0; i < characters + 1; ++i) { |
| 272 unsigned char* ptr = (unsigned char*)&msg[i]; | 274 unsigned char* ptr = (unsigned char*)&msg[i]; |
| 273 platform_string[i] = ptr[0] + 256 * ptr[1]; | 275 platform_string[i] = ptr[0] + 256 * ptr[1]; |
| 274 } | 276 } |
| 275 printf("Alert: %ls\n", platform_string); | 277 printf("Alert: %ls\n", platform_string); |
| 276 free(platform_string); | 278 free(platform_string); |
| 277 return 0; | 279 return 0; |
| 278 } | 280 } |
| 279 | 281 |
| 280 void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) { | 282 void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) { |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 397 TestLoader(const char* pBuf, size_t len); | 399 TestLoader(const char* pBuf, size_t len); |
| 398 | 400 |
| 399 const char* m_pBuf; | 401 const char* m_pBuf; |
| 400 size_t m_Len; | 402 size_t m_Len; |
| 401 }; | 403 }; |
| 402 | 404 |
| 403 TestLoader::TestLoader(const char* pBuf, size_t len) | 405 TestLoader::TestLoader(const char* pBuf, size_t len) |
| 404 : m_pBuf(pBuf), m_Len(len) { | 406 : m_pBuf(pBuf), m_Len(len) { |
| 405 } | 407 } |
| 406 | 408 |
| 407 int Get_Block(void* param, unsigned long pos, unsigned char* pBuf, | 409 int GetBlock(void* param, |
| 408 unsigned long size) { | 410 unsigned long pos, |
| 409 TestLoader* pLoader = (TestLoader*) param; | 411 unsigned char* pBuf, |
| 412 unsigned long size) { | |
| 413 TestLoader* pLoader = static_cast<TestLoader*>(param); | |
| 410 if (pos + size < pos || pos + size > pLoader->m_Len) return 0; | 414 if (pos + size < pos || pos + size > pLoader->m_Len) return 0; |
| 411 memcpy(pBuf, pLoader->m_pBuf + pos, size); | 415 memcpy(pBuf, pLoader->m_pBuf + pos, size); |
| 412 return 1; | 416 return 1; |
| 413 } | 417 } |
| 414 | 418 |
| 415 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { | 419 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
| 416 return true; | 420 return true; |
| 417 } | 421 } |
| 418 | 422 |
| 419 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { | 423 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 432 FPDF_FORMFILLINFO form_callbacks; | 436 FPDF_FORMFILLINFO form_callbacks; |
| 433 memset(&form_callbacks, '\0', sizeof(form_callbacks)); | 437 memset(&form_callbacks, '\0', sizeof(form_callbacks)); |
| 434 form_callbacks.version = 2; | 438 form_callbacks.version = 2; |
| 435 form_callbacks.m_pJsPlatform = &platform_callbacks; | 439 form_callbacks.m_pJsPlatform = &platform_callbacks; |
| 436 | 440 |
| 437 TestLoader loader(pBuf, len); | 441 TestLoader loader(pBuf, len); |
| 438 | 442 |
| 439 FPDF_FILEACCESS file_access; | 443 FPDF_FILEACCESS file_access; |
| 440 memset(&file_access, '\0', sizeof(file_access)); | 444 memset(&file_access, '\0', sizeof(file_access)); |
| 441 file_access.m_FileLen = static_cast<unsigned long>(len); | 445 file_access.m_FileLen = static_cast<unsigned long>(len); |
| 442 file_access.m_GetBlock = Get_Block; | 446 file_access.m_GetBlock = GetBlock; |
| 443 file_access.m_Param = &loader; | 447 file_access.m_Param = &loader; |
| 444 | 448 |
| 445 FX_FILEAVAIL file_avail; | 449 FX_FILEAVAIL file_avail; |
| 446 memset(&file_avail, '\0', sizeof(file_avail)); | 450 memset(&file_avail, '\0', sizeof(file_avail)); |
| 447 file_avail.version = 1; | 451 file_avail.version = 1; |
| 448 file_avail.IsDataAvail = Is_Data_Avail; | 452 file_avail.IsDataAvail = Is_Data_Avail; |
| 449 | 453 |
| 450 FX_DOWNLOADHINTS hints; | 454 FX_DOWNLOADHINTS hints; |
| 451 memset(&hints, '\0', sizeof(hints)); | 455 memset(&hints, '\0', sizeof(hints)); |
| 452 hints.version = 1; | 456 hints.version = 1; |
| 453 hints.AddSegment = Add_Segment; | 457 hints.AddSegment = Add_Segment; |
| 454 | 458 |
| 455 FPDF_DOCUMENT doc; | 459 FPDF_DOCUMENT doc; |
| 456 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); | 460 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); |
| 457 | 461 |
| 458 (void) FPDFAvail_IsDocAvail(pdf_avail, &hints); | 462 (void)FPDFAvail_IsDocAvail(pdf_avail, &hints); |
| 459 | 463 |
| 460 if (!FPDFAvail_IsLinearized(pdf_avail)) { | 464 if (!FPDFAvail_IsLinearized(pdf_avail)) { |
| 461 fprintf(stderr, "Non-linearized path...\n"); | 465 fprintf(stderr, "Non-linearized path...\n"); |
| 462 doc = FPDF_LoadCustomDocument(&file_access, NULL); | 466 doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 463 } else { | 467 } else { |
| 464 fprintf(stderr, "Linearized path...\n"); | 468 fprintf(stderr, "Linearized path...\n"); |
| 465 doc = FPDFAvail_GetDocument(pdf_avail, NULL); | 469 doc = FPDFAvail_GetDocument(pdf_avail, nullptr); |
| 466 } | 470 } |
| 467 | 471 |
| 468 if (!doc) | 472 if (!doc) { |
|
Lei Zhang
2015/09/23 21:48:28
We conflicted here. This looks reasonable so I'll
Tom Sepez
2015/09/23 21:52:12
Acknowledged.
| |
| 469 { | |
| 470 fprintf(stderr, "Load pdf docs unsuccessful.\n"); | 473 fprintf(stderr, "Load pdf docs unsuccessful.\n"); |
| 471 return; | 474 return; |
| 472 } | 475 } |
| 473 | 476 |
| 474 (void) FPDF_GetDocPermissions(doc); | 477 (void)FPDF_GetDocPermissions(doc); |
| 475 (void) FPDFAvail_IsFormAvail(pdf_avail, &hints); | 478 (void)FPDFAvail_IsFormAvail(pdf_avail, &hints); |
| 476 | 479 |
| 477 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); | 480 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); |
| 478 int docType = DOCTYPE_PDF; | 481 int docType = DOCTYPE_PDF; |
| 479 if (FPDF_HasXFAField(doc, &docType)) | 482 if (FPDF_HasXFAField(doc, &docType)) |
| 480 { | 483 { |
| 481 if (docType != DOCTYPE_PDF && !FPDF_LoadXFA(doc)) | 484 if (docType != DOCTYPE_PDF && !FPDF_LoadXFA(doc)) |
| 482 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); | 485 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); |
| 483 } | 486 } |
| 484 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); | 487 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); |
| 485 FPDF_SetFormFieldHighlightAlpha(form, 100); | 488 FPDF_SetFormFieldHighlightAlpha(form, 100); |
| 486 | 489 |
| 487 int first_page = FPDFAvail_GetFirstPageNum(doc); | 490 int first_page = FPDFAvail_GetFirstPageNum(doc); |
| 488 (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints); | 491 (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints); |
| 489 | 492 |
| 490 int page_count = FPDF_GetPageCount(doc); | 493 int page_count = FPDF_GetPageCount(doc); |
| 491 for (int i = 0; i < page_count; ++i) { | 494 for (int i = 0; i < page_count; ++i) { |
| 492 (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints); | 495 (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints); |
| 493 } | 496 } |
| 494 | 497 |
| 495 FORM_DoDocumentJSAction(form); | 498 FORM_DoDocumentJSAction(form); |
| 496 FORM_DoDocumentOpenAction(form); | 499 FORM_DoDocumentOpenAction(form); |
| 497 | 500 |
| 498 int rendered_pages = 0; | 501 int rendered_pages = 0; |
| 499 int bad_pages = 0; | 502 int bad_pages = 0; |
| 500 for (int i = 0; i < page_count; ++i) { | 503 for (int i = 0; i < page_count; ++i) { |
| 501 FPDF_PAGE page = FPDF_LoadPage(doc, i); | 504 FPDF_PAGE page = FPDF_LoadPage(doc, i); |
| 502 if (!page) { | 505 if (!page) { |
| 503 bad_pages ++; | 506 ++bad_pages; |
| 504 continue; | 507 continue; |
| 505 } | 508 } |
| 506 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); | 509 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); |
| 507 FORM_OnAfterLoadPage(page, form); | 510 FORM_OnAfterLoadPage(page, form); |
| 508 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); | 511 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); |
| 509 | 512 |
| 510 double scale = 1.0; | 513 double scale = 1.0; |
| 511 if (!options.scale_factor_as_string.empty()) { | 514 if (!options.scale_factor_as_string.empty()) { |
| 512 std::stringstream(options.scale_factor_as_string) >> scale; | 515 std::stringstream(options.scale_factor_as_string) >> scale; |
| 513 } | 516 } |
| 514 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); | 517 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); |
| 515 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); | 518 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); |
| 516 | 519 |
| 517 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); | 520 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); |
| 518 if (!bitmap) { | 521 if (!bitmap) { |
| 519 fprintf(stderr, "Page was too large to be rendered.\n"); | 522 fprintf(stderr, "Page was too large to be rendered.\n"); |
| 520 bad_pages++; | 523 bad_pages++; |
| 521 continue; | 524 continue; |
| 522 } | 525 } |
| 523 | 526 |
| 524 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); | 527 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 525 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); | 528 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); |
| 526 rendered_pages ++; | 529 ++rendered_pages; |
| 527 | 530 |
| 528 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); | 531 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); |
| 529 int stride = FPDFBitmap_GetStride(bitmap); | 532 int stride = FPDFBitmap_GetStride(bitmap); |
| 530 const char* buffer = | 533 const char* buffer = |
| 531 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); | 534 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); |
| 532 | 535 |
| 533 switch (options.output_format) { | 536 switch (options.output_format) { |
| 534 #ifdef _WIN32 | 537 #ifdef _WIN32 |
| 535 case OUTPUT_BMP: | 538 case OUTPUT_BMP: |
| 536 WriteBmp(name.c_str(), i, buffer, stride, width, height); | 539 WriteBmp(name.c_str(), i, buffer, stride, width, height); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 629 RenderPdf(filename, file_contents, file_length, options); | 632 RenderPdf(filename, file_contents, file_length, options); |
| 630 free(file_contents); | 633 free(file_contents); |
| 631 } | 634 } |
| 632 | 635 |
| 633 FPDF_DestroyLibrary(); | 636 FPDF_DestroyLibrary(); |
| 634 v8::V8::ShutdownPlatform(); | 637 v8::V8::ShutdownPlatform(); |
| 635 delete platform; | 638 delete platform; |
| 636 | 639 |
| 637 return 0; | 640 return 0; |
| 638 } | 641 } |
| OLD | NEW |