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(); |
Hal Canary | 53e5e7d | 2017-12-08 14:25:14 -0500 | [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 |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 51 | SkFont font(sk_tool_utils::create_portable_typeface(), 385); |
| 52 | font.setEdging(SkFont::Edging::kAlias); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 53 | const char* text = "O"; |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 54 | |
| 55 | SkRect bounds; |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 56 | font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 57 | |
| 58 | SkScalar yOffset = bounds.height(); |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 59 | sk_tool_utils::add_to_text_blob(&builder, text, font, 10, yOffset); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 60 | SkScalar corruptedAx = bounds.width(); |
| 61 | SkScalar corruptedAy = yOffset; |
| 62 | |
| 63 | const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf; |
| 64 | const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf; |
| 65 | |
| 66 | SkScalar xOffset = boundsHalfWidth; |
| 67 | yOffset = boundsHalfHeight; |
| 68 | |
| 69 | // LCD |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 70 | font.setSize(32); |
| 71 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 72 | font.setSubpixel(true); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 73 | text = "LCD!!!!!"; |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 74 | font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds); |
| 75 | sk_tool_utils::add_to_text_blob(&builder, text, font, xOffset - bounds.width() * 0.25f, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 76 | yOffset - bounds.height() * 0.5f); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 77 | yOffset += bounds.height(); |
| 78 | |
| 79 | // color emoji |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 80 | if (fEmojiTypeface) { |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 81 | font.setEdging(SkFont::Edging::kAlias); |
| 82 | font.setSubpixel(false); |
| 83 | font.setTypeface(fEmojiTypeface); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 84 | text = fEmojiText; |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 85 | font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds); |
| 86 | sk_tool_utils::add_to_text_blob(&builder, text, font, xOffset - bounds.width() * 0.3f, |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 87 | yOffset); |
| 88 | } |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 89 | |
| 90 | // Corrupted font |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 91 | font.setSize(12); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 92 | text = "aA"; |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 93 | font.setTypeface(fReallyBigATypeface); |
| 94 | sk_tool_utils::add_to_text_blob(&builder, text, font, corruptedAx, corruptedAy); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 95 | fBlob = builder.make(); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | SkString onShortName() override { |
Mike Klein | f436fbc | 2017-11-15 10:42:46 -0500 | [diff] [blame] | 99 | return SkStringPrintf("mixedtextblobs%s", |
| 100 | sk_tool_utils::platform_font_manager()); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | SkISize onISize() override { |
| 104 | return SkISize::Make(kWidth, kHeight); |
| 105 | } |
| 106 | |
| 107 | void onDraw(SkCanvas* canvas) override { |
| 108 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 109 | canvas->drawColor(SK_ColorGRAY); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 110 | |
| 111 | SkPaint paint; |
| 112 | |
| 113 | // setup work needed to draw text with different clips |
| 114 | paint.setColor(SK_ColorBLACK); |
| 115 | canvas->translate(10, 40); |
| 116 | |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 117 | // compute the bounds of the text and setup some clips |
| 118 | SkRect bounds = fBlob->bounds(); |
| 119 | |
| 120 | const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf; |
| 121 | const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf; |
| 122 | const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf; |
| 123 | const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf; |
| 124 | |
| 125 | SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(), |
| 126 | boundsHalfWidth, boundsHalfHeight); |
| 127 | SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(), |
| 128 | boundsHalfWidth, boundsHalfHeight); |
| 129 | SkRect interiorClip = bounds; |
| 130 | interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight); |
| 131 | |
| 132 | const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip}; |
| 133 | |
| 134 | size_t count = sizeof(clipRects) / sizeof(SkRect); |
| 135 | for (size_t x = 0; x < count; ++x) { |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 136 | draw_blob(canvas, fBlob.get(), paint, clipRects[x]); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 137 | if (x == (count >> 1) - 1) { |
| 138 | canvas->translate(SkScalarFloorToScalar(bounds.width() + SkIntToScalar(25)), |
| 139 | -(x * SkScalarFloorToScalar(bounds.height() + |
| 140 | SkIntToScalar(25)))); |
| 141 | } else { |
| 142 | canvas->translate(0, SkScalarFloorToScalar(bounds.height() + SkIntToScalar(25))); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private: |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 148 | sk_sp<SkTypeface> fEmojiTypeface; |
| 149 | sk_sp<SkTypeface> fReallyBigATypeface; |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 150 | const char* fEmojiText; |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 151 | sk_sp<SkTextBlob> fBlob; |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 152 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 153 | static constexpr int kWidth = 1250; |
| 154 | static constexpr int kHeight = 700; |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 155 | |
| 156 | typedef GM INHERITED; |
| 157 | }; |
| 158 | |
| 159 | ////////////////////////////////////////////////////////////////////////////// |
| 160 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 161 | DEF_GM(return new MixedTextBlobsGM;) |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 162 | } |