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

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

Issue 68263003: Use SkPathJoin and SkBasename in Pdfviewer. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 1 month 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkCanvas.h" 8 #include "SkCanvas.h"
9 #include "SkCommandLineFlags.h" 9 #include "SkCommandLineFlags.h"
10 #include "SkDevice.h" 10 #include "SkDevice.h"
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 } 79 }
80 if (page >= 0) { 80 if (page >= 0) {
81 path->appendf("%i.", page); 81 path->appendf("%i.", page);
82 } 82 }
83 path->append(new_extension); 83 path->append(new_extension);
84 return true; 84 return true;
85 } 85 }
86 return false; 86 return false;
87 } 87 }
88 88
89 static void make_filepath(SkString* path, const SkString& dir, const SkString& n ame) {
90 size_t len = dir.size();
91 path->set(dir);
92 if (0 < len && '/' != dir[len - 1]) {
93 path->append("/");
94 }
95 path->append(name);
96 }
97
98 static bool is_path_seperator(const char chr) {
99 #if defined(SK_BUILD_FOR_WIN)
100 return chr == '\\' || chr == '/';
101 #else
102 return chr == '/';
103 #endif
104 }
105
106 static void get_basename(SkString* basename, const SkString& path) {
107 if (path.size() == 0) {
108 basename->reset();
109 return;
110 }
111
112 size_t end = path.size() - 1;
113
114 // Paths pointing to directories often have a trailing slash,
115 // we remove it so the name is not empty
116 if (is_path_seperator(path[end])) {
117 if (end == 0) {
118 basename->reset();
119 return;
120 }
121
122 end -= 1;
123 }
124
125 size_t i = end;
126 do {
127 --i;
128 if (is_path_seperator(path[i])) {
129 const char* basenameStart = path.c_str() + i + 1;
130 size_t basenameLength = end - i;
131 basename->set(basenameStart, basenameLength);
132 return;
133 }
134 } while (i > 0);
135
136 basename->set(path.c_str(), end + 1);
137 }
138
139 /** Builds the output filename. path = dir/name, and it replaces expected 89 /** Builds the output filename. path = dir/name, and it replaces expected
140 * .skp extension with .pdf extention. 90 * .skp extension with .pdf extention.
141 * @param path Output filename. 91 * @param path Output filename.
142 * @param name The name of the file. 92 * @param name The name of the file.
143 * @returns false if the file did not has the expected extension. 93 * @returns false if the file did not has the expected extension.
144 * if false is returned, contents of path are undefined. 94 * if false is returned, contents of path are undefined.
145 */ 95 */
146 static bool make_output_filepath(SkString* path, const SkString& dir, 96 static bool make_output_filepath(SkString* path, const SkString& dir,
147 const SkString& name, 97 const SkString& name,
148 int page) { 98 int page) {
149 make_filepath(path, dir, name); 99 *path = SkOSPath::SkPathJoin(dir.c_str(), name.c_str());
150 return add_page_and_replace_filename_extension(path, page, 100 return add_page_and_replace_filename_extension(path, page,
151 PDF_FILE_EXTENSION, 101 PDF_FILE_EXTENSION,
152 PNG_FILE_EXTENSION); 102 PNG_FILE_EXTENSION);
153 } 103 }
154 104
155 static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color) { 105 static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color) {
156 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); 106 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
157 107
158 bitmap->allocPixels(); 108 bitmap->allocPixels();
159 bitmap->eraseColor(color); 109 bitmap->eraseColor(color);
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 207
258 /** Reads an skp file, renders it to pdf and writes the output to a pdf file 208 /** Reads an skp file, renders it to pdf and writes the output to a pdf file
259 * @param inputPath The skp file to be read. 209 * @param inputPath The skp file to be read.
260 * @param outputDir Output dir. 210 * @param outputDir Output dir.
261 * @param renderer The object responsible to render the skp object into pdf. 211 * @param renderer The object responsible to render the skp object into pdf.
262 */ 212 */
263 static bool process_pdf(const SkString& inputPath, const SkString& outputDir, 213 static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
264 SkPdfRenderer& renderer) { 214 SkPdfRenderer& renderer) {
265 SkDebugf("Loading PDF: %s\n", inputPath.c_str()); 215 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
266 216
267 SkString inputFilename; 217 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
268 get_basename(&inputFilename, inputPath);
269 218
270 bool success = true; 219 bool success = true;
271 220
272 success = renderer.load(inputPath); 221 success = renderer.load(inputPath);
273 if (FLAGS_showMemoryUsage) { 222 if (FLAGS_showMemoryUsage) {
274 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUs ed()); 223 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUs ed());
275 } 224 }
276 225
277 // TODO(edisonn): bench timers 226 // TODO(edisonn): bench timers
278 if (FLAGS_benchLoad > 0) { 227 if (FLAGS_benchLoad > 0) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 * @param outputDir Output dir. 299 * @param outputDir Output dir.
351 * @param renderer The object responsible to render the skp object into pdf. 300 * @param renderer The object responsible to render the skp object into pdf.
352 */ 301 */
353 static int process_input(const char* input, const SkString& outputDir, 302 static int process_input(const char* input, const SkString& outputDir,
354 SkPdfRenderer& renderer) { 303 SkPdfRenderer& renderer) {
355 int failures = 0; 304 int failures = 0;
356 if (sk_isdir(input)) { 305 if (sk_isdir(input)) {
357 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION); 306 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
358 SkString inputFilename; 307 SkString inputFilename;
359 while (iter.next(&inputFilename)) { 308 while (iter.next(&inputFilename)) {
360 SkString inputPath; 309 SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str ());
361 SkString _input;
362 _input.append(input);
363 make_filepath(&inputPath, _input, inputFilename);
364 if (!process_pdf(inputPath, outputDir, renderer)) { 310 if (!process_pdf(inputPath, outputDir, renderer)) {
365 ++failures; 311 ++failures;
366 } 312 }
367 } 313 }
368 } else { 314 } else {
369 SkString inputPath(input); 315 SkString inputPath(input);
370 if (!process_pdf(inputPath, outputDir, renderer)) { 316 if (!process_pdf(inputPath, outputDir, renderer)) {
371 ++failures; 317 ++failures;
372 } 318 }
373 } 319 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 } 351 }
406 352
407 return 0; 353 return 0;
408 } 354 }
409 355
410 #if !defined SK_BUILD_FOR_IOS 356 #if !defined SK_BUILD_FOR_IOS
411 int main(int argc, char * const argv[]) { 357 int main(int argc, char * const argv[]) {
412 return tool_main(argc, (char**) argv); 358 return tool_main(argc, (char**) argv);
413 } 359 }
414 #endif 360 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698