caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 1 | ο»Ώ/* |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 8 | #include "sk_tool_utils.h" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils_flags.h" |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 10 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 11 | #include "Resources.h" |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 12 | #include "SkBitmap.h" |
| 13 | #include "SkCanvas.h" |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 14 | #include "SkCommonFlags.h" |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 15 | #include "SkShader.h" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 16 | #include "SkTestScalerContext.h" |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 17 | #include "SkTextBlob.h" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 18 | |
| 19 | DEFINE_bool(portableFonts, false, "Use portable fonts"); |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 20 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 21 | namespace sk_tool_utils { |
| 22 | |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame^] | 23 | /* these are the default fonts chosen by Chrome for serif, sans-serif, and monospace */ |
| 24 | static const char* gStandardFontNames[][3] = { |
| 25 | { "Times", "Helvetica", "Courier" }, // Mac |
| 26 | { "Times New Roman", "Helvetica", "Courier" }, // iOS |
| 27 | { "Times New Roman", "Arial", "Courier New" }, // Win |
| 28 | { "Times New Roman", "Arial", "Monospace" }, // Ubuntu |
| 29 | { "Droid Serif", "Droid Sans", "Droid Sans Mono" }, // Android |
| 30 | { "Tinos", "Arimo", "Cousine" } // ChromeOS |
| 31 | }; |
| 32 | |
| 33 | const char* platform_font_name(const char* name) { |
| 34 | SkString platform = major_platform_os_name(); |
| 35 | int index; |
| 36 | if (!strcmp(name, "serif")) { |
| 37 | index = 0; |
| 38 | } else if (!strcmp(name, "san-serif")) { |
| 39 | index = 1; |
| 40 | } else if (!strcmp(name, "monospace")) { |
| 41 | index = 2; |
| 42 | } else { |
| 43 | return name; |
| 44 | } |
| 45 | if (platform.equals("Mac")) { |
| 46 | return gStandardFontNames[0][index]; |
| 47 | } |
| 48 | if (platform.equals("iOS")) { |
| 49 | return gStandardFontNames[1][index]; |
| 50 | } |
| 51 | if (platform.equals("Win")) { |
| 52 | return gStandardFontNames[2][index]; |
| 53 | } |
| 54 | if (platform.equals("Ubuntu")) { |
| 55 | return gStandardFontNames[3][index]; |
| 56 | } |
| 57 | if (platform.equals("Android")) { |
| 58 | return gStandardFontNames[4][index]; |
| 59 | } |
| 60 | if (platform.equals("ChromeOS")) { |
| 61 | return gStandardFontNames[5][index]; |
| 62 | } |
| 63 | return name; |
| 64 | } |
| 65 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 66 | const char* platform_os_emoji() { |
| 67 | const char* osName = platform_os_name(); |
caryclark | e663341 | 2015-07-23 09:11:58 -0700 | [diff] [blame] | 68 | if (!strcmp(osName, "Android") || !strcmp(osName, "Ubuntu")) { |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 69 | return "CBDT"; |
| 70 | } |
| 71 | if (!strncmp(osName, "Mac", 3)) { |
| 72 | return "SBIX"; |
| 73 | } |
| 74 | return ""; |
| 75 | } |
| 76 | |
caryclark | 3b0f98e | 2015-07-23 11:52:01 -0700 | [diff] [blame] | 77 | void emoji_typeface(SkAutoTUnref<SkTypeface>* tf) { |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 78 | if (!strcmp(sk_tool_utils::platform_os_emoji(), "CBDT")) { |
caryclark | 3b0f98e | 2015-07-23 11:52:01 -0700 | [diff] [blame] | 79 | tf->reset(GetResourceAsTypeface("/fonts/Funkster.ttf")); |
| 80 | return; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 81 | } |
| 82 | if (!strcmp(sk_tool_utils::platform_os_emoji(), "SBIX")) { |
caryclark | 3b0f98e | 2015-07-23 11:52:01 -0700 | [diff] [blame] | 83 | tf->reset(SkTypeface::CreateFromName("Apple Color Emoji", SkTypeface::kNormal)); |
| 84 | return; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 85 | } |
caryclark | 3b0f98e | 2015-07-23 11:52:01 -0700 | [diff] [blame] | 86 | tf->reset(NULL); |
| 87 | return; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | const char* emoji_sample_text() { |
| 91 | if (!strcmp(sk_tool_utils::platform_os_emoji(), "CBDT")) { |
| 92 | return "Hamburgefons"; |
| 93 | } |
| 94 | if (!strcmp(sk_tool_utils::platform_os_emoji(), "SBIX")) { |
| 95 | return "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" // π°π‘π
|
| 96 | "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80" // πͺππ |
| 97 | "\xF0\x9F\x9A\xBB" "\xF0\x9F\x92\xA9" "\xF0\x9F\x93\xB7" // π»π©π· |
| 98 | "\xF0\x9F\x93\xA6" // π¦ |
| 99 | "\xF0\x9F\x87\xBA" "\xF0\x9F\x87\xB8" "\xF0\x9F\x87\xA6"; // πΊπΈπ¦ |
| 100 | } |
| 101 | return ""; |
| 102 | } |
| 103 | |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 104 | const char* platform_os_name() { |
| 105 | for (int index = 0; index < FLAGS_key.count(); index += 2) { |
| 106 | if (!strcmp("os", FLAGS_key[index])) { |
| 107 | return FLAGS_key[index + 1]; |
| 108 | } |
| 109 | } |
| 110 | // when running SampleApp or dm without a --key pair, omit the platform name |
| 111 | return ""; |
| 112 | } |
| 113 | |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame^] | 114 | // omit version number in returned value |
| 115 | SkString major_platform_os_name() { |
| 116 | SkString name; |
| 117 | for (int index = 0; index < FLAGS_key.count(); index += 2) { |
| 118 | if (!strcmp("os", FLAGS_key[index])) { |
| 119 | const char* platform = FLAGS_key[index + 1]; |
| 120 | const char* end = platform; |
| 121 | while (*end && (*end < '0' || *end > '9')) { |
| 122 | ++end; |
| 123 | } |
| 124 | name.append(platform, end - platform); |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | return name; |
| 129 | } |
| 130 | |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 131 | const char* platform_extra_config(const char* config) { |
| 132 | for (int index = 0; index < FLAGS_key.count(); index += 2) { |
| 133 | if (!strcmp("extra_config", FLAGS_key[index]) && !strcmp(config, FLAGS_key[index + 1])) { |
| 134 | return config; |
| 135 | } |
| 136 | } |
| 137 | return ""; |
| 138 | } |
| 139 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 140 | const char* colortype_name(SkColorType ct) { |
| 141 | switch (ct) { |
| 142 | case kUnknown_SkColorType: return "Unknown"; |
| 143 | case kAlpha_8_SkColorType: return "Alpha_8"; |
| 144 | case kIndex_8_SkColorType: return "Index_8"; |
| 145 | case kARGB_4444_SkColorType: return "ARGB_4444"; |
| 146 | case kRGB_565_SkColorType: return "RGB_565"; |
| 147 | case kRGBA_8888_SkColorType: return "RGBA_8888"; |
| 148 | case kBGRA_8888_SkColorType: return "BGRA_8888"; |
| 149 | default: |
| 150 | SkASSERT(false); |
| 151 | return "unexpected colortype"; |
| 152 | } |
| 153 | } |
| 154 | |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 155 | SkColor color_to_565(SkColor color) { |
| 156 | SkPMColor pmColor = SkPreMultiplyColor(color); |
caryclark | d85093c | 2015-06-12 11:49:04 -0700 | [diff] [blame] | 157 | U16CPU color16 = SkPixel32ToPixel16(pmColor); |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 158 | return SkPixel16ToColor(color16); |
caryclark | d85093c | 2015-06-12 11:49:04 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 161 | SkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style) { |
| 162 | SkTypeface* face; |
| 163 | if (FLAGS_portableFonts) { |
| 164 | face = create_font(name, style); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 165 | } else { |
| 166 | face = SkTypeface::CreateFromName(name, style); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 167 | } |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 168 | return face; |
| 169 | } |
| 170 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 171 | SkTypeface* create_portable_typeface_always(const char* name, SkTypeface::Style style) { |
| 172 | return create_font(name, style); |
| 173 | } |
| 174 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 175 | void set_portable_typeface(SkPaint* paint, const char* name, SkTypeface::Style style) { |
| 176 | SkTypeface* face = create_portable_typeface(name, style); |
| 177 | SkSafeUnref(paint->setTypeface(face)); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 178 | } |
| 179 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 180 | void set_portable_typeface_always(SkPaint* paint, const char* name, SkTypeface::Style style) { |
| 181 | SkTypeface* face = create_font(name, style); |
| 182 | SkSafeUnref(paint->setTypeface(face)); |
| 183 | } |
| 184 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 185 | void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y, |
| 186 | SkColorType colorType, SkAlphaType alphaType) { |
| 187 | SkBitmap tmp(bitmap); |
| 188 | tmp.lockPixels(); |
skia.committer@gmail.com | e62513f | 2014-03-08 03:02:06 +0000 | [diff] [blame] | 189 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 190 | const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType); |
skia.committer@gmail.com | e62513f | 2014-03-08 03:02:06 +0000 | [diff] [blame] | 191 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 192 | canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y); |
| 193 | } |
| 194 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 195 | SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size) { |
| 196 | SkBitmap bm; |
| 197 | bm.allocN32Pixels(2 * size, 2 * size); |
| 198 | bm.eraseColor(c1); |
| 199 | bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2); |
| 200 | bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2); |
| 201 | return SkShader::CreateBitmapShader( |
| 202 | bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); |
| 203 | } |
| 204 | |
| 205 | void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) { |
| 206 | SkPaint paint; |
| 207 | paint.setShader(create_checkerboard_shader(c1, c2, size))->unref(); |
halcanary | f77365f | 2015-01-27 08:38:35 -0800 | [diff] [blame] | 208 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 209 | canvas->drawPaint(paint); |
| 210 | } |
| 211 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 212 | void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint, |
| 213 | SkScalar x, SkScalar y) { |
| 214 | SkPaint paint(origPaint); |
| 215 | SkTDArray<uint16_t> glyphs; |
| 216 | |
| 217 | size_t len = strlen(text); |
| 218 | glyphs.append(paint.textToGlyphs(text, len, NULL)); |
| 219 | paint.textToGlyphs(text, len, glyphs.begin()); |
| 220 | |
| 221 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 222 | const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y, |
| 223 | NULL); |
| 224 | memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t)); |
| 225 | } |
| 226 | |
| 227 | |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 228 | } // namespace sk_tool_utils |