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

Side by Side Diff: experimental/PdfViewer/pdf_viewer_main.cpp

Issue 20087003: pdfviewer: remove dependency on picture_utils. add utility function to render SkStream into bitmap. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 5 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
OLDNEW
1 #include "SkCanvas.h" 1 #include "SkCanvas.h"
2 #include "SkCommandLineFlags.h" 2 #include "SkCommandLineFlags.h"
3 #include "SkDevice.h" 3 #include "SkDevice.h"
4 #include "SkGraphics.h" 4 #include "SkGraphics.h"
5 #include "SkImageDecoder.h" 5 #include "SkImageDecoder.h"
6 #include "SkImageEncoder.h" 6 #include "SkImageEncoder.h"
7 #include "SkOSFile.h" 7 #include "SkOSFile.h"
8 #include "SkPicture.h" 8 #include "SkPicture.h"
9 #include "SkStream.h" 9 #include "SkStream.h"
10 #include "SkTypeface.h" 10 #include "SkTypeface.h"
11 #include "SkTArray.h" 11 #include "SkTArray.h"
12 #include "picture_utils.h"
13 #include "SkNulCanvas.h" 12 #include "SkNulCanvas.h"
14 13
15 #include "SkPdfRenderer.h" 14 #include "SkPdfRenderer.h"
16 15
17 DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to proces s."); 16 DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to proces s.");
18 DEFINE_string2(writePath, w, "", "Directory to write the rendered pages."); 17 DEFINE_string2(writePath, w, "", "Directory to write the rendered pages.");
19 DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page."); 18 DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page.");
20 DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage."); 19 DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage.");
21 DEFINE_string2(pages, p, "all", "What pages to render and how:\n" 20 DEFINE_string2(pages, p, "all", "What pages to render and how:\n"
22 "\tall - all pages\n" 21 "\tall - all pages\n"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 } 66 }
68 if (page >= 0) { 67 if (page >= 0) {
69 path->appendf("%i.", page); 68 path->appendf("%i.", page);
70 } 69 }
71 path->append(new_extension); 70 path->append(new_extension);
72 return true; 71 return true;
73 } 72 }
74 return false; 73 return false;
75 } 74 }
76 75
76 void make_filepath(SkString* path, const SkString& dir, const SkString& name) {
77 size_t len = dir.size();
78 path->set(dir);
79 if (0 < len && '/' != dir[len - 1]) {
80 path->append("/");
81 }
82 path->append(name);
83 }
84
85 bool is_path_seperator(const char chr) {
86 #if defined(SK_BUILD_FOR_WIN)
87 return chr == '\\' || chr == '/';
88 #else
89 return chr == '/';
90 #endif
91 }
92
93 void get_basename(SkString* basename, const SkString& path) {
94 if (path.size() == 0) {
95 basename->reset();
96 return;
97 }
98
99 size_t end = path.size() - 1;
100
101 // Paths pointing to directories often have a trailing slash,
102 // we remove it so the name is not empty
103 if (is_path_seperator(path[end])) {
104 if (end == 0) {
105 basename->reset();
106 return;
107 }
108
109 end -= 1;
110 }
111
112 size_t i = end;
113 do {
114 --i;
115 if (is_path_seperator(path[i])) {
116 const char* basenameStart = path.c_str() + i + 1;
117 size_t basenameLength = end - i;
118 basename->set(basenameStart, basenameLength);
119 return;
120 }
121 } while (i > 0);
122
123 basename->set(path.c_str(), end + 1);
124 }
125
77 /** Builds the output filename. path = dir/name, and it replaces expected 126 /** Builds the output filename. path = dir/name, and it replaces expected
78 * .skp extension with .pdf extention. 127 * .skp extension with .pdf extention.
79 * @param path Output filename. 128 * @param path Output filename.
80 * @param name The name of the file. 129 * @param name The name of the file.
81 * @returns false if the file did not has the expected extension. 130 * @returns false if the file did not has the expected extension.
82 * if false is returned, contents of path are undefined. 131 * if false is returned, contents of path are undefined.
83 */ 132 */
84
85
86 static bool make_output_filepath(SkString* path, const SkString& dir, 133 static bool make_output_filepath(SkString* path, const SkString& dir,
87 const SkString& name, 134 const SkString& name,
88 int page) { 135 int page) {
89 sk_tools::make_filepath(path, dir, name); 136 make_filepath(path, dir, name);
90 return add_page_and_replace_filename_extension(path, page, 137 return add_page_and_replace_filename_extension(path, page,
91 PDF_FILE_EXTENSION, 138 PDF_FILE_EXTENSION,
92 PNG_FILE_EXTENSION); 139 PNG_FILE_EXTENSION);
93 } 140 }
94 141
95 static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) { 142 static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
96 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); 143 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
97 144
98 bitmap->allocPixels(); 145 bitmap->allocPixels();
99 bitmap->eraseColor(color); 146 bitmap->eraseColor(color);
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 /** Reads an skp file, renders it to pdf and writes the output to a pdf file 207 /** Reads an skp file, renders it to pdf and writes the output to a pdf file
161 * @param inputPath The skp file to be read. 208 * @param inputPath The skp file to be read.
162 * @param outputDir Output dir. 209 * @param outputDir Output dir.
163 * @param renderer The object responsible to render the skp object into pdf. 210 * @param renderer The object responsible to render the skp object into pdf.
164 */ 211 */
165 static bool process_pdf(const SkString& inputPath, const SkString& outputDir, 212 static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
166 SkPdfRenderer& renderer) { 213 SkPdfRenderer& renderer) {
167 SkDebugf("Loading PDF: %s\n", inputPath.c_str()); 214 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
168 215
169 SkString inputFilename; 216 SkString inputFilename;
170 sk_tools::get_basename(&inputFilename, inputPath); 217 get_basename(&inputFilename, inputPath);
171 218
172 SkFILEStream inputStream; 219 SkFILEStream inputStream;
173 inputStream.setPath(inputPath.c_str()); 220 inputStream.setPath(inputPath.c_str());
174 if (!inputStream.isValid()) { 221 if (!inputStream.isValid()) {
175 SkDebugf("Could not open file %s\n", inputPath.c_str()); 222 SkDebugf("Could not open file %s\n", inputPath.c_str());
176 return false; 223 return false;
177 } 224 }
178 225
179 bool success = false; 226 bool success = false;
180 227
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 static int process_input(const char* input, const SkString& outputDir, 281 static int process_input(const char* input, const SkString& outputDir,
235 SkPdfRenderer& renderer) { 282 SkPdfRenderer& renderer) {
236 int failures = 0; 283 int failures = 0;
237 if (sk_isdir(input)) { 284 if (sk_isdir(input)) {
238 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION); 285 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
239 SkString inputFilename; 286 SkString inputFilename;
240 while (iter.next(&inputFilename)) { 287 while (iter.next(&inputFilename)) {
241 SkString inputPath; 288 SkString inputPath;
242 SkString _input; 289 SkString _input;
243 _input.append(input); 290 _input.append(input);
244 sk_tools::make_filepath(&inputPath, _input, inputFilename); 291 make_filepath(&inputPath, _input, inputFilename);
245 if (!process_pdf(inputPath, outputDir, renderer)) { 292 if (!process_pdf(inputPath, outputDir, renderer)) {
246 ++failures; 293 ++failures;
247 } 294 }
248 } 295 }
249 } else { 296 } else {
250 SkString inputPath(input); 297 SkString inputPath(input);
251 if (!process_pdf(inputPath, outputDir, renderer)) { 298 if (!process_pdf(inputPath, outputDir, renderer)) {
252 ++failures; 299 ++failures;
253 } 300 }
254 } 301 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 } 333 }
287 334
288 return 0; 335 return 0;
289 } 336 }
290 337
291 #if !defined SK_BUILD_FOR_IOS 338 #if !defined SK_BUILD_FOR_IOS
292 int main(int argc, char * const argv[]) { 339 int main(int argc, char * const argv[]) {
293 return tool_main(argc, (char**) argv); 340 return tool_main(argc, (char**) argv);
294 } 341 }
295 #endif 342 #endif
OLDNEW
« no previous file with comments | « experimental/PdfViewer/SkPdfRenderer.cpp ('k') | experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698