commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | #ifndef sk_tool_utils_DEFINED |
| 9 | #define sk_tool_utils_DEFINED |
| 10 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 11 | #include "SkColor.h" |
robertphillips | 3e5c2b1 | 2015-03-23 05:46:51 -0700 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 13 | #include "SkImageInfo.h" |
robertphillips | 3e5c2b1 | 2015-03-23 05:46:51 -0700 | [diff] [blame] | 14 | #include "SkPixelSerializer.h" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 15 | #include "SkTypeface.h" |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 16 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 17 | class SkBitmap; |
| 18 | class SkCanvas; |
| 19 | class SkPaint; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 20 | class SkPath; |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 21 | class SkShader; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 22 | class SkTestFont; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 23 | class SkTextBlobBuilder; |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 24 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 25 | namespace sk_tool_utils { |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 26 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 27 | const char* colortype_name(SkColorType); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Map opaque colors from 8888 to 565. |
| 31 | */ |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 32 | SkColor color_to_565(SkColor color); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Return a color emoji typeface if available. |
| 36 | */ |
caryclark | 3b0f98e | 2015-07-23 11:52:01 -0700 | [diff] [blame] | 37 | void emoji_typeface(SkAutoTUnref<SkTypeface>* ); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * If the platform supports color emoji, return sample text the emoji can render. |
| 41 | */ |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 42 | const char* emoji_sample_text(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * If the platform supports color emoji, return the type (i.e. "CBDT", "SBIX", ""). |
| 46 | */ |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 47 | const char* platform_os_emoji(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Return the platform name with the version number ("Mac10.9", "Win8", etc.) if available. |
| 51 | */ |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 52 | const char* platform_os_name(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Return the platform name without the version number ("Mac", "Win", etc.) if available. |
| 56 | */ |
| 57 | SkString major_platform_os_name(); |
| 58 | |
| 59 | /** |
| 60 | * Return the platform extra config (e.g. "GDI") if available. |
| 61 | */ |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 62 | const char* platform_extra_config(const char* config); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 63 | |
| 64 | /** |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 65 | * Map serif, san-serif, and monospace to the platform-specific font name. |
| 66 | */ |
| 67 | const char* platform_font_name(const char* name); |
| 68 | |
| 69 | /** |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 70 | * Sets the paint to use a platform-independent text renderer |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 71 | */ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 72 | void set_portable_typeface(SkPaint* paint, const char* name = nullptr, |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 73 | SkTypeface::Style style = SkTypeface::kNormal); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 76 | * Returns a platform-independent text renderer. |
| 77 | */ |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 78 | SkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 79 | |
caryclark | f53ce80 | 2015-06-15 06:48:30 -0700 | [diff] [blame] | 80 | /** Call to clean up portable font references. */ |
| 81 | void release_portable_typefaces(); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 84 | * Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims |
| 85 | * the pixels are colorType + alphaType |
| 86 | */ |
| 87 | void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 88 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 89 | // private to sk_tool_utils |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 90 | SkTypeface* create_font(const char* name, SkTypeface::Style); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 91 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 92 | /** Returns a newly created CheckerboardShader. */ |
| 93 | SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size); |
| 94 | |
| 95 | /** Draw a checkerboard pattern in the current canvas, restricted to |
halcanary | f77365f | 2015-01-27 08:38:35 -0800 | [diff] [blame] | 96 | the current clip, using SkXfermode::kSrc_Mode. */ |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 97 | void draw_checkerboard(SkCanvas* canvas, |
| 98 | SkColor color1, |
| 99 | SkColor color2, |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 100 | int checkSize); |
| 101 | |
| 102 | /** Make it easier to create a bitmap-based checkerboard */ |
| 103 | SkBitmap create_checkerboard_bitmap(int w, int h, |
| 104 | SkColor c1, SkColor c2, |
| 105 | int checkSize); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 106 | |
| 107 | /** A default checkerboard. */ |
| 108 | inline void draw_checkerboard(SkCanvas* canvas) { |
| 109 | sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8); |
| 110 | } |
| 111 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 112 | SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, |
| 113 | int textSize, const char* str); |
| 114 | |
robertphillips | 3e5c2b1 | 2015-03-23 05:46:51 -0700 | [diff] [blame] | 115 | // Encodes to PNG, unless there is already encoded data, in which case that gets |
| 116 | // used. |
| 117 | class PngPixelSerializer : public SkPixelSerializer { |
| 118 | public: |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 119 | bool onUseEncodedData(const void*, size_t) override { return true; } |
robertphillips | 3e5c2b1 | 2015-03-23 05:46:51 -0700 | [diff] [blame] | 120 | SkData* onEncodePixels(const SkImageInfo& info, const void* pixels, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 121 | size_t rowBytes) override { |
robertphillips | 3e5c2b1 | 2015-03-23 05:46:51 -0700 | [diff] [blame] | 122 | return SkImageEncoder::EncodeData(info, pixels, rowBytes, |
| 123 | SkImageEncoder::kPNG_Type, 100); |
| 124 | } |
| 125 | }; |
| 126 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 127 | // A helper for inserting a drawtext call into a SkTextBlobBuilder |
| 128 | void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint, |
| 129 | SkScalar x, SkScalar y); |
| 130 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 131 | void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 132 | |
| 133 | void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 134 | |
| 135 | void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 136 | |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 137 | void make_big_path(SkPath& path); |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 138 | } // namespace sk_tool_utils |
| 139 | |
| 140 | #endif // sk_tool_utils_DEFINED |