blob: b20c60ba7632d9ed98f6f6cdd84f47e06ac4c99f [file] [log] [blame]
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +00001/*
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
halcanaryb0cce2c2015-01-26 12:49:00 -080011#include "SkColor.h"
robertphillips3e5c2b12015-03-23 05:46:51 -070012#include "SkImageEncoder.h"
tfarina20108912014-06-21 10:54:17 -070013#include "SkImageInfo.h"
robertphillips3e5c2b12015-03-23 05:46:51 -070014#include "SkPixelSerializer.h"
caryclark5fb6bd42014-06-23 11:25:00 -070015#include "SkTypeface.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000016
Cary Clark992c7b02014-07-31 08:58:44 -040017class SkBitmap;
18class SkCanvas;
19class SkPaint;
halcanaryb0cce2c2015-01-26 12:49:00 -080020class SkShader;
Cary Clark992c7b02014-07-31 08:58:44 -040021class SkTestFont;
joshualitt9e36c1a2015-04-14 12:17:27 -070022class SkTextBlobBuilder;
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +000023
Cary Clark992c7b02014-07-31 08:58:44 -040024namespace sk_tool_utils {
caryclark5fb6bd42014-06-23 11:25:00 -070025
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000026 const char* colortype_name(SkColorType);
caryclark37213552015-07-24 11:08:01 -070027
28 /**
29 * Map opaque colors from 8888 to 565.
30 */
caryclark65cdba62015-06-15 06:51:08 -070031 SkColor color_to_565(SkColor color);
caryclark37213552015-07-24 11:08:01 -070032
33 /**
34 * Return a color emoji typeface if available.
35 */
caryclark3b0f98e2015-07-23 11:52:01 -070036 void emoji_typeface(SkAutoTUnref<SkTypeface>* );
caryclark37213552015-07-24 11:08:01 -070037
38 /**
39 * If the platform supports color emoji, return sample text the emoji can render.
40 */
caryclarkc3dcb672015-07-21 12:27:36 -070041 const char* emoji_sample_text();
caryclark37213552015-07-24 11:08:01 -070042
43 /**
44 * If the platform supports color emoji, return the type (i.e. "CBDT", "SBIX", "").
45 */
caryclarkc3dcb672015-07-21 12:27:36 -070046 const char* platform_os_emoji();
caryclark37213552015-07-24 11:08:01 -070047
48 /**
49 * Return the platform name with the version number ("Mac10.9", "Win8", etc.) if available.
50 */
caryclark6531c362015-07-20 13:38:56 -070051 const char* platform_os_name();
caryclark37213552015-07-24 11:08:01 -070052
53 /**
54 * Return the platform name without the version number ("Mac", "Win", etc.) if available.
55 */
56 SkString major_platform_os_name();
57
58 /**
59 * Return the platform extra config (e.g. "GDI") if available.
60 */
caryclark6531c362015-07-20 13:38:56 -070061 const char* platform_extra_config(const char* config);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000062
63 /**
caryclark37213552015-07-24 11:08:01 -070064 * Map serif, san-serif, and monospace to the platform-specific font name.
65 */
66 const char* platform_font_name(const char* name);
67
68 /**
caryclark1818acb2015-07-24 12:09:25 -070069 * Sets the paint to use a platform-independent text renderer
caryclark5fb6bd42014-06-23 11:25:00 -070070 */
halcanary96fcdcc2015-08-27 07:41:13 -070071 void set_portable_typeface(SkPaint* paint, const char* name = nullptr,
Cary Clark992c7b02014-07-31 08:58:44 -040072 SkTypeface::Style style = SkTypeface::kNormal);
caryclark37213552015-07-24 11:08:01 -070073
74 /**
caryclark37213552015-07-24 11:08:01 -070075 * Returns a platform-independent text renderer.
76 */
caryclark1818acb2015-07-24 12:09:25 -070077 SkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style);
caryclark37213552015-07-24 11:08:01 -070078
caryclarkf53ce802015-06-15 06:48:30 -070079 /** Call to clean up portable font references. */
80 void release_portable_typefaces();
caryclark5fb6bd42014-06-23 11:25:00 -070081
82 /**
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000083 * Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims
84 * the pixels are colorType + alphaType
85 */
86 void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000087
Cary Clark992c7b02014-07-31 08:58:44 -040088 // private to sk_tool_utils
robertphillips943a4622015-09-03 13:32:33 -070089 SkTypeface* create_font(const char* name, SkTypeface::Style);
Cary Clark992c7b02014-07-31 08:58:44 -040090
halcanaryb0cce2c2015-01-26 12:49:00 -080091 /** Returns a newly created CheckerboardShader. */
92 SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size);
93
94 /** Draw a checkerboard pattern in the current canvas, restricted to
halcanaryf77365f2015-01-27 08:38:35 -080095 the current clip, using SkXfermode::kSrc_Mode. */
halcanaryb0cce2c2015-01-26 12:49:00 -080096 void draw_checkerboard(SkCanvas* canvas,
97 SkColor color1,
98 SkColor color2,
robertphillips943a4622015-09-03 13:32:33 -070099 int checkSize);
100
101 /** Make it easier to create a bitmap-based checkerboard */
102 SkBitmap create_checkerboard_bitmap(int w, int h,
103 SkColor c1, SkColor c2,
104 int checkSize);
halcanaryb0cce2c2015-01-26 12:49:00 -0800105
106 /** A default checkerboard. */
107 inline void draw_checkerboard(SkCanvas* canvas) {
108 sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
109 }
110
robertphillips943a4622015-09-03 13:32:33 -0700111 SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
112 int textSize, const char* str);
113
robertphillips3e5c2b12015-03-23 05:46:51 -0700114 // Encodes to PNG, unless there is already encoded data, in which case that gets
115 // used.
116 class PngPixelSerializer : public SkPixelSerializer {
117 public:
reedc9e190d2015-09-28 09:58:41 -0700118 bool onUseEncodedData(const void*, size_t) override { return true; }
robertphillips3e5c2b12015-03-23 05:46:51 -0700119 SkData* onEncodePixels(const SkImageInfo& info, const void* pixels,
mtklein36352bf2015-03-25 18:17:31 -0700120 size_t rowBytes) override {
robertphillips3e5c2b12015-03-23 05:46:51 -0700121 return SkImageEncoder::EncodeData(info, pixels, rowBytes,
122 SkImageEncoder::kPNG_Type, 100);
123 }
124 };
125
joshualitt9e36c1a2015-04-14 12:17:27 -0700126 // A helper for inserting a drawtext call into a SkTextBlobBuilder
127 void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
128 SkScalar x, SkScalar y);
129
robertphillipsea4529d2015-08-17 15:04:47 -0700130 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
131
132 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
133
134 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
135
tfarina20108912014-06-21 10:54:17 -0700136} // namespace sk_tool_utils
137
138#endif // sk_tool_utils_DEFINED