Chromium Code Reviews| Index: samples/pdfium_test.cc |
| diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc |
| index 5ee14642e4b7993f2d5de807f67f7427ca65ddfb..dc858db250221ce87a693129820a3b852dce549a 100644 |
| --- a/samples/pdfium_test.cc |
| +++ b/samples/pdfium_test.cc |
| @@ -42,6 +42,7 @@ |
| enum OutputFormat { |
| OUTPUT_NONE, |
| + OUTPUT_TEXT, |
| OUTPUT_PPM, |
| OUTPUT_PNG, |
| #ifdef _WIN32 |
| @@ -112,6 +113,36 @@ static void WritePpm(const char* pdf_name, int num, const void* buffer_void, |
| fclose(fp); |
| } |
| +void WriteText(FPDF_PAGE page, const char* pdf_name, int num) { |
| + char filename[256]; |
| + int chars_formatted = |
| + snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num); |
| + if (chars_formatted < 0 || |
| + static_cast<size_t>(chars_formatted) >= sizeof(filename)) { |
| + fprintf(stderr, "Filename %s is too long\n", filename); |
| + return; |
| + } |
| + |
| + FILE* fp = fopen(filename, "w"); |
| + if (!fp) { |
| + fprintf(stderr, "Failed to open %s for output\n", filename); |
| + return; |
| + } |
| + |
| + unsigned char bom[] = {0xFF, 0xFE, 0x00, 0x00}; |
|
hal.canary
2016/06/14 19:27:57
What is the output format? UTF-32-LE?
dsinclair
2016/06/14 20:25:27
Yup, UTF32-LE.
|
| + fwrite(bom, sizeof(unsigned char), sizeof(bom), fp); |
| + |
| + FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); |
| + for (int i = 0; i < FPDFText_CountChars(textpage); i++) { |
| + wchar_t c = FPDFText_GetUnicode(textpage, i); |
| + fwrite(&c, sizeof(wchar_t), 1, fp); |
|
hal.canary
2016/06/14 19:23:12
sizeof(wchar_t) is "compiler-dependent and therefo
dsinclair
2016/06/14 20:25:27
Done.
|
| + } |
| + |
| + FPDFText_ClosePage(textpage); |
| + |
| + (void)fclose(fp); |
| +} |
| + |
| static void WritePng(const char* pdf_name, int num, const void* buffer_void, |
| int stride, int width, int height) { |
| if (!CheckDimensions(stride, width, height)) |
| @@ -354,6 +385,12 @@ bool ParseCommandLine(const std::vector<std::string>& args, |
| return false; |
| } |
| options->output_format = OUTPUT_PNG; |
| + } else if (cur_arg == "--text") { |
| + if (options->output_format != OUTPUT_NONE) { |
| + fprintf(stderr, "Duplicate or conflicting --text argument\n"); |
| + return false; |
| + } |
| + options->output_format = OUTPUT_TEXT; |
| #ifdef PDF_ENABLE_SKIA |
| } else if (cur_arg == "--skp") { |
| if (options->output_format != OUTPUT_NONE) { |
| @@ -528,6 +565,10 @@ bool RenderPage(const std::string& name, |
| WriteEmf(page, name.c_str(), page_index); |
| break; |
| #endif |
| + case OUTPUT_TEXT: |
| + WriteText(page, name.c_str(), page_index); |
| + break; |
| + |
| case OUTPUT_PNG: |
| WritePng(name.c_str(), page_index, buffer, stride, width, height); |
| break; |