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

Side by Side Diff: tools/parse_pdfs_main.cpp

Issue 13483012: podofo draft Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « gyp/tools.gyp ('k') | tools/podofo_config.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkCanvas.h"
9 #include "SkDevice.h"
10 #include "SkGraphics.h"
11 #include "SkImageDecoder.h"
12 #include "SkOSFile.h"
13 #include "SkPicture.h"
14 #include "SkStream.h"
15 #include "SkTArray.h"
16 #include "PdfRenderer.h"
17 #include "picture_utils.h"
18
19 #ifdef SK_USE_CDB
20 #include "win_dbghelp.h"
21 #endif
22
23 #include <iostream>
24 #include <cstdio>
25
26 #include "podofo.h"
27 #include "TextExtractor.h"
28
29
30 using namespace std;
31 using namespace PoDoFo;
32
33 int total = 0;
34
35 class SkPdfParser : public SkRefCnt {
36 public:
37 bool load(const SkString inputFileName, SkPicture* out) {
38
39 try
40 {
41 std::cout << "Processing: " << inputFileName.c_str() << std::endl;
42
43 PdfMemDocument doc( inputFileName.c_str() );
44 if( !doc.GetPageCount() )
45 {
46 std::cout << "ERROR: This document contains no page!" << inputFileNa me.c_str() << std::endl;
47 return false;
48 } else {
49 return true;
50 }
51 }
52 catch( const PdfError & e )
53 {
54 std::cout << "ERROR: This document can't be parsed!" << inputFileName.c_ str() << std::endl;
55 return false;
56 }
57
58 return true;
59 }
60 bool write(void*) const { return false; }
61 };
62
63 /**
64 * TODO parse_pdfs
65 *
66 * Given list of directories and files to use as input, expects to find .skp
67 * files and it will convert them to .pdf files writing them in the output
68 * directory.
69 *
70 * Returns zero exit code if all .skp files were converted successfully,
71 * otherwise returns error code 1.
72 */
73
74 static const char PDF_FILE_EXTENSION[] = "pdf";
75 static const char SKP_FILE_EXTENSION[] = "skp";
76
77 static void usage(const char* argv0) {
78 SkDebugf("SKP to PDF rendering tool\n");
79 SkDebugf("\n"
80 "Usage: \n"
81 " %s <input>... -w <outputDir> \n"
82 , argv0);
83 SkDebugf("\n\n");
84 SkDebugf(
85 " input: A list of directories and files to use as input. Files are\n"
86 " expected to have the .skp extension.\n\n");
87 SkDebugf(
88 " outputDir: directory to write the rendered pdfs.\n\n");
89 SkDebugf("\n");
90 }
91
92 /** Replaces the extension of a file.
93 * @param path File name whose extension will be changed.
94 * @param old_extension The old extension.
95 * @param new_extension The new extension.
96 * @returns false if the file did not has the expected extension.
97 * if false is returned, contents of path are undefined.
98 */
99 static bool replace_filename_extension(SkString* path,
100 const char old_extension[],
101 const char new_extension[]) {
102 if (path->endsWith(old_extension)) {
103 path->remove(path->size() - strlen(old_extension),
104 strlen(old_extension));
105 if (!path->endsWith(".")) {
106 return false;
107 }
108 path->append(new_extension);
109 return true;
110 }
111 return false;
112 }
113
114 /** Builds the output filename. path = dir/name, and it replaces expected
115 * .skp extension with .pdf extention.
116 * @param path Output filename.
117 * @param name The name of the file.
118 * @returns false if the file did not has the expected extension.
119 * if false is returned, contents of path are undefined.
120 */
121 static bool make_output_filepath(SkString* path, const SkString& dir,
122 const SkString& name) {
123 sk_tools::make_filepath(path, dir, name);
124 return replace_filename_extension(path,
125 PDF_FILE_EXTENSION,
126 SKP_FILE_EXTENSION);
127 }
128
129 /** Write the output of pdf renderer to a file.
130 * @param outputDir Output dir.
131 * @param inputFilename The skp file that was read.
132 * @param renderer The object responsible to write the pdf file.
133 */
134 static bool write_output(const SkString& outputDir,
135 const SkString& inputFilename,
136 const SkPdfParser& renderer) {
137 if (outputDir.isEmpty()) {
138 SkDynamicMemoryWStream stream;
139 renderer.write(&stream);
140 return true;
141 }
142
143 SkString outputPath;
144 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
145 return false;
146 }
147
148 SkFILEWStream stream(outputPath.c_str());
149 if (!stream.isValid()) {
150 SkDebugf("Could not write to file %s\n", outputPath.c_str());
151 return false;
152 }
153 renderer.write(&stream);
154
155 return true;
156 }
157
158 /** Reads an skp file, renders it to pdf and writes the output to a pdf file
159 * @param inputPath The skp file to be read.
160 * @param outputDir Output dir.
161 * @param renderer The object responsible to render the skp object into pdf.
162 */
163 static bool parse_pdf(const SkString& inputPath, const SkString& outputDir,
164 SkPdfParser& renderer) {
165 SkString inputFilename;
166 sk_tools::get_basename(&inputFilename, inputPath);
167
168 SkFILEStream inputStream;
169 inputStream.setPath(inputPath.c_str());
170 if (!inputStream.isValid()) {
171 SkDebugf("Could not open file %s\n", inputPath.c_str());
172 return false;
173 }
174
175 bool success = false;
176 // SkAutoTUnref<SkPicture> picture(SkNEW(SkPicture));
177
178 success = renderer.load(inputPath, NULL);
179
180
181 // success = write_output(outputDir, inputFilename, renderer);
182
183 //renderer.end();
184 return success;
185 }
186
187 /** For each file in the directory or for the file passed in input, call
188 * parse_pdf.
189 * @param input A directory or an pdf file.
190 * @param outputDir Output dir.
191 * @param renderer The object responsible to render the skp object into pdf.
192 */
193 static int process_input(const SkString& input, const SkString& outputDir,
194 SkPdfParser& renderer) {
195 int failures = 0;
196 if (sk_isdir(input.c_str())) {
197 SkOSFile::Iter iter(input.c_str(), PDF_FILE_EXTENSION);
198 SkString inputFilename;
199 while (iter.next(&inputFilename)) {
200 SkString inputPath;
201 sk_tools::make_filepath(&inputPath, input, inputFilename);
202 if (!parse_pdf(inputPath, outputDir, renderer)) {
203 ++failures;
204 }
205 }
206 } else {
207 SkString inputPath(input);
208 if (!parse_pdf(inputPath, outputDir, renderer)) {
209 ++failures;
210 }
211 }
212 return failures;
213 }
214
215 static void parse_commandline(int argc, char* const argv[],
216 SkTArray<SkString>* inputs,
217 SkString* outputDir) {
218 const char* argv0 = argv[0];
219 char* const* stop = argv + argc;
220
221 for (++argv; argv < stop; ++argv) {
222 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
223 usage(argv0);
224 exit(-1);
225 } else if (0 == strcmp(*argv, "-w")) {
226 ++argv;
227 if (argv >= stop) {
228 SkDebugf("Missing outputDir for -w\n");
229 usage(argv0);
230 exit(-1);
231 }
232 *outputDir = SkString(*argv);
233 } else {
234 inputs->push_back(SkString(*argv));
235 }
236 }
237
238 if (inputs->count() < 1) {
239 usage(argv0);
240 exit(-1);
241 }
242 }
243
244 int tool_main_core(int argc, char** argv);
245 int tool_main_core(int argc, char** argv) {
246 SkAutoGraphics ag;
247 SkTArray<SkString> inputs;
248
249 SkAutoTUnref<SkPdfParser>
250 renderer(SkNEW(SkPdfParser));
251 SkASSERT(renderer.get());
252
253 SkString outputDir;
254 parse_commandline(argc, argv, &inputs, &outputDir);
255
256 int failures = 0;
257 for (int i = 0; i < inputs.count(); i ++) {
258 failures += process_input(inputs[i], outputDir, *renderer);
259 }
260
261 if (failures != 0) {
262 SkDebugf("Failed to render %i PDFs.\n", failures);
263 return 1;
264 }
265
266 return 0;
267 }
268
269 int tool_main(int argc, char** argv);
270 int tool_main(int argc, char** argv) {
271 #ifdef SK_USE_CDB
272 setUpDebuggingFromArgs(argv[0]);
273 __try {
274 #endif
275 return tool_main_core(argc, argv);
276 #ifdef SK_USE_CDB
277 }
278 __except(GenerateDumpAndPrintCallstack(GetExceptionInformation()))
279 {
280 return -1;
281 }
282 #endif
283 return 0;
284 }
285
286 #if !defined SK_BUILD_FOR_IOS
287 int main(int argc, char * const argv[]) {
288 return tool_main(argc, (char**) argv);
289 }
290 #endif
OLDNEW
« no previous file with comments | « gyp/tools.gyp ('k') | tools/podofo_config.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698