blob: 1c8ef353e451ab8ab8471c30b9a9c137245da77f [file] [log] [blame]
joshualitteef5b3e2015-04-03 08:07:26 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "tools/ToolUtils.h"
joshualitteef5b3e2015-04-03 08:07:26 -070010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkTextBlob.h"
14#include "include/core/SkTypeface.h"
15#include "include/effects/SkGradientShader.h"
16#include "tools/Resources.h"
joshualitteef5b3e2015-04-03 08:07:26 -070017
18namespace skiagm {
19
joshualitteef5b3e2015-04-03 08:07:26 -070020static 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);
Mike Reed9407e242019-02-15 16:13:57 -050029 paint.setAlphaf(0.125f);
joshualitteef5b3e2015-04-03 08:07:26 -070030 canvas->drawTextBlob(blob, 0, 0, paint);
31 canvas->clipRect(clipRect);
Mike Reed9407e242019-02-15 16:13:57 -050032 paint.setAlphaf(1.0f);
joshualitteef5b3e2015-04-03 08:07:26 -070033 canvas->drawTextBlob(blob, 0, 0, paint);
34 canvas->restore();
35}
36
37class MixedTextBlobsGM : public GM {
38public:
39 MixedTextBlobsGM() { }
40
41protected:
42 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050043 fEmojiTypeface = ToolUtils::planet_typeface();
Ben Wagner8dce0542019-03-08 18:05:47 -050044 fEmojiText = "♁♃";
Hal Canary53e5e7d2017-12-08 14:25:14 -050045 fReallyBigATypeface = MakeResourceAsTypeface("fonts/ReallyBigA.ttf");
joshualitteef5b3e2015-04-03 08:07:26 -070046
47 SkTextBlobBuilder builder;
48
49 // make textblob
50 // Text so large we draw as paths
Mike Kleinea3f0142019-03-20 11:12:10 -050051 SkFont font(ToolUtils::create_portable_typeface(), 385);
Mike Reed12a6d452018-12-21 22:22:31 -050052 font.setEdging(SkFont::Edging::kAlias);
joshualitteef5b3e2015-04-03 08:07:26 -070053 const char* text = "O";
joshualitteef5b3e2015-04-03 08:07:26 -070054
55 SkRect bounds;
Mike Reed12a6d452018-12-21 22:22:31 -050056 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
joshualitteef5b3e2015-04-03 08:07:26 -070057
58 SkScalar yOffset = bounds.height();
Mike Kleinea3f0142019-03-20 11:12:10 -050059 ToolUtils::add_to_text_blob(&builder, text, font, 10, yOffset);
joshualitteef5b3e2015-04-03 08:07:26 -070060 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 Reed12a6d452018-12-21 22:22:31 -050070 font.setSize(32);
71 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
72 font.setSubpixel(true);
joshualitteef5b3e2015-04-03 08:07:26 -070073 text = "LCD!!!!!";
Mike Reed12a6d452018-12-21 22:22:31 -050074 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
Mike Kleinea3f0142019-03-20 11:12:10 -050075 ToolUtils::add_to_text_blob(&builder,
76 text,
77 font,
78 xOffset - bounds.width() * 0.25f,
79 yOffset - bounds.height() * 0.5f);
joshualitteef5b3e2015-04-03 08:07:26 -070080
Ben Wagner8dce0542019-03-08 18:05:47 -050081 // color emoji font with large glyph
caryclarkc3dcb672015-07-21 12:27:36 -070082 if (fEmojiTypeface) {
Mike Reed12a6d452018-12-21 22:22:31 -050083 font.setEdging(SkFont::Edging::kAlias);
84 font.setSubpixel(false);
85 font.setTypeface(fEmojiTypeface);
Ben Wagner8dce0542019-03-08 18:05:47 -050086 font.measureText(fEmojiText, strlen(fEmojiText), kUTF8_SkTextEncoding, &bounds);
Mike Kleinea3f0142019-03-20 11:12:10 -050087 ToolUtils::add_to_text_blob(&builder, fEmojiText, font, xOffset, yOffset);
caryclarkc3dcb672015-07-21 12:27:36 -070088 }
joshualitteef5b3e2015-04-03 08:07:26 -070089
Ben Wagner8dce0542019-03-08 18:05:47 -050090 // outline font with large glyph
Mike Reed12a6d452018-12-21 22:22:31 -050091 font.setSize(12);
joshualitteef5b3e2015-04-03 08:07:26 -070092 text = "aA";
Mike Reed12a6d452018-12-21 22:22:31 -050093 font.setTypeface(fReallyBigATypeface);
Mike Kleinea3f0142019-03-20 11:12:10 -050094 ToolUtils::add_to_text_blob(&builder, text, font, corruptedAx, corruptedAy);
fmalita37283c22016-09-13 10:00:23 -070095 fBlob = builder.make();
joshualitteef5b3e2015-04-03 08:07:26 -070096 }
97
98 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -060099 return SkString("mixedtextblobs");
joshualitteef5b3e2015-04-03 08:07:26 -0700100 }
101
102 SkISize onISize() override {
103 return SkISize::Make(kWidth, kHeight);
104 }
105
106 void onDraw(SkCanvas* canvas) override {
107
Mike Kleind46dce32018-08-16 10:17:03 -0400108 canvas->drawColor(SK_ColorGRAY);
joshualitteef5b3e2015-04-03 08:07:26 -0700109
110 SkPaint paint;
111
112 // setup work needed to draw text with different clips
113 paint.setColor(SK_ColorBLACK);
114 canvas->translate(10, 40);
115
joshualitteef5b3e2015-04-03 08:07:26 -0700116 // compute the bounds of the text and setup some clips
117 SkRect bounds = fBlob->bounds();
118
119 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
120 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
121 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
122 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
123
124 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
125 boundsHalfWidth, boundsHalfHeight);
126 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
127 boundsHalfWidth, boundsHalfHeight);
128 SkRect interiorClip = bounds;
129 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
130
131 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip};
132
133 size_t count = sizeof(clipRects) / sizeof(SkRect);
134 for (size_t x = 0; x < count; ++x) {
fmalita37283c22016-09-13 10:00:23 -0700135 draw_blob(canvas, fBlob.get(), paint, clipRects[x]);
joshualitteef5b3e2015-04-03 08:07:26 -0700136 if (x == (count >> 1) - 1) {
137 canvas->translate(SkScalarFloorToScalar(bounds.width() + SkIntToScalar(25)),
138 -(x * SkScalarFloorToScalar(bounds.height() +
139 SkIntToScalar(25))));
140 } else {
141 canvas->translate(0, SkScalarFloorToScalar(bounds.height() + SkIntToScalar(25)));
142 }
143 }
144 }
145
146private:
bungeman13b9c952016-05-12 10:09:30 -0700147 sk_sp<SkTypeface> fEmojiTypeface;
148 sk_sp<SkTypeface> fReallyBigATypeface;
joshualitteef5b3e2015-04-03 08:07:26 -0700149 const char* fEmojiText;
fmalita37283c22016-09-13 10:00:23 -0700150 sk_sp<SkTextBlob> fBlob;
joshualitteef5b3e2015-04-03 08:07:26 -0700151
mtkleindbfd7ab2016-09-01 11:24:54 -0700152 static constexpr int kWidth = 1250;
153 static constexpr int kHeight = 700;
joshualitteef5b3e2015-04-03 08:07:26 -0700154
155 typedef GM INHERITED;
156};
157
158//////////////////////////////////////////////////////////////////////////////
159
halcanary385fe4d2015-08-26 13:07:48 -0700160DEF_GM(return new MixedTextBlobsGM;)
joshualitteef5b3e2015-04-03 08:07:26 -0700161}