twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 "picture_utils.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkPicture.h" |
| 11 | #include "SkString.h" |
| 12 | #include "SkStream.h" |
| 13 | |
| 14 | namespace sk_tools { |
| 15 | |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 16 | void make_filepath(SkString* path, const SkString& dir, const SkString& name) { |
| 17 | size_t len = dir.size(); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 18 | path->set(dir); |
| 19 | if (0 < len && '/' != dir[len - 1]) { |
| 20 | path->append("/"); |
| 21 | } |
| 22 | path->append(name); |
| 23 | } |
| 24 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | bool is_path_seperator(const char chr) { |
| 27 | #if defined(SK_BUILD_FOR_WIN) |
| 28 | return chr == '\\' || chr == '/'; |
| 29 | #else |
| 30 | return chr == '/'; |
| 31 | #endif |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void get_basename(SkString* basename, const SkString& path) { |
| 36 | if (path.size() == 0) { |
| 37 | basename->reset(); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | size_t end = path.size() - 1; |
| 42 | |
| 43 | // Paths pointing to directories often have a trailing slash, |
| 44 | // we remove it so the name is not empty |
| 45 | if (is_path_seperator(path[end])) { |
| 46 | if (end == 0) { |
| 47 | basename->reset(); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | end -= 1; |
| 52 | } |
| 53 | |
| 54 | size_t i = end; |
| 55 | do { |
| 56 | --i; |
| 57 | if (is_path_seperator(path[i])) { |
| 58 | const char* basenameStart = path.c_str() + i + 1; |
| 59 | size_t basenameLength = end - i; |
| 60 | basename->set(basenameStart, basenameLength); |
| 61 | return; |
| 62 | } |
| 63 | } while (i > 0); |
| 64 | |
| 65 | basename->set(path.c_str(), end + 1); |
| 66 | } |
| 67 | |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 68 | bool is_percentage(char* const string) { |
| 69 | SkString skString(string); |
| 70 | return skString.endsWith("%"); |
| 71 | } |
| 72 | |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 73 | void setup_bitmap(SkBitmap* bitmap, int width, int height) { |
| 74 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 75 | bitmap->allocPixels(); |
| 76 | bitmap->eraseColor(0); |
| 77 | } |
| 78 | |
| 79 | } |