joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 10 | |
| 11 | #include "Resources.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkGradientShader.h" |
| 14 | #include "SkStream.h" |
| 15 | #include "SkTextBlob.h" |
| 16 | #include "SkTypeface.h" |
| 17 | |
| 18 | namespace skiagm { |
| 19 | |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 20 | static void draw_blob(SkCanvas* canvas, const SkTextBlob* blob, const SkPaint& skPaint, |
| 21 | const SkRect& clipRect) { |
| 22 | SkPaint clipHairline; |
| 23 | clipHairline.setColor(SK_ColorWHITE); |
| 24 | clipHairline.setStyle(SkPaint::kStroke_Style); |
| 25 | |
| 26 | SkPaint paint(skPaint); |
| 27 | canvas->save(); |
| 28 | canvas->drawRect(clipRect, clipHairline); |
| 29 | paint.setAlpha(0x20); |
| 30 | canvas->drawTextBlob(blob, 0, 0, paint); |
| 31 | canvas->clipRect(clipRect); |
| 32 | paint.setAlpha(0xFF); |
| 33 | canvas->drawTextBlob(blob, 0, 0, paint); |
| 34 | canvas->restore(); |
| 35 | } |
| 36 | |
| 37 | class MixedTextBlobsGM : public GM { |
| 38 | public: |
| 39 | MixedTextBlobsGM() { } |
| 40 | |
| 41 | protected: |
| 42 | void onOnceBeforeDraw() override { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 43 | fEmojiTypeface = sk_tool_utils::emoji_typeface(); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 44 | fEmojiText = sk_tool_utils::emoji_sample_text(); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 45 | fReallyBigATypeface = MakeResourceAsTypeface("/fonts/ReallyBigA.ttf"); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 46 | |
| 47 | SkTextBlobBuilder builder; |
| 48 | |
| 49 | // make textblob |
| 50 | // Text so large we draw as paths |
| 51 | SkPaint paint; |
jvanverth | 34d7288 | 2015-06-22 08:08:09 -0700 | [diff] [blame] | 52 | paint.setTextSize(385); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 53 | const char* text = "O"; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 54 | sk_tool_utils::set_portable_typeface(&paint); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 55 | |
| 56 | SkRect bounds; |
| 57 | paint.measureText(text, strlen(text), &bounds); |
| 58 | |
| 59 | SkScalar yOffset = bounds.height(); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 60 | sk_tool_utils::add_to_text_blob(&builder, text, paint, 10, yOffset); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 61 | SkScalar corruptedAx = bounds.width(); |
| 62 | SkScalar corruptedAy = yOffset; |
| 63 | |
| 64 | const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf; |
| 65 | const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf; |
| 66 | |
| 67 | SkScalar xOffset = boundsHalfWidth; |
| 68 | yOffset = boundsHalfHeight; |
| 69 | |
| 70 | // LCD |
| 71 | paint.setTextSize(32); |
| 72 | text = "LCD!!!!!"; |
joshualitt | 20dac88 | 2015-07-24 13:16:24 -0700 | [diff] [blame] | 73 | paint.setAntiAlias(true); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 74 | paint.setSubpixelText(true); |
| 75 | paint.setLCDRenderText(true); |
| 76 | paint.measureText(text, strlen(text), &bounds); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 77 | sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.25f, |
| 78 | yOffset - bounds.height() * 0.5f); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 79 | yOffset += bounds.height(); |
| 80 | |
| 81 | // color emoji |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 82 | if (fEmojiTypeface) { |
joshualitt | 20dac88 | 2015-07-24 13:16:24 -0700 | [diff] [blame] | 83 | paint.setAntiAlias(false); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 84 | paint.setSubpixelText(false); |
| 85 | paint.setLCDRenderText(false); |
| 86 | paint.setTypeface(fEmojiTypeface); |
| 87 | text = fEmojiText; |
| 88 | paint.measureText(text, strlen(text), &bounds); |
| 89 | sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.3f, |
| 90 | yOffset); |
| 91 | } |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 92 | |
| 93 | // Corrupted font |
| 94 | paint.setTextSize(12); |
| 95 | text = "aA"; |
| 96 | paint.setTypeface(fReallyBigATypeface); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 97 | sk_tool_utils::add_to_text_blob(&builder, text, paint, corruptedAx, corruptedAy); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 98 | fBlob = builder.make(); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | SkString onShortName() override { |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 102 | SkString name("mixedtextblobs"); |
| 103 | name.append(sk_tool_utils::platform_os_emoji()); |
| 104 | return name; |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | SkISize onISize() override { |
| 108 | return SkISize::Make(kWidth, kHeight); |
| 109 | } |
| 110 | |
| 111 | void onDraw(SkCanvas* canvas) override { |
| 112 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 113 | canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 114 | |
| 115 | SkPaint paint; |
| 116 | |
| 117 | // setup work needed to draw text with different clips |
| 118 | paint.setColor(SK_ColorBLACK); |
| 119 | canvas->translate(10, 40); |
| 120 | |
| 121 | paint.setTextSize(40); |
| 122 | |
| 123 | // compute the bounds of the text and setup some clips |
| 124 | SkRect bounds = fBlob->bounds(); |
| 125 | |
| 126 | const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf; |
| 127 | const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf; |
| 128 | const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf; |
| 129 | const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf; |
| 130 | |
| 131 | SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(), |
| 132 | boundsHalfWidth, boundsHalfHeight); |
| 133 | SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(), |
| 134 | boundsHalfWidth, boundsHalfHeight); |
| 135 | SkRect interiorClip = bounds; |
| 136 | interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight); |
| 137 | |
| 138 | const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip}; |
| 139 | |
| 140 | size_t count = sizeof(clipRects) / sizeof(SkRect); |
| 141 | for (size_t x = 0; x < count; ++x) { |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 142 | draw_blob(canvas, fBlob.get(), paint, clipRects[x]); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 143 | if (x == (count >> 1) - 1) { |
| 144 | canvas->translate(SkScalarFloorToScalar(bounds.width() + SkIntToScalar(25)), |
| 145 | -(x * SkScalarFloorToScalar(bounds.height() + |
| 146 | SkIntToScalar(25)))); |
| 147 | } else { |
| 148 | canvas->translate(0, SkScalarFloorToScalar(bounds.height() + SkIntToScalar(25))); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private: |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 154 | sk_sp<SkTypeface> fEmojiTypeface; |
| 155 | sk_sp<SkTypeface> fReallyBigATypeface; |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 156 | const char* fEmojiText; |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 157 | sk_sp<SkTextBlob> fBlob; |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 158 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 159 | static constexpr int kWidth = 1250; |
| 160 | static constexpr int kHeight = 700; |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 161 | |
| 162 | typedef GM INHERITED; |
| 163 | }; |
| 164 | |
| 165 | ////////////////////////////////////////////////////////////////////////////// |
| 166 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 167 | DEF_GM(return new MixedTextBlobsGM;) |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 168 | } |