Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: samples/pdfium_test.cc

Issue 2578893004: Gold support in PDFium (Closed)
Patch Set: Cleanup and added to DEPS Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « samples/DEPS ('k') | testing/tools/common.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 9
10 #include <map> 10 #include <map>
11 #include <sstream> 11 #include <sstream>
12 #include <string> 12 #include <string>
13 #include <utility> 13 #include <utility>
14 #include <vector> 14 #include <vector>
15 15
16 #if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_ 16 #if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_
17 #define _SKIA_SUPPORT_ 17 #define _SKIA_SUPPORT_
18 #endif 18 #endif
19 19
20 #include "core/fdrm/crypto/fx_crypt.h"
20 #include "public/fpdf_dataavail.h" 21 #include "public/fpdf_dataavail.h"
21 #include "public/fpdf_edit.h" 22 #include "public/fpdf_edit.h"
22 #include "public/fpdf_ext.h" 23 #include "public/fpdf_ext.h"
23 #include "public/fpdf_formfill.h" 24 #include "public/fpdf_formfill.h"
24 #include "public/fpdf_text.h" 25 #include "public/fpdf_text.h"
25 #include "public/fpdfview.h" 26 #include "public/fpdfview.h"
26 #include "samples/image_diff_png.h" 27 #include "samples/image_diff_png.h"
27 #include "testing/test_support.h" 28 #include "testing/test_support.h"
28 29
29 #ifdef _WIN32 30 #ifdef _WIN32
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 } 103 }
103 104
104 static bool CheckDimensions(int stride, int width, int height) { 105 static bool CheckDimensions(int stride, int width, int height) {
105 if (stride < 0 || width < 0 || height < 0) 106 if (stride < 0 || width < 0 || height < 0)
106 return false; 107 return false;
107 if (height > 0 && width > INT_MAX / height) 108 if (height > 0 && width > INT_MAX / height)
108 return false; 109 return false;
109 return true; 110 return true;
110 } 111 }
111 112
112 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, 113 static void OutputMD5Hash(const char* file_name, const char* buffer, int len) {
113 int stride, int width, int height) { 114 // Get the MD5 hash and write it to stdout.
115 uint8_t digest[16];
116 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(buffer), len, digest);
117 printf("MD5:%s:", file_name);
118 for (int i = 0; i < 16; i++) {
119 printf("%02x", digest[i]);
caryclark 2016/12/20 22:42:59 pdfium style doesn't require braces here
stephana 2016/12/21 16:12:06 Done.
120 }
121 printf("\n");
122 }
123
124 static std::string WritePpm(const char* pdf_name,
125 int num,
126 const void* buffer_void,
127 int stride,
128 int width,
129 int height) {
114 const char* buffer = reinterpret_cast<const char*>(buffer_void); 130 const char* buffer = reinterpret_cast<const char*>(buffer_void);
115 131
116 if (!CheckDimensions(stride, width, height)) 132 if (!CheckDimensions(stride, width, height))
117 return; 133 return "";
118 134
119 int out_len = width * height; 135 int out_len = width * height;
120 if (out_len > INT_MAX / 3) 136 if (out_len > INT_MAX / 3)
121 return; 137 return "";
122 out_len *= 3; 138 out_len *= 3;
123 139
124 char filename[256]; 140 char filename[256];
125 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num); 141 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
126 FILE* fp = fopen(filename, "wb"); 142 FILE* fp = fopen(filename, "wb");
127 if (!fp) 143 if (!fp)
128 return; 144 return "";
129 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height); 145 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
130 // Source data is B, G, R, unused. 146 // Source data is B, G, R, unused.
131 // Dest data is R, G, B. 147 // Dest data is R, G, B.
132 std::vector<char> result(out_len); 148 std::vector<char> result(out_len);
133 for (int h = 0; h < height; ++h) { 149 for (int h = 0; h < height; ++h) {
134 const char* src_line = buffer + (stride * h); 150 const char* src_line = buffer + (stride * h);
135 char* dest_line = result.data() + (width * h * 3); 151 char* dest_line = result.data() + (width * h * 3);
136 for (int w = 0; w < width; ++w) { 152 for (int w = 0; w < width; ++w) {
137 // R 153 // R
138 dest_line[w * 3] = src_line[(w * 4) + 2]; 154 dest_line[w * 3] = src_line[(w * 4) + 2];
139 // G 155 // G
140 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1]; 156 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
141 // B 157 // B
142 dest_line[(w * 3) + 2] = src_line[w * 4]; 158 dest_line[(w * 3) + 2] = src_line[w * 4];
143 } 159 }
144 } 160 }
145 fwrite(result.data(), out_len, 1, fp); 161 fwrite(result.data(), out_len, 1, fp);
146 fclose(fp); 162 fclose(fp);
163 return std::string(filename);
147 } 164 }
148 165
149 void WriteText(FPDF_PAGE page, const char* pdf_name, int num) { 166 void WriteText(FPDF_PAGE page, const char* pdf_name, int num) {
150 char filename[256]; 167 char filename[256];
151 int chars_formatted = 168 int chars_formatted =
152 snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num); 169 snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num);
153 if (chars_formatted < 0 || 170 if (chars_formatted < 0 ||
154 static_cast<size_t>(chars_formatted) >= sizeof(filename)) { 171 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
155 fprintf(stderr, "Filename %s is too long\n", filename); 172 fprintf(stderr, "Filename %s is too long\n", filename);
156 return; 173 return;
(...skipping 13 matching lines...) Expand all
170 for (int i = 0; i < FPDFText_CountChars(textpage); i++) { 187 for (int i = 0; i < FPDFText_CountChars(textpage); i++) {
171 uint32_t c = FPDFText_GetUnicode(textpage, i); 188 uint32_t c = FPDFText_GetUnicode(textpage, i);
172 fwrite(&c, sizeof(c), 1, fp); 189 fwrite(&c, sizeof(c), 1, fp);
173 } 190 }
174 191
175 FPDFText_ClosePage(textpage); 192 FPDFText_ClosePage(textpage);
176 193
177 (void)fclose(fp); 194 (void)fclose(fp);
178 } 195 }
179 196
180 static void WritePng(const char* pdf_name, int num, const void* buffer_void, 197 static std::string WritePng(const char* pdf_name,
181 int stride, int width, int height) { 198 int num,
199 const void* buffer_void,
200 int stride,
201 int width,
202 int height) {
182 if (!CheckDimensions(stride, width, height)) 203 if (!CheckDimensions(stride, width, height))
183 return; 204 return "";
184 205
185 std::vector<unsigned char> png_encoding; 206 std::vector<unsigned char> png_encoding;
186 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void); 207 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
187 if (!image_diff_png::EncodeBGRAPNG( 208 if (!image_diff_png::EncodeBGRAPNG(
188 buffer, width, height, stride, false, &png_encoding)) { 209 buffer, width, height, stride, false, &png_encoding)) {
189 fprintf(stderr, "Failed to convert bitmap to PNG\n"); 210 fprintf(stderr, "Failed to convert bitmap to PNG\n");
190 return; 211 return "";
191 } 212 }
192 213
193 char filename[256]; 214 char filename[256];
194 int chars_formatted = snprintf( 215 int chars_formatted = snprintf(
195 filename, sizeof(filename), "%s.%d.png", pdf_name, num); 216 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
196 if (chars_formatted < 0 || 217 if (chars_formatted < 0 ||
197 static_cast<size_t>(chars_formatted) >= sizeof(filename)) { 218 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
198 fprintf(stderr, "Filename %s is too long\n", filename); 219 fprintf(stderr, "Filename %s is too long\n", filename);
199 return; 220 return "";
200 } 221 }
201 222
202 FILE* fp = fopen(filename, "wb"); 223 FILE* fp = fopen(filename, "wb");
203 if (!fp) { 224 if (!fp) {
204 fprintf(stderr, "Failed to open %s for output\n", filename); 225 fprintf(stderr, "Failed to open %s for output\n", filename);
205 return; 226 return "";
206 } 227 }
207 228
208 size_t bytes_written = fwrite( 229 size_t bytes_written = fwrite(
209 &png_encoding.front(), 1, png_encoding.size(), fp); 230 &png_encoding.front(), 1, png_encoding.size(), fp);
210 if (bytes_written != png_encoding.size()) 231 if (bytes_written != png_encoding.size())
211 fprintf(stderr, "Failed to write to %s\n", filename); 232 fprintf(stderr, "Failed to write to %s\n", filename);
212 233
213 (void)fclose(fp); 234 (void)fclose(fp);
235 return std::string(filename);
214 } 236 }
215 237
216 #ifdef _WIN32 238 #ifdef _WIN32
217 static void WriteBmp(const char* pdf_name, int num, const void* buffer, 239 static std::string WriteBmp(const char* pdf_name,
218 int stride, int width, int height) { 240 int num,
241 const void* buffer,
242 int stride,
243 int width,
244 int height) {
219 if (!CheckDimensions(stride, width, height)) 245 if (!CheckDimensions(stride, width, height))
220 return; 246 return "";
221 247
222 int out_len = stride * height; 248 int out_len = stride * height;
223 if (out_len > INT_MAX / 3) 249 if (out_len > INT_MAX / 3)
224 return; 250 return "";
225 251
226 char filename[256]; 252 char filename[256];
227 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num); 253 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
228 FILE* fp = fopen(filename, "wb"); 254 FILE* fp = fopen(filename, "wb");
229 if (!fp) 255 if (!fp)
230 return; 256 return "";
231 257
232 BITMAPINFO bmi = {}; 258 BITMAPINFO bmi = {};
233 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD); 259 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
234 bmi.bmiHeader.biWidth = width; 260 bmi.bmiHeader.biWidth = width;
235 bmi.bmiHeader.biHeight = -height; // top-down image 261 bmi.bmiHeader.biHeight = -height; // top-down image
236 bmi.bmiHeader.biPlanes = 1; 262 bmi.bmiHeader.biPlanes = 1;
237 bmi.bmiHeader.biBitCount = 32; 263 bmi.bmiHeader.biBitCount = 32;
238 bmi.bmiHeader.biCompression = BI_RGB; 264 bmi.bmiHeader.biCompression = BI_RGB;
239 bmi.bmiHeader.biSizeImage = 0; 265 bmi.bmiHeader.biSizeImage = 0;
240 266
241 BITMAPFILEHEADER file_header = {}; 267 BITMAPFILEHEADER file_header = {};
242 file_header.bfType = 0x4d42; 268 file_header.bfType = 0x4d42;
243 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len; 269 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
244 file_header.bfOffBits = file_header.bfSize - out_len; 270 file_header.bfOffBits = file_header.bfSize - out_len;
245 271
246 fwrite(&file_header, sizeof(file_header), 1, fp); 272 fwrite(&file_header, sizeof(file_header), 1, fp);
247 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp); 273 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
248 fwrite(buffer, out_len, 1, fp); 274 fwrite(buffer, out_len, 1, fp);
249 fclose(fp); 275 fclose(fp);
276 return std::string(filename);
250 } 277 }
251 278
252 void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) { 279 void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
253 int width = static_cast<int>(FPDF_GetPageWidth(page)); 280 int width = static_cast<int>(FPDF_GetPageWidth(page));
254 int height = static_cast<int>(FPDF_GetPageHeight(page)); 281 int height = static_cast<int>(FPDF_GetPageHeight(page));
255 282
256 char filename[256]; 283 char filename[256];
257 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num); 284 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
258 285
259 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr); 286 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
260 287
261 HRGN rgn = CreateRectRgn(0, 0, width, height); 288 HRGN rgn = CreateRectRgn(0, 0, width, height);
262 SelectClipRgn(dc, rgn); 289 SelectClipRgn(dc, rgn);
263 DeleteObject(rgn); 290 DeleteObject(rgn);
264 291
265 SelectObject(dc, GetStockObject(NULL_PEN)); 292 SelectObject(dc, GetStockObject(NULL_PEN));
266 SelectObject(dc, GetStockObject(WHITE_BRUSH)); 293 SelectObject(dc, GetStockObject(WHITE_BRUSH));
267 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less. 294 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
268 Rectangle(dc, 0, 0, width + 1, height + 1); 295 Rectangle(dc, 0, 0, width + 1, height + 1);
269 296
270 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, 297 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
271 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); 298 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
272 299
273 DeleteEnhMetaFile(CloseEnhMetaFile(dc)); 300 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
274 } 301 }
275 #endif 302 #endif
276 303
277 #ifdef PDF_ENABLE_SKIA 304 #ifdef PDF_ENABLE_SKIA
278 void WriteSkp(const char* pdf_name, int num, SkPictureRecorder* recorder) { 305 static std::string WriteSkp(const char* pdf_name,
306 int num,
307 SkPictureRecorder* recorder) {
279 char filename[256]; 308 char filename[256];
280 int chars_formatted = 309 int chars_formatted =
281 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num); 310 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num);
282 311
283 if (chars_formatted < 0 || 312 if (chars_formatted < 0 ||
284 static_cast<size_t>(chars_formatted) >= sizeof(filename)) { 313 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
285 fprintf(stderr, "Filename %s is too long\n", filename); 314 fprintf(stderr, "Filename %s is too long\n", filename);
286 return; 315 return "";
287 } 316 }
288 317
289 sk_sp<SkPicture> picture(recorder->finishRecordingAsPicture()); 318 sk_sp<SkPicture> picture(recorder->finishRecordingAsPicture());
290 SkFILEWStream wStream(filename); 319 SkFILEWStream wStream(filename);
291 picture->serialize(&wStream); 320 picture->serialize(&wStream);
321 return std::string(filename);
292 } 322 }
293 #endif 323 #endif
294 324
295 // These example JS platform callback handlers are entirely optional, 325 // These example JS platform callback handlers are entirely optional,
296 // and exist here to show the flow of information from a document back 326 // and exist here to show the flow of information from a document back
297 // to the embedder. 327 // to the embedder.
298 int ExampleAppAlert(IPDF_JSPLATFORM*, 328 int ExampleAppAlert(IPDF_JSPLATFORM*,
299 FPDF_WIDESTRING msg, 329 FPDF_WIDESTRING msg,
300 FPDF_WIDESTRING title, 330 FPDF_WIDESTRING title,
301 int type, 331 int type,
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 if (bitmap) { 651 if (bitmap) {
622 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF; 652 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
623 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color); 653 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
624 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, FPDF_ANNOT); 654 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, FPDF_ANNOT);
625 655
626 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, FPDF_ANNOT); 656 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, FPDF_ANNOT);
627 int stride = FPDFBitmap_GetStride(bitmap); 657 int stride = FPDFBitmap_GetStride(bitmap);
628 const char* buffer = 658 const char* buffer =
629 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); 659 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
630 660
661 std::string&& image_file_name = "";
631 switch (options.output_format) { 662 switch (options.output_format) {
632 #ifdef _WIN32 663 #ifdef _WIN32
633 case OUTPUT_BMP: 664 case OUTPUT_BMP:
634 WriteBmp(name.c_str(), page_index, buffer, stride, width, height); 665 image_file_name =
666 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
635 break; 667 break;
636 668
637 case OUTPUT_EMF: 669 case OUTPUT_EMF:
638 WriteEmf(page, name.c_str(), page_index); 670 WriteEmf(page, name.c_str(), page_index);
639 break; 671 break;
640 #endif 672 #endif
641 case OUTPUT_TEXT: 673 case OUTPUT_TEXT:
642 WriteText(page, name.c_str(), page_index); 674 WriteText(page, name.c_str(), page_index);
643 break; 675 break;
644 676
645 case OUTPUT_PNG: 677 case OUTPUT_PNG:
646 WritePng(name.c_str(), page_index, buffer, stride, width, height); 678 image_file_name =
679 WritePng(name.c_str(), page_index, buffer, stride, width, height);
647 break; 680 break;
648 681
649 case OUTPUT_PPM: 682 case OUTPUT_PPM:
650 WritePpm(name.c_str(), page_index, buffer, stride, width, height); 683 image_file_name =
684 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
651 break; 685 break;
652 686
653 #ifdef PDF_ENABLE_SKIA 687 #ifdef PDF_ENABLE_SKIA
654 case OUTPUT_SKP: { 688 case OUTPUT_SKP: {
655 std::unique_ptr<SkPictureRecorder> recorder( 689 std::unique_ptr<SkPictureRecorder> recorder(
656 reinterpret_cast<SkPictureRecorder*>( 690 reinterpret_cast<SkPictureRecorder*>(
657 FPDF_RenderPageSkp(page, width, height))); 691 FPDF_RenderPageSkp(page, width, height)));
658 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0); 692 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0);
659 WriteSkp(name.c_str(), page_index, recorder.get()); 693 image_file_name = WriteSkp(name.c_str(), page_index, recorder.get());
660 } break; 694 } break;
661 #endif 695 #endif
662 default: 696 default:
663 break; 697 break;
664 } 698 }
665 699
700 // Write the filename and the MD5 of the buffer to stdout if we wrote a
701 // file.
702 if (image_file_name != "") {
703 OutputMD5Hash(image_file_name.c_str(), buffer, stride * height);
caryclark 2016/12/20 22:42:59 Maybe add a new command line flag that permits thi
stephana 2016/12/21 16:12:06 Done.
704 }
705
666 FPDFBitmap_Destroy(bitmap); 706 FPDFBitmap_Destroy(bitmap);
667 } else { 707 } else {
668 fprintf(stderr, "Page was too large to be rendered.\n"); 708 fprintf(stderr, "Page was too large to be rendered.\n");
669 } 709 }
670 710
671 form_fill_info.loaded_pages.erase(page_index); 711 form_fill_info.loaded_pages.erase(page_index);
672 712
673 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); 713 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
674 FORM_OnBeforeClosePage(page, form); 714 FORM_OnBeforeClosePage(page, form);
675 FPDFText_ClosePage(text_page); 715 FPDFText_ClosePage(text_page);
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 delete platform; 1001 delete platform;
962 1002
963 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 1003 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
964 free(const_cast<char*>(natives.data)); 1004 free(const_cast<char*>(natives.data));
965 free(const_cast<char*>(snapshot.data)); 1005 free(const_cast<char*>(snapshot.data));
966 #endif // V8_USE_EXTERNAL_STARTUP_DATA 1006 #endif // V8_USE_EXTERNAL_STARTUP_DATA
967 #endif // PDF_ENABLE_V8 1007 #endif // PDF_ENABLE_V8
968 1008
969 return 0; 1009 return 0;
970 } 1010 }
OLDNEW
« no previous file with comments | « samples/DEPS ('k') | testing/tools/common.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698