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