diff options
author | caryclark <caryclark@google.com> | 2015-07-21 12:27:36 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-21 12:27:36 -0700 |
commit | c3dcb67f07173af1c17771965ab7254910d52ef3 (patch) | |
tree | 6ce7c2da52adcd3f547e0fedfb759de873afd098 | |
parent | 654ad19fbd5a295c4bd3072dc8a7b88f212bbd49 (diff) |
make color emoji gm portable
R=bungeman@google.com
Review URL: https://codereview.chromium.org/1248703004
-rw-r--r-- | gm/coloremoji.cpp | 177 | ||||
-rwxr-xr-x | gm/dftext.cpp | 34 | ||||
-rw-r--r-- | gm/mixedtextblobs.cpp | 32 | ||||
-rw-r--r-- | tools/sk_tool_utils.cpp | 38 | ||||
-rw-r--r-- | tools/sk_tool_utils.h | 3 |
5 files changed, 154 insertions, 130 deletions
diff --git a/gm/coloremoji.cpp b/gm/coloremoji.cpp index 36897c52a8..029377e8e4 100644 --- a/gm/coloremoji.cpp +++ b/gm/coloremoji.cpp @@ -46,29 +46,22 @@ namespace skiagm { class ColorEmojiGM : public GM { public: - ColorEmojiGM() : fCBDT_CBLC_Typeface(NULL), fSBIX_Typeface(NULL) { } + ColorEmojiGM() { } protected: struct EmojiFont { SkTypeface* typeface; const char* text; - } emojiFonts[2]; + } emojiFont; virtual void onOnceBeforeDraw() override { - fCBDT_CBLC_Typeface.reset(GetResourceAsTypeface("/fonts/Funkster.ttf")); - emojiFonts[0].typeface = fCBDT_CBLC_Typeface; - emojiFonts[0].text = "hamburgerfons"; - - fSBIX_Typeface.reset(SkTypeface::CreateFromName("Apple Color Emoji", SkTypeface::kNormal)); - emojiFonts[1].typeface = fSBIX_Typeface; - emojiFonts[1].text = "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" // π°π‘π
- "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80" // πͺππ - "\xF0\x9F\x9A\xBB" "\xF0\x9F\x92\xA9" "\xF0\x9F\x93\xB7" // π»π©π· - "\xF0\x9F\x93\xA6" // π¦ - "\xF0\x9F\x87\xBA" "\xF0\x9F\x87\xB8" "\xF0\x9F\x87\xA6"; // πΊπΈπ¦ + emojiFont.typeface = sk_tool_utils::emoji_typeface(); + emojiFont.text = sk_tool_utils::emoji_sample_text(); } SkString onShortName() override { - return SkString("coloremoji"); + SkString name("coloremoji"); + name.append(sk_tool_utils::platform_os_emoji()); + return name; } SkISize onISize() override { @@ -77,90 +70,88 @@ protected: void onDraw(SkCanvas* canvas) override { - canvas->drawColor(SK_ColorGRAY); - - for (size_t i = 0; i < SK_ARRAY_COUNT(emojiFonts); ++i) { - SkPaint paint; - paint.setTypeface(emojiFonts[i].typeface); - const char* text = emojiFonts[i].text; - - // draw text at different point sizes - const int textSize[] = { 10, 30, 50, }; - const int textYOffset[] = { 10, 40, 100, }; - SkASSERT(sizeof(textSize) == sizeof(textYOffset)); - size_t y_offset = 0; - for (size_t y = 0; y < sizeof(textSize) / sizeof(int); y++) { - paint.setTextSize(SkIntToScalar(textSize[y])); - canvas->drawText(text, strlen(text), 10, SkIntToScalar(textYOffset[y]), paint); - y_offset += textYOffset[y]; - } + canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); + + SkPaint paint; + paint.setTypeface(emojiFont.typeface); + const char* text = emojiFont.text; + + // draw text at different point sizes + const int textSize[] = { 10, 30, 50, }; + const int textYOffset[] = { 10, 40, 100, }; + SkASSERT(sizeof(textSize) == sizeof(textYOffset)); + size_t y_offset = 0; + for (size_t y = 0; y < sizeof(textSize) / sizeof(int); y++) { + paint.setTextSize(SkIntToScalar(textSize[y])); + canvas->drawText(text, strlen(text), 10, SkIntToScalar(textYOffset[y]), paint); + y_offset += textYOffset[y]; + } + + // draw with shaders and image filters + for (int makeLinear = 0; makeLinear < 2; makeLinear++) { + for (int makeBlur = 0; makeBlur < 2; makeBlur++) { + for (int makeGray = 0; makeGray < 2; makeGray++) { + SkPaint shaderPaint; + shaderPaint.setTypeface(paint.getTypeface()); + if (SkToBool(makeLinear)) { + shaderPaint.setShader(MakeLinear())->unref(); + } - // draw with shaders and image filters - for (int makeLinear = 0; makeLinear < 2; makeLinear++) { - for (int makeBlur = 0; makeBlur < 2; makeBlur++) { - for (int makeGray = 0; makeGray < 2; makeGray++) { - SkPaint shaderPaint; - shaderPaint.setTypeface(paint.getTypeface()); - if (SkToBool(makeLinear)) { - shaderPaint.setShader(MakeLinear())->unref(); - } - - if (SkToBool(makeBlur) && SkToBool(makeGray)) { - SkAutoTUnref<SkImageFilter> grayScale(make_grayscale(NULL)); - SkAutoTUnref<SkImageFilter> blur(make_blur(3.0f, grayScale)); - shaderPaint.setImageFilter(blur); - } else if (SkToBool(makeBlur)) { - SkAutoTUnref<SkImageFilter> blur(make_blur(3.0f, NULL)); - shaderPaint.setImageFilter(blur); - } else if (SkToBool(makeGray)) { - SkAutoTUnref<SkImageFilter> grayScale(make_grayscale(NULL)); - shaderPaint.setImageFilter(grayScale); - } - shaderPaint.setTextSize(30); - canvas->drawText(text, strlen(text), 380, SkIntToScalar(y_offset), - shaderPaint); - y_offset += 32; + if (SkToBool(makeBlur) && SkToBool(makeGray)) { + SkAutoTUnref<SkImageFilter> grayScale(make_grayscale(NULL)); + SkAutoTUnref<SkImageFilter> blur(make_blur(3.0f, grayScale)); + shaderPaint.setImageFilter(blur); + } else if (SkToBool(makeBlur)) { + SkAutoTUnref<SkImageFilter> blur(make_blur(3.0f, NULL)); + shaderPaint.setImageFilter(blur); + } else if (SkToBool(makeGray)) { + SkAutoTUnref<SkImageFilter> grayScale(make_grayscale(NULL)); + shaderPaint.setImageFilter(grayScale); } + shaderPaint.setTextSize(30); + canvas->drawText(text, strlen(text), 380, SkIntToScalar(y_offset), + shaderPaint); + y_offset += 32; } } + } - // setup work needed to draw text with different clips - canvas->translate(10, 160); - paint.setTextSize(40); - - // compute the bounds of the text - SkRect bounds; - paint.measureText(text, strlen(text), &bounds); - - const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf; - const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf; - const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf; - const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf; - - SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(), - boundsHalfWidth, boundsHalfHeight); - SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(), - boundsHalfWidth, boundsHalfHeight); - SkRect interiorClip = bounds; - interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight); - - const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip }; - - SkPaint clipHairline; - clipHairline.setColor(SK_ColorWHITE); - clipHairline.setStyle(SkPaint::kStroke_Style); - - for (size_t x = 0; x < sizeof(clipRects) / sizeof(SkRect); ++x) { - canvas->save(); - canvas->drawRect(clipRects[x], clipHairline); - paint.setAlpha(0x20); - canvas->drawText(text, strlen(text), 0, 0, paint); - canvas->clipRect(clipRects[x]); - paint.setAlpha(0xFF); - canvas->drawText(text, strlen(text), 0, 0, paint); - canvas->restore(); - canvas->translate(0, bounds.height() + SkIntToScalar(25)); - } + // setup work needed to draw text with different clips + canvas->translate(10, 160); + paint.setTextSize(40); + + // compute the bounds of the text + SkRect bounds; + paint.measureText(text, strlen(text), &bounds); + + const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf; + const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf; + const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf; + const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf; + + SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(), + boundsHalfWidth, boundsHalfHeight); + SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(), + boundsHalfWidth, boundsHalfHeight); + SkRect interiorClip = bounds; + interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight); + + const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip }; + + SkPaint clipHairline; + clipHairline.setColor(SK_ColorWHITE); + clipHairline.setStyle(SkPaint::kStroke_Style); + + for (size_t x = 0; x < sizeof(clipRects) / sizeof(SkRect); ++x) { + canvas->save(); + canvas->drawRect(clipRects[x], clipHairline); + paint.setAlpha(0x20); + canvas->drawText(text, strlen(text), 0, 0, paint); + canvas->clipRect(clipRects[x]); + paint.setAlpha(0xFF); + canvas->drawText(text, strlen(text), 0, 0, paint); + canvas->restore(); + canvas->translate(0, bounds.height() + SkIntToScalar(25)); } } diff --git a/gm/dftext.cpp b/gm/dftext.cpp index 0ee01e3309..089308329e 100755 --- a/gm/dftext.cpp +++ b/gm/dftext.cpp @@ -15,20 +15,18 @@ class DFTextGM : public skiagm::GM { public: DFTextGM() { this->setBGColor(0xFFFFFFFF); - fTypeface = NULL; - } - - virtual ~DFTextGM() { - SkSafeUnref(fTypeface); } protected: void onOnceBeforeDraw() override { - fTypeface = GetResourceAsTypeface("/fonts/Funkster.ttf"); + fEmojiTypeface.reset(sk_tool_utils::emoji_typeface()); + fEmojiText = sk_tool_utils::emoji_sample_text(); } SkString onShortName() override { - return SkString("dftext"); + SkString name("dftext"); + name.append(sk_tool_utils::platform_os_emoji()); + return name; } SkISize onISize() override { @@ -73,7 +71,7 @@ protected: paint.setAntiAlias(true); paint.setSubpixelText(true); - sk_tool_utils::set_portable_typeface(&paint, "Times New Roman", SkTypeface::kNormal); + sk_tool_utils::set_portable_typeface_always(&paint, "serif", SkTypeface::kNormal); const char* text = "Hamburgefons"; const size_t textLen = strlen(text); @@ -151,7 +149,7 @@ protected: 0xFF000000, }; - paint.setColor(0xFFF1F1F1); + paint.setColor(0xFFF7F3F7); SkRect r = SkRect::MakeLTRB(670, 250, 820, 460); canvas->drawRect(r, paint); @@ -169,7 +167,7 @@ protected: y += paint.getFontMetrics(NULL); } - paint.setColor(0xFF1F1F1F); + paint.setColor(0xFF181C18); r = SkRect::MakeLTRB(820, 250, 970, 460); canvas->drawRect(r, paint); @@ -204,14 +202,11 @@ protected: } // check color emoji - paint.setTypeface(fTypeface); -#ifdef SK_BUILD_FOR_ANDROID - paint.setTextSize(SkIntToScalar(19)); -#else - paint.setTextSize(SkIntToScalar(22)); -#endif - canvas->drawText(text, textLen, 670, 100, paint); - + if (fEmojiTypeface) { + paint.setTypeface(fEmojiTypeface); + paint.setTextSize(SkIntToScalar(19)); + canvas->drawText(fEmojiText, strlen(fEmojiText), 670, 100, paint); + } #if SK_SUPPORT_GPU // render offscreen buffer if (surface) { @@ -226,7 +221,8 @@ protected: } private: - SkTypeface* fTypeface; + SkAutoTUnref<SkTypeface> fEmojiTypeface; + const char* fEmojiText; typedef skiagm::GM INHERITED; }; diff --git a/gm/mixedtextblobs.cpp b/gm/mixedtextblobs.cpp index ab7beb909b..ead1b24975 100644 --- a/gm/mixedtextblobs.cpp +++ b/gm/mixedtextblobs.cpp @@ -39,14 +39,8 @@ public: protected: void onOnceBeforeDraw() override { -#ifndef SK_BUILD_FOR_MAC - fEmojiTypeface.reset(GetResourceAsTypeface("/fonts/Funkster.ttf")); - fEmojiText = "Emoji!!!"; -#else - fEmojiTypeface.reset(SkTypeface::CreateFromName("Apple Color Emoji", SkTypeface::kNormal)); - fEmojiText = "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" // π°π‘π
- "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80"; // πͺππ -#endif + fEmojiTypeface.reset(sk_tool_utils::emoji_typeface()); + fEmojiText = sk_tool_utils::emoji_sample_text(); fReallyBigATypeface.reset(GetResourceAsTypeface("/fonts/ReallyBigA.ttf")); SkTextBlobBuilder builder; @@ -83,13 +77,15 @@ protected: yOffset += bounds.height(); // color emoji - paint.setSubpixelText(false); - paint.setLCDRenderText(false); - paint.setTypeface(fEmojiTypeface); - text = fEmojiText; - paint.measureText(text, strlen(text), &bounds); - sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.3f, - yOffset); + if (fEmojiTypeface) { + paint.setSubpixelText(false); + paint.setLCDRenderText(false); + paint.setTypeface(fEmojiTypeface); + text = fEmojiText; + paint.measureText(text, strlen(text), &bounds); + sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.3f, + yOffset); + } // Corrupted font paint.setTextSize(12); @@ -100,7 +96,9 @@ protected: } SkString onShortName() override { - return SkString("mixedtextblobs"); + SkString name("mixedtextblobs"); + name.append(sk_tool_utils::platform_os_emoji()); + return name; } SkISize onISize() override { @@ -109,7 +107,7 @@ protected: void onDraw(SkCanvas* canvas) override { - canvas->drawColor(SK_ColorGRAY); + canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); SkPaint paint; diff --git a/tools/sk_tool_utils.cpp b/tools/sk_tool_utils.cpp index b06fcda3a6..b8b43f6fe7 100644 --- a/tools/sk_tool_utils.cpp +++ b/tools/sk_tool_utils.cpp @@ -1,4 +1,4 @@ -/* +ο»Ώ/* * Copyright 2014 Google Inc. * * Use of this source code is governed by a BSD-style license that can be @@ -8,6 +8,7 @@ #include "sk_tool_utils.h" #include "sk_tool_utils_flags.h" +#include "Resources.h" #include "SkBitmap.h" #include "SkCanvas.h" #include "SkCommonFlags.h" @@ -20,6 +21,41 @@ DEFINE_bool(resourceFonts, false, "Use resource fonts"); namespace sk_tool_utils { +const char* platform_os_emoji() { + const char* osName = platform_os_name(); + if (!strcmp(osName, "Android") || !strcmp(osName, "Unbuntu")) { + return "CBDT"; + } + if (!strncmp(osName, "Mac", 3)) { + return "SBIX"; + } + return ""; +} + +SkTypeface* emoji_typeface() { + if (!strcmp(sk_tool_utils::platform_os_emoji(), "CBDT")) { + return GetResourceAsTypeface("/fonts/Funkster.ttf"); + } + if (!strcmp(sk_tool_utils::platform_os_emoji(), "SBIX")) { + return SkTypeface::CreateFromName("Apple Color Emoji", SkTypeface::kNormal); + } + return NULL; +} + +const char* emoji_sample_text() { + if (!strcmp(sk_tool_utils::platform_os_emoji(), "CBDT")) { + return "Hamburgefons"; + } + if (!strcmp(sk_tool_utils::platform_os_emoji(), "SBIX")) { + return "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" // π°π‘π
+ "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80" // πͺππ + "\xF0\x9F\x9A\xBB" "\xF0\x9F\x92\xA9" "\xF0\x9F\x93\xB7" // π»π©π· + "\xF0\x9F\x93\xA6" // π¦ + "\xF0\x9F\x87\xBA" "\xF0\x9F\x87\xB8" "\xF0\x9F\x87\xA6"; // πΊπΈπ¦ + } + return ""; +} + const char* platform_os_name() { for (int index = 0; index < FLAGS_key.count(); index += 2) { if (!strcmp("os", FLAGS_key[index])) { diff --git a/tools/sk_tool_utils.h b/tools/sk_tool_utils.h index 864c33dd21..921ca2ad39 100644 --- a/tools/sk_tool_utils.h +++ b/tools/sk_tool_utils.h @@ -25,6 +25,9 @@ namespace sk_tool_utils { const char* colortype_name(SkColorType); SkColor color_to_565(SkColor color); + SkTypeface* emoji_typeface(); + const char* emoji_sample_text(); + const char* platform_os_emoji(); const char* platform_os_name(); const char* platform_extra_config(const char* config); |